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