From patchwork Thu Jun 30 00:01:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: mohamed Elhadidy X-Patchwork-Id: 36535 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp639303pzh; Wed, 29 Jun 2022 17:03:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1stEh59v9N/sqRzf7lenH5RYnSeuMPtjqfOfQO0jyTxOhTWDcqEj6SjXb7Y0NM7X4NSh2/U X-Received: by 2002:a05:6402:4410:b0:434:f35f:132e with SMTP id y16-20020a056402441000b00434f35f132emr7694074eda.215.1656547421384; Wed, 29 Jun 2022 17:03:41 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id i5-20020a170906a28500b0070ed13d358fsi19295201ejz.194.2022.06.29.17.03.40; Wed, 29 Jun 2022 17:03:41 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@hotmail.com header.s=selector1 header.b=oyIasYMe; arc=fail (body hash mismatch); spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=hotmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2DD1C68B6F5; Thu, 30 Jun 2022 03:03:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-oln040092074098.outbound.protection.outlook.com [40.92.74.98]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3F14568AFBA for ; Thu, 30 Jun 2022 03:03:16 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WMty6sAXh7Z8bQlp4YWgN3m39ua9NfmQyQS26waBPeC1he3S7tQO6DLW85pkgps7XOOyJvMNtN0PCZ5owQnwGR1VqhuDfg+4YwUC/tHFc6jJdsYpUm1p5nJjys+UsAG73f1C0aUPx5lpRO3T4tRYedQWDzSZlMIF419W9YOxPH1FgkFNQak8hemY7lojI5tqPHjGb42m/wUhfj23t9f1qhVvCsV6YfwFzWPH+L+k7I1DWi+INpvYtYWd4Zs7bSo5EfU9syoamEwAxaJS+4gYQDVXghlOWf256Mhir2JpHBs0Mw3mJ2SLKegueEQyvHZm7+e/qAzdW6AexD42TlGkbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=dREZVZNnc4voL32nCiikPtOer/bHmOMbDOzH2U0rrzo=; b=N2xaWvpcD6AQ/X01HYaIgQHG8e/KA/IwO+RJsBzEmaVVt4B5RXtMhIR177i2vUP7sZpnypluDW0CxYQRcIMU+SDD9noBuJwDL530PgFAM06aryeQYTifxuo/wxFaWuIKEPMA9NEbUYXOiOU/wHBmlZVhXT9pv8sx1tBWjs7wTxoE4SgDg8AR5Y25IeiB8vm7WXNKmUlDMQ6VWCLjaACQZb+q1NDILN0MKXurmViBjmT2Ernwi6DfsCvSLw53XV/pnnGIVjugBmXGjU/d+bwLj1WyODdBmj9PT9B1tMHfafFT03pGvq3IPfcz9U9xzU5mY4eaO8JZRo3hr0u0ra70ZA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=dREZVZNnc4voL32nCiikPtOer/bHmOMbDOzH2U0rrzo=; b=oyIasYMe90V57TqupCad8WjXQJmywtdOfK0dOD1+Xi/wbq3sxlLMvzfmeRFeKWvRnXh1IcEdeocGOA3/T4YLiglA9NiiDGYsSk0HM9lngJAVpbgcD12PQVuQgNg4Be6pq+MKNVECdJaxfx357Ret+JaG5KqSv2iSL7Or23CNPMGd9UChg4BkokfSQ9vQ5lNmmAU7ioGsF+zUxoJDWHL6vFpgbgGJf7OYjf69nO+EdG+nYDmUe+xh1eo5W/Hio5/9On4IDWhZNDkbGJ2lR1WPsTiUAE7ve4ZMalum0BeGxKbel6e3Ae6fmkufM5DAV5TdeABK+28bx2YWb9LHdyZrPw== Received: from AM9PR04MB8940.eurprd04.prod.outlook.com (2603:10a6:20b:40b::6) by AS8PR04MB8436.eurprd04.prod.outlook.com (2603:10a6:20b:347::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.14; Thu, 30 Jun 2022 00:03:13 +0000 Received: from AM9PR04MB8940.eurprd04.prod.outlook.com ([fe80::d976:d179:896b:6a2a]) by AM9PR04MB8940.eurprd04.prod.outlook.com ([fe80::d976:d179:896b:6a2a%4]) with mapi id 15.20.5395.014; Thu, 30 Jun 2022 00:03:13 +0000 From: mohamed Elhadidy To: ffmpeg-devel@ffmpeg.org Date: Thu, 30 Jun 2022 02:01:53 +0200 Message-ID: X-Mailer: git-send-email 2.36.1 X-TMN: [/HaQC10tSWGWBqa1hZ35krn8ppTbrjNDmm2xeeSNJ6Y=] X-ClientProxiedBy: MR1P264CA0070.FRAP264.PROD.OUTLOOK.COM (2603:10a6:501:3f::18) To AM9PR04MB8940.eurprd04.prod.outlook.com (2603:10a6:20b:40b::6) X-Microsoft-Original-Message-ID: <20220630000152.102161-1-mohamedpower_50@hotmail.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 2415e110-0382-493b-faa3-08da5a2becc4 X-MS-TrafficTypeDiagnostic: AS8PR04MB8436:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8SZC93cS94UiV8FCrUeutqoPGxzXwOoPz+FskDOK7TTq0xUbfniKcOOs5G7/p8EcYptJzcKeDI16IkFZqPTtNBJozJEayFN69MJR+/YISFOjwQjTOfpxQQDeqnx1I3mbEJI/hFNootoL8sj0FlWFY003r5dvCvunkw/X4gH5IwXK3QsRDszGh8IgKWY1TcplJVOnjhV2OGxb51yrxg7DfkKtqACJ4GG2GIGlOQWqXCTa9DAmGPJywjJIH5N2QQtjh8dxonrAL9qxBY7vegHUQMfzMmwkBW7t69OxtDqfaykmBboE3QsyT30tED3FrFb5CeBLFU80z7xNuWvolam98Ad7SN3565HusGWBa2/j3LB+nAHb1rJTrSCfyIhpKQUG2l6YU+onao3iYYlu5kGg9s3K7LGptgW9yJ/7Ary1OSzHUS7TWzVEy4qUmZRklLlNwEXsC85bCX4XKiCgh4P8FimIyoHjFWYVVmCgy2O0SE81LT5LnauqmmgYEOo7ovok0zEKWie0Sa9vPgBSzZZy5cu5hu66aJW2UWBWn0wytVsMLVqma9jybBhMm02lxIbHhu1uZ2pPaWuT58Hoda1hOFZjXARJ2C5AUCWbZ6svkY+3w2P2PyoVU8i7/D7Sqfzt X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7sPpo/35CW+1u6xdfLD7gjBd/UOgMHzlJ31mgccyNQaKPug4jTnt/1vrZiQPQFWiQEu5Fno4vJq1ihztuFftoWEiSDpGP46uZPVIL6lN3y0O78avD18BevZCnseTSj8q4T83d1AC0NRRA6TRObKt8qH5+04I2oVvlobwVOdI26jnaQ4wMEJr3NVukaO4RITWc4cl6HkDFtUPybDXm6n7JoqHG3PgsJ0oRyjoKMiCNy8uZicquh/g4vDU18b+fnHhd9bC8tYdI41HHKjTkPyWBu6EuFV0dUG9kbBil9is/U0MaswZcPPZT4z2RLTtFHcEmKNVWceLOHXuE953TvJdaQkr0CNhKQo32paTqQPtsWbq2ib+2v4c/4RyWwYUEdPkySW0W9yxd/8dgqOXDb10V/IMn9gcGeNJlM8NXFMMTQxUbmXF6tPr7ik0NCq+CeiBUJVpLBEQB1Fr6X3+dr1RvWkL3wBzNu5k6AtkM7WQHOcQAwyDwKaHHLdUU9kt0CEMUu8jYm+JYv6zL+PSrQW/EJYiy4UZuxwXarZAWWNOJ7jY5A2LTjv13d2ajX7XtnlVCcktGWjW2MlSuvWXzl4seAD7r2sWvGfLHMdWCf/ZYLQH8LfHebE7M7J20hxlmo43s5E4JBn5048lDiHfvwEfwdDWC+kzRhhjA4OhRPySWgVOfLxLR4u4DtlDXAVwv3oMJ4fLedgwt41ewp7gkwzcP5+Z/Z6metwunjJjq32WmOfwOiGxsm2iHetnQ2yMpxgvWRi0G9hyFJosc+uhcbcf6TjcosVfIcSDFO+fBOShxNmTsDl59SglsyJ0kWf18BapbTdD6f1eQyGWkS8fW8mvR9XeqcIiXNb0O9ykTV40+jc9yh07pCptE7HPR24vALKW2KogW06JTAgvG4Y83bot6ugQrneU6ATmGgc1KIo8gr77cJJAQL4R+0xYSbehpnUFlMK/xBu6Gi6K7cMV6OU/cILvV8Y9G6Ung6sfyp1WoYDQs/vvxBgwAjVSI+95x/v1QOzcwMeFTj6aLkVNnO0heP+8d/s7FsrFy+rraNecJdqqEDYDf4Qq5p7BE9bAAKLP2AUyEOS8b7oceW0lI2xj5VInLLGwu/+y8oo6qoIrN/JsW3qHGyv3bw3qDDnFAO3bQgEeCIQyyx5sokDlPavQtwuPRH9xQd2BH3sV8J59HPIURxFvbj4dDOG9hiKgZS1TkNu2BbYA4z+A2Ct6hWYdbBeIgSKWcHOI/1XMtoQHetnajwbmoGYtNK/StJQdC06lsE43ByC1Pj1lBS5QsempX2LxuHNQ/CfZb3ql+gdydKmbZvVUgVD5+o4cVuHqAta8 X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-03a34.templateTenant X-MS-Exchange-CrossTenant-Network-Message-Id: 2415e110-0382-493b-faa3-08da5a2becc4 X-MS-Exchange-CrossTenant-AuthSource: AM9PR04MB8940.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2022 00:03:13.2521 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR04MB8436 Subject: [FFmpeg-devel] [GSoC'22] Added Chromakey CUDA filter 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: Mohamed Khaled Mohamed <56936494+MohamedElhadidy0019@users.noreply.github.com> Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: AHawmZbZZ1n3 From: Mohamed Khaled Mohamed <56936494+MohamedElhadidy0019@users.noreply.github.com> GSoC'22 GPU accelerated video filters Added CUDA chromakeyfilter libavfilter/vf_chromakey_cuda.cu:the CUDA kernel for the filter libavfilter/vf_chromakey_cuda.c: the C side that calls the kernel and gets user input libavfilter/allfilters.c: added the filter to it libavfilter/Makefile: added the filter to it cuda/cuda_runtime.h: added two math CUDA functions that are used in the filter --- compat/cuda/cuda_runtime.h | 2 + libavfilter/Makefile | 2 + libavfilter/allfilters.c | 1 + libavfilter/vf_chromakey_cuda.c | 520 +++++++++++++++++++++++++++++++ libavfilter/vf_chromakey_cuda.cu | 248 +++++++++++++++ 5 files changed, 773 insertions(+) create mode 100644 libavfilter/vf_chromakey_cuda.c create mode 100644 libavfilter/vf_chromakey_cuda.cu diff --git a/compat/cuda/cuda_runtime.h b/compat/cuda/cuda_runtime.h index 30cd085e48..5837c1ad37 100644 --- a/compat/cuda/cuda_runtime.h +++ b/compat/cuda/cuda_runtime.h @@ -181,7 +181,9 @@ static inline __device__ double trunc(double a) { return __builtin_trunc(a); } static inline __device__ float fabsf(float a) { return __builtin_fabsf(a); } static inline __device__ float fabs(float a) { return __builtin_fabsf(a); } static inline __device__ double fabs(double a) { return __builtin_fabs(a); } +static inline __device__ float sqrtf(float a) { return __builtin_sqrtf(a); } +static inline __device__ float __saturatef(float a) { return __nvvm_saturate_f(a); } static inline __device__ float __sinf(float a) { return __nvvm_sin_approx_f(a); } static inline __device__ float __cosf(float a) { return __nvvm_cos_approx_f(a); } static inline __device__ float __expf(float a) { return __nvvm_ex2_approx_f(a * (float)__builtin_log2(__builtin_exp(1))); } diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 22b0a0ca15..8aee10fc76 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -210,6 +210,8 @@ OBJS-$(CONFIG_CAS_FILTER) += vf_cas.o OBJS-$(CONFIG_CHROMABER_VULKAN_FILTER) += vf_chromaber_vulkan.o vulkan.o vulkan_filter.o OBJS-$(CONFIG_CHROMAHOLD_FILTER) += vf_chromakey.o OBJS-$(CONFIG_CHROMAKEY_FILTER) += vf_chromakey.o +OBJS-$(CONFIG_CHROMAKEY_CUDA_FILTER) += vf_chromakey_cuda.o vf_chromakey_cuda.ptx.o framesync.o + OBJS-$(CONFIG_CHROMANR_FILTER) += vf_chromanr.o OBJS-$(CONFIG_CHROMASHIFT_FILTER) += vf_chromashift.o OBJS-$(CONFIG_CIESCOPE_FILTER) += vf_ciescope.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index ec70feef11..da1a96b23c 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -195,6 +195,7 @@ extern const AVFilter ff_vf_cas; extern const AVFilter ff_vf_chromaber_vulkan; extern const AVFilter ff_vf_chromahold; extern const AVFilter ff_vf_chromakey; +extern const AVFilter ff_vf_chromakey_cuda; extern const AVFilter ff_vf_chromanr; extern const AVFilter ff_vf_chromashift; extern const AVFilter ff_vf_ciescope; diff --git a/libavfilter/vf_chromakey_cuda.c b/libavfilter/vf_chromakey_cuda.c new file mode 100644 index 0000000000..822488d436 --- /dev/null +++ b/libavfilter/vf_chromakey_cuda.c @@ -0,0 +1,520 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +* DEALINGS IN THE SOFTWARE. +*/ + +#include +#include +#include + +#include "libavutil/avstring.h" +#include "libavutil/common.h" +#include "libavutil/hwcontext.h" +#include "libavutil/hwcontext_cuda_internal.h" +#include "libavutil/cuda_check.h" +#include "libavutil/internal.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" + +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +#include "cuda/load_helper.h" + + + +#define FIXNUM(x) lrint((x) * (1 << 10)) +#define RGB_TO_U(rgb) (((- FIXNUM(0.16874) * rgb[0] - FIXNUM(0.33126) * rgb[1] + FIXNUM(0.50000) * rgb[2] + (1 << 9) - 1) >> 10) + 128) +#define RGB_TO_V(rgb) ((( FIXNUM(0.50000) * rgb[0] - FIXNUM(0.41869) * rgb[1] - FIXNUM(0.08131) * rgb[2] + (1 << 9) - 1) >> 10) + 128) + + +static const enum AVPixelFormat supported_formats[] = { + AV_PIX_FMT_YUV420P, + AV_PIX_FMT_NV12, + AV_PIX_FMT_YUV444P, + AV_PIX_FMT_YUVA420P + +}; + +#define DIV_UP(a, b) ( ((a) + (b) - 1) / (b) ) +#define BLOCKX 32 +#define BLOCKY 16 + +#define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, s->hwctx->internal->cuda_dl, x) + + +typedef struct ChromakeyCUDAContext { + const AVClass *class; + + AVCUDADeviceContext *hwctx; + + enum AVPixelFormat in_fmt, out_fmt; + const AVPixFmtDescriptor *in_desc, *out_desc; + int in_planes, out_planes; + int in_plane_depths[4]; + int in_plane_channels[4]; + + + uint8_t chromakey_rgba[4]; + uint16_t chromakey_uv[2]; + int is_yuv; + float similarity; + float blend; + + + + AVBufferRef *frames_ctx; + AVFrame *frame; + + AVFrame *tmp_frame; + + char *w_expr; ///< width expression string + char *h_expr; ///< height expression string + + CUcontext cu_ctx; + CUmodule cu_module; + CUfunction cu_func; + CUfunction cu_func_uv; + CUstream cu_stream; +} ChromakeyCUDAContext; + +static av_cold int cudachromakey_init(AVFilterContext *ctx) +{ + ChromakeyCUDAContext *s = ctx->priv; + + s->frame = av_frame_alloc(); + if (!s->frame) + return AVERROR(ENOMEM); + + s->tmp_frame = av_frame_alloc(); + if (!s->tmp_frame) + return AVERROR(ENOMEM); + + return 0; +} + +static av_cold void cudachromakey_uninit(AVFilterContext *ctx) +{ + ChromakeyCUDAContext *s = ctx->priv; + + if (s->hwctx && s->cu_module) { + CudaFunctions *cu = s->hwctx->internal->cuda_dl; + CUcontext context; + + CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx)); + CHECK_CU(cu->cuModuleUnload(s->cu_module)); + s->cu_module = NULL; + CHECK_CU(cu->cuCtxPopCurrent(&context)); + } + + av_frame_free(&s->frame); + av_buffer_unref(&s->frames_ctx); + av_frame_free(&s->tmp_frame); +} + +static av_cold int init_hwframe_ctx(ChromakeyCUDAContext *s, AVBufferRef *device_ctx, int width, int height) +{ + AVBufferRef *out_ref = NULL; + AVHWFramesContext *out_ctx; + int ret; + + out_ref = av_hwframe_ctx_alloc(device_ctx); + if (!out_ref) + return AVERROR(ENOMEM); + out_ctx = (AVHWFramesContext*)out_ref->data; + + out_ctx->format = AV_PIX_FMT_CUDA; + out_ctx->sw_format = s->out_fmt; + out_ctx->width = width; + out_ctx->height = height; + + ret = av_hwframe_ctx_init(out_ref); + if (ret < 0) + goto fail; + + av_frame_unref(s->frame); + ret = av_hwframe_get_buffer(out_ref, s->frame, 0); + if (ret < 0) + goto fail; + + av_buffer_unref(&s->frames_ctx); + s->frames_ctx = out_ref; + + return 0; +fail: + av_buffer_unref(&out_ref); + return ret; +} + +static int format_is_supported(enum AVPixelFormat fmt) +{ + int i; + + for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) + if (supported_formats[i] == fmt) + return 1; + return 0; +} + +static av_cold void set_format_info(AVFilterContext *ctx, enum AVPixelFormat in_format, enum AVPixelFormat out_format) +{ + ChromakeyCUDAContext *s = ctx->priv; + int i, p, d; + + s->in_fmt = in_format; + s->out_fmt = out_format; + + s->in_desc = av_pix_fmt_desc_get(s->in_fmt); + s->out_desc = av_pix_fmt_desc_get(s->out_fmt); + s->in_planes = av_pix_fmt_count_planes(s->in_fmt); + s->out_planes = av_pix_fmt_count_planes(s->out_fmt); + + // find maximum step of each component of each plane + // For our subset of formats, this should accurately tell us how many channels CUDA needs + // i.e. 1 for Y plane, 2 for UV plane of NV12, 4 for single plane of RGB0 formats + + for (i = 0; i < s->in_desc->nb_components; i++) { + d = (s->in_desc->comp[i].depth + 7) / 8; + p = s->in_desc->comp[i].plane; + s->in_plane_channels[p] = FFMAX(s->in_plane_channels[p], s->in_desc->comp[i].step / d); + + s->in_plane_depths[p] = s->in_desc->comp[i].depth; + } +} + +static av_cold int init_processing_chain(AVFilterContext *ctx, int width, int height) +{ + ChromakeyCUDAContext *s = ctx->priv; + AVHWFramesContext *in_frames_ctx; + int ret; + + /* check that we have a hw context */ + if (!ctx->inputs[0]->hw_frames_ctx) { + av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n"); + return AVERROR(EINVAL); + } + in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data; + + if (!format_is_supported(in_frames_ctx->sw_format)) { + av_log(ctx, AV_LOG_ERROR, "Unsupported format: %s\n", av_get_pix_fmt_name(in_frames_ctx->sw_format)); + return AVERROR(ENOSYS); + } + + set_format_info(ctx, in_frames_ctx->sw_format,AV_PIX_FMT_YUVA420P ); + + ret = init_hwframe_ctx(s, in_frames_ctx->device_ref, width, height); + if (ret < 0) + return ret; + + ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->frames_ctx); + if (!ctx->outputs[0]->hw_frames_ctx) + return AVERROR(ENOMEM); + + return 0; +} + +static av_cold int cudachromakey_load_functions(AVFilterContext *ctx) +{ + ChromakeyCUDAContext *s = ctx->priv; + CUcontext context, cuda_ctx = s->hwctx->cuda_ctx; + CudaFunctions *cu = s->hwctx->internal->cuda_dl; + int ret; + + extern const unsigned char ff_vf_chromakey_cuda_ptx_data[]; + extern const unsigned int ff_vf_chromakey_cuda_ptx_len; + + ret = CHECK_CU(cu->cuCtxPushCurrent(cuda_ctx)); + if (ret < 0) + return ret; + + ret = ff_cuda_load_module(ctx, s->hwctx, &s->cu_module, + ff_vf_chromakey_cuda_ptx_data, ff_vf_chromakey_cuda_ptx_len); + if (ret < 0) + goto fail; + + ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func, s->cu_module, "Process_uchar")); + if (ret < 0) { + av_log(ctx, AV_LOG_FATAL, "Failed loading Process_uchar\n"); + goto fail; + } + + ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uv, s->cu_module, "Process_uchar2")); + if (ret < 0) { + av_log(ctx, AV_LOG_FATAL, "Failed loading Process_uchar2\n"); + goto fail; + } + +fail: + CHECK_CU(cu->cuCtxPopCurrent(&context)); + + return ret; +} + +static av_cold int cudachromakey_config_props(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + AVFilterLink *inlink = outlink->src->inputs[0]; + ChromakeyCUDAContext *s = ctx->priv; + AVHWFramesContext *frames_ctx = (AVHWFramesContext*)inlink->hw_frames_ctx->data; + AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx; + int ret; + + s->hwctx = device_hwctx; + s->cu_stream = s->hwctx->stream; + + ret = init_processing_chain(ctx, inlink->w, inlink->h); + if (ret < 0) + return ret; + + outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; + + ret = cudachromakey_load_functions(ctx); + if (ret < 0) + return ret; + + return 0; +} + +static int call_cuda_kernel(AVFilterContext *ctx, CUfunction func, + CUtexObject src_tex[3], AVFrame *out_frame, + int width, int height, int pitch, + int width_uv, int height_uv, int pitch_uv, + float u_key,float v_key, float similarity, + float blend ) +{ + ChromakeyCUDAContext *s = ctx->priv; + CudaFunctions *cu = s->hwctx->internal->cuda_dl; + int ret; + + CUdeviceptr dst_devptr[4] = { + (CUdeviceptr)out_frame->data[0], (CUdeviceptr)out_frame->data[1], + (CUdeviceptr)out_frame->data[2], (CUdeviceptr)out_frame->data[3] + + }; + + void *args_uchar[] = { + &src_tex[0], &src_tex[1], &src_tex[2], + &dst_devptr[0], &dst_devptr[1], &dst_devptr[2],&dst_devptr[3], + &width, &height, &pitch, + &width_uv, &height_uv, &pitch_uv,&u_key,&v_key,&similarity,&blend + }; + + ret = CHECK_CU(cu->cuLaunchKernel(func, + DIV_UP(width, BLOCKX), DIV_UP(height, BLOCKY), 1, + BLOCKX, BLOCKY, 1, 0, s->cu_stream, args_uchar, NULL)); + if (ret < 0) + return ret; + + // This ruins performance, but catches errors that happened in the Kernel immediately + // Remove for performance/production test + ret = CHECK_CU(cu->cuStreamSynchronize(s->cu_stream)); + + return ret; +} + +static int cudachromakey_process_internal(AVFilterContext *ctx, + AVFrame *out, AVFrame *in) +{ + ChromakeyCUDAContext *s = ctx->priv; + CudaFunctions *cu = s->hwctx->internal->cuda_dl; + CUcontext context, cuda_ctx = s->hwctx->cuda_ctx; + float u_key,v_key; + int i, ret; + + CUtexObject tex[3] = { 0, 0, 0 }; + if(s->is_yuv){ + u_key=s->chromakey_rgba[1]; + v_key=s->chromakey_rgba[2]; + + }else{ + u_key = RGB_TO_U(s->chromakey_rgba); + v_key = RGB_TO_V(s->chromakey_rgba); + } + ret = CHECK_CU(cu->cuCtxPushCurrent(cuda_ctx)); + if (ret < 0) + return ret; + + for (i = 0; i < s->in_planes; i++) { + CUDA_TEXTURE_DESC tex_desc = { + .filterMode = CU_TR_FILTER_MODE_LINEAR, + .flags = 0, // CU_TRSF_READ_AS_INTEGER to get raw ints instead of normalized floats from tex2D + }; + + CUDA_RESOURCE_DESC res_desc = { + .resType = CU_RESOURCE_TYPE_PITCH2D, + .res.pitch2D.format = CU_AD_FORMAT_UNSIGNED_INT8, + .res.pitch2D.numChannels = s->in_plane_channels[i], + .res.pitch2D.pitchInBytes = in->linesize[i], + .res.pitch2D.devPtr = (CUdeviceptr)in->data[i], + }; + + if (i == 1 || i == 2) { + res_desc.res.pitch2D.width = AV_CEIL_RSHIFT(in->width, s->in_desc->log2_chroma_w); + res_desc.res.pitch2D.height = AV_CEIL_RSHIFT(in->height, s->in_desc->log2_chroma_h); + } else { + res_desc.res.pitch2D.width = in->width; + res_desc.res.pitch2D.height = in->height; + } + + ret = CHECK_CU(cu->cuTexObjectCreate(&tex[i], &res_desc, &tex_desc, NULL)); + if (ret < 0) + goto exit; + } + + ret = call_cuda_kernel(ctx, (s->in_plane_channels[1] > 1) ? s->cu_func_uv : s->cu_func, + tex, out, + out->width, out->height, out->linesize[0], + AV_CEIL_RSHIFT(out->width, s->out_desc->log2_chroma_w), + AV_CEIL_RSHIFT(out->height, s->out_desc->log2_chroma_h), + out->linesize[1], + u_key,v_key,s->similarity,s->blend ); + if (ret < 0) + goto exit; + +exit: + for (i = 0; i < s->in_planes; i++) + if (tex[i]) + CHECK_CU(cu->cuTexObjectDestroy(tex[i])); + + CHECK_CU(cu->cuCtxPopCurrent(&context)); + + return ret; +} + +static int cudachromakey_process(AVFilterContext *ctx, AVFrame *out, AVFrame *in) +{ + ChromakeyCUDAContext *s = ctx->priv; + AVFrame *src = in; + int ret; + + ret = cudachromakey_process_internal(ctx, s->frame, src); + if (ret < 0) + return ret; + + src = s->frame; + ret = av_hwframe_get_buffer(src->hw_frames_ctx, s->tmp_frame, 0); + if (ret < 0) + return ret; + + av_frame_move_ref(out, s->frame); + av_frame_move_ref(s->frame, s->tmp_frame); + + ret = av_frame_copy_props(out, in); + if (ret < 0) + return ret; + + return 0; +} + +static int cudachromakey_filter_frame(AVFilterLink *link, AVFrame *in) +{ + AVFilterContext *ctx = link->dst; + ChromakeyCUDAContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + CudaFunctions *cu = s->hwctx->internal->cuda_dl; + + AVFrame *out = NULL; + CUcontext context; + int ret = 0; + + out = av_frame_alloc(); + if (!out) { + ret = AVERROR(ENOMEM); + goto fail; + } + + ret = CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx)); + if (ret < 0) + goto fail; + + ret = cudachromakey_process(ctx, out, in); + + CHECK_CU(cu->cuCtxPopCurrent(&context)); + if (ret < 0) + goto fail; + + av_frame_free(&in); + return ff_filter_frame(outlink, out); +fail: + av_frame_free(&in); + av_frame_free(&out); + return ret; +} + +static AVFrame *cudachromakey_get_video_buffer(AVFilterLink *inlink, int w, int h) +{ + return ff_default_get_video_buffer(inlink, w, h); +} + +#define OFFSET(x) offsetof(ChromakeyCUDAContext, x) +#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM) +static const AVOption options[] = { + { "color", "set the chromakey key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS }, + { "similarity", "set the chromakey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS }, + { "blend", "set the chromakey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS }, + { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, + { "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 }, + { NULL }, +}; + +static const AVClass cudachromakey_class = { + .class_name = "cudachromakey", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +static const AVFilterPad cudachromakey_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = cudachromakey_filter_frame, + .get_buffer.video = cudachromakey_get_video_buffer, + }, +}; + +static const AVFilterPad cudachromakey_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = cudachromakey_config_props, + }, +}; + +const AVFilter ff_vf_chromakey_cuda = { + .name = "chromakey_cuda", + .description = NULL_IF_CONFIG_SMALL("GPU accelerated chromakey filter"), + + .init = cudachromakey_init, + .uninit = cudachromakey_uninit, + + .priv_size = sizeof(ChromakeyCUDAContext), + .priv_class = &cudachromakey_class, + + FILTER_INPUTS(cudachromakey_inputs), + FILTER_OUTPUTS(cudachromakey_outputs), + + FILTER_SINGLE_PIXFMT(AV_PIX_FMT_CUDA), + + .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE, +}; diff --git a/libavfilter/vf_chromakey_cuda.cu b/libavfilter/vf_chromakey_cuda.cu new file mode 100644 index 0000000000..fef118a0f8 --- /dev/null +++ b/libavfilter/vf_chromakey_cuda.cu @@ -0,0 +1,248 @@ +/* + * Copyright (c) 2022, Chromakey + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include "cuda/vector_helpers.cuh" + +extern "C" +{ + + +/** + * @brief function contains the main logic of chroma keying, and changes the alpahc channel with the suitable value + * + * @param src_tex texture U or texture UV , decided based on the passed is_uchar2 flag + * @param src_tex_V texture V , used only if is_uchar2 flag is false + * @param dst_A alpha channel destination + * @param width_uv width of uv channels + * @param height_uv height of uv channels + * @param width width of alpha channel + * @param height height of alpha channel + * @param pitch pitch of alpha channel + * @param x current x coordinate of pixel + * @param y current y coordinate of pixel + * @param chromakey_uv uv values for chroma keying + * @param similarity similarity of keying + * @param blend blend of keying + * @param is_uchar2 true means that sent texture is uchar2 for the UV channels, false means U and V are sent seperatley + * @param resize_ratio the ratio of alpha channel to UV channels + * @return void + */ +__device__ static inline void change_alpha_channel( + cudaTextureObject_t src_tex, cudaTextureObject_t src_tex_V, + uchar *dst_A, int width_uv, + int height_uv, int width, + int height, int pitch, + int x, int y, + float2 chromakey_uv, float similarity, + float blend, bool is_uchar2, + uchar resize_ratio) +{ + + uchar window_size = 3; + int start_r = x - window_size / 2; + int start_c = y - window_size / 2; + + uchar counter = 0; + float diff = 0.0f; + float du, dv; + + // loop over the eight neighbourhood of the current pixel(x,y) + for (uchar i = 0; i < window_size; i++) + { + for (uchar j = 0; j < window_size; j++) + { + int r = start_r + i; + int c = start_c + j; + bool check_flag = (r >= 0 && r < width_uv && c >= 0 && c < height_uv); + + if (!check_flag) + continue; + + float u_value, v_value; + if (is_uchar2){ + float2 temp_uv = tex2D(src_tex, r, c); + u_value = temp_uv.x; + v_value = temp_uv.y; + }else{ + u_value = tex2D(src_tex, r, c); + v_value = tex2D(src_tex_V, r, c); + } + + du = (u_value * 255.0f) - chromakey_uv.x; + dv = (v_value * 255.0f) - chromakey_uv.y; + diff += sqrtf((du * du + dv * dv) / (255.0f * 255.0f * 2.f)); + counter++; + } + } + + if (counter > 0){ + diff = diff / counter; + }else{ + diff /= 9.0f; + } + + uchar alpha_value; + if(blend>0.0001f){ + alpha_value=__saturatef((diff - similarity) / blend)*255; + }else{ + alpha_value=(diff < similarity ? 0 : 1)*255; + } + + //write the value in the aloha channel with regarding the ratio of (alpha_size : uv_size) + for (uchar k = 0; k < resize_ratio; k++) + { + for (uchar l = 0; l < resize_ratio; l++) + { + int x_resize = x * resize_ratio + k; + int y_resize = y * resize_ratio + l; + int a_channel_resize = y_resize * pitch + x_resize; + if (y_resize >= height || x_resize >= width) + continue; + dst_A[a_channel_resize] = alpha_value; + } + } + + +} + + +/** + * @brief Function to process yuv420p video , and pass it to change_alpha_channel function, the output video is yuva420p + * + * @param src_tex_Y Y channel of input video frame + * @param src_tex_U U channel of input video frame + * @param src_tex_V V channel of input video frame + * @param dst_Y Y channel of output video frame + * @param dst_U U channel of output video frame + * @param dst_V V channel of output video frame + * @param dst_A A channel of output video frame + * @param width width of Y channel for input,video frame + * @param height height of Y channel for input,video frame + * @param pitch pitch(line size) of Y channel for input,video frame + * @param width_uv width of UV for input,output video frame + * @param height_uv height of UV for input,output video frame + * @param pitch_uv pitch(line size) of UV for input,output video frame + * @param u_key u part of chromakey (user input) + * @param v_key v part of chromakey (user input) + * @param similarity similairty user want (user input) + * @param blend blend user want (user input) + * @return void + */ +__global__ void Process_uchar( + cudaTextureObject_t src_tex_Y, cudaTextureObject_t src_tex_U, + cudaTextureObject_t src_tex_V,uchar *dst_Y, + uchar *dst_U, uchar *dst_V, + uchar *dst_A,int width, + int height, int pitch, + int width_uv, int height_uv, + int pitch_uv,float u_key, + float v_key, float similarity, + float blend) +{ + + uchar resize_ratio = 2; + int x = blockIdx.x * blockDim.x + threadIdx.x; + int y = blockIdx.y * blockDim.y + threadIdx.y; + + if (y >= height || x >= width) + return; + dst_Y[y * pitch + x] = tex2D(src_tex_Y, x, y)*255; + if (y >= height_uv || x >= width_uv) + return; + + int uv_index = y * pitch_uv + x; + dst_U[uv_index]=tex2D(src_tex_U,x,y)*255; + dst_V[uv_index]=tex2D(src_tex_V,x,y)*255; + + + + change_alpha_channel(src_tex_U,src_tex_V, + dst_A,width_uv,height_uv, + width,height, + pitch,x, + y,make_float2(u_key,v_key), + similarity,blend, + false,resize_ratio); + +} + + + +/** + * @brief Function to process nv12 video , and pass it to change_alpha_channel function, the output video is yuva420p + * + * @param src_tex_Y Y channel of input video frame + * @param src_tex_UV UV channels of input video frame + * @param unused1 unused parameter + * @param dst_Y Y channel of output video frame + * @param dst_U U channel of output video frame + * @param dst_V V channel of output video frame + * @param dst_A A channel of output video frame + * @param width width of Y channel for input,video frame + * @param height height of Y channel for input,video frame + * @param pitch pitch(line size) of Y channel for input,video frame + * @param width_uv width of UV for input,output video frame + * @param height_uv height of UV for input,output video frame + * @param pitch_uv pitch(line size) of UV for input, output video frame + * @param u_key u part of chromakey (user input) + * @param v_key v part of chromakey (user input) + * @param similarity similairty user want (user input) + * @param blend blend user want (user input) + * @return void + */ +__global__ void Process_uchar2( + cudaTextureObject_t src_tex_Y, cudaTextureObject_t src_tex_UV, + cudaTextureObject_t unused1,uchar *dst_Y, + uchar *dst_U, uchar *dst_V, + uchar *dst_A,int width, + int height, int pitch, + int width_uv, int height_uv, + int pitch_uv,float u_key, + float v_key, float similarity, + float blend) +{ + + uchar resize_ratio = 2; // alphachannel_size : uv_channel_size ratio + int x = blockIdx.x * blockDim.x + threadIdx.x; // x coordinate of current pixel + int y = blockIdx.y * blockDim.y + threadIdx.y; // y coordinate of current pixel + + if (y >= height || x >= width) + return; + dst_Y[y * pitch + x] = tex2D(src_tex_Y, x, y)*255; + + if (y >= height_uv || x >= width_uv) + return; + int uv_index= y * pitch_uv + x; + float2 uv_temp=tex2D(src_tex_UV,x,y); + dst_U[uv_index]=uv_temp.x*255; + dst_V[uv_index]=uv_temp.y*255; + + change_alpha_channel(src_tex_UV, unused1, + dst_A, width_uv, + height_uv, width, + height, pitch, + x, y, + make_float2(u_key,v_key), similarity, + blend, true, + resize_ratio); +} +}