• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = drv->vscreen->pscreen->context_create(drv->vscreen->pscreen,
155                                                      NULL, 0);
156    if (!drv->pipe)
157       goto error_pipe;
158 
159    drv->htab = handle_table_create();
160    if (!drv->htab)
161       goto error_htab;
162 
163    if (!vl_compositor_init(&drv->compositor, drv->pipe))
164       goto error_compositor;
165    if (!vl_compositor_init_state(&drv->cstate, drv->pipe))
166       goto error_compositor_state;
167 
168    vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_601, NULL, true, &drv->csc);
169    if (!vl_compositor_set_csc_matrix(&drv->cstate, (const vl_csc_matrix *)&drv->csc, 1.0f, 0.0f))
170       goto error_csc_matrix;
171    (void) mtx_init(&drv->mutex, mtx_plain);
172 
173    ctx->pDriverData = (void *)drv;
174    ctx->version_major = 0;
175    ctx->version_minor = 1;
176    *ctx->vtable = vtable;
177    *ctx->vtable_vpp = vtable_vpp;
178    ctx->max_profiles = PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH - PIPE_VIDEO_PROFILE_UNKNOWN;
179    ctx->max_entrypoints = 2;
180    ctx->max_attributes = 1;
181    ctx->max_image_formats = VL_VA_MAX_IMAGE_FORMATS;
182    ctx->max_subpic_formats = 1;
183    ctx->max_display_attributes = 1;
184    ctx->str_vendor = "mesa gallium vaapi";
185 
186    return VA_STATUS_SUCCESS;
187 
188 error_csc_matrix:
189    vl_compositor_cleanup_state(&drv->cstate);
190 
191 error_compositor_state:
192    vl_compositor_cleanup(&drv->compositor);
193 
194 error_compositor:
195    handle_table_destroy(drv->htab);
196 
197 error_htab:
198    drv->pipe->destroy(drv->pipe);
199 
200 error_pipe:
201    drv->vscreen->destroy(drv->vscreen);
202 
203 error_screen:
204    FREE(drv);
205    return VA_STATUS_ERROR_ALLOCATION_FAILED;
206 }
207 
208 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)209 vlVaCreateContext(VADriverContextP ctx, VAConfigID config_id, int picture_width,
210                   int picture_height, int flag, VASurfaceID *render_targets,
211                   int num_render_targets, VAContextID *context_id)
212 {
213    vlVaDriver *drv;
214    vlVaContext *context;
215    vlVaConfig *config;
216    int is_vpp;
217 
218    if (!ctx)
219       return VA_STATUS_ERROR_INVALID_CONTEXT;
220 
221    drv = VL_VA_DRIVER(ctx);
222    mtx_lock(&drv->mutex);
223    config = handle_table_get(drv->htab, config_id);
224    mtx_unlock(&drv->mutex);
225 
226    is_vpp = config->profile == PIPE_VIDEO_PROFILE_UNKNOWN && !picture_width &&
227             !picture_height && !flag && !render_targets && !num_render_targets;
228 
229    if (!(picture_width && picture_height) && !is_vpp)
230       return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
231 
232    context = CALLOC(1, sizeof(vlVaContext));
233    if (!context)
234       return VA_STATUS_ERROR_ALLOCATION_FAILED;
235 
236    if (is_vpp) {
237       context->decoder = NULL;
238    } else {
239       context->templat.profile = config->profile;
240       context->templat.entrypoint = config->entrypoint;
241       context->templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
242       context->templat.width = picture_width;
243       context->templat.height = picture_height;
244       context->templat.expect_chunked_decode = true;
245 
246       switch (u_reduce_video_profile(context->templat.profile)) {
247       case PIPE_VIDEO_FORMAT_MPEG12:
248       case PIPE_VIDEO_FORMAT_VC1:
249       case PIPE_VIDEO_FORMAT_MPEG4:
250          context->templat.max_references = 2;
251          break;
252 
253       case PIPE_VIDEO_FORMAT_MPEG4_AVC:
254          context->templat.max_references = 0;
255          if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_ENCODE) {
256             context->desc.h264.pps = CALLOC_STRUCT(pipe_h264_pps);
257             if (!context->desc.h264.pps) {
258                FREE(context);
259                return VA_STATUS_ERROR_ALLOCATION_FAILED;
260             }
261             context->desc.h264.pps->sps = CALLOC_STRUCT(pipe_h264_sps);
262             if (!context->desc.h264.pps->sps) {
263                FREE(context->desc.h264.pps);
264                FREE(context);
265                return VA_STATUS_ERROR_ALLOCATION_FAILED;
266             }
267          }
268          break;
269 
270      case PIPE_VIDEO_FORMAT_HEVC:
271          context->templat.max_references = num_render_targets;
272          context->desc.h265.pps = CALLOC_STRUCT(pipe_h265_pps);
273          if (!context->desc.h265.pps) {
274             FREE(context);
275             return VA_STATUS_ERROR_ALLOCATION_FAILED;
276          }
277          context->desc.h265.pps->sps = CALLOC_STRUCT(pipe_h265_sps);
278          if (!context->desc.h265.pps->sps) {
279             FREE(context->desc.h265.pps);
280             FREE(context);
281             return VA_STATUS_ERROR_ALLOCATION_FAILED;
282          }
283          break;
284 
285       default:
286          break;
287       }
288    }
289 
290    context->desc.base.profile = config->profile;
291    context->desc.base.entry_point = config->entrypoint;
292    if (config->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
293       context->desc.h264enc.rate_ctrl.rate_ctrl_method = config->rc;
294       context->desc.h264enc.frame_idx = util_hash_table_create(handle_hash, handle_compare);
295    }
296 
297    mtx_lock(&drv->mutex);
298    *context_id = handle_table_add(drv->htab, context);
299    mtx_unlock(&drv->mutex);
300 
301    return VA_STATUS_SUCCESS;
302 }
303 
304 VAStatus
vlVaDestroyContext(VADriverContextP ctx,VAContextID context_id)305 vlVaDestroyContext(VADriverContextP ctx, VAContextID context_id)
306 {
307    vlVaDriver *drv;
308    vlVaContext *context;
309 
310    if (!ctx)
311       return VA_STATUS_ERROR_INVALID_CONTEXT;
312 
313    drv = VL_VA_DRIVER(ctx);
314    mtx_lock(&drv->mutex);
315    context = handle_table_get(drv->htab, context_id);
316    if (!context) {
317       mtx_unlock(&drv->mutex);
318       return VA_STATUS_ERROR_INVALID_CONTEXT;
319    }
320 
321    if (context->decoder) {
322       if (context->desc.base.entry_point == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
323          if (context->desc.h264enc.frame_idx)
324             util_hash_table_destroy (context->desc.h264enc.frame_idx);
325       } else {
326          if (u_reduce_video_profile(context->decoder->profile) ==
327                PIPE_VIDEO_FORMAT_MPEG4_AVC) {
328             FREE(context->desc.h264.pps->sps);
329             FREE(context->desc.h264.pps);
330          }
331          if (u_reduce_video_profile(context->decoder->profile) ==
332                PIPE_VIDEO_FORMAT_HEVC) {
333             FREE(context->desc.h265.pps->sps);
334             FREE(context->desc.h265.pps);
335          }
336       }
337       context->decoder->destroy(context->decoder);
338    }
339    if (context->deint) {
340       vl_deint_filter_cleanup(context->deint);
341       FREE(context->deint);
342    }
343    FREE(context);
344    handle_table_remove(drv->htab, context_id);
345    mtx_unlock(&drv->mutex);
346 
347    return VA_STATUS_SUCCESS;
348 }
349 
350 VAStatus
vlVaTerminate(VADriverContextP ctx)351 vlVaTerminate(VADriverContextP ctx)
352 {
353    vlVaDriver *drv;
354 
355    if (!ctx)
356       return VA_STATUS_ERROR_INVALID_CONTEXT;
357 
358    drv = ctx->pDriverData;
359    vl_compositor_cleanup_state(&drv->cstate);
360    vl_compositor_cleanup(&drv->compositor);
361    drv->pipe->destroy(drv->pipe);
362    drv->vscreen->destroy(drv->vscreen);
363    handle_table_destroy(drv->htab);
364    mtx_destroy(&drv->mutex);
365    FREE(drv);
366 
367    return VA_STATUS_SUCCESS;
368 }
369