gpu_context_api_va.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief a header that defines wrappers for internal GPU plugin-specific
7  * shared Video Acceleration device contexts
8  * and shared memory blobs which contain Video Acceleration surfaces
9  *
10  * @file gpu_context_api_va.hpp
11  */
12 #pragma once
13 
14 #include <memory>
15 #include <string>
16 
18 
19 #include <va/va.h>
20 
21 namespace InferenceEngine {
22 
23 namespace gpu {
24 /**
25 * @brief This class represents an abstraction for GPU plugin remote context
26 * which is shared with VA display object.
27 * The plugin object derived from this class can be obtained either with
28 * GetContext() method of Executable network or using CreateContext() Core call.
29 * @note User can also obtain OpenCL context handle from this class.
30 */
31 class VAContext : public ClContext {
32 public:
33  /**
34  * @brief A smart pointer to the VAContext object
35  */
36  using Ptr = std::shared_ptr<VAContext>;
37 
38  /**
39  * @brief VADisplay conversion operator for the VAContext object.
40  * @return Underlying VADisplay object handle
41  */
42  operator VADisplay() {
43  return _ObjFromParams<VADisplay, gpu_handle_param>(getParams(),
44  GPU_PARAM_KEY(VA_DEVICE),
45  GPU_PARAM_KEY(CONTEXT_TYPE), GPU_PARAM_VALUE(VA_SHARED));
46  }
47 };
48 
49 /**
50 * @brief This class represents an abstraction for GPU plugin remote blob
51 * which is shared with VA output surface.
52 * The plugin object derived from this class can be obtained with CreateBlob() call.
53 * @note User can also obtain OpenCL 2D image handle from this class.
54 */
55 class VASurfaceBlob : public ClImage2DBlob {
56 public:
57  /**
58  * @brief A smart pointer to the VASurfaceBlob object
59  */
60  using Ptr = std::shared_ptr<VASurfaceBlob>;
61 
62  /**
63  * @brief Creates a VASurfaceBlob object with the specified dimensions and layout.
64  * @param tensorDesc Tensor description
65  */
67 
68  /**
69  * @brief VASurfaceID conversion operator for the VASurfaceBlob object.
70  * @return VA surface handle
71  */
72  operator VASurfaceID() {
73  return _ObjFromParams<VASurfaceID, uint32_t>(getParams(),
74  GPU_PARAM_KEY(DEV_OBJECT_HANDLE),
75  GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(VA_SURFACE));
76  }
77 
78  /**
79  * @brief Returns plane ID of underlying video decoder surface
80  */
81  uint32_t plane() {
82  return _ObjFromParams<uint32_t, uint32_t>(getParams(),
83  GPU_PARAM_KEY(VA_PLANE),
84  GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(VA_SURFACE));
85  }
86 };
87 
88 /**
89 * @brief This function is used to obtain a NV12 compound blob object from NV12 VA decoder output.
90 * The resulting compound contains two remote blobs for Y and UV planes of the surface.
91 */
92 static inline Blob::Ptr make_shared_blob_nv12(size_t height, size_t width, RemoteContext::Ptr ctx, VASurfaceID nv12_surf) {
93  auto casted = std::dynamic_pointer_cast<VAContext>(ctx);
94  if (nullptr == casted) {
95  THROW_IE_EXCEPTION << "Invalid remote context passed";
96  }
97 
98  // despite of layout, blob dimensions always follow in N,C,H,W order
99  TensorDesc ydesc(Precision::U8, { 1, 1, height, width }, Layout::NHWC);
100  ParamMap blobParams = {
101  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(VA_SURFACE) },
102  { GPU_PARAM_KEY(DEV_OBJECT_HANDLE), nv12_surf },
103  { GPU_PARAM_KEY(VA_PLANE), uint32_t(0) }
104  };
105  Blob::Ptr y_blob = std::dynamic_pointer_cast<Blob>(casted->CreateBlob(ydesc, blobParams));
106 
107  TensorDesc uvdesc(Precision::U8, { 1, 2, height / 2, width / 2 }, Layout::NHWC);
108  blobParams[GPU_PARAM_KEY(VA_PLANE)] = uint32_t(1);
109  Blob::Ptr uv_blob = std::dynamic_pointer_cast<Blob>(casted->CreateBlob(uvdesc, blobParams));
110 
111  return InferenceEngine::make_shared_blob<NV12Blob>(y_blob, uv_blob);
112 }
113 
114 /**
115 * @brief This function is used to obtain remote context object from VA display handle
116 */
117 static inline VAContext::Ptr make_shared_context(Core& core, std::string deviceName, VADisplay device) {
118  ParamMap contextParams = {
119  { GPU_PARAM_KEY(CONTEXT_TYPE), GPU_PARAM_VALUE(VA_SHARED) },
120  { GPU_PARAM_KEY(VA_DEVICE), static_cast<gpu_handle_param>(device) }
121  };
122  return std::dynamic_pointer_cast<VAContext>(core.CreateContext(deviceName, contextParams));
123 }
124 
125 /**
126 * @brief This function is used to obtain remote blob object from VA surface handle
127 */
128 static inline VASurfaceBlob::Ptr make_shared_blob(const TensorDesc& desc, RemoteContext::Ptr ctx, VASurfaceID surface, uint32_t plane = 0) {
129  auto casted = std::dynamic_pointer_cast<VAContext>(ctx);
130  if (nullptr == casted) {
131  THROW_IE_EXCEPTION << "Invalid remote context passed";
132  }
133  ParamMap params = {
134  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(VA_SURFACE) },
135  { GPU_PARAM_KEY(DEV_OBJECT_HANDLE), surface },
136  { GPU_PARAM_KEY(VA_PLANE), plane }
137  };
138  return std::dynamic_pointer_cast<VASurfaceBlob>(casted->CreateBlob(desc, params));
139 }
140 
141 } // namespace gpu
142 } // namespace InferenceEngine
InferenceEngine::gpu::make_shared_context
static VAContext::Ptr make_shared_context(Core &core, std::string deviceName, VADisplay device)
This function is used to obtain remote context object from VA display handle.
Definition: gpu_context_api_va.hpp:117
InferenceEngine::gpu::make_shared_blob_nv12
static Blob::Ptr make_shared_blob_nv12(size_t height, size_t width, RemoteContext::Ptr ctx, VASurfaceID nv12_surf)
This function is used to obtain a NV12 compound blob object from NV12 VA decoder output....
Definition: gpu_context_api_va.hpp:92
InferenceEngine::RemoteContext::getParams
virtual ParamMap getParams() const =0
Returns a map of device-specific parameters required for low-level operations with underlying object....
InferenceEngine::Core
This class represents Inference Engine Core entity.
Definition: ie_core.hpp:29
InferenceEngine::Blob::tensorDesc
TensorDesc tensorDesc
The tensor descriptor of the given blob.
Definition: ie_blob.h:218
InferenceEngine::TensorDesc
This class defines Tensor description.
Definition: ie_layouts.h:153
InferenceEngine::gpu::ClImage2DBlob
This class represents an abstraction for GPU plugin remote blob which can be shared with user-supplie...
Definition: gpu_context_api_ocl.hpp:132
InferenceEngine::gpu::VASurfaceBlob::Ptr
std::shared_ptr< VASurfaceBlob > Ptr
A smart pointer to the VASurfaceBlob object.
Definition: gpu_context_api_va.hpp:60
InferenceEngine::gpu::VAContext::Ptr
std::shared_ptr< VAContext > Ptr
A smart pointer to the VAContext object.
Definition: gpu_context_api_va.hpp:36
InferenceEngine::gpu::VAContext
This class represents an abstraction for GPU plugin remote context which is shared with VA display ob...
Definition: gpu_context_api_va.hpp:31
GPU_PARAM_VALUE
#define GPU_PARAM_VALUE(name)
Shortcut for defining configuration values.
Definition: gpu_params.hpp:32
InferenceEngine::gpu::VASurfaceBlob::plane
uint32_t plane()
Returns plane ID of underlying video decoder surface.
Definition: gpu_context_api_va.hpp:81
InferenceEngine::RemoteBlob::getParams
virtual ParamMap getParams() const =0
Returns a map of device-specific parameters required for low-level operations with underlying object....
InferenceEngine::ParamMap
std::map< std::string, Parameter > ParamMap
An std::map object containing low-level object parameters of classes that are derived from RemoteBlob...
Definition: ie_remote_context.hpp:26
InferenceEngine::gpu::ClContext::Ptr
std::shared_ptr< ClContext > Ptr
A smart pointer to the ClContext object.
Definition: gpu_context_api_ocl.hpp:38
InferenceEngine::RemoteContext::Ptr
std::shared_ptr< RemoteContext > Ptr
A smart pointer to the RemoteContext object.
Definition: ie_remote_context.hpp:99
InferenceEngine::Blob::Ptr
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:43
GPU_PARAM_KEY
#define GPU_PARAM_KEY(name)
Shortcut for defining configuration keys.
Definition: gpu_params.hpp:27
THROW_IE_EXCEPTION
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
desc
Represents detailed information for an error.
Definition: ie_c_api.h:122
InferenceEngine::gpu::VASurfaceBlob
This class represents an abstraction for GPU plugin remote blob which is shared with VA output surfac...
Definition: gpu_context_api_va.hpp:55
InferenceEngine::Precision::U8
@ U8
Definition: ie_precision.hpp:34
InferenceEngine::gpu::ClContext
This class represents an abstraction for GPU plugin remote context which is shared with OpenCL contex...
Definition: gpu_context_api_ocl.hpp:33
InferenceEngine::gpu::make_shared_blob
static VASurfaceBlob::Ptr make_shared_blob(const TensorDesc &desc, RemoteContext::Ptr ctx, VASurfaceID surface, uint32_t plane=0)
This function is used to obtain remote blob object from VA surface handle.
Definition: gpu_context_api_va.hpp:128
gpu_context_api_ocl.hpp
a header that defines wrappers for internal GPU plugin-specific OpenCL context and OpenCL shared memo...
InferenceEngine::gpu_handle_param
void * gpu_handle_param
Shortcut for defining a handle parameter.
Definition: gpu_params.hpp:20
InferenceEngine::gpu::VASurfaceBlob::VASurfaceBlob
VASurfaceBlob(const TensorDesc &tensorDesc)
Creates a VASurfaceBlob object with the specified dimensions and layout.
Definition: gpu_context_api_va.hpp:66
InferenceEngine::gpu::ClBlob::Ptr
std::shared_ptr< ClBlob > Ptr
A smart pointer to the ClBlob object.
Definition: gpu_context_api_ocl.hpp:74