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 = 1;
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
221 if (!ctx)
222 return VA_STATUS_ERROR_INVALID_CONTEXT;
223
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);
228
229 is_vpp = config->profile == PIPE_VIDEO_PROFILE_UNKNOWN && !picture_width &&
230 !picture_height && !flag && !render_targets && !num_render_targets;
231
232 if (!(picture_width && picture_height) && !is_vpp)
233 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
234
235 context = CALLOC(1, sizeof(vlVaContext));
236 if (!context)
237 return VA_STATUS_ERROR_ALLOCATION_FAILED;
238
239 if (is_vpp) {
240 context->decoder = NULL;
241 } else {
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;
248
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;
254 break;
255
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) {
261 FREE(context);
262 return VA_STATUS_ERROR_ALLOCATION_FAILED;
263 }
264 context->desc.h264.pps->sps = CALLOC_STRUCT(pipe_h264_sps);
265 if (!context->desc.h264.pps->sps) {
266 FREE(context->desc.h264.pps);
267 FREE(context);
268 return VA_STATUS_ERROR_ALLOCATION_FAILED;
269 }
270 }
271 break;
272
273 case PIPE_VIDEO_FORMAT_HEVC:
274 if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE) {
275 context->desc.h265.pps = CALLOC_STRUCT(pipe_h265_pps);
276 if (!context->desc.h265.pps) {
277 FREE(context);
278 return VA_STATUS_ERROR_ALLOCATION_FAILED;
279 }
280 context->desc.h265.pps->sps = CALLOC_STRUCT(pipe_h265_sps);
281 if (!context->desc.h265.pps->sps) {
282 FREE(context->desc.h265.pps);
283 FREE(context);
284 return VA_STATUS_ERROR_ALLOCATION_FAILED;
285 }
286 }
287 break;
288
289 case PIPE_VIDEO_FORMAT_VP9:
290 break;
291
292 default:
293 break;
294 }
295 }
296
297 context->desc.base.profile = config->profile;
298 context->desc.base.entry_point = config->entrypoint;
299 if (config->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
300 switch (u_reduce_video_profile(context->templat.profile)) {
301 case PIPE_VIDEO_FORMAT_MPEG4_AVC:
302 context->desc.h264enc.rate_ctrl.rate_ctrl_method = config->rc;
303 context->desc.h264enc.frame_idx = util_hash_table_create_ptr_keys();
304 break;
305 case PIPE_VIDEO_FORMAT_HEVC:
306 context->desc.h265enc.rc.rate_ctrl_method = config->rc;
307 context->desc.h265enc.frame_idx = util_hash_table_create_ptr_keys();
308 break;
309 default:
310 break;
311 }
312 }
313
314 mtx_lock(&drv->mutex);
315 *context_id = handle_table_add(drv->htab, context);
316 mtx_unlock(&drv->mutex);
317
318 return VA_STATUS_SUCCESS;
319 }
320
321 VAStatus
vlVaDestroyContext(VADriverContextP ctx,VAContextID context_id)322 vlVaDestroyContext(VADriverContextP ctx, VAContextID context_id)
323 {
324 vlVaDriver *drv;
325 vlVaContext *context;
326
327 if (!ctx)
328 return VA_STATUS_ERROR_INVALID_CONTEXT;
329
330 drv = VL_VA_DRIVER(ctx);
331 mtx_lock(&drv->mutex);
332 context = handle_table_get(drv->htab, context_id);
333 if (!context) {
334 mtx_unlock(&drv->mutex);
335 return VA_STATUS_ERROR_INVALID_CONTEXT;
336 }
337
338 if (context->decoder) {
339 if (context->desc.base.entry_point == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
340 if (u_reduce_video_profile(context->decoder->profile) ==
341 PIPE_VIDEO_FORMAT_MPEG4_AVC) {
342 if (context->desc.h264enc.frame_idx)
343 _mesa_hash_table_destroy(context->desc.h264enc.frame_idx, NULL);
344 }
345 if (u_reduce_video_profile(context->decoder->profile) ==
346 PIPE_VIDEO_FORMAT_HEVC) {
347 if (context->desc.h265enc.frame_idx)
348 _mesa_hash_table_destroy(context->desc.h265enc.frame_idx, NULL);
349 }
350 } else {
351 if (u_reduce_video_profile(context->decoder->profile) ==
352 PIPE_VIDEO_FORMAT_MPEG4_AVC) {
353 FREE(context->desc.h264.pps->sps);
354 FREE(context->desc.h264.pps);
355 }
356 if (u_reduce_video_profile(context->decoder->profile) ==
357 PIPE_VIDEO_FORMAT_HEVC) {
358 FREE(context->desc.h265.pps->sps);
359 FREE(context->desc.h265.pps);
360 }
361 }
362 context->decoder->destroy(context->decoder);
363 }
364 if (context->blit_cs)
365 drv->pipe->delete_compute_state(drv->pipe, context->blit_cs);
366 if (context->deint) {
367 vl_deint_filter_cleanup(context->deint);
368 FREE(context->deint);
369 }
370 FREE(context);
371 handle_table_remove(drv->htab, context_id);
372 mtx_unlock(&drv->mutex);
373
374 return VA_STATUS_SUCCESS;
375 }
376
377 VAStatus
vlVaTerminate(VADriverContextP ctx)378 vlVaTerminate(VADriverContextP ctx)
379 {
380 vlVaDriver *drv;
381
382 if (!ctx)
383 return VA_STATUS_ERROR_INVALID_CONTEXT;
384
385 drv = ctx->pDriverData;
386 vl_compositor_cleanup_state(&drv->cstate);
387 vl_compositor_cleanup(&drv->compositor);
388 drv->pipe->destroy(drv->pipe);
389 drv->vscreen->destroy(drv->vscreen);
390 handle_table_destroy(drv->htab);
391 mtx_destroy(&drv->mutex);
392 FREE(drv);
393
394 return VA_STATUS_SUCCESS;
395 }
396