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 "vl/vl_deint_filter.h"
35 #include "vl/vl_winsys.h"
36
37 #include "va_private.h"
38
39 #include <va/va_drmcommon.h>
40
41 static struct VADriverVTable vtable =
42 {
43 &vlVaTerminate,
44 &vlVaQueryConfigProfiles,
45 &vlVaQueryConfigEntrypoints,
46 &vlVaGetConfigAttributes,
47 &vlVaCreateConfig,
48 &vlVaDestroyConfig,
49 &vlVaQueryConfigAttributes,
50 &vlVaCreateSurfaces,
51 &vlVaDestroySurfaces,
52 &vlVaCreateContext,
53 &vlVaDestroyContext,
54 &vlVaCreateBuffer,
55 &vlVaBufferSetNumElements,
56 &vlVaMapBuffer,
57 &vlVaUnmapBuffer,
58 &vlVaDestroyBuffer,
59 &vlVaBeginPicture,
60 &vlVaRenderPicture,
61 &vlVaEndPicture,
62 &vlVaSyncSurface,
63 &vlVaQuerySurfaceStatus,
64 &vlVaQuerySurfaceError,
65 &vlVaPutSurface,
66 &vlVaQueryImageFormats,
67 &vlVaCreateImage,
68 &vlVaDeriveImage,
69 &vlVaDestroyImage,
70 &vlVaSetImagePalette,
71 &vlVaGetImage,
72 &vlVaPutImage,
73 &vlVaQuerySubpictureFormats,
74 &vlVaCreateSubpicture,
75 &vlVaDestroySubpicture,
76 &vlVaSubpictureImage,
77 &vlVaSetSubpictureChromakey,
78 &vlVaSetSubpictureGlobalAlpha,
79 &vlVaAssociateSubpicture,
80 &vlVaDeassociateSubpicture,
81 &vlVaQueryDisplayAttributes,
82 &vlVaGetDisplayAttributes,
83 &vlVaSetDisplayAttributes,
84 &vlVaBufferInfo,
85 &vlVaLockSurface,
86 &vlVaUnlockSurface,
87 NULL, /* DEPRECATED VaGetSurfaceAttributes */
88 &vlVaCreateSurfaces2,
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,
100 #endif
101 };
102
103 static struct VADriverVTableVPP vtable_vpp =
104 {
105 1,
106 &vlVaQueryVideoProcFilters,
107 &vlVaQueryVideoProcFilterCaps,
108 &vlVaQueryVideoProcPipelineCaps
109 };
110
111 PUBLIC VAStatus
VA_DRIVER_INIT_FUNC(VADriverContextP ctx)112 VA_DRIVER_INIT_FUNC(VADriverContextP ctx)
113 {
114 vlVaDriver *drv;
115
116 if (!ctx)
117 return VA_STATUS_ERROR_INVALID_CONTEXT;
118
119 drv = CALLOC(1, sizeof(vlVaDriver));
120 if (!drv)
121 return VA_STATUS_ERROR_ALLOCATION_FAILED;
122
123 switch (ctx->display_type) {
124 case VA_DISPLAY_ANDROID:
125 FREE(drv);
126 return VA_STATUS_ERROR_UNIMPLEMENTED;
127 case VA_DISPLAY_GLX:
128 case VA_DISPLAY_X11:
129 drv->vscreen = vl_dri3_screen_create(ctx->native_dpy, ctx->x11_screen);
130 if (!drv->vscreen)
131 drv->vscreen = vl_dri2_screen_create(ctx->native_dpy, ctx->x11_screen);
132 break;
133 case VA_DISPLAY_WAYLAND:
134 case VA_DISPLAY_DRM:
135 case VA_DISPLAY_DRM_RENDERNODES: {
136 const struct drm_state *drm_info = (struct drm_state *) ctx->drm_state;
137
138 if (!drm_info || drm_info->fd < 0) {
139 FREE(drv);
140 return VA_STATUS_ERROR_INVALID_PARAMETER;
141 }
142
143 drv->vscreen = vl_drm_screen_create(drm_info->fd);
144 break;
145 }
146 default:
147 FREE(drv);
148 return VA_STATUS_ERROR_INVALID_DISPLAY;
149 }
150
151 if (!drv->vscreen)
152 goto error_screen;
153
154 drv->pipe = pipe_create_multimedia_context(drv->vscreen->pscreen);
155 if (!drv->pipe)
156 goto error_pipe;
157
158 drv->htab = handle_table_create();
159 if (!drv->htab)
160 goto error_htab;
161
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;
166
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);
171
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 = 0;
183
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;
188
189 return VA_STATUS_SUCCESS;
190
191 error_csc_matrix:
192 vl_compositor_cleanup_state(&drv->cstate);
193
194 error_compositor_state:
195 vl_compositor_cleanup(&drv->compositor);
196
197 error_compositor:
198 handle_table_destroy(drv->htab);
199
200 error_htab:
201 drv->pipe->destroy(drv->pipe);
202
203 error_pipe:
204 drv->vscreen->destroy(drv->vscreen);
205
206 error_screen:
207 FREE(drv);
208 return VA_STATUS_ERROR_ALLOCATION_FAILED;
209 }
210
211 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)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)
215 {
216 vlVaDriver *drv;
217 vlVaContext *context;
218 vlVaConfig *config;
219 int is_vpp;
220 int max_supported_width,max_supported_height;
221
222 if (!ctx)
223 return VA_STATUS_ERROR_INVALID_CONTEXT;
224
225 drv = VL_VA_DRIVER(ctx);
226 mtx_lock(&drv->mutex);
227 config = handle_table_get(drv->htab, config_id);
228 mtx_unlock(&drv->mutex);
229
230 if (!config)
231 return VA_STATUS_ERROR_INVALID_CONFIG;
232
233 is_vpp = config->profile == PIPE_VIDEO_PROFILE_UNKNOWN && !picture_width &&
234 !picture_height && !flag && !render_targets && !num_render_targets;
235
236 if (!(picture_width && picture_height) && !is_vpp)
237 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
238
239 context = CALLOC(1, sizeof(vlVaContext));
240 if (!context)
241 return VA_STATUS_ERROR_ALLOCATION_FAILED;
242
243 if (is_vpp) {
244 context->decoder = NULL;
245 } else {
246 if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_UNKNOWN) {
247 max_supported_width = drv->vscreen->pscreen->get_video_param(drv->vscreen->pscreen,
248 config->profile, config->entrypoint,
249 PIPE_VIDEO_CAP_MAX_WIDTH);
250 max_supported_height = drv->vscreen->pscreen->get_video_param(drv->vscreen->pscreen,
251 config->profile, config->entrypoint,
252 PIPE_VIDEO_CAP_MAX_HEIGHT);
253
254 if (picture_width > max_supported_width || picture_height > max_supported_height) {
255 FREE(context);
256 return VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED;
257 }
258 }
259 context->templat.profile = config->profile;
260 context->templat.entrypoint = config->entrypoint;
261 context->templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
262 context->templat.width = picture_width;
263 context->templat.height = picture_height;
264 context->templat.expect_chunked_decode = true;
265
266 switch (u_reduce_video_profile(context->templat.profile)) {
267 case PIPE_VIDEO_FORMAT_MPEG12:
268 case PIPE_VIDEO_FORMAT_VC1:
269 case PIPE_VIDEO_FORMAT_MPEG4:
270 context->templat.max_references = 2;
271 break;
272
273 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
274 context->templat.max_references = 0;
275 if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE) {
276 context->desc.h264.pps = CALLOC_STRUCT(pipe_h264_pps);
277 if (!context->desc.h264.pps) {
278 FREE(context);
279 return VA_STATUS_ERROR_ALLOCATION_FAILED;
280 }
281 context->desc.h264.pps->sps = CALLOC_STRUCT(pipe_h264_sps);
282 if (!context->desc.h264.pps->sps) {
283 FREE(context->desc.h264.pps);
284 FREE(context);
285 return VA_STATUS_ERROR_ALLOCATION_FAILED;
286 }
287 }
288 break;
289
290 case PIPE_VIDEO_FORMAT_HEVC:
291 if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE) {
292 context->desc.h265.pps = CALLOC_STRUCT(pipe_h265_pps);
293 if (!context->desc.h265.pps) {
294 FREE(context);
295 return VA_STATUS_ERROR_ALLOCATION_FAILED;
296 }
297 context->desc.h265.pps->sps = CALLOC_STRUCT(pipe_h265_sps);
298 if (!context->desc.h265.pps->sps) {
299 FREE(context->desc.h265.pps);
300 FREE(context);
301 return VA_STATUS_ERROR_ALLOCATION_FAILED;
302 }
303 }
304 break;
305
306 case PIPE_VIDEO_FORMAT_VP9:
307 break;
308
309 default:
310 break;
311 }
312 }
313
314 context->desc.base.profile = config->profile;
315 context->desc.base.entry_point = config->entrypoint;
316 if (config->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
317 switch (u_reduce_video_profile(context->templat.profile)) {
318 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
319 context->desc.h264enc.rate_ctrl[0].rate_ctrl_method = config->rc;
320 context->desc.h264enc.frame_idx = util_hash_table_create_ptr_keys();
321 break;
322 case PIPE_VIDEO_FORMAT_HEVC:
323 context->desc.h265enc.rc.rate_ctrl_method = config->rc;
324 context->desc.h265enc.frame_idx = util_hash_table_create_ptr_keys();
325 break;
326 default:
327 break;
328 }
329 }
330
331 mtx_lock(&drv->mutex);
332 *context_id = handle_table_add(drv->htab, context);
333 mtx_unlock(&drv->mutex);
334
335 return VA_STATUS_SUCCESS;
336 }
337
338 VAStatus
vlVaDestroyContext(VADriverContextP ctx,VAContextID context_id)339 vlVaDestroyContext(VADriverContextP ctx, VAContextID context_id)
340 {
341 vlVaDriver *drv;
342 vlVaContext *context;
343
344 if (!ctx)
345 return VA_STATUS_ERROR_INVALID_CONTEXT;
346
347 drv = VL_VA_DRIVER(ctx);
348 mtx_lock(&drv->mutex);
349 context = handle_table_get(drv->htab, context_id);
350 if (!context) {
351 mtx_unlock(&drv->mutex);
352 return VA_STATUS_ERROR_INVALID_CONTEXT;
353 }
354
355 if (context->decoder) {
356 if (context->desc.base.entry_point == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
357 if (u_reduce_video_profile(context->decoder->profile) ==
358 PIPE_VIDEO_FORMAT_MPEG4_AVC) {
359 if (context->desc.h264enc.frame_idx)
360 _mesa_hash_table_destroy(context->desc.h264enc.frame_idx, NULL);
361 }
362 if (u_reduce_video_profile(context->decoder->profile) ==
363 PIPE_VIDEO_FORMAT_HEVC) {
364 if (context->desc.h265enc.frame_idx)
365 _mesa_hash_table_destroy(context->desc.h265enc.frame_idx, NULL);
366 }
367 } else {
368 if (u_reduce_video_profile(context->decoder->profile) ==
369 PIPE_VIDEO_FORMAT_MPEG4_AVC) {
370 FREE(context->desc.h264.pps->sps);
371 FREE(context->desc.h264.pps);
372 }
373 if (u_reduce_video_profile(context->decoder->profile) ==
374 PIPE_VIDEO_FORMAT_HEVC) {
375 FREE(context->desc.h265.pps->sps);
376 FREE(context->desc.h265.pps);
377 }
378 }
379 context->decoder->destroy(context->decoder);
380 }
381 if (context->blit_cs)
382 drv->pipe->delete_compute_state(drv->pipe, context->blit_cs);
383 if (context->deint) {
384 vl_deint_filter_cleanup(context->deint);
385 FREE(context->deint);
386 }
387 FREE(context);
388 handle_table_remove(drv->htab, context_id);
389 mtx_unlock(&drv->mutex);
390
391 return VA_STATUS_SUCCESS;
392 }
393
394 VAStatus
vlVaTerminate(VADriverContextP ctx)395 vlVaTerminate(VADriverContextP ctx)
396 {
397 vlVaDriver *drv;
398
399 if (!ctx)
400 return VA_STATUS_ERROR_INVALID_CONTEXT;
401
402 drv = ctx->pDriverData;
403 vl_compositor_cleanup_state(&drv->cstate);
404 vl_compositor_cleanup(&drv->compositor);
405 drv->pipe->destroy(drv->pipe);
406 drv->vscreen->destroy(drv->vscreen);
407 handle_table_destroy(drv->htab);
408 mtx_destroy(&drv->mutex);
409 FREE(drv);
410
411 return VA_STATUS_SUCCESS;
412 }
413