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_winsys.h"
36 #include "va_private.h"
38 #include <va/va_drmcommon.h>
40 static struct VADriverVTable vtable
=
43 &vlVaQueryConfigProfiles
,
44 &vlVaQueryConfigEntrypoints
,
45 &vlVaGetConfigAttributes
,
48 &vlVaQueryConfigAttributes
,
54 &vlVaBufferSetNumElements
,
62 &vlVaQuerySurfaceStatus
,
63 &vlVaQuerySurfaceError
,
65 &vlVaQueryImageFormats
,
72 &vlVaQuerySubpictureFormats
,
73 &vlVaCreateSubpicture
,
74 &vlVaDestroySubpicture
,
76 &vlVaSetSubpictureChromakey
,
77 &vlVaSetSubpictureGlobalAlpha
,
78 &vlVaAssociateSubpicture
,
79 &vlVaDeassociateSubpicture
,
80 &vlVaQueryDisplayAttributes
,
81 &vlVaGetDisplayAttributes
,
82 &vlVaSetDisplayAttributes
,
86 NULL
, /* DEPRECATED VaGetSurfaceAttributes */
88 &vlVaQuerySurfaceAttributes
,
89 &vlVaAcquireBufferHandle
,
90 &vlVaReleaseBufferHandle
93 static struct VADriverVTableVPP vtable_vpp
=
96 &vlVaQueryVideoProcFilters
,
97 &vlVaQueryVideoProcFilterCaps
,
98 &vlVaQueryVideoProcPipelineCaps
102 VA_DRIVER_INIT_FUNC(VADriverContextP ctx
)
105 struct drm_state
*drm_info
;
108 return VA_STATUS_ERROR_INVALID_CONTEXT
;
110 drv
= CALLOC(1, sizeof(vlVaDriver
));
112 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
114 switch (ctx
->display_type
) {
115 case VA_DISPLAY_ANDROID
:
116 case VA_DISPLAY_WAYLAND
:
118 return VA_STATUS_ERROR_UNIMPLEMENTED
;
121 drv
->vscreen
= vl_dri2_screen_create(ctx
->native_dpy
, ctx
->x11_screen
);
126 case VA_DISPLAY_DRM_RENDERNODES
: {
127 drm_info
= (struct drm_state
*) ctx
->drm_state
;
129 if (!drm_info
|| drm_info
->fd
< 0) {
131 return VA_STATUS_ERROR_INVALID_PARAMETER
;
134 drv
->vscreen
= vl_drm_screen_create(drm_info
->fd
);
141 return VA_STATUS_ERROR_INVALID_DISPLAY
;
144 drv
->pipe
= drv
->vscreen
->pscreen
->context_create(drv
->vscreen
->pscreen
,
149 drv
->htab
= handle_table_create();
153 vl_compositor_init(&drv
->compositor
, drv
->pipe
);
154 vl_compositor_init_state(&drv
->cstate
, drv
->pipe
);
156 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601
, NULL
, true, &drv
->csc
);
157 vl_compositor_set_csc_matrix(&drv
->cstate
, (const vl_csc_matrix
*)&drv
->csc
);
158 pipe_mutex_init(drv
->mutex
);
160 ctx
->pDriverData
= (void *)drv
;
161 ctx
->version_major
= 0;
162 ctx
->version_minor
= 1;
163 *ctx
->vtable
= vtable
;
164 *ctx
->vtable_vpp
= vtable_vpp
;
165 ctx
->max_profiles
= PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH
- PIPE_VIDEO_PROFILE_UNKNOWN
;
166 ctx
->max_entrypoints
= 1;
167 ctx
->max_attributes
= 1;
168 ctx
->max_image_formats
= VL_VA_MAX_IMAGE_FORMATS
;
169 ctx
->max_subpic_formats
= 1;
170 ctx
->max_display_attributes
= 1;
171 ctx
->str_vendor
= "mesa gallium vaapi";
173 return VA_STATUS_SUCCESS
;
176 drv
->pipe
->destroy(drv
->pipe
);
179 drv
->vscreen
->destroy(drv
->vscreen
);
183 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
187 vlVaCreateContext(VADriverContextP ctx
, VAConfigID config_id
, int picture_width
,
188 int picture_height
, int flag
, VASurfaceID
*render_targets
,
189 int num_render_targets
, VAContextID
*context_id
)
192 vlVaContext
*context
;
196 return VA_STATUS_ERROR_INVALID_CONTEXT
;
198 is_vpp
= config_id
== PIPE_VIDEO_PROFILE_UNKNOWN
&& !picture_width
&&
199 !picture_height
&& !flag
&& !render_targets
&& !num_render_targets
;
201 if (!(picture_width
&& picture_height
) && !is_vpp
)
202 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT
;
204 drv
= VL_VA_DRIVER(ctx
);
205 context
= CALLOC(1, sizeof(vlVaContext
));
207 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
210 context
->decoder
= NULL
;
211 if (!drv
->compositor
.upload
) {
213 return VA_STATUS_ERROR_INVALID_CONTEXT
;
216 context
->templat
.profile
= config_id
;
217 context
->templat
.entrypoint
= PIPE_VIDEO_ENTRYPOINT_BITSTREAM
;
218 context
->templat
.chroma_format
= PIPE_VIDEO_CHROMA_FORMAT_420
;
219 context
->templat
.width
= picture_width
;
220 context
->templat
.height
= picture_height
;
221 context
->templat
.expect_chunked_decode
= true;
223 switch (u_reduce_video_profile(context
->templat
.profile
)) {
224 case PIPE_VIDEO_FORMAT_MPEG12
:
225 case PIPE_VIDEO_FORMAT_VC1
:
226 case PIPE_VIDEO_FORMAT_MPEG4
:
227 context
->templat
.max_references
= 2;
230 case PIPE_VIDEO_FORMAT_MPEG4_AVC
:
231 context
->templat
.max_references
= 0;
232 context
->desc
.h264
.pps
= CALLOC_STRUCT(pipe_h264_pps
);
233 if (!context
->desc
.h264
.pps
) {
235 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
237 context
->desc
.h264
.pps
->sps
= CALLOC_STRUCT(pipe_h264_sps
);
238 if (!context
->desc
.h264
.pps
->sps
) {
239 FREE(context
->desc
.h264
.pps
);
241 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
245 case PIPE_VIDEO_FORMAT_HEVC
:
246 context
->templat
.max_references
= num_render_targets
;
247 context
->desc
.h265
.pps
= CALLOC_STRUCT(pipe_h265_pps
);
248 if (!context
->desc
.h265
.pps
) {
250 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
252 context
->desc
.h265
.pps
->sps
= CALLOC_STRUCT(pipe_h265_sps
);
253 if (!context
->desc
.h265
.pps
->sps
) {
254 FREE(context
->desc
.h265
.pps
);
256 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
265 context
->desc
.base
.profile
= config_id
;
266 pipe_mutex_lock(drv
->mutex
);
267 *context_id
= handle_table_add(drv
->htab
, context
);
268 pipe_mutex_unlock(drv
->mutex
);
270 return VA_STATUS_SUCCESS
;
274 vlVaDestroyContext(VADriverContextP ctx
, VAContextID context_id
)
277 vlVaContext
*context
;
280 return VA_STATUS_ERROR_INVALID_CONTEXT
;
282 drv
= VL_VA_DRIVER(ctx
);
283 pipe_mutex_lock(drv
->mutex
);
284 context
= handle_table_get(drv
->htab
, context_id
);
286 if (context
->decoder
) {
287 if (u_reduce_video_profile(context
->decoder
->profile
) ==
288 PIPE_VIDEO_FORMAT_MPEG4_AVC
) {
289 FREE(context
->desc
.h264
.pps
->sps
);
290 FREE(context
->desc
.h264
.pps
);
292 if (u_reduce_video_profile(context
->decoder
->profile
) ==
293 PIPE_VIDEO_FORMAT_HEVC
) {
294 FREE(context
->desc
.h265
.pps
->sps
);
295 FREE(context
->desc
.h265
.pps
);
297 context
->decoder
->destroy(context
->decoder
);
300 handle_table_remove(drv
->htab
, context_id
);
301 pipe_mutex_unlock(drv
->mutex
);
303 return VA_STATUS_SUCCESS
;
307 vlVaTerminate(VADriverContextP ctx
)
312 return VA_STATUS_ERROR_INVALID_CONTEXT
;
314 drv
= ctx
->pDriverData
;
315 vl_compositor_cleanup_state(&drv
->cstate
);
316 vl_compositor_cleanup(&drv
->compositor
);
317 drv
->pipe
->destroy(drv
->pipe
);
318 drv
->vscreen
->destroy(drv
->vscreen
);
319 handle_table_destroy(drv
->htab
);
320 pipe_mutex_destroy(drv
->mutex
);
323 return VA_STATUS_SUCCESS
;