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
32 #include "state_tracker/drm_driver.h"
33
34 #include "util/u_memory.h"
35 #include "util/u_handle_table.h"
36 #include "util/u_rect.h"
37 #include "util/u_sampler.h"
38 #include "util/u_surface.h"
39
40 #include "vl/vl_compositor.h"
41 #include "vl/vl_video_buffer.h"
42 #include "vl/vl_winsys.h"
43
44 #include "va_private.h"
45
46 #include <va/va_drmcommon.h>
47 #include <drm-uapi/drm_fourcc.h>
48
49 static const enum pipe_format vpp_surface_formats[] = {
50 PIPE_FORMAT_B8G8R8A8_UNORM, PIPE_FORMAT_R8G8B8A8_UNORM,
51 PIPE_FORMAT_B8G8R8X8_UNORM, PIPE_FORMAT_R8G8B8X8_UNORM
52 };
53
54 VAStatus
vlVaCreateSurfaces(VADriverContextP ctx,int width,int height,int format,int num_surfaces,VASurfaceID * surfaces)55 vlVaCreateSurfaces(VADriverContextP ctx, int width, int height, int format,
56 int num_surfaces, VASurfaceID *surfaces)
57 {
58 return vlVaCreateSurfaces2(ctx, format, width, height, surfaces, num_surfaces,
59 NULL, 0);
60 }
61
62 VAStatus
vlVaDestroySurfaces(VADriverContextP ctx,VASurfaceID * surface_list,int num_surfaces)63 vlVaDestroySurfaces(VADriverContextP ctx, VASurfaceID *surface_list, int num_surfaces)
64 {
65 vlVaDriver *drv;
66 int i;
67
68 if (!ctx)
69 return VA_STATUS_ERROR_INVALID_CONTEXT;
70
71 drv = VL_VA_DRIVER(ctx);
72 mtx_lock(&drv->mutex);
73 for (i = 0; i < num_surfaces; ++i) {
74 vlVaSurface *surf = handle_table_get(drv->htab, surface_list[i]);
75 if (!surf) {
76 mtx_unlock(&drv->mutex);
77 return VA_STATUS_ERROR_INVALID_SURFACE;
78 }
79 if (surf->buffer)
80 surf->buffer->destroy(surf->buffer);
81 util_dynarray_fini(&surf->subpics);
82 FREE(surf);
83 handle_table_remove(drv->htab, surface_list[i]);
84 }
85 mtx_unlock(&drv->mutex);
86
87 return VA_STATUS_SUCCESS;
88 }
89
90 VAStatus
vlVaSyncSurface(VADriverContextP ctx,VASurfaceID render_target)91 vlVaSyncSurface(VADriverContextP ctx, VASurfaceID render_target)
92 {
93 vlVaDriver *drv;
94 vlVaContext *context;
95 vlVaSurface *surf;
96
97 if (!ctx)
98 return VA_STATUS_ERROR_INVALID_CONTEXT;
99
100 drv = VL_VA_DRIVER(ctx);
101 if (!drv)
102 return VA_STATUS_ERROR_INVALID_CONTEXT;
103
104 mtx_lock(&drv->mutex);
105 surf = handle_table_get(drv->htab, render_target);
106
107 if (!surf || !surf->buffer) {
108 mtx_unlock(&drv->mutex);
109 return VA_STATUS_ERROR_INVALID_SURFACE;
110 }
111
112 if (!surf->feedback) {
113 // No outstanding operation: nothing to do.
114 mtx_unlock(&drv->mutex);
115 return VA_STATUS_SUCCESS;
116 }
117
118 context = handle_table_get(drv->htab, surf->ctx);
119 if (!context) {
120 mtx_unlock(&drv->mutex);
121 return VA_STATUS_ERROR_INVALID_CONTEXT;
122 }
123
124 if (context->decoder->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE) {
125 int frame_diff;
126 if (context->desc.h264enc.frame_num_cnt >= surf->frame_num_cnt)
127 frame_diff = context->desc.h264enc.frame_num_cnt - surf->frame_num_cnt;
128 else
129 frame_diff = 0xFFFFFFFF - surf->frame_num_cnt + 1 + context->desc.h264enc.frame_num_cnt;
130 if ((frame_diff == 0) &&
131 (surf->force_flushed == false) &&
132 (context->desc.h264enc.frame_num_cnt % 2 != 0)) {
133 context->decoder->flush(context->decoder);
134 context->first_single_submitted = true;
135 }
136 context->decoder->get_feedback(context->decoder, surf->feedback, &(surf->coded_buf->coded_size));
137 surf->feedback = NULL;
138 }
139 mtx_unlock(&drv->mutex);
140 return VA_STATUS_SUCCESS;
141 }
142
143 VAStatus
vlVaQuerySurfaceStatus(VADriverContextP ctx,VASurfaceID render_target,VASurfaceStatus * status)144 vlVaQuerySurfaceStatus(VADriverContextP ctx, VASurfaceID render_target, VASurfaceStatus *status)
145 {
146 if (!ctx)
147 return VA_STATUS_ERROR_INVALID_CONTEXT;
148
149 return VA_STATUS_SUCCESS;
150 }
151
152 VAStatus
vlVaQuerySurfaceError(VADriverContextP ctx,VASurfaceID render_target,VAStatus error_status,void ** error_info)153 vlVaQuerySurfaceError(VADriverContextP ctx, VASurfaceID render_target, VAStatus error_status, void **error_info)
154 {
155 if (!ctx)
156 return VA_STATUS_ERROR_INVALID_CONTEXT;
157
158 return VA_STATUS_ERROR_UNIMPLEMENTED;
159 }
160
161 static void
upload_sampler(struct pipe_context * pipe,struct pipe_sampler_view * dst,const struct pipe_box * dst_box,const void * src,unsigned src_stride,unsigned src_x,unsigned src_y)162 upload_sampler(struct pipe_context *pipe, struct pipe_sampler_view *dst,
163 const struct pipe_box *dst_box, const void *src, unsigned src_stride,
164 unsigned src_x, unsigned src_y)
165 {
166 struct pipe_transfer *transfer;
167 void *map;
168
169 map = pipe->transfer_map(pipe, dst->texture, 0, PIPE_TRANSFER_WRITE,
170 dst_box, &transfer);
171 if (!map)
172 return;
173
174 util_copy_rect(map, dst->texture->format, transfer->stride, 0, 0,
175 dst_box->width, dst_box->height,
176 src, src_stride, src_x, src_y);
177
178 pipe->transfer_unmap(pipe, transfer);
179 }
180
181 static VAStatus
vlVaPutSubpictures(vlVaSurface * surf,vlVaDriver * drv,struct pipe_surface * surf_draw,struct u_rect * dirty_area,struct u_rect * src_rect,struct u_rect * dst_rect)182 vlVaPutSubpictures(vlVaSurface *surf, vlVaDriver *drv,
183 struct pipe_surface *surf_draw, struct u_rect *dirty_area,
184 struct u_rect *src_rect, struct u_rect *dst_rect)
185 {
186 vlVaSubpicture *sub;
187 int i;
188
189 if (!(surf->subpics.data || surf->subpics.size))
190 return VA_STATUS_SUCCESS;
191
192 for (i = 0; i < surf->subpics.size/sizeof(vlVaSubpicture *); i++) {
193 struct pipe_blend_state blend;
194 void *blend_state;
195 vlVaBuffer *buf;
196 struct pipe_box box;
197 struct u_rect *s, *d, sr, dr, c;
198 int sw, sh, dw, dh;
199
200 sub = ((vlVaSubpicture **)surf->subpics.data)[i];
201 if (!sub)
202 continue;
203
204 buf = handle_table_get(drv->htab, sub->image->buf);
205 if (!buf)
206 return VA_STATUS_ERROR_INVALID_IMAGE;
207
208 box.x = 0;
209 box.y = 0;
210 box.z = 0;
211 box.width = sub->dst_rect.x1 - sub->dst_rect.x0;
212 box.height = sub->dst_rect.y1 - sub->dst_rect.y0;
213 box.depth = 1;
214
215 s = &sub->src_rect;
216 d = &sub->dst_rect;
217 sw = s->x1 - s->x0;
218 sh = s->y1 - s->y0;
219 dw = d->x1 - d->x0;
220 dh = d->y1 - d->y0;
221 c.x0 = MAX2(d->x0, s->x0);
222 c.y0 = MAX2(d->y0, s->y0);
223 c.x1 = MIN2(d->x0 + dw, src_rect->x1);
224 c.y1 = MIN2(d->y0 + dh, src_rect->y1);
225 sr.x0 = s->x0 + (c.x0 - d->x0)*(sw/(float)dw);
226 sr.y0 = s->y0 + (c.y0 - d->y0)*(sh/(float)dh);
227 sr.x1 = s->x0 + (c.x1 - d->x0)*(sw/(float)dw);
228 sr.y1 = s->y0 + (c.y1 - d->y0)*(sh/(float)dh);
229
230 s = src_rect;
231 d = dst_rect;
232 sw = s->x1 - s->x0;
233 sh = s->y1 - s->y0;
234 dw = d->x1 - d->x0;
235 dh = d->y1 - d->y0;
236 dr.x0 = d->x0 + c.x0*(dw/(float)sw);
237 dr.y0 = d->y0 + c.y0*(dh/(float)sh);
238 dr.x1 = d->x0 + c.x1*(dw/(float)sw);
239 dr.y1 = d->y0 + c.y1*(dh/(float)sh);
240
241 memset(&blend, 0, sizeof(blend));
242 blend.independent_blend_enable = 0;
243 blend.rt[0].blend_enable = 1;
244 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
245 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
246 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ZERO;
247 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
248 blend.rt[0].rgb_func = PIPE_BLEND_ADD;
249 blend.rt[0].alpha_func = PIPE_BLEND_ADD;
250 blend.rt[0].colormask = PIPE_MASK_RGBA;
251 blend.logicop_enable = 0;
252 blend.logicop_func = PIPE_LOGICOP_CLEAR;
253 blend.dither = 0;
254 blend_state = drv->pipe->create_blend_state(drv->pipe, &blend);
255
256 vl_compositor_clear_layers(&drv->cstate);
257 vl_compositor_set_layer_blend(&drv->cstate, 0, blend_state, false);
258 upload_sampler(drv->pipe, sub->sampler, &box, buf->data,
259 sub->image->pitches[0], 0, 0);
260 vl_compositor_set_rgba_layer(&drv->cstate, &drv->compositor, 0, sub->sampler,
261 &sr, NULL, NULL);
262 vl_compositor_set_layer_dst_area(&drv->cstate, 0, &dr);
263 vl_compositor_render(&drv->cstate, &drv->compositor, surf_draw, dirty_area, false);
264 drv->pipe->delete_blend_state(drv->pipe, blend_state);
265 }
266
267 return VA_STATUS_SUCCESS;
268 }
269
270 VAStatus
vlVaPutSurface(VADriverContextP ctx,VASurfaceID surface_id,void * draw,short srcx,short srcy,unsigned short srcw,unsigned short srch,short destx,short desty,unsigned short destw,unsigned short desth,VARectangle * cliprects,unsigned int number_cliprects,unsigned int flags)271 vlVaPutSurface(VADriverContextP ctx, VASurfaceID surface_id, void* draw, short srcx, short srcy,
272 unsigned short srcw, unsigned short srch, short destx, short desty,
273 unsigned short destw, unsigned short desth, VARectangle *cliprects,
274 unsigned int number_cliprects, unsigned int flags)
275 {
276 vlVaDriver *drv;
277 vlVaSurface *surf;
278 struct pipe_screen *screen;
279 struct pipe_resource *tex;
280 struct pipe_surface surf_templ, *surf_draw;
281 struct vl_screen *vscreen;
282 struct u_rect src_rect, *dirty_area;
283 struct u_rect dst_rect = {destx, destx + destw, desty, desty + desth};
284 enum pipe_format format;
285 VAStatus status;
286
287 if (!ctx)
288 return VA_STATUS_ERROR_INVALID_CONTEXT;
289
290 drv = VL_VA_DRIVER(ctx);
291 mtx_lock(&drv->mutex);
292 surf = handle_table_get(drv->htab, surface_id);
293 if (!surf) {
294 mtx_unlock(&drv->mutex);
295 return VA_STATUS_ERROR_INVALID_SURFACE;
296 }
297
298 screen = drv->pipe->screen;
299 vscreen = drv->vscreen;
300
301 tex = vscreen->texture_from_drawable(vscreen, draw);
302 if (!tex) {
303 mtx_unlock(&drv->mutex);
304 return VA_STATUS_ERROR_INVALID_DISPLAY;
305 }
306
307 dirty_area = vscreen->get_dirty_area(vscreen);
308
309 memset(&surf_templ, 0, sizeof(surf_templ));
310 surf_templ.format = tex->format;
311 surf_draw = drv->pipe->create_surface(drv->pipe, tex, &surf_templ);
312 if (!surf_draw) {
313 pipe_resource_reference(&tex, NULL);
314 mtx_unlock(&drv->mutex);
315 return VA_STATUS_ERROR_INVALID_DISPLAY;
316 }
317
318 src_rect.x0 = srcx;
319 src_rect.y0 = srcy;
320 src_rect.x1 = srcw + srcx;
321 src_rect.y1 = srch + srcy;
322
323 format = surf->buffer->buffer_format;
324
325 vl_compositor_clear_layers(&drv->cstate);
326
327 if (format == PIPE_FORMAT_B8G8R8A8_UNORM || format == PIPE_FORMAT_B8G8R8X8_UNORM ||
328 format == PIPE_FORMAT_R8G8B8A8_UNORM || format == PIPE_FORMAT_R8G8B8X8_UNORM) {
329 struct pipe_sampler_view **views;
330
331 views = surf->buffer->get_sampler_view_planes(surf->buffer);
332 vl_compositor_set_rgba_layer(&drv->cstate, &drv->compositor, 0, views[0], &src_rect, NULL, NULL);
333 } else
334 vl_compositor_set_buffer_layer(&drv->cstate, &drv->compositor, 0, surf->buffer, &src_rect, NULL, VL_COMPOSITOR_WEAVE);
335
336 vl_compositor_set_layer_dst_area(&drv->cstate, 0, &dst_rect);
337 vl_compositor_render(&drv->cstate, &drv->compositor, surf_draw, dirty_area, true);
338
339 status = vlVaPutSubpictures(surf, drv, surf_draw, dirty_area, &src_rect, &dst_rect);
340 if (status) {
341 mtx_unlock(&drv->mutex);
342 return status;
343 }
344
345 /* flush before calling flush_frontbuffer so that rendering is flushed
346 * to back buffer so the texture can be copied in flush_frontbuffer
347 */
348 drv->pipe->flush(drv->pipe, NULL, 0);
349
350 screen->flush_frontbuffer(screen, tex, 0, 0,
351 vscreen->get_private(vscreen), NULL);
352
353
354 pipe_resource_reference(&tex, NULL);
355 pipe_surface_reference(&surf_draw, NULL);
356 mtx_unlock(&drv->mutex);
357
358 return VA_STATUS_SUCCESS;
359 }
360
361 VAStatus
vlVaLockSurface(VADriverContextP ctx,VASurfaceID surface,unsigned int * fourcc,unsigned int * luma_stride,unsigned int * chroma_u_stride,unsigned int * chroma_v_stride,unsigned int * luma_offset,unsigned int * chroma_u_offset,unsigned int * chroma_v_offset,unsigned int * buffer_name,void ** buffer)362 vlVaLockSurface(VADriverContextP ctx, VASurfaceID surface, unsigned int *fourcc,
363 unsigned int *luma_stride, unsigned int *chroma_u_stride, unsigned int *chroma_v_stride,
364 unsigned int *luma_offset, unsigned int *chroma_u_offset, unsigned int *chroma_v_offset,
365 unsigned int *buffer_name, void **buffer)
366 {
367 if (!ctx)
368 return VA_STATUS_ERROR_INVALID_CONTEXT;
369
370 return VA_STATUS_ERROR_UNIMPLEMENTED;
371 }
372
373 VAStatus
vlVaUnlockSurface(VADriverContextP ctx,VASurfaceID surface)374 vlVaUnlockSurface(VADriverContextP ctx, VASurfaceID surface)
375 {
376 if (!ctx)
377 return VA_STATUS_ERROR_INVALID_CONTEXT;
378
379 return VA_STATUS_ERROR_UNIMPLEMENTED;
380 }
381
382 VAStatus
vlVaQuerySurfaceAttributes(VADriverContextP ctx,VAConfigID config_id,VASurfaceAttrib * attrib_list,unsigned int * num_attribs)383 vlVaQuerySurfaceAttributes(VADriverContextP ctx, VAConfigID config_id,
384 VASurfaceAttrib *attrib_list, unsigned int *num_attribs)
385 {
386 vlVaDriver *drv;
387 vlVaConfig *config;
388 VASurfaceAttrib *attribs;
389 struct pipe_screen *pscreen;
390 int i, j;
391
392 STATIC_ASSERT(ARRAY_SIZE(vpp_surface_formats) <= VL_VA_MAX_IMAGE_FORMATS);
393
394 if (config_id == VA_INVALID_ID)
395 return VA_STATUS_ERROR_INVALID_CONFIG;
396
397 if (!attrib_list && !num_attribs)
398 return VA_STATUS_ERROR_INVALID_PARAMETER;
399
400 if (!attrib_list) {
401 *num_attribs = VL_VA_MAX_IMAGE_FORMATS + VASurfaceAttribCount;
402 return VA_STATUS_SUCCESS;
403 }
404
405 if (!ctx)
406 return VA_STATUS_ERROR_INVALID_CONTEXT;
407
408 drv = VL_VA_DRIVER(ctx);
409
410 if (!drv)
411 return VA_STATUS_ERROR_INVALID_CONTEXT;
412
413 mtx_lock(&drv->mutex);
414 config = handle_table_get(drv->htab, config_id);
415 mtx_unlock(&drv->mutex);
416
417 if (!config)
418 return VA_STATUS_ERROR_INVALID_CONFIG;
419
420 pscreen = VL_VA_PSCREEN(ctx);
421
422 if (!pscreen)
423 return VA_STATUS_ERROR_INVALID_CONTEXT;
424
425 attribs = CALLOC(VL_VA_MAX_IMAGE_FORMATS + VASurfaceAttribCount,
426 sizeof(VASurfaceAttrib));
427
428 if (!attribs)
429 return VA_STATUS_ERROR_ALLOCATION_FAILED;
430
431 i = 0;
432
433 /* vlVaCreateConfig returns PIPE_VIDEO_PROFILE_UNKNOWN
434 * only for VAEntrypointVideoProc. */
435 if (config->profile == PIPE_VIDEO_PROFILE_UNKNOWN) {
436 if (config->rt_format & VA_RT_FORMAT_RGB32) {
437 for (j = 0; j < ARRAY_SIZE(vpp_surface_formats); ++j) {
438 attribs[i].type = VASurfaceAttribPixelFormat;
439 attribs[i].value.type = VAGenericValueTypeInteger;
440 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
441 attribs[i].value.value.i = PipeFormatToVaFourcc(vpp_surface_formats[j]);
442 i++;
443 }
444 }
445 }
446 if (config->rt_format & VA_RT_FORMAT_YUV420) {
447 attribs[i].type = VASurfaceAttribPixelFormat;
448 attribs[i].value.type = VAGenericValueTypeInteger;
449 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
450 attribs[i].value.value.i = VA_FOURCC_NV12;
451 i++;
452 }
453 if (config->rt_format & VA_RT_FORMAT_YUV420_10BPP) {
454 attribs[i].type = VASurfaceAttribPixelFormat;
455 attribs[i].value.type = VAGenericValueTypeInteger;
456 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
457 attribs[i].value.value.i = VA_FOURCC_P010;
458 i++;
459 attribs[i].type = VASurfaceAttribPixelFormat;
460 attribs[i].value.type = VAGenericValueTypeInteger;
461 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
462 attribs[i].value.value.i = VA_FOURCC_P016;
463 i++;
464 }
465
466 attribs[i].type = VASurfaceAttribMemoryType;
467 attribs[i].value.type = VAGenericValueTypeInteger;
468 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
469 attribs[i].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA |
470 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
471 i++;
472
473 attribs[i].type = VASurfaceAttribExternalBufferDescriptor;
474 attribs[i].value.type = VAGenericValueTypePointer;
475 attribs[i].flags = VA_SURFACE_ATTRIB_SETTABLE;
476 attribs[i].value.value.p = NULL; /* ignore */
477 i++;
478
479 if (config->entrypoint != PIPE_VIDEO_ENTRYPOINT_UNKNOWN) {
480 attribs[i].type = VASurfaceAttribMaxWidth;
481 attribs[i].value.type = VAGenericValueTypeInteger;
482 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
483 attribs[i].value.value.i =
484 pscreen->get_video_param(pscreen,
485 config->profile, config->entrypoint,
486 PIPE_VIDEO_CAP_MAX_WIDTH);
487 i++;
488
489 attribs[i].type = VASurfaceAttribMaxHeight;
490 attribs[i].value.type = VAGenericValueTypeInteger;
491 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
492 attribs[i].value.value.i =
493 pscreen->get_video_param(pscreen,
494 config->profile, config->entrypoint,
495 PIPE_VIDEO_CAP_MAX_HEIGHT);
496 i++;
497 } else {
498 attribs[i].type = VASurfaceAttribMaxWidth;
499 attribs[i].value.type = VAGenericValueTypeInteger;
500 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
501 attribs[i].value.value.i = vl_video_buffer_max_size(pscreen);
502 i++;
503
504 attribs[i].type = VASurfaceAttribMaxHeight;
505 attribs[i].value.type = VAGenericValueTypeInteger;
506 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
507 attribs[i].value.value.i = vl_video_buffer_max_size(pscreen);
508 i++;
509 }
510
511 if (i > *num_attribs) {
512 *num_attribs = i;
513 FREE(attribs);
514 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
515 }
516
517 *num_attribs = i;
518 memcpy(attrib_list, attribs, i * sizeof(VASurfaceAttrib));
519 FREE(attribs);
520
521 return VA_STATUS_SUCCESS;
522 }
523
524 static VAStatus
suface_from_external_memory(VADriverContextP ctx,vlVaSurface * surface,VASurfaceAttribExternalBuffers * memory_attibute,unsigned index,struct pipe_video_buffer * templat)525 suface_from_external_memory(VADriverContextP ctx, vlVaSurface *surface,
526 VASurfaceAttribExternalBuffers *memory_attibute,
527 unsigned index, struct pipe_video_buffer *templat)
528 {
529 vlVaDriver *drv;
530 struct pipe_screen *pscreen;
531 struct pipe_resource *resource;
532 struct pipe_resource res_templ;
533 struct winsys_handle whandle;
534 struct pipe_resource *resources[VL_NUM_COMPONENTS];
535
536 pscreen = VL_VA_PSCREEN(ctx);
537 drv = VL_VA_DRIVER(ctx);
538
539 if (!memory_attibute || !memory_attibute->buffers ||
540 index > memory_attibute->num_buffers)
541 return VA_STATUS_ERROR_INVALID_PARAMETER;
542
543 if (surface->templat.width != memory_attibute->width ||
544 surface->templat.height != memory_attibute->height ||
545 memory_attibute->num_planes < 1)
546 return VA_STATUS_ERROR_INVALID_PARAMETER;
547
548 switch (memory_attibute->pixel_format) {
549 case VA_FOURCC_RGBA:
550 case VA_FOURCC_RGBX:
551 case VA_FOURCC_BGRA:
552 case VA_FOURCC_BGRX:
553 if (memory_attibute->num_planes != 1)
554 return VA_STATUS_ERROR_INVALID_PARAMETER;
555 break;
556 default:
557 return VA_STATUS_ERROR_INVALID_PARAMETER;
558 }
559
560 memset(&res_templ, 0, sizeof(res_templ));
561 res_templ.target = PIPE_TEXTURE_2D;
562 res_templ.last_level = 0;
563 res_templ.depth0 = 1;
564 res_templ.array_size = 1;
565 res_templ.width0 = memory_attibute->width;
566 res_templ.height0 = memory_attibute->height;
567 res_templ.format = surface->templat.buffer_format;
568 res_templ.bind = PIPE_BIND_SAMPLER_VIEW;
569 res_templ.usage = PIPE_USAGE_DEFAULT;
570
571 memset(&whandle, 0, sizeof(struct winsys_handle));
572 whandle.type = DRM_API_HANDLE_TYPE_FD;
573 whandle.handle = memory_attibute->buffers[index];
574 whandle.stride = memory_attibute->pitches[index];
575
576 resource = pscreen->resource_from_handle(pscreen, &res_templ, &whandle,
577 PIPE_HANDLE_USAGE_READ_WRITE);
578
579 if (!resource)
580 return VA_STATUS_ERROR_ALLOCATION_FAILED;
581
582 memset(resources, 0, sizeof resources);
583 resources[0] = resource;
584
585 surface->buffer = vl_video_buffer_create_ex2(drv->pipe, templat, resources);
586 if (!surface->buffer)
587 return VA_STATUS_ERROR_ALLOCATION_FAILED;
588
589 return VA_STATUS_SUCCESS;
590 }
591
592 VAStatus
vlVaHandleSurfaceAllocate(vlVaDriver * drv,vlVaSurface * surface,struct pipe_video_buffer * templat)593 vlVaHandleSurfaceAllocate(vlVaDriver *drv, vlVaSurface *surface,
594 struct pipe_video_buffer *templat)
595 {
596 struct pipe_surface **surfaces;
597 unsigned i;
598
599 surface->buffer = drv->pipe->create_video_buffer(drv->pipe, templat);
600 if (!surface->buffer)
601 return VA_STATUS_ERROR_ALLOCATION_FAILED;
602
603 surfaces = surface->buffer->get_surfaces(surface->buffer);
604 for (i = 0; i < VL_MAX_SURFACES; ++i) {
605 union pipe_color_union c = {};
606
607 if (!surfaces[i])
608 continue;
609
610 if (i > !!surface->buffer->interlaced)
611 c.f[0] = c.f[1] = c.f[2] = c.f[3] = 0.5f;
612
613 drv->pipe->clear_render_target(drv->pipe, surfaces[i], &c, 0, 0,
614 surfaces[i]->width, surfaces[i]->height,
615 false);
616 }
617 drv->pipe->flush(drv->pipe, NULL, 0);
618
619 return VA_STATUS_SUCCESS;
620 }
621
622 VAStatus
vlVaCreateSurfaces2(VADriverContextP ctx,unsigned int format,unsigned int width,unsigned int height,VASurfaceID * surfaces,unsigned int num_surfaces,VASurfaceAttrib * attrib_list,unsigned int num_attribs)623 vlVaCreateSurfaces2(VADriverContextP ctx, unsigned int format,
624 unsigned int width, unsigned int height,
625 VASurfaceID *surfaces, unsigned int num_surfaces,
626 VASurfaceAttrib *attrib_list, unsigned int num_attribs)
627 {
628 vlVaDriver *drv;
629 VASurfaceAttribExternalBuffers *memory_attibute;
630 struct pipe_video_buffer templat;
631 struct pipe_screen *pscreen;
632 int i;
633 int memory_type;
634 int expected_fourcc;
635 VAStatus vaStatus;
636 vlVaSurface *surf;
637
638 if (!ctx)
639 return VA_STATUS_ERROR_INVALID_CONTEXT;
640
641 if (!(width && height))
642 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
643
644 drv = VL_VA_DRIVER(ctx);
645
646 if (!drv)
647 return VA_STATUS_ERROR_INVALID_CONTEXT;
648
649 pscreen = VL_VA_PSCREEN(ctx);
650
651 if (!pscreen)
652 return VA_STATUS_ERROR_INVALID_CONTEXT;
653
654 /* Default. */
655 memory_attibute = NULL;
656 memory_type = VA_SURFACE_ATTRIB_MEM_TYPE_VA;
657 expected_fourcc = 0;
658
659 for (i = 0; i < num_attribs && attrib_list; i++) {
660 if ((attrib_list[i].type == VASurfaceAttribPixelFormat) &&
661 (attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE)) {
662 if (attrib_list[i].value.type != VAGenericValueTypeInteger)
663 return VA_STATUS_ERROR_INVALID_PARAMETER;
664 expected_fourcc = attrib_list[i].value.value.i;
665 }
666
667 if ((attrib_list[i].type == VASurfaceAttribMemoryType) &&
668 (attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE)) {
669
670 if (attrib_list[i].value.type != VAGenericValueTypeInteger)
671 return VA_STATUS_ERROR_INVALID_PARAMETER;
672
673 switch (attrib_list[i].value.value.i) {
674 case VA_SURFACE_ATTRIB_MEM_TYPE_VA:
675 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME:
676 memory_type = attrib_list[i].value.value.i;
677 break;
678 default:
679 return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
680 }
681 }
682
683 if ((attrib_list[i].type == VASurfaceAttribExternalBufferDescriptor) &&
684 (attrib_list[i].flags == VA_SURFACE_ATTRIB_SETTABLE)) {
685 if (attrib_list[i].value.type != VAGenericValueTypePointer)
686 return VA_STATUS_ERROR_INVALID_PARAMETER;
687 memory_attibute = (VASurfaceAttribExternalBuffers *)attrib_list[i].value.value.p;
688 }
689 }
690
691 if (VA_RT_FORMAT_YUV420 != format &&
692 VA_RT_FORMAT_YUV422 != format &&
693 VA_RT_FORMAT_YUV444 != format &&
694 VA_RT_FORMAT_YUV420_10BPP != format &&
695 VA_RT_FORMAT_RGB32 != format) {
696 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
697 }
698
699 switch (memory_type) {
700 case VA_SURFACE_ATTRIB_MEM_TYPE_VA:
701 break;
702 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME:
703 if (!memory_attibute)
704 return VA_STATUS_ERROR_INVALID_PARAMETER;
705
706 expected_fourcc = memory_attibute->pixel_format;
707 break;
708 default:
709 assert(0);
710 }
711
712 memset(&templat, 0, sizeof(templat));
713
714 templat.buffer_format = pscreen->get_video_param(
715 pscreen,
716 PIPE_VIDEO_PROFILE_UNKNOWN,
717 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
718 PIPE_VIDEO_CAP_PREFERED_FORMAT
719 );
720 templat.interlaced = pscreen->get_video_param(
721 pscreen,
722 PIPE_VIDEO_PROFILE_UNKNOWN,
723 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
724 PIPE_VIDEO_CAP_PREFERS_INTERLACED
725 );
726
727 if (expected_fourcc) {
728 enum pipe_format expected_format = VaFourccToPipeFormat(expected_fourcc);
729
730 if (expected_format != templat.buffer_format || memory_attibute)
731 templat.interlaced = 0;
732
733 templat.buffer_format = expected_format;
734 }
735
736 templat.chroma_format = ChromaToPipe(format);
737
738 templat.width = width;
739 templat.height = height;
740
741 memset(surfaces, VA_INVALID_ID, num_surfaces * sizeof(VASurfaceID));
742
743 mtx_lock(&drv->mutex);
744 for (i = 0; i < num_surfaces; i++) {
745 surf = CALLOC(1, sizeof(vlVaSurface));
746 if (!surf) {
747 vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
748 goto no_res;
749 }
750
751 surf->templat = templat;
752
753 switch (memory_type) {
754 case VA_SURFACE_ATTRIB_MEM_TYPE_VA:
755 /* The application will clear the TILING flag when the surface is
756 * intended to be exported as dmabuf. Adding shared flag because not
757 * null memory_attibute means VASurfaceAttribExternalBuffers is used.
758 */
759 if (memory_attibute &&
760 !(memory_attibute->flags & VA_SURFACE_EXTBUF_DESC_ENABLE_TILING))
761 templat.bind = PIPE_BIND_LINEAR | PIPE_BIND_SHARED;
762
763 vaStatus = vlVaHandleSurfaceAllocate(drv, surf, &templat);
764 if (vaStatus != VA_STATUS_SUCCESS)
765 goto free_surf;
766 break;
767
768 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME:
769 vaStatus = suface_from_external_memory(ctx, surf, memory_attibute, i, &templat);
770 if (vaStatus != VA_STATUS_SUCCESS)
771 goto free_surf;
772 break;
773
774 default:
775 assert(0);
776 }
777
778 util_dynarray_init(&surf->subpics, NULL);
779 surfaces[i] = handle_table_add(drv->htab, surf);
780 if (!surfaces[i]) {
781 vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
782 goto destroy_surf;
783 }
784 }
785 mtx_unlock(&drv->mutex);
786
787 return VA_STATUS_SUCCESS;
788
789 destroy_surf:
790 surf->buffer->destroy(surf->buffer);
791
792 free_surf:
793 FREE(surf);
794
795 no_res:
796 mtx_unlock(&drv->mutex);
797 if (i)
798 vlVaDestroySurfaces(ctx, surfaces, i);
799
800 return vaStatus;
801 }
802
803 VAStatus
vlVaQueryVideoProcFilters(VADriverContextP ctx,VAContextID context,VAProcFilterType * filters,unsigned int * num_filters)804 vlVaQueryVideoProcFilters(VADriverContextP ctx, VAContextID context,
805 VAProcFilterType *filters, unsigned int *num_filters)
806 {
807 unsigned int num = 0;
808
809 if (!ctx)
810 return VA_STATUS_ERROR_INVALID_CONTEXT;
811
812 if (!num_filters || !filters)
813 return VA_STATUS_ERROR_INVALID_PARAMETER;
814
815 filters[num++] = VAProcFilterDeinterlacing;
816
817 *num_filters = num;
818
819 return VA_STATUS_SUCCESS;
820 }
821
822 VAStatus
vlVaQueryVideoProcFilterCaps(VADriverContextP ctx,VAContextID context,VAProcFilterType type,void * filter_caps,unsigned int * num_filter_caps)823 vlVaQueryVideoProcFilterCaps(VADriverContextP ctx, VAContextID context,
824 VAProcFilterType type, void *filter_caps,
825 unsigned int *num_filter_caps)
826 {
827 unsigned int i;
828
829 if (!ctx)
830 return VA_STATUS_ERROR_INVALID_CONTEXT;
831
832 if (!filter_caps || !num_filter_caps)
833 return VA_STATUS_ERROR_INVALID_PARAMETER;
834
835 i = 0;
836
837 switch (type) {
838 case VAProcFilterNone:
839 break;
840 case VAProcFilterDeinterlacing: {
841 VAProcFilterCapDeinterlacing *deint = filter_caps;
842
843 if (*num_filter_caps < 3) {
844 *num_filter_caps = 3;
845 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
846 }
847
848 deint[i++].type = VAProcDeinterlacingBob;
849 deint[i++].type = VAProcDeinterlacingWeave;
850 deint[i++].type = VAProcDeinterlacingMotionAdaptive;
851 break;
852 }
853
854 case VAProcFilterNoiseReduction:
855 case VAProcFilterSharpening:
856 case VAProcFilterColorBalance:
857 case VAProcFilterSkinToneEnhancement:
858 return VA_STATUS_ERROR_UNIMPLEMENTED;
859 default:
860 assert(0);
861 }
862
863 *num_filter_caps = i;
864
865 return VA_STATUS_SUCCESS;
866 }
867
868 static VAProcColorStandardType vpp_input_color_standards[] = {
869 VAProcColorStandardBT601
870 };
871
872 static VAProcColorStandardType vpp_output_color_standards[] = {
873 VAProcColorStandardBT601
874 };
875
876 VAStatus
vlVaQueryVideoProcPipelineCaps(VADriverContextP ctx,VAContextID context,VABufferID * filters,unsigned int num_filters,VAProcPipelineCaps * pipeline_cap)877 vlVaQueryVideoProcPipelineCaps(VADriverContextP ctx, VAContextID context,
878 VABufferID *filters, unsigned int num_filters,
879 VAProcPipelineCaps *pipeline_cap)
880 {
881 unsigned int i = 0;
882
883 if (!ctx)
884 return VA_STATUS_ERROR_INVALID_CONTEXT;
885
886 if (!pipeline_cap)
887 return VA_STATUS_ERROR_INVALID_PARAMETER;
888
889 if (num_filters && !filters)
890 return VA_STATUS_ERROR_INVALID_PARAMETER;
891
892 pipeline_cap->pipeline_flags = 0;
893 pipeline_cap->filter_flags = 0;
894 pipeline_cap->num_forward_references = 0;
895 pipeline_cap->num_backward_references = 0;
896 pipeline_cap->num_input_color_standards = ARRAY_SIZE(vpp_input_color_standards);
897 pipeline_cap->input_color_standards = vpp_input_color_standards;
898 pipeline_cap->num_output_color_standards = ARRAY_SIZE(vpp_output_color_standards);
899 pipeline_cap->output_color_standards = vpp_output_color_standards;
900
901 for (i = 0; i < num_filters; i++) {
902 vlVaBuffer *buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, filters[i]);
903 VAProcFilterParameterBufferBase *filter;
904
905 if (!buf || buf->type != VAProcFilterParameterBufferType)
906 return VA_STATUS_ERROR_INVALID_BUFFER;
907
908 filter = buf->data;
909 switch (filter->type) {
910 case VAProcFilterDeinterlacing: {
911 VAProcFilterParameterBufferDeinterlacing *deint = buf->data;
912 if (deint->algorithm == VAProcDeinterlacingMotionAdaptive) {
913 pipeline_cap->num_forward_references = 2;
914 pipeline_cap->num_backward_references = 1;
915 }
916 break;
917 }
918 default:
919 return VA_STATUS_ERROR_UNIMPLEMENTED;
920 }
921 }
922
923 return VA_STATUS_SUCCESS;
924 }
925
926 #if VA_CHECK_VERSION(1, 1, 0)
927 VAStatus
vlVaExportSurfaceHandle(VADriverContextP ctx,VASurfaceID surface_id,uint32_t mem_type,uint32_t flags,void * descriptor)928 vlVaExportSurfaceHandle(VADriverContextP ctx,
929 VASurfaceID surface_id,
930 uint32_t mem_type,
931 uint32_t flags,
932 void *descriptor)
933 {
934 vlVaDriver *drv;
935 vlVaSurface *surf;
936 struct pipe_surface **surfaces;
937 struct pipe_screen *screen;
938 VAStatus ret;
939 unsigned int usage;
940 int i, p;
941
942 VADRMPRIMESurfaceDescriptor *desc = descriptor;
943
944 if (mem_type != VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2)
945 return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
946 if (flags & VA_EXPORT_SURFACE_COMPOSED_LAYERS)
947 return VA_STATUS_ERROR_INVALID_SURFACE;
948
949 drv = VL_VA_DRIVER(ctx);
950 screen = VL_VA_PSCREEN(ctx);
951 mtx_lock(&drv->mutex);
952
953 surf = handle_table_get(drv->htab, surface_id);
954 if (!surf || !surf->buffer) {
955 mtx_unlock(&drv->mutex);
956 return VA_STATUS_ERROR_INVALID_SURFACE;
957 }
958
959 if (surf->buffer->interlaced) {
960 struct pipe_video_buffer *interlaced = surf->buffer;
961 struct u_rect src_rect, dst_rect;
962
963 surf->templat.interlaced = false;
964
965 ret = vlVaHandleSurfaceAllocate(drv, surf, &surf->templat);
966 if (ret != VA_STATUS_SUCCESS) {
967 mtx_unlock(&drv->mutex);
968 return VA_STATUS_ERROR_ALLOCATION_FAILED;
969 }
970
971 src_rect.x0 = dst_rect.x0 = 0;
972 src_rect.y0 = dst_rect.y0 = 0;
973 src_rect.x1 = dst_rect.x1 = surf->templat.width;
974 src_rect.y1 = dst_rect.y1 = surf->templat.height;
975
976 vl_compositor_yuv_deint_full(&drv->cstate, &drv->compositor,
977 interlaced, surf->buffer,
978 &src_rect, &dst_rect,
979 VL_COMPOSITOR_WEAVE);
980
981 interlaced->destroy(interlaced);
982 }
983
984 surfaces = surf->buffer->get_surfaces(surf->buffer);
985
986 usage = 0;
987 if (flags & VA_EXPORT_SURFACE_READ_ONLY)
988 usage |= PIPE_HANDLE_USAGE_READ;
989 if (flags & VA_EXPORT_SURFACE_WRITE_ONLY)
990 usage |= PIPE_HANDLE_USAGE_WRITE;
991
992 desc->fourcc = PipeFormatToVaFourcc(surf->buffer->buffer_format);
993 desc->width = surf->buffer->width;
994 desc->height = surf->buffer->height;
995
996 for (p = 0; p < VL_MAX_SURFACES; p++) {
997 struct winsys_handle whandle;
998 struct pipe_resource *resource;
999 uint32_t drm_format;
1000
1001 if (!surfaces[p])
1002 break;
1003
1004 resource = surfaces[p]->texture;
1005
1006 switch (resource->format) {
1007 case PIPE_FORMAT_R8_UNORM:
1008 drm_format = DRM_FORMAT_R8;
1009 break;
1010 case PIPE_FORMAT_R8G8_UNORM:
1011 drm_format = DRM_FORMAT_GR88;
1012 break;
1013 case PIPE_FORMAT_R16_UNORM:
1014 drm_format = DRM_FORMAT_R16;
1015 break;
1016 case PIPE_FORMAT_R16G16_UNORM:
1017 drm_format = DRM_FORMAT_GR1616;
1018 break;
1019 case PIPE_FORMAT_B8G8R8A8_UNORM:
1020 drm_format = DRM_FORMAT_ARGB8888;
1021 break;
1022 case PIPE_FORMAT_R8G8B8A8_UNORM:
1023 drm_format = DRM_FORMAT_ABGR8888;
1024 break;
1025 case PIPE_FORMAT_B8G8R8X8_UNORM:
1026 drm_format = DRM_FORMAT_XRGB8888;
1027 break;
1028 case PIPE_FORMAT_R8G8B8X8_UNORM:
1029 drm_format = DRM_FORMAT_XBGR8888;
1030 break;
1031 default:
1032 ret = VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
1033 goto fail;
1034 }
1035
1036 memset(&whandle, 0, sizeof(whandle));
1037 whandle.type = DRM_API_HANDLE_TYPE_FD;
1038
1039 if (!screen->resource_get_handle(screen, drv->pipe, resource,
1040 &whandle, usage)) {
1041 ret = VA_STATUS_ERROR_INVALID_SURFACE;
1042 goto fail;
1043 }
1044
1045 desc->objects[p].fd = (int)whandle.handle;
1046 desc->objects[p].size = 0;
1047 desc->objects[p].drm_format_modifier = whandle.modifier;
1048
1049 desc->layers[p].drm_format = drm_format;
1050 desc->layers[p].num_planes = 1;
1051 desc->layers[p].object_index[0] = p;
1052 desc->layers[p].offset[0] = whandle.offset;
1053 desc->layers[p].pitch[0] = whandle.stride;
1054 }
1055
1056 desc->num_objects = p;
1057 desc->num_layers = p;
1058
1059 mtx_unlock(&drv->mutex);
1060
1061 return VA_STATUS_SUCCESS;
1062
1063 fail:
1064 for (i = 0; i < p; i++)
1065 close(desc->objects[i].fd);
1066
1067 mtx_unlock(&drv->mutex);
1068
1069 return ret;
1070 }
1071 #endif
1072