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