1 /**************************************************************************
3 * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4 * Copyright 2014 Advanced Micro Devices, Inc.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
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 "vl/vl_deint_filter.h"
35 #include "vl/vl_winsys.h"
37 #include "va_private.h"
39 #include <va/va_drmcommon.h>
41 static struct VADriverVTable vtable
=
44 &vlVaQueryConfigProfiles
,
45 &vlVaQueryConfigEntrypoints
,
46 &vlVaGetConfigAttributes
,
49 &vlVaQueryConfigAttributes
,
55 &vlVaBufferSetNumElements
,
63 &vlVaQuerySurfaceStatus
,
64 &vlVaQuerySurfaceError
,
66 &vlVaQueryImageFormats
,
73 &vlVaQuerySubpictureFormats
,
74 &vlVaCreateSubpicture
,
75 &vlVaDestroySubpicture
,
77 &vlVaSetSubpictureChromakey
,
78 &vlVaSetSubpictureGlobalAlpha
,
79 &vlVaAssociateSubpicture
,
80 &vlVaDeassociateSubpicture
,
81 &vlVaQueryDisplayAttributes
,
82 &vlVaGetDisplayAttributes
,
83 &vlVaSetDisplayAttributes
,
87 NULL
, /* DEPRECATED VaGetSurfaceAttributes */
89 &vlVaQuerySurfaceAttributes
,
90 &vlVaAcquireBufferHandle
,
91 &vlVaReleaseBufferHandle
,
92 #if VA_CHECK_VERSION(1, 1, 0)
93 NULL
, /* vaCreateMFContext */
94 NULL
, /* vaMFAddContext */
95 NULL
, /* vaMFReleaseContext */
96 NULL
, /* vaMFSubmit */
97 NULL
, /* vaCreateBuffer2 */
98 NULL
, /* vaQueryProcessingRate */
99 &vlVaExportSurfaceHandle
,
103 static struct VADriverVTableVPP vtable_vpp
=
106 &vlVaQueryVideoProcFilters
,
107 &vlVaQueryVideoProcFilterCaps
,
108 &vlVaQueryVideoProcPipelineCaps
112 VA_DRIVER_INIT_FUNC(VADriverContextP ctx
)
117 return VA_STATUS_ERROR_INVALID_CONTEXT
;
119 drv
= CALLOC(1, sizeof(vlVaDriver
));
121 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
123 switch (ctx
->display_type
) {
124 case VA_DISPLAY_ANDROID
:
126 return VA_STATUS_ERROR_UNIMPLEMENTED
;
129 drv
->vscreen
= vl_dri3_screen_create(ctx
->native_dpy
, ctx
->x11_screen
);
131 drv
->vscreen
= vl_dri2_screen_create(ctx
->native_dpy
, ctx
->x11_screen
);
133 case VA_DISPLAY_WAYLAND
:
135 case VA_DISPLAY_DRM_RENDERNODES
: {
136 const struct drm_state
*drm_info
= (struct drm_state
*) ctx
->drm_state
;
138 if (!drm_info
|| drm_info
->fd
< 0) {
140 return VA_STATUS_ERROR_INVALID_PARAMETER
;
143 drv
->vscreen
= vl_drm_screen_create(drm_info
->fd
);
148 return VA_STATUS_ERROR_INVALID_DISPLAY
;
154 drv
->pipe
= pipe_create_multimedia_context(drv
->vscreen
->pscreen
);
158 drv
->htab
= handle_table_create();
162 if (!vl_compositor_init(&drv
->compositor
, drv
->pipe
))
163 goto error_compositor
;
164 if (!vl_compositor_init_state(&drv
->cstate
, drv
->pipe
))
165 goto error_compositor_state
;
167 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601
, NULL
, true, &drv
->csc
);
168 if (!vl_compositor_set_csc_matrix(&drv
->cstate
, (const vl_csc_matrix
*)&drv
->csc
, 1.0f
, 0.0f
))
169 goto error_csc_matrix
;
170 (void) mtx_init(&drv
->mutex
, mtx_plain
);
172 ctx
->pDriverData
= (void *)drv
;
173 ctx
->version_major
= 0;
174 ctx
->version_minor
= 1;
175 *ctx
->vtable
= vtable
;
176 *ctx
->vtable_vpp
= vtable_vpp
;
177 ctx
->max_profiles
= PIPE_VIDEO_PROFILE_MAX
- PIPE_VIDEO_PROFILE_UNKNOWN
- 1;
178 ctx
->max_entrypoints
= 2;
179 ctx
->max_attributes
= 1;
180 ctx
->max_image_formats
= VL_VA_MAX_IMAGE_FORMATS
;
181 ctx
->max_subpic_formats
= 1;
182 ctx
->max_display_attributes
= 1;
184 snprintf(drv
->vendor_string
, sizeof(drv
->vendor_string
),
185 "Mesa Gallium driver " PACKAGE_VERSION
" for %s",
186 drv
->vscreen
->pscreen
->get_name(drv
->vscreen
->pscreen
));
187 ctx
->str_vendor
= drv
->vendor_string
;
189 return VA_STATUS_SUCCESS
;
192 vl_compositor_cleanup_state(&drv
->cstate
);
194 error_compositor_state
:
195 vl_compositor_cleanup(&drv
->compositor
);
198 handle_table_destroy(drv
->htab
);
201 drv
->pipe
->destroy(drv
->pipe
);
204 drv
->vscreen
->destroy(drv
->vscreen
);
208 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
212 vlVaCreateContext(VADriverContextP ctx
, VAConfigID config_id
, int picture_width
,
213 int picture_height
, int flag
, VASurfaceID
*render_targets
,
214 int num_render_targets
, VAContextID
*context_id
)
217 vlVaContext
*context
;
222 return VA_STATUS_ERROR_INVALID_CONTEXT
;
224 drv
= VL_VA_DRIVER(ctx
);
225 mtx_lock(&drv
->mutex
);
226 config
= handle_table_get(drv
->htab
, config_id
);
227 mtx_unlock(&drv
->mutex
);
229 is_vpp
= config
->profile
== PIPE_VIDEO_PROFILE_UNKNOWN
&& !picture_width
&&
230 !picture_height
&& !flag
&& !render_targets
&& !num_render_targets
;
232 if (!(picture_width
&& picture_height
) && !is_vpp
)
233 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT
;
235 context
= CALLOC(1, sizeof(vlVaContext
));
237 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
240 context
->decoder
= NULL
;
242 context
->templat
.profile
= config
->profile
;
243 context
->templat
.entrypoint
= config
->entrypoint
;
244 context
->templat
.chroma_format
= PIPE_VIDEO_CHROMA_FORMAT_420
;
245 context
->templat
.width
= picture_width
;
246 context
->templat
.height
= picture_height
;
247 context
->templat
.expect_chunked_decode
= true;
249 switch (u_reduce_video_profile(context
->templat
.profile
)) {
250 case PIPE_VIDEO_FORMAT_MPEG12
:
251 case PIPE_VIDEO_FORMAT_VC1
:
252 case PIPE_VIDEO_FORMAT_MPEG4
:
253 context
->templat
.max_references
= 2;
256 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
257 context
->templat
.max_references
= 0;
258 if (config
->entrypoint
!= PIPE_VIDEO_ENTRYPOINT_ENCODE
) {
259 context
->desc
.h264
.pps
= CALLOC_STRUCT(pipe_h264_pps
);
260 if (!context
->desc
.h264
.pps
) {
262 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
264 context
->desc
.h264
.pps
->sps
= CALLOC_STRUCT(pipe_h264_sps
);
265 if (!context
->desc
.h264
.pps
->sps
) {
266 FREE(context
->desc
.h264
.pps
);
268 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
273 case PIPE_VIDEO_FORMAT_HEVC
:
274 context
->templat
.max_references
= num_render_targets
;
275 if (config
->entrypoint
!= PIPE_VIDEO_ENTRYPOINT_ENCODE
) {
276 context
->desc
.h265
.pps
= CALLOC_STRUCT(pipe_h265_pps
);
277 if (!context
->desc
.h265
.pps
) {
279 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
281 context
->desc
.h265
.pps
->sps
= CALLOC_STRUCT(pipe_h265_sps
);
282 if (!context
->desc
.h265
.pps
->sps
) {
283 FREE(context
->desc
.h265
.pps
);
285 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
290 case PIPE_VIDEO_FORMAT_VP9
:
291 context
->templat
.max_references
= num_render_targets
;
299 context
->desc
.base
.profile
= config
->profile
;
300 context
->desc
.base
.entry_point
= config
->entrypoint
;
301 if (config
->entrypoint
== PIPE_VIDEO_ENTRYPOINT_ENCODE
) {
302 switch (u_reduce_video_profile(context
->templat
.profile
)) {
303 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
304 context
->desc
.h264enc
.rate_ctrl
.rate_ctrl_method
= config
->rc
;
305 context
->desc
.h264enc
.frame_idx
= util_hash_table_create_ptr_keys();
307 case PIPE_VIDEO_FORMAT_HEVC
:
308 context
->desc
.h265enc
.rc
.rate_ctrl_method
= config
->rc
;
309 context
->desc
.h265enc
.frame_idx
= util_hash_table_create_ptr_keys();
316 mtx_lock(&drv
->mutex
);
317 *context_id
= handle_table_add(drv
->htab
, context
);
318 mtx_unlock(&drv
->mutex
);
320 return VA_STATUS_SUCCESS
;
324 vlVaDestroyContext(VADriverContextP ctx
, VAContextID context_id
)
327 vlVaContext
*context
;
330 return VA_STATUS_ERROR_INVALID_CONTEXT
;
332 drv
= VL_VA_DRIVER(ctx
);
333 mtx_lock(&drv
->mutex
);
334 context
= handle_table_get(drv
->htab
, context_id
);
336 mtx_unlock(&drv
->mutex
);
337 return VA_STATUS_ERROR_INVALID_CONTEXT
;
340 if (context
->decoder
) {
341 if (context
->desc
.base
.entry_point
== PIPE_VIDEO_ENTRYPOINT_ENCODE
) {
342 if (u_reduce_video_profile(context
->decoder
->profile
) ==
343 PIPE_VIDEO_FORMAT_MPEG4_AVC
) {
344 if (context
->desc
.h264enc
.frame_idx
)
345 _mesa_hash_table_destroy(context
->desc
.h264enc
.frame_idx
, NULL
);
347 if (u_reduce_video_profile(context
->decoder
->profile
) ==
348 PIPE_VIDEO_FORMAT_HEVC
) {
349 if (context
->desc
.h265enc
.frame_idx
)
350 _mesa_hash_table_destroy(context
->desc
.h265enc
.frame_idx
, NULL
);
353 if (u_reduce_video_profile(context
->decoder
->profile
) ==
354 PIPE_VIDEO_FORMAT_MPEG4_AVC
) {
355 FREE(context
->desc
.h264
.pps
->sps
);
356 FREE(context
->desc
.h264
.pps
);
358 if (u_reduce_video_profile(context
->decoder
->profile
) ==
359 PIPE_VIDEO_FORMAT_HEVC
) {
360 FREE(context
->desc
.h265
.pps
->sps
);
361 FREE(context
->desc
.h265
.pps
);
364 context
->decoder
->destroy(context
->decoder
);
366 if (context
->blit_cs
)
367 drv
->pipe
->delete_compute_state(drv
->pipe
, context
->blit_cs
);
368 if (context
->deint
) {
369 vl_deint_filter_cleanup(context
->deint
);
370 FREE(context
->deint
);
373 handle_table_remove(drv
->htab
, context_id
);
374 mtx_unlock(&drv
->mutex
);
376 return VA_STATUS_SUCCESS
;
380 vlVaTerminate(VADriverContextP ctx
)
385 return VA_STATUS_ERROR_INVALID_CONTEXT
;
387 drv
= ctx
->pDriverData
;
388 vl_compositor_cleanup_state(&drv
->cstate
);
389 vl_compositor_cleanup(&drv
->compositor
);
390 drv
->pipe
->destroy(drv
->pipe
);
391 drv
->vscreen
->destroy(drv
->vscreen
);
392 handle_table_destroy(drv
->htab
);
393 mtx_destroy(&drv
->mutex
);
396 return VA_STATUS_SUCCESS
;