[FFmpeg-devel] [PATCH 07/23] avutil/hwcontext_opencl: Allocate pub and priv device hwctx together

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Mon Feb 12 02:03:19 EET 2024


This is possible because the lifetime of both coincide.
Besides reducing the number of allocations this also simplifies
access to OpenCLDeviceContext as one no longer has to
go through AVHWDeviceInternal.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavutil/hwcontext_opencl.c | 50 +++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 23 deletions(-)

diff --git a/libavutil/hwcontext_opencl.c b/libavutil/hwcontext_opencl.c
index c1764ed098..54c050ab40 100644
--- a/libavutil/hwcontext_opencl.c
+++ b/libavutil/hwcontext_opencl.c
@@ -80,6 +80,11 @@ extern int ff_qsv_get_surface_base_handle(mfxFrameSurface1 *surf,
 
 
 typedef struct OpenCLDeviceContext {
+    /**
+     * The public AVOpenCLDeviceContext. See hwcontext_opencl.h for it.
+     */
+    AVOpenCLDeviceContext p;
+
     // Default command queue to use for transfer/mapping operations on
     // the device.  If the user supplies one, this is a reference to it.
     // Otherwise, it is newly-created.
@@ -276,8 +281,8 @@ static int opencl_check_device_extension(cl_device_id device_id,
 static av_unused int opencl_check_extension(AVHWDeviceContext *hwdev,
                                             const char *name)
 {
-    AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
-    OpenCLDeviceContext    *priv = hwdev->internal->priv;
+    OpenCLDeviceContext    *priv = hwdev->hwctx;
+    AVOpenCLDeviceContext *hwctx = &priv->p;
 
     if (opencl_check_platform_extension(priv->platform_id, name)) {
         av_log(hwdev, AV_LOG_DEBUG,
@@ -658,8 +663,8 @@ static int opencl_device_create(AVHWDeviceContext *hwdev, const char *device,
 
 static int opencl_device_init(AVHWDeviceContext *hwdev)
 {
-    AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
-    OpenCLDeviceContext    *priv = hwdev->internal->priv;
+    OpenCLDeviceContext    *priv = hwdev->hwctx;
+    AVOpenCLDeviceContext *hwctx = &priv->p;
     cl_int cle;
 
     if (hwctx->command_queue) {
@@ -894,7 +899,7 @@ static int opencl_device_init(AVHWDeviceContext *hwdev)
 
 static void opencl_device_uninit(AVHWDeviceContext *hwdev)
 {
-    OpenCLDeviceContext *priv = hwdev->internal->priv;
+    OpenCLDeviceContext *priv = hwdev->hwctx;
     cl_int cle;
 
     if (priv->command_queue) {
@@ -1685,7 +1690,7 @@ fail:
 static int opencl_frames_init_command_queue(AVHWFramesContext *hwfc)
 {
     AVOpenCLFramesContext *hwctx = hwfc->hwctx;
-    OpenCLDeviceContext *devpriv = hwfc->device_ctx->internal->priv;
+    OpenCLDeviceContext *devpriv = hwfc->device_ctx->hwctx;
     OpenCLFramesContext    *priv = hwfc->internal->priv;
     cl_int cle;
 
@@ -2079,8 +2084,8 @@ static int opencl_map_from_drm_beignet(AVHWFramesContext *dst_fc,
                                        AVFrame *dst, const AVFrame *src,
                                        int flags)
 {
-    AVOpenCLDeviceContext *hwctx = dst_fc->device_ctx->hwctx;
-    OpenCLDeviceContext    *priv = dst_fc->device_ctx->internal->priv;
+    OpenCLDeviceContext    *priv = dst_fc->device_ctx->hwctx;
+    AVOpenCLDeviceContext *hwctx = &priv->p;
     DRMBeignetToOpenCLMapping *mapping;
     const AVDRMFrameDescriptor *desc;
     cl_int cle;
@@ -2211,7 +2216,7 @@ static void opencl_unmap_from_qsv(AVHWFramesContext *dst_fc,
                                   HWMapDescriptor *hwmap)
 {
     AVOpenCLFrameDescriptor    *desc = hwmap->priv;
-    OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
+    OpenCLDeviceContext *device_priv = dst_fc->device_ctx->hwctx;
     OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
     cl_event event;
     cl_int cle;
@@ -2246,8 +2251,8 @@ static int opencl_map_from_qsv(AVHWFramesContext *dst_fc, AVFrame *dst,
 {
     AVHWFramesContext *src_fc =
         (AVHWFramesContext*)src->hw_frames_ctx->data;
-    AVOpenCLDeviceContext   *dst_dev = dst_fc->device_ctx->hwctx;
-    OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
+    OpenCLDeviceContext *device_priv = dst_fc->device_ctx->hwctx;
+    AVOpenCLDeviceContext   *dst_dev = &device_priv->p;
     OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
     AVOpenCLFrameDescriptor *desc;
     VASurfaceID va_surface;
@@ -2345,7 +2350,7 @@ static void opencl_unmap_from_dxva2(AVHWFramesContext *dst_fc,
                                     HWMapDescriptor *hwmap)
 {
     AVOpenCLFrameDescriptor    *desc = hwmap->priv;
-    OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
+    OpenCLDeviceContext *device_priv = dst_fc->device_ctx->hwctx;
     OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
     cl_event event;
     cl_int cle;
@@ -2370,7 +2375,7 @@ static int opencl_map_from_dxva2(AVHWFramesContext *dst_fc, AVFrame *dst,
     AVHWFramesContext *src_fc =
         (AVHWFramesContext*)src->hw_frames_ctx->data;
     AVDXVA2FramesContext  *src_hwctx = src_fc->hwctx;
-    OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
+    OpenCLDeviceContext *device_priv = dst_fc->device_ctx->hwctx;
     OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
     AVOpenCLFrameDescriptor *desc;
     cl_event event;
@@ -2431,9 +2436,9 @@ fail:
 static int opencl_frames_derive_from_dxva2(AVHWFramesContext *dst_fc,
                                            AVHWFramesContext *src_fc, int flags)
 {
-    AVOpenCLDeviceContext   *dst_dev = dst_fc->device_ctx->hwctx;
     AVDXVA2FramesContext  *src_hwctx = src_fc->hwctx;
-    OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
+    OpenCLDeviceContext *device_priv = dst_fc->device_ctx->hwctx;
+    AVOpenCLDeviceContext   *dst_dev = &device_priv->p;
     OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
     cl_mem_flags cl_flags;
     cl_int cle;
@@ -2510,7 +2515,7 @@ static void opencl_unmap_from_d3d11(AVHWFramesContext *dst_fc,
                                     HWMapDescriptor *hwmap)
 {
     AVOpenCLFrameDescriptor    *desc = hwmap->priv;
-    OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
+    OpenCLDeviceContext *device_priv = dst_fc->device_ctx->hwctx;
     OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
     cl_event event;
     cl_int cle;
@@ -2529,7 +2534,7 @@ static void opencl_unmap_from_d3d11(AVHWFramesContext *dst_fc,
 static int opencl_map_from_d3d11(AVHWFramesContext *dst_fc, AVFrame *dst,
                                  const AVFrame *src, int flags)
 {
-    OpenCLDeviceContext  *device_priv = dst_fc->device_ctx->internal->priv;
+    OpenCLDeviceContext  *device_priv = dst_fc->device_ctx->hwctx;
     OpenCLFramesContext  *frames_priv = dst_fc->internal->priv;
     AVOpenCLFrameDescriptor *desc;
     cl_event event;
@@ -2587,9 +2592,9 @@ fail:
 static int opencl_frames_derive_from_d3d11(AVHWFramesContext *dst_fc,
                                            AVHWFramesContext *src_fc, int flags)
 {
-    AVOpenCLDeviceContext    *dst_dev = dst_fc->device_ctx->hwctx;
     AVD3D11VAFramesContext *src_hwctx = src_fc->hwctx;
-    OpenCLDeviceContext  *device_priv = dst_fc->device_ctx->internal->priv;
+    OpenCLDeviceContext  *device_priv = dst_fc->device_ctx->hwctx;
+    AVOpenCLDeviceContext    *dst_dev = &device_priv->p;
     OpenCLFramesContext  *frames_priv = dst_fc->internal->priv;
     cl_mem_flags cl_flags;
     cl_int cle;
@@ -2830,7 +2835,7 @@ static int opencl_map_from(AVHWFramesContext *hwfc, AVFrame *dst,
 static int opencl_map_to(AVHWFramesContext *hwfc, AVFrame *dst,
                          const AVFrame *src, int flags)
 {
-    av_unused OpenCLDeviceContext *priv = hwfc->device_ctx->internal->priv;
+    av_unused OpenCLDeviceContext *priv = hwfc->device_ctx->hwctx;
     av_assert0(dst->format == AV_PIX_FMT_OPENCL);
     switch (src->format) {
 #if HAVE_OPENCL_DRM_BEIGNET
@@ -2871,7 +2876,7 @@ static int opencl_map_to(AVHWFramesContext *hwfc, AVFrame *dst,
 static int opencl_frames_derive_to(AVHWFramesContext *dst_fc,
                                    AVHWFramesContext *src_fc, int flags)
 {
-    av_unused OpenCLDeviceContext *priv = dst_fc->device_ctx->internal->priv;
+    av_unused OpenCLDeviceContext *priv = dst_fc->device_ctx->hwctx;
     switch (src_fc->device_ctx->type) {
 #if HAVE_OPENCL_DRM_BEIGNET
     case AV_HWDEVICE_TYPE_DRM:
@@ -2932,8 +2937,7 @@ const HWContextType ff_hwcontext_type_opencl = {
     .type                   = AV_HWDEVICE_TYPE_OPENCL,
     .name                   = "OpenCL",
 
-    .device_hwctx_size      = sizeof(AVOpenCLDeviceContext),
-    .device_priv_size       = sizeof(OpenCLDeviceContext),
+    .device_hwctx_size      = sizeof(OpenCLDeviceContext),
     .frames_hwctx_size      = sizeof(AVOpenCLFramesContext),
     .frames_priv_size       = sizeof(OpenCLFramesContext),
 
-- 
2.34.1



More information about the ffmpeg-devel mailing list