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