st/va: cleanup error handling in vlVaCreateSurfaces2
[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
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 DEBUG_GET_ONCE_BOOL_OPTION(nointerlace, "VAAPI_DISABLE_INTERLACE", FALSE);
47
48 #include <va/va_drmcommon.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 int frame_diff;
127 if (context->desc.h264enc.frame_num_cnt >= surf->frame_num_cnt)
128 frame_diff = context->desc.h264enc.frame_num_cnt - surf->frame_num_cnt;
129 else
130 frame_diff = 0xFFFFFFFF - surf->frame_num_cnt + 1 + context->desc.h264enc.frame_num_cnt;
131 if ((frame_diff == 0) &&
132 (surf->force_flushed == false) &&
133 (context->desc.h264enc.frame_num_cnt % 2 != 0)) {
134 context->decoder->flush(context->decoder);
135 context->first_single_submitted = true;
136 }
137 context->decoder->get_feedback(context->decoder, surf->feedback, &(surf->coded_buf->coded_size));
138 surf->feedback = NULL;
139 }
140 mtx_unlock(&drv->mutex);
141 return VA_STATUS_SUCCESS;
142 }
143
144 VAStatus
145 vlVaQuerySurfaceStatus(VADriverContextP ctx, VASurfaceID render_target, VASurfaceStatus *status)
146 {
147 if (!ctx)
148 return VA_STATUS_ERROR_INVALID_CONTEXT;
149
150 return VA_STATUS_SUCCESS;
151 }
152
153 VAStatus
154 vlVaQuerySurfaceError(VADriverContextP ctx, VASurfaceID render_target, VAStatus error_status, void **error_info)
155 {
156 if (!ctx)
157 return VA_STATUS_ERROR_INVALID_CONTEXT;
158
159 return VA_STATUS_ERROR_UNIMPLEMENTED;
160 }
161
162 static void
163 upload_sampler(struct pipe_context *pipe, struct pipe_sampler_view *dst,
164 const struct pipe_box *dst_box, const void *src, unsigned src_stride,
165 unsigned src_x, unsigned src_y)
166 {
167 struct pipe_transfer *transfer;
168 void *map;
169
170 map = pipe->transfer_map(pipe, dst->texture, 0, PIPE_TRANSFER_WRITE,
171 dst_box, &transfer);
172 if (!map)
173 return;
174
175 util_copy_rect(map, dst->texture->format, transfer->stride, 0, 0,
176 dst_box->width, dst_box->height,
177 src, src_stride, src_x, src_y);
178
179 pipe->transfer_unmap(pipe, transfer);
180 }
181
182 static VAStatus
183 vlVaPutSubpictures(vlVaSurface *surf, vlVaDriver *drv,
184 struct pipe_surface *surf_draw, struct u_rect *dirty_area,
185 struct u_rect *src_rect, struct u_rect *dst_rect)
186 {
187 vlVaSubpicture *sub;
188 int i;
189
190 if (!(surf->subpics.data || surf->subpics.size))
191 return VA_STATUS_SUCCESS;
192
193 for (i = 0; i < surf->subpics.size/sizeof(vlVaSubpicture *); i++) {
194 struct pipe_blend_state blend;
195 void *blend_state;
196 vlVaBuffer *buf;
197 struct pipe_box box;
198 struct u_rect *s, *d, sr, dr, c;
199 int sw, sh, dw, dh;
200
201 sub = ((vlVaSubpicture **)surf->subpics.data)[i];
202 if (!sub)
203 continue;
204
205 buf = handle_table_get(drv->htab, sub->image->buf);
206 if (!buf)
207 return VA_STATUS_ERROR_INVALID_IMAGE;
208
209 box.x = 0;
210 box.y = 0;
211 box.z = 0;
212 box.width = sub->dst_rect.x1 - sub->dst_rect.x0;
213 box.height = sub->dst_rect.y1 - sub->dst_rect.y0;
214 box.depth = 1;
215
216 s = &sub->src_rect;
217 d = &sub->dst_rect;
218 sw = s->x1 - s->x0;
219 sh = s->y1 - s->y0;
220 dw = d->x1 - d->x0;
221 dh = d->y1 - d->y0;
222 c.x0 = MAX2(d->x0, s->x0);
223 c.y0 = MAX2(d->y0, s->y0);
224 c.x1 = MIN2(d->x0 + dw, src_rect->x1);
225 c.y1 = MIN2(d->y0 + dh, src_rect->y1);
226 sr.x0 = s->x0 + (c.x0 - d->x0)*(sw/(float)dw);
227 sr.y0 = s->y0 + (c.y0 - d->y0)*(sh/(float)dh);
228 sr.x1 = s->x0 + (c.x1 - d->x0)*(sw/(float)dw);
229 sr.y1 = s->y0 + (c.y1 - d->y0)*(sh/(float)dh);
230
231 s = src_rect;
232 d = dst_rect;
233 sw = s->x1 - s->x0;
234 sh = s->y1 - s->y0;
235 dw = d->x1 - d->x0;
236 dh = d->y1 - d->y0;
237 dr.x0 = d->x0 + c.x0*(dw/(float)sw);
238 dr.y0 = d->y0 + c.y0*(dh/(float)sh);
239 dr.x1 = d->x0 + c.x1*(dw/(float)sw);
240 dr.y1 = d->y0 + c.y1*(dh/(float)sh);
241
242 memset(&blend, 0, sizeof(blend));
243 blend.independent_blend_enable = 0;
244 blend.rt[0].blend_enable = 1;
245 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
246 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
247 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ZERO;
248 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
249 blend.rt[0].rgb_func = PIPE_BLEND_ADD;
250 blend.rt[0].alpha_func = PIPE_BLEND_ADD;
251 blend.rt[0].colormask = PIPE_MASK_RGBA;
252 blend.logicop_enable = 0;
253 blend.logicop_func = PIPE_LOGICOP_CLEAR;
254 blend.dither = 0;
255 blend_state = drv->pipe->create_blend_state(drv->pipe, &blend);
256
257 vl_compositor_clear_layers(&drv->cstate);
258 vl_compositor_set_layer_blend(&drv->cstate, 0, blend_state, false);
259 upload_sampler(drv->pipe, sub->sampler, &box, buf->data,
260 sub->image->pitches[0], 0, 0);
261 vl_compositor_set_rgba_layer(&drv->cstate, &drv->compositor, 0, sub->sampler,
262 &sr, NULL, NULL);
263 vl_compositor_set_layer_dst_area(&drv->cstate, 0, &dr);
264 vl_compositor_render(&drv->cstate, &drv->compositor, surf_draw, dirty_area, false);
265 drv->pipe->delete_blend_state(drv->pipe, blend_state);
266 }
267
268 return VA_STATUS_SUCCESS;
269 }
270
271 VAStatus
272 vlVaPutSurface(VADriverContextP ctx, VASurfaceID surface_id, void* draw, short srcx, short srcy,
273 unsigned short srcw, unsigned short srch, short destx, short desty,
274 unsigned short destw, unsigned short desth, VARectangle *cliprects,
275 unsigned int number_cliprects, unsigned int flags)
276 {
277 vlVaDriver *drv;
278 vlVaSurface *surf;
279 struct pipe_screen *screen;
280 struct pipe_resource *tex;
281 struct pipe_surface surf_templ, *surf_draw;
282 struct vl_screen *vscreen;
283 struct u_rect src_rect, *dirty_area;
284 struct u_rect dst_rect = {destx, destx + destw, desty, desty + desth};
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 vl_compositor_clear_layers(&drv->cstate);
324 vl_compositor_set_buffer_layer(&drv->cstate, &drv->compositor, 0, surf->buffer, &src_rect, NULL, VL_COMPOSITOR_WEAVE);
325 vl_compositor_set_layer_dst_area(&drv->cstate, 0, &dst_rect);
326 vl_compositor_render(&drv->cstate, &drv->compositor, surf_draw, dirty_area, true);
327
328 status = vlVaPutSubpictures(surf, drv, surf_draw, dirty_area, &src_rect, &dst_rect);
329 if (status) {
330 mtx_unlock(&drv->mutex);
331 return status;
332 }
333
334 /* flush before calling flush_frontbuffer so that rendering is flushed
335 * to back buffer so the texture can be copied in flush_frontbuffer
336 */
337 drv->pipe->flush(drv->pipe, NULL, 0);
338
339 screen->flush_frontbuffer(screen, tex, 0, 0,
340 vscreen->get_private(vscreen), NULL);
341
342
343 pipe_resource_reference(&tex, NULL);
344 pipe_surface_reference(&surf_draw, NULL);
345 mtx_unlock(&drv->mutex);
346
347 return VA_STATUS_SUCCESS;
348 }
349
350 VAStatus
351 vlVaLockSurface(VADriverContextP ctx, VASurfaceID surface, unsigned int *fourcc,
352 unsigned int *luma_stride, unsigned int *chroma_u_stride, unsigned int *chroma_v_stride,
353 unsigned int *luma_offset, unsigned int *chroma_u_offset, unsigned int *chroma_v_offset,
354 unsigned int *buffer_name, void **buffer)
355 {
356 if (!ctx)
357 return VA_STATUS_ERROR_INVALID_CONTEXT;
358
359 return VA_STATUS_ERROR_UNIMPLEMENTED;
360 }
361
362 VAStatus
363 vlVaUnlockSurface(VADriverContextP ctx, VASurfaceID surface)
364 {
365 if (!ctx)
366 return VA_STATUS_ERROR_INVALID_CONTEXT;
367
368 return VA_STATUS_ERROR_UNIMPLEMENTED;
369 }
370
371 VAStatus
372 vlVaQuerySurfaceAttributes(VADriverContextP ctx, VAConfigID config_id,
373 VASurfaceAttrib *attrib_list, unsigned int *num_attribs)
374 {
375 vlVaDriver *drv;
376 vlVaConfig *config;
377 VASurfaceAttrib *attribs;
378 struct pipe_screen *pscreen;
379 int i, j;
380
381 STATIC_ASSERT(ARRAY_SIZE(vpp_surface_formats) <= VL_VA_MAX_IMAGE_FORMATS);
382
383 if (config_id == VA_INVALID_ID)
384 return VA_STATUS_ERROR_INVALID_CONFIG;
385
386 if (!attrib_list && !num_attribs)
387 return VA_STATUS_ERROR_INVALID_PARAMETER;
388
389 if (!attrib_list) {
390 *num_attribs = VL_VA_MAX_IMAGE_FORMATS + VASurfaceAttribCount;
391 return VA_STATUS_SUCCESS;
392 }
393
394 if (!ctx)
395 return VA_STATUS_ERROR_INVALID_CONTEXT;
396
397 drv = VL_VA_DRIVER(ctx);
398
399 if (!drv)
400 return VA_STATUS_ERROR_INVALID_CONTEXT;
401
402 mtx_lock(&drv->mutex);
403 config = handle_table_get(drv->htab, config_id);
404 mtx_unlock(&drv->mutex);
405
406 if (!config)
407 return VA_STATUS_ERROR_INVALID_CONFIG;
408
409 pscreen = VL_VA_PSCREEN(ctx);
410
411 if (!pscreen)
412 return VA_STATUS_ERROR_INVALID_CONTEXT;
413
414 attribs = CALLOC(VL_VA_MAX_IMAGE_FORMATS + VASurfaceAttribCount,
415 sizeof(VASurfaceAttrib));
416
417 if (!attribs)
418 return VA_STATUS_ERROR_ALLOCATION_FAILED;
419
420 i = 0;
421
422 /* vlVaCreateConfig returns PIPE_VIDEO_PROFILE_UNKNOWN
423 * only for VAEntrypointVideoProc. */
424 if (config->profile == PIPE_VIDEO_PROFILE_UNKNOWN) {
425 if (config->rt_format & VA_RT_FORMAT_RGB32) {
426 for (j = 0; j < ARRAY_SIZE(vpp_surface_formats); ++j) {
427 attribs[i].type = VASurfaceAttribPixelFormat;
428 attribs[i].value.type = VAGenericValueTypeInteger;
429 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
430 attribs[i].value.value.i = PipeFormatToVaFourcc(vpp_surface_formats[j]);
431 i++;
432 }
433 }
434 if (config->rt_format & VA_RT_FORMAT_YUV420) {
435 attribs[i].type = VASurfaceAttribPixelFormat;
436 attribs[i].value.type = VAGenericValueTypeInteger;
437 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
438 attribs[i].value.value.i = VA_FOURCC_NV12;
439 i++;
440 }
441 } else {
442 /* Assume VAEntrypointVLD for now. */
443 attribs[i].type = VASurfaceAttribPixelFormat;
444 attribs[i].value.type = VAGenericValueTypeInteger;
445 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE | VA_SURFACE_ATTRIB_SETTABLE;
446 attribs[i].value.value.i = VA_FOURCC_NV12;
447 i++;
448 }
449
450 attribs[i].type = VASurfaceAttribMemoryType;
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_SURFACE_ATTRIB_MEM_TYPE_VA |
454 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
455 i++;
456
457 attribs[i].type = VASurfaceAttribExternalBufferDescriptor;
458 attribs[i].value.type = VAGenericValueTypePointer;
459 attribs[i].flags = VA_SURFACE_ATTRIB_SETTABLE;
460 attribs[i].value.value.p = NULL; /* ignore */
461 i++;
462
463 attribs[i].type = VASurfaceAttribMaxWidth;
464 attribs[i].value.type = VAGenericValueTypeInteger;
465 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
466 attribs[i].value.value.i = vl_video_buffer_max_size(pscreen);
467 i++;
468
469 attribs[i].type = VASurfaceAttribMaxHeight;
470 attribs[i].value.type = VAGenericValueTypeInteger;
471 attribs[i].flags = VA_SURFACE_ATTRIB_GETTABLE;
472 attribs[i].value.value.i = vl_video_buffer_max_size(pscreen);
473 i++;
474
475 if (i > *num_attribs) {
476 *num_attribs = i;
477 FREE(attribs);
478 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
479 }
480
481 *num_attribs = i;
482 memcpy(attrib_list, attribs, i * sizeof(VASurfaceAttrib));
483 FREE(attribs);
484
485 return VA_STATUS_SUCCESS;
486 }
487
488 static VAStatus
489 suface_from_external_memory(VADriverContextP ctx, vlVaSurface *surface,
490 VASurfaceAttribExternalBuffers *memory_attibute,
491 unsigned index, struct pipe_video_buffer *templat)
492 {
493 vlVaDriver *drv;
494 struct pipe_screen *pscreen;
495 struct pipe_resource *resource;
496 struct pipe_resource res_templ;
497 struct winsys_handle whandle;
498 struct pipe_resource *resources[VL_NUM_COMPONENTS];
499
500 pscreen = VL_VA_PSCREEN(ctx);
501 drv = VL_VA_DRIVER(ctx);
502
503 if (!memory_attibute || !memory_attibute->buffers ||
504 index > memory_attibute->num_buffers)
505 return VA_STATUS_ERROR_INVALID_PARAMETER;
506
507 if (surface->templat.width != memory_attibute->width ||
508 surface->templat.height != memory_attibute->height ||
509 memory_attibute->num_planes < 1)
510 return VA_STATUS_ERROR_INVALID_PARAMETER;
511
512 switch (memory_attibute->pixel_format) {
513 case VA_FOURCC_RGBA:
514 case VA_FOURCC_RGBX:
515 case VA_FOURCC_BGRA:
516 case VA_FOURCC_BGRX:
517 if (memory_attibute->num_planes != 1)
518 return VA_STATUS_ERROR_INVALID_PARAMETER;
519 break;
520 default:
521 return VA_STATUS_ERROR_INVALID_PARAMETER;
522 }
523
524 memset(&res_templ, 0, sizeof(res_templ));
525 res_templ.target = PIPE_TEXTURE_2D;
526 res_templ.last_level = 0;
527 res_templ.depth0 = 1;
528 res_templ.array_size = 1;
529 res_templ.width0 = memory_attibute->width;
530 res_templ.height0 = memory_attibute->height;
531 res_templ.format = surface->templat.buffer_format;
532 res_templ.bind = PIPE_BIND_SAMPLER_VIEW;
533 res_templ.usage = PIPE_USAGE_DEFAULT;
534
535 memset(&whandle, 0, sizeof(struct winsys_handle));
536 whandle.type = DRM_API_HANDLE_TYPE_FD;
537 whandle.handle = memory_attibute->buffers[index];
538 whandle.stride = memory_attibute->pitches[index];
539
540 resource = pscreen->resource_from_handle(pscreen, &res_templ, &whandle,
541 PIPE_HANDLE_USAGE_READ_WRITE);
542
543 if (!resource)
544 return VA_STATUS_ERROR_ALLOCATION_FAILED;
545
546 memset(resources, 0, sizeof resources);
547 resources[0] = resource;
548
549 surface->buffer = vl_video_buffer_create_ex2(drv->pipe, templat, resources);
550 if (!surface->buffer)
551 return VA_STATUS_ERROR_ALLOCATION_FAILED;
552
553 return VA_STATUS_SUCCESS;
554 }
555
556 VAStatus
557 vlVaCreateSurfaces2(VADriverContextP ctx, unsigned int format,
558 unsigned int width, unsigned int height,
559 VASurfaceID *surfaces, unsigned int num_surfaces,
560 VASurfaceAttrib *attrib_list, unsigned int num_attribs)
561 {
562 vlVaDriver *drv;
563 VASurfaceAttribExternalBuffers *memory_attibute;
564 struct pipe_video_buffer templat;
565 struct pipe_screen *pscreen;
566 int i;
567 int memory_type;
568 int expected_fourcc;
569 VAStatus vaStatus;
570 vlVaSurface *surf;
571
572 if (!ctx)
573 return VA_STATUS_ERROR_INVALID_CONTEXT;
574
575 if (!(width && height))
576 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
577
578 drv = VL_VA_DRIVER(ctx);
579
580 if (!drv)
581 return VA_STATUS_ERROR_INVALID_CONTEXT;
582
583 pscreen = VL_VA_PSCREEN(ctx);
584
585 if (!pscreen)
586 return VA_STATUS_ERROR_INVALID_CONTEXT;
587
588 /* Default. */
589 memory_attibute = NULL;
590 memory_type = VA_SURFACE_ATTRIB_MEM_TYPE_VA;
591 expected_fourcc = 0;
592
593 for (i = 0; i < num_attribs && attrib_list; i++) {
594 if ((attrib_list[i].type == VASurfaceAttribPixelFormat) &&
595 (attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE)) {
596 if (attrib_list[i].value.type != VAGenericValueTypeInteger)
597 return VA_STATUS_ERROR_INVALID_PARAMETER;
598 expected_fourcc = attrib_list[i].value.value.i;
599 }
600
601 if ((attrib_list[i].type == VASurfaceAttribMemoryType) &&
602 (attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE)) {
603
604 if (attrib_list[i].value.type != VAGenericValueTypeInteger)
605 return VA_STATUS_ERROR_INVALID_PARAMETER;
606
607 switch (attrib_list[i].value.value.i) {
608 case VA_SURFACE_ATTRIB_MEM_TYPE_VA:
609 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME:
610 memory_type = attrib_list[i].value.value.i;
611 break;
612 default:
613 return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
614 }
615 }
616
617 if ((attrib_list[i].type == VASurfaceAttribExternalBufferDescriptor) &&
618 (attrib_list[i].flags == VA_SURFACE_ATTRIB_SETTABLE)) {
619 if (attrib_list[i].value.type != VAGenericValueTypePointer)
620 return VA_STATUS_ERROR_INVALID_PARAMETER;
621 memory_attibute = (VASurfaceAttribExternalBuffers *)attrib_list[i].value.value.p;
622 }
623 }
624
625 if (VA_RT_FORMAT_YUV420 != format &&
626 VA_RT_FORMAT_YUV422 != format &&
627 VA_RT_FORMAT_YUV444 != format &&
628 VA_RT_FORMAT_RGB32 != format) {
629 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
630 }
631
632 switch (memory_type) {
633 case VA_SURFACE_ATTRIB_MEM_TYPE_VA:
634 break;
635 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME:
636 if (!memory_attibute)
637 return VA_STATUS_ERROR_INVALID_PARAMETER;
638
639 expected_fourcc = memory_attibute->pixel_format;
640 break;
641 default:
642 assert(0);
643 }
644
645 memset(&templat, 0, sizeof(templat));
646
647 templat.buffer_format = pscreen->get_video_param(
648 pscreen,
649 PIPE_VIDEO_PROFILE_UNKNOWN,
650 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
651 PIPE_VIDEO_CAP_PREFERED_FORMAT
652 );
653 templat.interlaced = pscreen->get_video_param(
654 pscreen,
655 PIPE_VIDEO_PROFILE_UNKNOWN,
656 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
657 PIPE_VIDEO_CAP_PREFERS_INTERLACED
658 );
659
660 if (expected_fourcc) {
661 enum pipe_format expected_format = VaFourccToPipeFormat(expected_fourcc);
662
663 if (expected_format != templat.buffer_format || memory_attibute)
664 templat.interlaced = 0;
665
666 templat.buffer_format = expected_format;
667 }
668
669 templat.chroma_format = ChromaToPipe(format);
670
671 templat.width = width;
672 templat.height = height;
673 if (debug_get_option_nointerlace())
674 templat.interlaced = false;
675
676 memset(surfaces, VA_INVALID_ID, num_surfaces * sizeof(VASurfaceID));
677
678 mtx_lock(&drv->mutex);
679 for (i = 0; i < num_surfaces; i++) {
680 surf = CALLOC(1, sizeof(vlVaSurface));
681 if (!surf) {
682 vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
683 goto no_res;
684 }
685
686 surf->templat = templat;
687
688 switch (memory_type) {
689 case VA_SURFACE_ATTRIB_MEM_TYPE_VA:
690 /* The application will clear the TILING flag when the surface is
691 * intended to be exported as dmabuf. Adding shared flag because not
692 * null memory_attibute means VASurfaceAttribExternalBuffers is used.
693 */
694 if (memory_attibute &&
695 !(memory_attibute->flags & VA_SURFACE_EXTBUF_DESC_ENABLE_TILING))
696 templat.bind = PIPE_BIND_LINEAR | PIPE_BIND_SHARED;
697
698 surf->buffer = drv->pipe->create_video_buffer(drv->pipe, &templat);
699 if (!surf->buffer) {
700 vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
701 goto free_surf;
702 }
703 break;
704
705 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME:
706 vaStatus = suface_from_external_memory(ctx, surf, memory_attibute, i, &templat);
707 if (vaStatus != VA_STATUS_SUCCESS)
708 goto free_surf;
709 break;
710
711 default:
712 assert(0);
713 }
714
715 util_dynarray_init(&surf->subpics);
716 surfaces[i] = handle_table_add(drv->htab, surf);
717 if (!surfaces[i]) {
718 vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
719 goto destroy_surf;
720 }
721 }
722 mtx_unlock(&drv->mutex);
723
724 return VA_STATUS_SUCCESS;
725
726 destroy_surf:
727 surf->buffer->destroy(surf->buffer);
728
729 free_surf:
730 FREE(surf);
731
732 no_res:
733 mtx_unlock(&drv->mutex);
734 if (i)
735 vlVaDestroySurfaces(ctx, surfaces, i);
736
737 return vaStatus;
738 }
739
740 VAStatus
741 vlVaQueryVideoProcFilters(VADriverContextP ctx, VAContextID context,
742 VAProcFilterType *filters, unsigned int *num_filters)
743 {
744 unsigned int num = 0;
745
746 if (!ctx)
747 return VA_STATUS_ERROR_INVALID_CONTEXT;
748
749 if (!num_filters || !filters)
750 return VA_STATUS_ERROR_INVALID_PARAMETER;
751
752 filters[num++] = VAProcFilterDeinterlacing;
753
754 *num_filters = num;
755
756 return VA_STATUS_SUCCESS;
757 }
758
759 VAStatus
760 vlVaQueryVideoProcFilterCaps(VADriverContextP ctx, VAContextID context,
761 VAProcFilterType type, void *filter_caps,
762 unsigned int *num_filter_caps)
763 {
764 unsigned int i;
765
766 if (!ctx)
767 return VA_STATUS_ERROR_INVALID_CONTEXT;
768
769 if (!filter_caps || !num_filter_caps)
770 return VA_STATUS_ERROR_INVALID_PARAMETER;
771
772 i = 0;
773
774 switch (type) {
775 case VAProcFilterNone:
776 break;
777 case VAProcFilterDeinterlacing: {
778 VAProcFilterCapDeinterlacing *deint = filter_caps;
779
780 if (*num_filter_caps < 3) {
781 *num_filter_caps = 3;
782 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
783 }
784
785 deint[i++].type = VAProcDeinterlacingBob;
786 deint[i++].type = VAProcDeinterlacingWeave;
787 deint[i++].type = VAProcDeinterlacingMotionAdaptive;
788 break;
789 }
790
791 case VAProcFilterNoiseReduction:
792 case VAProcFilterSharpening:
793 case VAProcFilterColorBalance:
794 case VAProcFilterSkinToneEnhancement:
795 return VA_STATUS_ERROR_UNIMPLEMENTED;
796 default:
797 assert(0);
798 }
799
800 *num_filter_caps = i;
801
802 return VA_STATUS_SUCCESS;
803 }
804
805 static VAProcColorStandardType vpp_input_color_standards[] = {
806 VAProcColorStandardBT601
807 };
808
809 static VAProcColorStandardType vpp_output_color_standards[] = {
810 VAProcColorStandardBT601
811 };
812
813 VAStatus
814 vlVaQueryVideoProcPipelineCaps(VADriverContextP ctx, VAContextID context,
815 VABufferID *filters, unsigned int num_filters,
816 VAProcPipelineCaps *pipeline_cap)
817 {
818 unsigned int i = 0;
819
820 if (!ctx)
821 return VA_STATUS_ERROR_INVALID_CONTEXT;
822
823 if (!pipeline_cap)
824 return VA_STATUS_ERROR_INVALID_PARAMETER;
825
826 if (num_filters && !filters)
827 return VA_STATUS_ERROR_INVALID_PARAMETER;
828
829 pipeline_cap->pipeline_flags = 0;
830 pipeline_cap->filter_flags = 0;
831 pipeline_cap->num_forward_references = 0;
832 pipeline_cap->num_backward_references = 0;
833 pipeline_cap->num_input_color_standards = ARRAY_SIZE(vpp_input_color_standards);
834 pipeline_cap->input_color_standards = vpp_input_color_standards;
835 pipeline_cap->num_output_color_standards = ARRAY_SIZE(vpp_output_color_standards);
836 pipeline_cap->output_color_standards = vpp_output_color_standards;
837
838 for (i = 0; i < num_filters; i++) {
839 vlVaBuffer *buf = handle_table_get(VL_VA_DRIVER(ctx)->htab, filters[i]);
840 VAProcFilterParameterBufferBase *filter;
841
842 if (!buf || buf->type != VAProcFilterParameterBufferType)
843 return VA_STATUS_ERROR_INVALID_BUFFER;
844
845 filter = buf->data;
846 switch (filter->type) {
847 case VAProcFilterDeinterlacing: {
848 VAProcFilterParameterBufferDeinterlacing *deint = buf->data;
849 if (deint->algorithm == VAProcDeinterlacingMotionAdaptive) {
850 pipeline_cap->num_forward_references = 2;
851 pipeline_cap->num_backward_references = 1;
852 }
853 break;
854 }
855 default:
856 return VA_STATUS_ERROR_UNIMPLEMENTED;
857 }
858 }
859
860 return VA_STATUS_SUCCESS;
861 }