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