1 /**************************************************************************
2 *
3 * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4 * Copyright 2014 Advanced Micro Devices, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include "pipe/p_screen.h"
30 #include "pipe/p_video_codec.h"
31 #include "util/u_memory.h"
32 #include "util/u_handle_table.h"
33 #include "util/u_video.h"
34 #include "util/set.h"
35 #include "vl/vl_deint_filter.h"
36 #include "vl/vl_winsys.h"
37
38 #include "va_private.h"
39 #ifdef HAVE_DRISW_KMS
40 #include "loader/loader.h"
41 #endif
42
43 #include <va/va_drmcommon.h>
44
45 static struct VADriverVTable vtable =
46 {
47 &vlVaTerminate,
48 &vlVaQueryConfigProfiles,
49 &vlVaQueryConfigEntrypoints,
50 &vlVaGetConfigAttributes,
51 &vlVaCreateConfig,
52 &vlVaDestroyConfig,
53 &vlVaQueryConfigAttributes,
54 &vlVaCreateSurfaces,
55 &vlVaDestroySurfaces,
56 &vlVaCreateContext,
57 &vlVaDestroyContext,
58 &vlVaCreateBuffer,
59 &vlVaBufferSetNumElements,
60 &vlVaMapBuffer,
61 &vlVaUnmapBuffer,
62 &vlVaDestroyBuffer,
63 &vlVaBeginPicture,
64 &vlVaRenderPicture,
65 &vlVaEndPicture,
66 &vlVaSyncSurface,
67 &vlVaQuerySurfaceStatus,
68 &vlVaQuerySurfaceError,
69 &vlVaPutSurface,
70 &vlVaQueryImageFormats,
71 &vlVaCreateImage,
72 &vlVaDeriveImage,
73 &vlVaDestroyImage,
74 &vlVaSetImagePalette,
75 &vlVaGetImage,
76 &vlVaPutImage,
77 &vlVaQuerySubpictureFormats,
78 &vlVaCreateSubpicture,
79 &vlVaDestroySubpicture,
80 &vlVaSubpictureImage,
81 &vlVaSetSubpictureChromakey,
82 &vlVaSetSubpictureGlobalAlpha,
83 &vlVaAssociateSubpicture,
84 &vlVaDeassociateSubpicture,
85 &vlVaQueryDisplayAttributes,
86 &vlVaGetDisplayAttributes,
87 &vlVaSetDisplayAttributes,
88 &vlVaBufferInfo,
89 &vlVaLockSurface,
90 &vlVaUnlockSurface,
91 NULL, /* DEPRECATED VaGetSurfaceAttributes */
92 &vlVaCreateSurfaces2,
93 &vlVaQuerySurfaceAttributes,
94 &vlVaAcquireBufferHandle,
95 &vlVaReleaseBufferHandle,
96 #if VA_CHECK_VERSION(1, 1, 0)
97 NULL, /* vaCreateMFContext */
98 NULL, /* vaMFAddContext */
99 NULL, /* vaMFReleaseContext */
100 NULL, /* vaMFSubmit */
101 NULL, /* vaCreateBuffer2 */
102 NULL, /* vaQueryProcessingRate */
103 &vlVaExportSurfaceHandle,
104 #endif
105 #if VA_CHECK_VERSION(1, 15, 0)
106 &vlVaSyncSurface2,
107 &vlVaSyncBuffer,
108 #endif
109 #if VA_CHECK_VERSION(1, 21, 0)
110 NULL, /* vaCopy */
111 &vlVaMapBuffer2,
112 #endif
113 };
114
115 static struct VADriverVTableVPP vtable_vpp =
116 {
117 1,
118 &vlVaQueryVideoProcFilters,
119 &vlVaQueryVideoProcFilterCaps,
120 &vlVaQueryVideoProcPipelineCaps
121 };
122
123 VA_PUBLIC_API VAStatus
VA_DRIVER_INIT_FUNC(VADriverContextP ctx)124 VA_DRIVER_INIT_FUNC(VADriverContextP ctx)
125 {
126 vlVaDriver *drv;
127
128 if (!ctx)
129 return VA_STATUS_ERROR_INVALID_CONTEXT;
130
131 drv = CALLOC(1, sizeof(vlVaDriver));
132 if (!drv)
133 return VA_STATUS_ERROR_ALLOCATION_FAILED;
134
135 switch (ctx->display_type) {
136 #ifdef _WIN32
137 case VA_DISPLAY_WIN32: {
138 drv->vscreen = vl_win32_screen_create(ctx->native_dpy);
139 break;
140 }
141 #else
142 case VA_DISPLAY_ANDROID:
143 FREE(drv);
144 return VA_STATUS_ERROR_UNIMPLEMENTED;
145 case VA_DISPLAY_GLX:
146 case VA_DISPLAY_X11:
147 drv->vscreen = vl_dri3_screen_create(ctx->native_dpy, ctx->x11_screen);
148 if (!drv->vscreen)
149 drv->vscreen = vl_dri2_screen_create(ctx->native_dpy, ctx->x11_screen);
150 if (!drv->vscreen)
151 drv->vscreen = vl_xlib_swrast_screen_create(ctx->native_dpy, ctx->x11_screen);
152 break;
153 case VA_DISPLAY_WAYLAND:
154 case VA_DISPLAY_DRM:
155 case VA_DISPLAY_DRM_RENDERNODES: {
156 const struct drm_state *drm_info = (struct drm_state *) ctx->drm_state;
157
158 if (!drm_info || drm_info->fd < 0) {
159 FREE(drv);
160 return VA_STATUS_ERROR_INVALID_PARAMETER;
161 }
162 #ifdef HAVE_DRISW_KMS
163 char* drm_driver_name = loader_get_driver_for_fd(drm_info->fd);
164 if(drm_driver_name) {
165 if (strcmp(drm_driver_name, "vgem") == 0)
166 drv->vscreen = vl_vgem_drm_screen_create(drm_info->fd);
167 FREE(drm_driver_name);
168 }
169 #endif
170 if(!drv->vscreen)
171 drv->vscreen = vl_drm_screen_create(drm_info->fd);
172 break;
173 }
174 #endif
175 default:
176 FREE(drv);
177 return VA_STATUS_ERROR_INVALID_DISPLAY;
178 }
179
180 if (!drv->vscreen)
181 goto error_screen;
182
183 drv->pipe = pipe_create_multimedia_context(drv->vscreen->pscreen);
184 if (!drv->pipe)
185 goto error_pipe;
186
187 drv->htab = handle_table_create();
188 if (!drv->htab)
189 goto error_htab;
190
191 if (!vl_compositor_init(&drv->compositor, drv->pipe))
192 goto error_compositor;
193 if (!vl_compositor_init_state(&drv->cstate, drv->pipe))
194 goto error_compositor_state;
195
196 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601, NULL, true, &drv->csc);
197 if (!vl_compositor_set_csc_matrix(&drv->cstate, (const vl_csc_matrix *)&drv->csc, 1.0f, 0.0f))
198 goto error_csc_matrix;
199 (void) mtx_init(&drv->mutex, mtx_plain);
200
201 ctx->pDriverData = (void *)drv;
202 ctx->version_major = 0;
203 ctx->version_minor = 1;
204 *ctx->vtable = vtable;
205 *ctx->vtable_vpp = vtable_vpp;
206 ctx->max_profiles = PIPE_VIDEO_PROFILE_MAX - PIPE_VIDEO_PROFILE_UNKNOWN - 1;
207 ctx->max_entrypoints = 2;
208 ctx->max_attributes = 1;
209 ctx->max_image_formats = VL_VA_MAX_IMAGE_FORMATS;
210 ctx->max_subpic_formats = 1;
211 ctx->max_display_attributes = 0;
212
213 snprintf(drv->vendor_string, sizeof(drv->vendor_string),
214 "Mesa Gallium driver " PACKAGE_VERSION " for %s",
215 drv->vscreen->pscreen->get_name(drv->vscreen->pscreen));
216 ctx->str_vendor = drv->vendor_string;
217
218 return VA_STATUS_SUCCESS;
219
220 error_csc_matrix:
221 vl_compositor_cleanup_state(&drv->cstate);
222
223 error_compositor_state:
224 vl_compositor_cleanup(&drv->compositor);
225
226 error_compositor:
227 handle_table_destroy(drv->htab);
228
229 error_htab:
230 drv->pipe->destroy(drv->pipe);
231
232 error_pipe:
233 drv->vscreen->destroy(drv->vscreen);
234
235 error_screen:
236 FREE(drv);
237 return VA_STATUS_ERROR_ALLOCATION_FAILED;
238 }
239
240 VAStatus
vlVaCreateContext(VADriverContextP ctx,VAConfigID config_id,int picture_width,int picture_height,int flag,VASurfaceID * render_targets,int num_render_targets,VAContextID * context_id)241 vlVaCreateContext(VADriverContextP ctx, VAConfigID config_id, int picture_width,
242 int picture_height, int flag, VASurfaceID *render_targets,
243 int num_render_targets, VAContextID *context_id)
244 {
245 vlVaDriver *drv;
246 vlVaContext *context;
247 vlVaConfig *config;
248 int is_vpp;
249 int min_supported_width, min_supported_height;
250 int max_supported_width, max_supported_height;
251
252 if (!ctx)
253 return VA_STATUS_ERROR_INVALID_CONTEXT;
254
255 drv = VL_VA_DRIVER(ctx);
256 mtx_lock(&drv->mutex);
257 config = handle_table_get(drv->htab, config_id);
258 mtx_unlock(&drv->mutex);
259
260 if (!config)
261 return VA_STATUS_ERROR_INVALID_CONFIG;
262
263 is_vpp = config->profile == PIPE_VIDEO_PROFILE_UNKNOWN && !picture_width &&
264 !picture_height && !flag && !render_targets && !num_render_targets;
265
266 if (!(picture_width && picture_height) && !is_vpp)
267 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
268
269 context = CALLOC(1, sizeof(vlVaContext));
270 if (!context)
271 return VA_STATUS_ERROR_ALLOCATION_FAILED;
272
273 if (is_vpp && !drv->vscreen->pscreen->get_video_param(drv->vscreen->pscreen,
274 PIPE_VIDEO_PROFILE_UNKNOWN,
275 PIPE_VIDEO_ENTRYPOINT_PROCESSING,
276 PIPE_VIDEO_CAP_SUPPORTED)) {
277 context->decoder = NULL;
278 } else {
279 if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_PROCESSING) {
280 min_supported_width = drv->vscreen->pscreen->get_video_param(drv->vscreen->pscreen,
281 config->profile, config->entrypoint,
282 PIPE_VIDEO_CAP_MIN_WIDTH);
283 min_supported_height = drv->vscreen->pscreen->get_video_param(drv->vscreen->pscreen,
284 config->profile, config->entrypoint,
285 PIPE_VIDEO_CAP_MIN_HEIGHT);
286 max_supported_width = drv->vscreen->pscreen->get_video_param(drv->vscreen->pscreen,
287 config->profile, config->entrypoint,
288 PIPE_VIDEO_CAP_MAX_WIDTH);
289 max_supported_height = drv->vscreen->pscreen->get_video_param(drv->vscreen->pscreen,
290 config->profile, config->entrypoint,
291 PIPE_VIDEO_CAP_MAX_HEIGHT);
292
293 if (picture_width < min_supported_width || picture_height < min_supported_height ||
294 picture_width > max_supported_width || picture_height > max_supported_height) {
295 FREE(context);
296 return VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED;
297 }
298 }
299 context->templat.profile = config->profile;
300 context->templat.entrypoint = config->entrypoint;
301 context->templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
302 context->templat.width = picture_width;
303 context->templat.height = picture_height;
304 context->templat.expect_chunked_decode = true;
305
306 switch (u_reduce_video_profile(context->templat.profile)) {
307 case PIPE_VIDEO_FORMAT_MPEG12:
308 case PIPE_VIDEO_FORMAT_VC1:
309 case PIPE_VIDEO_FORMAT_MPEG4:
310 context->templat.max_references = 2;
311 break;
312
313 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
314 context->templat.max_references = 0;
315 if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE) {
316 context->desc.h264.pps = CALLOC_STRUCT(pipe_h264_pps);
317 if (!context->desc.h264.pps) {
318 FREE(context);
319 return VA_STATUS_ERROR_ALLOCATION_FAILED;
320 }
321 context->desc.h264.pps->sps = CALLOC_STRUCT(pipe_h264_sps);
322 if (!context->desc.h264.pps->sps) {
323 FREE(context->desc.h264.pps);
324 FREE(context);
325 return VA_STATUS_ERROR_ALLOCATION_FAILED;
326 }
327 }
328 break;
329
330 case PIPE_VIDEO_FORMAT_HEVC:
331 if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE) {
332 context->desc.h265.pps = CALLOC_STRUCT(pipe_h265_pps);
333 if (!context->desc.h265.pps) {
334 FREE(context);
335 return VA_STATUS_ERROR_ALLOCATION_FAILED;
336 }
337 context->desc.h265.pps->sps = CALLOC_STRUCT(pipe_h265_sps);
338 if (!context->desc.h265.pps->sps) {
339 FREE(context->desc.h265.pps);
340 FREE(context);
341 return VA_STATUS_ERROR_ALLOCATION_FAILED;
342 }
343 }
344 break;
345
346 case PIPE_VIDEO_FORMAT_VP9:
347 break;
348
349 default:
350 break;
351 }
352 }
353
354 context->desc.base.profile = config->profile;
355 context->desc.base.entry_point = config->entrypoint;
356 if (config->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
357 switch (u_reduce_video_profile(context->templat.profile)) {
358 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
359 context->desc.h264enc.rate_ctrl[0].rate_ctrl_method = config->rc;
360 context->desc.h264enc.frame_idx = util_hash_table_create_ptr_keys();
361 break;
362 case PIPE_VIDEO_FORMAT_HEVC:
363 context->desc.h265enc.rc.rate_ctrl_method = config->rc;
364 context->desc.h265enc.frame_idx = util_hash_table_create_ptr_keys();
365 break;
366 case PIPE_VIDEO_FORMAT_AV1:
367 context->desc.av1enc.rc[0].rate_ctrl_method = config->rc;
368 break;
369 default:
370 break;
371 }
372 }
373
374 context->surfaces = _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
375
376 mtx_lock(&drv->mutex);
377 *context_id = handle_table_add(drv->htab, context);
378 mtx_unlock(&drv->mutex);
379
380 return VA_STATUS_SUCCESS;
381 }
382
383 VAStatus
vlVaDestroyContext(VADriverContextP ctx,VAContextID context_id)384 vlVaDestroyContext(VADriverContextP ctx, VAContextID context_id)
385 {
386 vlVaDriver *drv;
387 vlVaContext *context;
388
389 if (!ctx)
390 return VA_STATUS_ERROR_INVALID_CONTEXT;
391
392 if (context_id == 0)
393 return VA_STATUS_ERROR_INVALID_CONTEXT;
394
395 drv = VL_VA_DRIVER(ctx);
396 mtx_lock(&drv->mutex);
397 context = handle_table_get(drv->htab, context_id);
398 if (!context) {
399 mtx_unlock(&drv->mutex);
400 return VA_STATUS_ERROR_INVALID_CONTEXT;
401 }
402
403 set_foreach(context->surfaces, entry) {
404 vlVaSurface *surf = (vlVaSurface *)entry->key;
405 assert(surf->ctx == context);
406 surf->ctx = NULL;
407 if (surf->fence && context->decoder && context->decoder->destroy_fence) {
408 context->decoder->destroy_fence(context->decoder, surf->fence);
409 surf->fence = NULL;
410 }
411 }
412 _mesa_set_destroy(context->surfaces, NULL);
413
414 if (context->decoder) {
415 if (context->desc.base.entry_point == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
416 if (u_reduce_video_profile(context->decoder->profile) ==
417 PIPE_VIDEO_FORMAT_MPEG4_AVC) {
418 if (context->desc.h264enc.frame_idx)
419 _mesa_hash_table_destroy(context->desc.h264enc.frame_idx, NULL);
420 }
421 if (u_reduce_video_profile(context->decoder->profile) ==
422 PIPE_VIDEO_FORMAT_HEVC) {
423 if (context->desc.h265enc.frame_idx)
424 _mesa_hash_table_destroy(context->desc.h265enc.frame_idx, NULL);
425 }
426 } else {
427 if (u_reduce_video_profile(context->decoder->profile) ==
428 PIPE_VIDEO_FORMAT_MPEG4_AVC) {
429 FREE(context->desc.h264.pps->sps);
430 FREE(context->desc.h264.pps);
431 }
432 if (u_reduce_video_profile(context->decoder->profile) ==
433 PIPE_VIDEO_FORMAT_HEVC) {
434 FREE(context->desc.h265.pps->sps);
435 FREE(context->desc.h265.pps);
436 }
437 }
438 context->decoder->destroy(context->decoder);
439 }
440 if (context->blit_cs)
441 drv->pipe->delete_compute_state(drv->pipe, context->blit_cs);
442 if (context->deint) {
443 vl_deint_filter_cleanup(context->deint);
444 FREE(context->deint);
445 }
446 FREE(context->desc.base.decrypt_key);
447 FREE(context);
448 handle_table_remove(drv->htab, context_id);
449 mtx_unlock(&drv->mutex);
450
451 return VA_STATUS_SUCCESS;
452 }
453
454 VAStatus
vlVaTerminate(VADriverContextP ctx)455 vlVaTerminate(VADriverContextP ctx)
456 {
457 vlVaDriver *drv;
458
459 if (!ctx)
460 return VA_STATUS_ERROR_INVALID_CONTEXT;
461
462 drv = ctx->pDriverData;
463 vl_compositor_cleanup_state(&drv->cstate);
464 vl_compositor_cleanup(&drv->compositor);
465 drv->pipe->destroy(drv->pipe);
466 drv->vscreen->destroy(drv->vscreen);
467 handle_table_destroy(drv->htab);
468 mtx_destroy(&drv->mutex);
469 FREE(drv);
470
471 return VA_STATUS_SUCCESS;
472 }
473