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
94 static struct VADriverVTableVPP vtable_vpp
=
97 &vlVaQueryVideoProcFilters
,
98 &vlVaQueryVideoProcFilterCaps
,
99 &vlVaQueryVideoProcPipelineCaps
103 VA_DRIVER_INIT_FUNC(VADriverContextP ctx
)
106 struct drm_state
*drm_info
;
109 return VA_STATUS_ERROR_INVALID_CONTEXT
;
111 drv
= CALLOC(1, sizeof(vlVaDriver
));
113 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
115 switch (ctx
->display_type
) {
116 case VA_DISPLAY_ANDROID
:
118 return VA_STATUS_ERROR_UNIMPLEMENTED
;
121 #if defined(HAVE_DRI3)
122 drv
->vscreen
= vl_dri3_screen_create(ctx
->native_dpy
, ctx
->x11_screen
);
125 drv
->vscreen
= vl_dri2_screen_create(ctx
->native_dpy
, ctx
->x11_screen
);
129 case VA_DISPLAY_WAYLAND
:
131 case VA_DISPLAY_DRM_RENDERNODES
: {
132 drm_info
= (struct drm_state
*) ctx
->drm_state
;
134 if (!drm_info
|| drm_info
->fd
< 0) {
136 return VA_STATUS_ERROR_INVALID_PARAMETER
;
139 drv
->vscreen
= vl_drm_screen_create(drm_info
->fd
);
146 return VA_STATUS_ERROR_INVALID_DISPLAY
;
149 drv
->pipe
= drv
->vscreen
->pscreen
->context_create(drv
->vscreen
->pscreen
,
154 drv
->htab
= handle_table_create();
158 vl_compositor_init(&drv
->compositor
, drv
->pipe
);
159 vl_compositor_init_state(&drv
->cstate
, drv
->pipe
);
161 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601
, NULL
, true, &drv
->csc
);
162 vl_compositor_set_csc_matrix(&drv
->cstate
, (const vl_csc_matrix
*)&drv
->csc
, 1.0f
, 0.0f
);
163 pipe_mutex_init(drv
->mutex
);
165 ctx
->pDriverData
= (void *)drv
;
166 ctx
->version_major
= 0;
167 ctx
->version_minor
= 1;
168 *ctx
->vtable
= vtable
;
169 *ctx
->vtable_vpp
= vtable_vpp
;
170 ctx
->max_profiles
= PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH
- PIPE_VIDEO_PROFILE_UNKNOWN
;
171 ctx
->max_entrypoints
= 1;
172 ctx
->max_attributes
= 1;
173 ctx
->max_image_formats
= VL_VA_MAX_IMAGE_FORMATS
;
174 ctx
->max_subpic_formats
= 1;
175 ctx
->max_display_attributes
= 1;
176 ctx
->str_vendor
= "mesa gallium vaapi";
178 return VA_STATUS_SUCCESS
;
181 drv
->pipe
->destroy(drv
->pipe
);
184 drv
->vscreen
->destroy(drv
->vscreen
);
188 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
192 vlVaCreateContext(VADriverContextP ctx
, VAConfigID config_id
, int picture_width
,
193 int picture_height
, int flag
, VASurfaceID
*render_targets
,
194 int num_render_targets
, VAContextID
*context_id
)
197 vlVaContext
*context
;
202 return VA_STATUS_ERROR_INVALID_CONTEXT
;
204 drv
= VL_VA_DRIVER(ctx
);
205 pipe_mutex_lock(drv
->mutex
);
206 config
= handle_table_get(drv
->htab
, config_id
);
207 pipe_mutex_unlock(drv
->mutex
);
209 is_vpp
= config
->profile
== PIPE_VIDEO_PROFILE_UNKNOWN
&& !picture_width
&&
210 !picture_height
&& !flag
&& !render_targets
&& !num_render_targets
;
212 if (!(picture_width
&& picture_height
) && !is_vpp
)
213 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT
;
215 context
= CALLOC(1, sizeof(vlVaContext
));
217 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
220 context
->decoder
= NULL
;
221 if (!drv
->compositor
.upload
) {
223 return VA_STATUS_ERROR_INVALID_CONTEXT
;
226 context
->templat
.profile
= config
->profile
;
227 context
->templat
.entrypoint
= config
->entrypoint
;
228 context
->templat
.chroma_format
= PIPE_VIDEO_CHROMA_FORMAT_420
;
229 context
->templat
.width
= picture_width
;
230 context
->templat
.height
= picture_height
;
231 context
->templat
.expect_chunked_decode
= true;
233 switch (u_reduce_video_profile(context
->templat
.profile
)) {
234 case PIPE_VIDEO_FORMAT_MPEG12
:
235 case PIPE_VIDEO_FORMAT_VC1
:
236 case PIPE_VIDEO_FORMAT_MPEG4
:
237 context
->templat
.max_references
= 2;
240 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
241 context
->templat
.max_references
= 0;
242 if (config
->entrypoint
!= PIPE_VIDEO_ENTRYPOINT_ENCODE
) {
243 context
->desc
.h264
.pps
= CALLOC_STRUCT(pipe_h264_pps
);
244 if (!context
->desc
.h264
.pps
) {
246 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
248 context
->desc
.h264
.pps
->sps
= CALLOC_STRUCT(pipe_h264_sps
);
249 if (!context
->desc
.h264
.pps
->sps
) {
250 FREE(context
->desc
.h264
.pps
);
252 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
257 case PIPE_VIDEO_FORMAT_HEVC
:
258 context
->templat
.max_references
= num_render_targets
;
259 context
->desc
.h265
.pps
= CALLOC_STRUCT(pipe_h265_pps
);
260 if (!context
->desc
.h265
.pps
) {
262 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
264 context
->desc
.h265
.pps
->sps
= CALLOC_STRUCT(pipe_h265_sps
);
265 if (!context
->desc
.h265
.pps
->sps
) {
266 FREE(context
->desc
.h265
.pps
);
268 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
277 context
->desc
.base
.profile
= config
->profile
;
278 context
->desc
.base
.entry_point
= config
->entrypoint
;
280 pipe_mutex_lock(drv
->mutex
);
281 *context_id
= handle_table_add(drv
->htab
, context
);
282 pipe_mutex_unlock(drv
->mutex
);
284 return VA_STATUS_SUCCESS
;
288 vlVaDestroyContext(VADriverContextP ctx
, VAContextID context_id
)
291 vlVaContext
*context
;
294 return VA_STATUS_ERROR_INVALID_CONTEXT
;
296 drv
= VL_VA_DRIVER(ctx
);
297 pipe_mutex_lock(drv
->mutex
);
298 context
= handle_table_get(drv
->htab
, context_id
);
300 pipe_mutex_unlock(drv
->mutex
);
301 return VA_STATUS_ERROR_INVALID_CONTEXT
;
304 if (context
->decoder
) {
305 if (context
->desc
.base
.entry_point
!= PIPE_VIDEO_ENTRYPOINT_ENCODE
) {
306 if (u_reduce_video_profile(context
->decoder
->profile
) ==
307 PIPE_VIDEO_FORMAT_MPEG4_AVC
) {
308 FREE(context
->desc
.h264
.pps
->sps
);
309 FREE(context
->desc
.h264
.pps
);
311 if (u_reduce_video_profile(context
->decoder
->profile
) ==
312 PIPE_VIDEO_FORMAT_HEVC
) {
313 FREE(context
->desc
.h265
.pps
->sps
);
314 FREE(context
->desc
.h265
.pps
);
317 context
->decoder
->destroy(context
->decoder
);
319 if (context
->deint
) {
320 vl_deint_filter_cleanup(context
->deint
);
321 FREE(context
->deint
);
324 handle_table_remove(drv
->htab
, context_id
);
325 pipe_mutex_unlock(drv
->mutex
);
327 return VA_STATUS_SUCCESS
;
331 vlVaTerminate(VADriverContextP ctx
)
336 return VA_STATUS_ERROR_INVALID_CONTEXT
;
338 drv
= ctx
->pDriverData
;
339 vl_compositor_cleanup_state(&drv
->cstate
);
340 vl_compositor_cleanup(&drv
->compositor
);
341 drv
->pipe
->destroy(drv
->pipe
);
342 drv
->vscreen
->destroy(drv
->vscreen
);
343 handle_table_destroy(drv
->htab
);
344 pipe_mutex_destroy(drv
->mutex
);
347 return VA_STATUS_SUCCESS
;