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