Debian Codegraph @

media_driver/linux/common/cp/ddi/media_ddi_prot.cpp

raw
1 /*
2 * Copyright (c) 2020, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 //!
23 //! \file media_ddi_prot.cpp
24 //! \brief The class implementation of DdiMediaProtected base for all protected session
25 //!
26
27 #include "codec_def_common_encode.h"
28 #include "codec_def_encode_jpeg.h"
29 #include "media_libva_util.h"
30 #include "media_libva_caps.h"
31 #include "media_libva.h"
32 #include "media_libva_caps_cp_interface.h"
33 #include "media_ddi_prot.h"
34
35 static bool isDefaultRegistered = DdiProtectedFactory::Register<DdiMediaProtected>(DDI_PROTECTED_DEFAULT);
36 static bool isProtectedContentRegistered = DdiProtectedFactory::Register<DdiMediaProtected>(DDI_PROTECTED_CONTENT);
37
38 std::map<uint32_t, DdiMediaProtected*> DdiMediaProtected::_impl;
39
40 DdiMediaProtected* DdiMediaProtected::GetInstance(uint32_t id)
41 {
42 if (_impl[id] == nullptr)
43 {
44 _impl[id] = DdiProtectedFactory::Create(id);
45 if (_impl[id] == nullptr)
46 {
47 DDI_ASSERTMESSAGE("DdiProtectedFactory::Create fail with id=%d", id);
48 return nullptr;
49 }
50 }
51
52 return _impl[id];
53 }
54
55 void DdiMediaProtected::FreeInstances()
56 {
57 std::map<uint32_t, DdiMediaProtected*>::iterator it;
58 for (it = _impl.begin(); it != _impl.end(); it++)
59 {
60 MOS_Delete(it->second);
61 }
62 }
63
64 bool DdiMediaProtected::CheckEntrypointSupported(VAEntrypoint entrypoint)
65 {
66 return false;
67 }
68
69 bool DdiMediaProtected::CheckAttribList(
70 VAProfile profile,
71 VAEntrypoint entrypoint,
72 VAConfigAttrib* attrib,
73 uint32_t numAttribs)
74 {
75 return false;
76 }
77
78 VAStatus DdiMediaProtected::DdiMedia_CreateProtectedSession(
79 VADriverContextP ctx,
80 VAConfigID config_id,
81 VAProtectedSessionID *protected_session)
82 {
83 DDI_FUNCTION_ENTER();
84
85 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
86 DDI_CHK_NULL(protected_session, "nullptr protected_session", VA_STATUS_ERROR_INVALID_PARAMETER);
87
88 VAStatus vaStatus = VA_STATUS_SUCCESS;
89 PDDI_MEDIA_CONTEXT mediaDrvCtx = DdiMedia_GetMediaContext(ctx);
90 DDI_CHK_NULL(mediaDrvCtx, "nullptr mediaDrvCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
91 DDI_CHK_NULL(mediaDrvCtx->m_caps, "nullptr mediaDrvCtx->m_caps", VA_STATUS_ERROR_INVALID_CONTEXT);
92
93 MediaLibvaCapsCpInterface *cpCaps = mediaDrvCtx->m_caps->GetCpCaps();
94 DDI_CHK_NULL(cpCaps, "nullptr cpCaps", VA_STATUS_ERROR_INVALID_CONTEXT);
95
96 if (cpCaps->IsCpConfigId(config_id))
97 {
98 DdiMediaProtected *prot = DdiMediaProtected::GetInstance(DDI_PROTECTED_CONTENT);
99 DDI_CHK_NULL(prot, "nullptr prot", VA_STATUS_ERROR_ALLOCATION_FAILED);
100 vaStatus = prot->CreateProtectedSession(ctx, config_id - DDI_CP_GEN_CONFIG_ATTRIBUTES_BASE, protected_session);
101 }
102 else
103 {
104 DDI_ASSERTMESSAGE("DDI: Invalid config_id");
105 vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
106 }
107
108 DDI_FUNCTION_EXIT(vaStatus);
109 return vaStatus;
110
111 }
112
113 VAStatus DdiMediaProtected::DdiMedia_DestroyProtectedSession(
114 VADriverContextP ctx,
115 VAProtectedSessionID protected_session)
116 {
117 DDI_FUNCTION_ENTER();
118
119 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
120
121 VAStatus vaStatus = VA_STATUS_SUCCESS;
122 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
123 void *ctxPtr = DdiMedia_GetContextFromProtectedSessionID(ctx, protected_session, &ctxType);
124 DDI_CHK_NULL(ctxPtr, "nullptr prot", VA_STATUS_ERROR_INVALID_CONTEXT);
125
126 if (ctxType == DDI_MEDIA_CONTEXT_TYPE_PROTECTED_CONTENT)
127 {
128 DdiMediaProtected *prot = DdiMediaProtected::GetInstance(DDI_PROTECTED_CONTENT);
129 DDI_CHK_NULL(prot, "nullptr prot", VA_STATUS_ERROR_ALLOCATION_FAILED);
130 vaStatus = prot->DestroyProtectedSession(ctx, protected_session);
131 }
132 else
133 {
134 vaStatus = VA_STATUS_ERROR_INVALID_CONTEXT;
135 }
136
137 DDI_FUNCTION_EXIT(vaStatus);
138 return vaStatus;
139 }
140
141 VAStatus DdiMediaProtected::DdiMedia_AttachProtectedSession(
142 VADriverContextP ctx,
143 VAContextID context,
144 VAProtectedSessionID protected_session)
145 {
146 DDI_FUNCTION_ENTER();
147
148 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
149
150 VAStatus vaStatus = VA_STATUS_SUCCESS;
151 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
152 void *ctxPtr = DdiMedia_GetContextFromProtectedSessionID(ctx, protected_session, &ctxType);
153 DDI_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
154
155 if (ctxType == DDI_MEDIA_CONTEXT_TYPE_PROTECTED_CONTENT)
156 {
157 DdiMediaProtected *prot = DdiMediaProtected::GetInstance(DDI_PROTECTED_CONTENT);
158 DDI_CHK_NULL(prot, "nullptr prot", VA_STATUS_ERROR_ALLOCATION_FAILED);
159 vaStatus = prot->AttachProtectedSession(ctx, context, protected_session);
160 }
161 else
162 {
163 DDI_ASSERTMESSAGE("DDI: Invalid protected_session");
164 vaStatus = VA_STATUS_ERROR_INVALID_CONTEXT;
165 }
166
167 DDI_FUNCTION_EXIT(vaStatus);
168 return vaStatus;
169 }
170
171 VAStatus DdiMediaProtected::DdiMedia_DetachProtectedSession(
172 VADriverContextP ctx,
173 VAContextID context)
174 {
175 DDI_FUNCTION_ENTER();
176
177 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
178
179 VAStatus vaStatus = VA_STATUS_SUCCESS;
180 if (context != 0)
181 {
182 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
183 void *ctxPtr = DdiMedia_GetContextFromContextID(ctx, context, &ctxType);
184 DDI_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
185 if (ctxType != DDI_MEDIA_CONTEXT_TYPE_DECODER &&
186 ctxType != DDI_MEDIA_CONTEXT_TYPE_ENCODER &&
187 ctxType != DDI_MEDIA_CONTEXT_TYPE_VP)
188 {
189 DDI_ASSERTMESSAGE("wrong context type %d", ctxType);
190 return VA_STATUS_ERROR_INVALID_CONTEXT;
191 }
192
193 DdiMediaProtected *prot = DdiMediaProtected::GetInstance(DDI_PROTECTED_CONTENT);
194 DDI_CHK_NULL(prot, "nullptr prot", VA_STATUS_ERROR_ALLOCATION_FAILED);
195 vaStatus = prot->DetachProtectedSession(ctx, context);
196 }
197
198 DDI_FUNCTION_EXIT(vaStatus);
199 return vaStatus;
200 }
201
202 VAStatus DdiMediaProtected::DdiMedia_ProtectedSessionExecute(
203 VADriverContextP ctx,
204 VAProtectedSessionID protected_session,
205 VABufferID data)
206 {
207 DDI_FUNCTION_ENTER();
208
209 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
210
211 VAStatus vaStatus = VA_STATUS_SUCCESS;
212 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
213 void *ctxPtr = DdiMedia_GetContextFromProtectedSessionID(ctx, protected_session, &ctxType);
214 DDI_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
215
216 if (ctxType == DDI_MEDIA_CONTEXT_TYPE_PROTECTED_CONTENT)
217 {
218 DdiMediaProtected *prot = DdiMediaProtected::GetInstance(DDI_PROTECTED_CONTENT);
219 DDI_CHK_NULL(prot, "nullptr prot", VA_STATUS_ERROR_ALLOCATION_FAILED);
220 vaStatus = prot->ProtectedSessionExecute(ctx, protected_session, data);
221 }
222 else
223 {
224 DDI_ASSERTMESSAGE("DDI: Invalid protected_session");
225 vaStatus = VA_STATUS_ERROR_INVALID_CONTEXT;
226 }
227
228 return vaStatus;
229 }
230
231 VAStatus DdiMediaProtected::DdiMedia_ProtectedSessionCreateBuffer(
232 VADriverContextP ctx,
233 VAContextID context,
234 VABufferType type,
235 uint32_t size,
236 uint32_t num_elements,
237 void *data,
238 VABufferID *bufId)
239 {
240 DDI_FUNCTION_ENTER();
241
242 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
243 DDI_CHK_NULL(bufId, "nullptr bufId", VA_STATUS_ERROR_INVALID_PARAMETER);
244
245 VAStatus vaStatus = VA_STATUS_SUCCESS;
246 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
247 void *ctxPtr = DdiMedia_GetContextFromProtectedSessionID(ctx, context, &ctxType);
248 DDI_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
249
250 if (ctxType == DDI_MEDIA_CONTEXT_TYPE_PROTECTED_CONTENT)
251 {
252 DdiMediaProtected *prot = DdiMediaProtected::GetInstance(DDI_PROTECTED_CONTENT);
253 DDI_CHK_NULL(prot, "nullptr prot", VA_STATUS_ERROR_ALLOCATION_FAILED);
254 vaStatus = prot->ProtectedSessionCreateBuffer(ctx, context, type, size, num_elements, data, bufId);
255 }
256 else
257 {
258 DDI_ASSERTMESSAGE("DDI: Invalid protected_session");
259 vaStatus = VA_STATUS_ERROR_INVALID_CONTEXT;
260 }
261
262 return vaStatus;
263 }
264
265 VAStatus DdiMediaProtected::CreateProtectedSession(
266 VADriverContextP ctx,
267 VAConfigID config_id,
268 VAProtectedSessionID *protected_session)
269 {
270 return VA_STATUS_ERROR_UNIMPLEMENTED;
271 }
272
273 VAStatus DdiMediaProtected::DestroyProtectedSession(
274 VADriverContextP ctx,
275 VAProtectedSessionID protected_session)
276 {
277 return VA_STATUS_ERROR_UNIMPLEMENTED;
278 }
279
280 VAStatus DdiMediaProtected::AttachProtectedSession(
281 VADriverContextP ctx,
282 VAContextID context,
283 VAProtectedSessionID protected_session)
284 {
285 return VA_STATUS_ERROR_UNIMPLEMENTED;
286 }
287
288 VAStatus DdiMediaProtected::DetachProtectedSession(
289 VADriverContextP ctx,
290 VAContextID context)
291 {
292 return VA_STATUS_ERROR_UNIMPLEMENTED;
293 }
294
295 VAStatus DdiMediaProtected::ProtectedSessionExecute(
296 VADriverContextP ctx,
297 VAProtectedSessionID protected_session,
298 VABufferID data)
299 {
300 return VA_STATUS_ERROR_UNIMPLEMENTED;
301 }
302
303 VAStatus DdiMediaProtected::ProtectedSessionCreateBuffer(
304 VADriverContextP ctx,
305 VAProtectedSessionID protected_session,
306 VABufferType type,
307 uint32_t size,
308 uint32_t num_elements,
309 void *data,
310 VABufferID *bufId)
311 {
312 return VA_STATUS_ERROR_UNIMPLEMENTED;
313 }
314
315 uint64_t DdiMediaProtected::GetProtectedSurfaceTag(PDDI_MEDIA_CONTEXT media_ctx)
316 {
317 return 0;
318 }
319
320 void DdiMedia_FreeProtectedSessionHeap(
321 VADriverContextP ctx,
322 PDDI_MEDIA_HEAP contextHeap,
323 int32_t vaContextOffset,
324 int32_t ctxNums)
325 {
326 if (nullptr == ctx || nullptr == contextHeap)
327 {
328 DDI_ASSERTMESSAGE("DDI: Invalid input arguments");
329 return;
330 }
331
332 PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT mediaContextHeapBase = (PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT)contextHeap->pHeapBase;
333 if (nullptr == mediaContextHeapBase)
334 return;
335
336 for (int32_t elementId = 0; ctxNums > 0 && elementId < contextHeap->uiAllocatedHeapElements; ++elementId)
337 {
338 PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT mediaContextHeapElmt = &mediaContextHeapBase[elementId];
339 if (nullptr != mediaContextHeapElmt)
340 {
341 if (nullptr == mediaContextHeapElmt->pVaContext)
342 continue;
343 VAContextID vaCtxID = (VAContextID)(mediaContextHeapElmt->uiVaContextID + vaContextOffset);
344 DdiMediaProtected::DdiMedia_DestroyProtectedSession(ctx, vaCtxID);
345 }
346 else
347 {
348 DDI_ASSERTMESSAGE("DDI: Invalid mediaContextHeapElmt");
349 }
350 ctxNums--;
351 }
352 }
353
354 static void* DdiMedia_GetVaContextFromHeap(
355 PDDI_MEDIA_HEAP mediaHeap,
356 uint32_t index,
357 PMEDIA_MUTEX_T mutex)
358 {
359 if (nullptr == mutex)
360 {
361 DDI_ASSERTMESSAGE("DDI: Invalid input arguments");
362 return nullptr;
363 }
364
365 PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT vaCtxHeapElmt = nullptr;
366 void *context = nullptr;
367
368 DdiMediaUtil_LockMutex(mutex);
369 if(nullptr == mediaHeap || index >= mediaHeap->uiAllocatedHeapElements)
370 {
371 DdiMediaUtil_UnLockMutex(mutex);
372 return nullptr;
373 }
374 vaCtxHeapElmt = (PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT)mediaHeap->pHeapBase;
375 vaCtxHeapElmt += index;
376 context = vaCtxHeapElmt->pVaContext;
377 DdiMediaUtil_UnLockMutex(mutex);
378
379 return context;
380 }
381
382 void* DdiMedia_GetContextFromProtectedSessionID(
383 VADriverContextP ctx,
384 VAProtectedSessionID vaID,
385 uint32_t *ctxType)
386 {
387 PDDI_MEDIA_CONTEXT mediaCtx = nullptr;
388 uint32_t heap_index = 0, prot_index = 0;
389 void *pSession = nullptr;
390
391 DDI_CHK_NULL(ctx, "nullptr ctx", nullptr);
392 DDI_CHK_NULL(ctxType, "nullptr ctxType", nullptr);
393
394 mediaCtx = DdiMedia_GetMediaContext(ctx);
395 prot_index = vaID & DDI_MEDIA_MASK_VACONTEXTID;
396 heap_index = vaID & DDI_MEDIA_MASK_VAPROTECTEDSESSION_ID;
397
398 if ((vaID&DDI_MEDIA_MASK_VACONTEXT_TYPE) != DDI_MEDIA_VACONTEXTID_OFFSET_PROT)
399 {
400 DDI_ASSERTMESSAGE("Invalid protected session: 0x%x", vaID);
401 *ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
402 return nullptr;
403 }
404
405 // 0 ~ 0x8000000: LP context
406 // 0x8000000~0x10000000: CP context
407 if (prot_index < DDI_MEDIA_VACONTEXTID_OFFSET_PROT_CP)
408 {
409 DDI_VERBOSEMESSAGE("LP protected session detected: 0x%x", vaID);
410 *ctxType = DDI_MEDIA_CONTEXT_TYPE_PROTECTED_LINK;
411 return DdiMedia_GetVaContextFromHeap(mediaCtx->pProtCtxHeap, heap_index, &mediaCtx->ProtMutex);
412 }
413
414 DDI_VERBOSEMESSAGE("CP protected session detected: 0x%x", vaID);
415 *ctxType = DDI_MEDIA_CONTEXT_TYPE_PROTECTED_CONTENT;
416 return DdiMedia_GetVaContextFromHeap(mediaCtx->pProtCtxHeap, heap_index, &mediaCtx->ProtMutex);
417 }