st/va: set the visible image dimensions in vlVaDeriveImage
[mesa.git] / src / gallium / state_trackers / va / image.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
31 #include "util/u_memory.h"
32 #include "util/u_handle_table.h"
33 #include "util/u_surface.h"
34 #include "util/u_video.h"
35
36 #include "vl/vl_winsys.h"
37 #include "vl/vl_video_buffer.h"
38
39 #include "va_private.h"
40
41 static const VAImageFormat formats[] =
42 {
43 {VA_FOURCC('N','V','1','2')},
44 {VA_FOURCC('P','0','1','0')},
45 {VA_FOURCC('P','0','1','6')},
46 {VA_FOURCC('I','4','2','0')},
47 {VA_FOURCC('Y','V','1','2')},
48 {VA_FOURCC('Y','U','Y','V')},
49 {VA_FOURCC('U','Y','V','Y')},
50 {.fourcc = VA_FOURCC('B','G','R','A'), .byte_order = VA_LSB_FIRST, 32, 32,
51 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000},
52 {.fourcc = VA_FOURCC('R','G','B','A'), .byte_order = VA_LSB_FIRST, 32, 32,
53 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000},
54 {.fourcc = VA_FOURCC('B','G','R','X'), .byte_order = VA_LSB_FIRST, 32, 24,
55 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000},
56 {.fourcc = VA_FOURCC('R','G','B','X'), .byte_order = VA_LSB_FIRST, 32, 24,
57 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}
58 };
59
60 static void
61 vlVaVideoSurfaceSize(vlVaSurface *p_surf, int component,
62 unsigned *width, unsigned *height)
63 {
64 *width = p_surf->templat.width;
65 *height = p_surf->templat.height;
66
67 vl_video_buffer_adjust_size(width, height, component,
68 p_surf->templat.chroma_format,
69 p_surf->templat.interlaced);
70 }
71
72 VAStatus
73 vlVaQueryImageFormats(VADriverContextP ctx, VAImageFormat *format_list, int *num_formats)
74 {
75 struct pipe_screen *pscreen;
76 enum pipe_format format;
77 int i;
78
79 STATIC_ASSERT(ARRAY_SIZE(formats) == VL_VA_MAX_IMAGE_FORMATS);
80
81 if (!ctx)
82 return VA_STATUS_ERROR_INVALID_CONTEXT;
83
84 if (!(format_list && num_formats))
85 return VA_STATUS_ERROR_INVALID_PARAMETER;
86
87 *num_formats = 0;
88 pscreen = VL_VA_PSCREEN(ctx);
89 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
90 format = VaFourccToPipeFormat(formats[i].fourcc);
91 if (pscreen->is_video_format_supported(pscreen, format,
92 PIPE_VIDEO_PROFILE_UNKNOWN,
93 PIPE_VIDEO_ENTRYPOINT_BITSTREAM))
94 format_list[(*num_formats)++] = formats[i];
95 }
96
97 return VA_STATUS_SUCCESS;
98 }
99
100 VAStatus
101 vlVaCreateImage(VADriverContextP ctx, VAImageFormat *format, int width, int height, VAImage *image)
102 {
103 VAStatus status;
104 vlVaDriver *drv;
105 VAImage *img;
106 int w, h;
107
108 if (!ctx)
109 return VA_STATUS_ERROR_INVALID_CONTEXT;
110
111 if (!(format && image && width && height))
112 return VA_STATUS_ERROR_INVALID_PARAMETER;
113
114 drv = VL_VA_DRIVER(ctx);
115
116 img = CALLOC(1, sizeof(VAImage));
117 if (!img)
118 return VA_STATUS_ERROR_ALLOCATION_FAILED;
119 mtx_lock(&drv->mutex);
120 img->image_id = handle_table_add(drv->htab, img);
121 mtx_unlock(&drv->mutex);
122
123 img->format = *format;
124 img->width = width;
125 img->height = height;
126 w = align(width, 2);
127 h = align(height, 2);
128
129 switch (format->fourcc) {
130 case VA_FOURCC('N','V','1','2'):
131 img->num_planes = 2;
132 img->pitches[0] = w;
133 img->offsets[0] = 0;
134 img->pitches[1] = w;
135 img->offsets[1] = w * h;
136 img->data_size = w * h * 3 / 2;
137 break;
138
139 case VA_FOURCC('P','0','1','0'):
140 case VA_FOURCC('P','0','1','6'):
141 img->num_planes = 2;
142 img->pitches[0] = w * 2;
143 img->offsets[0] = 0;
144 img->pitches[1] = w * 2;
145 img->offsets[1] = w * h * 2;
146 img->data_size = w * h * 3;
147 break;
148
149 case VA_FOURCC('I','4','2','0'):
150 case VA_FOURCC('Y','V','1','2'):
151 img->num_planes = 3;
152 img->pitches[0] = w;
153 img->offsets[0] = 0;
154 img->pitches[1] = w / 2;
155 img->offsets[1] = w * h;
156 img->pitches[2] = w / 2;
157 img->offsets[2] = w * h * 5 / 4;
158 img->data_size = w * h * 3 / 2;
159 break;
160
161 case VA_FOURCC('U','Y','V','Y'):
162 case VA_FOURCC('Y','U','Y','V'):
163 img->num_planes = 1;
164 img->pitches[0] = w * 2;
165 img->offsets[0] = 0;
166 img->data_size = w * h * 2;
167 break;
168
169 case VA_FOURCC('B','G','R','A'):
170 case VA_FOURCC('R','G','B','A'):
171 case VA_FOURCC('B','G','R','X'):
172 case VA_FOURCC('R','G','B','X'):
173 img->num_planes = 1;
174 img->pitches[0] = w * 4;
175 img->offsets[0] = 0;
176 img->data_size = w * h * 4;
177 break;
178
179 default:
180 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
181 }
182
183 status = vlVaCreateBuffer(ctx, 0, VAImageBufferType,
184 align(img->data_size, 16),
185 1, NULL, &img->buf);
186 if (status != VA_STATUS_SUCCESS)
187 return status;
188 *image = *img;
189
190 return status;
191 }
192
193 VAStatus
194 vlVaDeriveImage(VADriverContextP ctx, VASurfaceID surface, VAImage *image)
195 {
196 vlVaDriver *drv;
197 vlVaSurface *surf;
198 vlVaBuffer *img_buf;
199 VAImage *img;
200 struct pipe_screen *screen;
201 struct pipe_surface **surfaces;
202 int w;
203 int h;
204 int i;
205 unsigned stride = 0;
206 unsigned offset = 0;
207
208 if (!ctx)
209 return VA_STATUS_ERROR_INVALID_CONTEXT;
210
211 drv = VL_VA_DRIVER(ctx);
212
213 if (!drv)
214 return VA_STATUS_ERROR_INVALID_CONTEXT;
215
216 screen = VL_VA_PSCREEN(ctx);
217
218 if (!screen)
219 return VA_STATUS_ERROR_INVALID_CONTEXT;
220
221 surf = handle_table_get(drv->htab, surface);
222
223 if (!surf || !surf->buffer)
224 return VA_STATUS_ERROR_INVALID_SURFACE;
225
226 if (surf->buffer->interlaced)
227 return VA_STATUS_ERROR_OPERATION_FAILED;
228
229 surfaces = surf->buffer->get_surfaces(surf->buffer);
230 if (!surfaces || !surfaces[0]->texture)
231 return VA_STATUS_ERROR_ALLOCATION_FAILED;
232
233 img = CALLOC(1, sizeof(VAImage));
234 if (!img)
235 return VA_STATUS_ERROR_ALLOCATION_FAILED;
236
237 img->format.fourcc = PipeFormatToVaFourcc(surf->buffer->buffer_format);
238 img->buf = VA_INVALID_ID;
239 /* Use the visible dimensions. */
240 img->width = surf->templat.width;
241 img->height = surf->templat.height;
242 img->num_palette_entries = 0;
243 img->entry_bytes = 0;
244 /* Image data size is computed using internal dimensions. */
245 w = align(surf->buffer->width, 2);
246 h = align(surf->buffer->height, 2);
247
248 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
249 if (img->format.fourcc == formats[i].fourcc) {
250 img->format = formats[i];
251 break;
252 }
253 }
254
255 mtx_lock(&drv->mutex);
256 if (screen->resource_get_info) {
257 screen->resource_get_info(screen, surfaces[0]->texture, &stride,
258 &offset);
259 if (!stride)
260 offset = 0;
261 }
262
263 switch (img->format.fourcc) {
264 case VA_FOURCC('U','Y','V','Y'):
265 case VA_FOURCC('Y','U','Y','V'):
266 img->pitches[0] = stride > 0 ? stride : w * 2;
267 assert(img->pitches[0] >= (w * 2));
268 break;
269
270 case VA_FOURCC('B','G','R','A'):
271 case VA_FOURCC('R','G','B','A'):
272 case VA_FOURCC('B','G','R','X'):
273 case VA_FOURCC('R','G','B','X'):
274 img->pitches[0] = stride > 0 ? stride : w * 4;
275 assert(img->pitches[0] >= (w * 4));
276 break;
277
278 default:
279 /* VaDeriveImage only supports contiguous planes. But there is now a
280 more generic api vlVaExportSurfaceHandle. */
281 FREE(img);
282 mtx_unlock(&drv->mutex);
283 return VA_STATUS_ERROR_OPERATION_FAILED;
284 }
285
286 img->num_planes = 1;
287 img->offsets[0] = offset;
288 img->data_size = img->pitches[0] * h;
289
290 img_buf = CALLOC(1, sizeof(vlVaBuffer));
291 if (!img_buf) {
292 FREE(img);
293 mtx_unlock(&drv->mutex);
294 return VA_STATUS_ERROR_ALLOCATION_FAILED;
295 }
296
297 img->image_id = handle_table_add(drv->htab, img);
298
299 img_buf->type = VAImageBufferType;
300 img_buf->size = img->data_size;
301 img_buf->num_elements = 1;
302
303 pipe_resource_reference(&img_buf->derived_surface.resource, surfaces[0]->texture);
304
305 img->buf = handle_table_add(VL_VA_DRIVER(ctx)->htab, img_buf);
306 mtx_unlock(&drv->mutex);
307
308 *image = *img;
309
310 return VA_STATUS_SUCCESS;
311 }
312
313 VAStatus
314 vlVaDestroyImage(VADriverContextP ctx, VAImageID image)
315 {
316 vlVaDriver *drv;
317 VAImage *vaimage;
318 VAStatus status;
319
320 if (!ctx)
321 return VA_STATUS_ERROR_INVALID_CONTEXT;
322
323 drv = VL_VA_DRIVER(ctx);
324 mtx_lock(&drv->mutex);
325 vaimage = handle_table_get(drv->htab, image);
326 if (!vaimage) {
327 mtx_unlock(&drv->mutex);
328 return VA_STATUS_ERROR_INVALID_IMAGE;
329 }
330
331 handle_table_remove(VL_VA_DRIVER(ctx)->htab, image);
332 mtx_unlock(&drv->mutex);
333 status = vlVaDestroyBuffer(ctx, vaimage->buf);
334 FREE(vaimage);
335 return status;
336 }
337
338 VAStatus
339 vlVaSetImagePalette(VADriverContextP ctx, VAImageID image, unsigned char *palette)
340 {
341 if (!ctx)
342 return VA_STATUS_ERROR_INVALID_CONTEXT;
343
344 return VA_STATUS_ERROR_UNIMPLEMENTED;
345 }
346
347 VAStatus
348 vlVaGetImage(VADriverContextP ctx, VASurfaceID surface, int x, int y,
349 unsigned int width, unsigned int height, VAImageID image)
350 {
351 vlVaDriver *drv;
352 vlVaSurface *surf;
353 vlVaBuffer *img_buf;
354 VAImage *vaimage;
355 struct pipe_sampler_view **views;
356 enum pipe_format format;
357 bool convert = false;
358 void *data[3];
359 unsigned pitches[3], i, j;
360
361 if (!ctx)
362 return VA_STATUS_ERROR_INVALID_CONTEXT;
363
364 drv = VL_VA_DRIVER(ctx);
365
366 mtx_lock(&drv->mutex);
367 surf = handle_table_get(drv->htab, surface);
368 if (!surf || !surf->buffer) {
369 mtx_unlock(&drv->mutex);
370 return VA_STATUS_ERROR_INVALID_SURFACE;
371 }
372
373 vaimage = handle_table_get(drv->htab, image);
374 if (!vaimage) {
375 mtx_unlock(&drv->mutex);
376 return VA_STATUS_ERROR_INVALID_IMAGE;
377 }
378
379 if (x < 0 || y < 0) {
380 mtx_unlock(&drv->mutex);
381 return VA_STATUS_ERROR_INVALID_PARAMETER;
382 }
383
384 if (x + width > surf->templat.width ||
385 y + height > surf->templat.height) {
386 mtx_unlock(&drv->mutex);
387 return VA_STATUS_ERROR_INVALID_PARAMETER;
388 }
389
390 if (width > vaimage->width ||
391 height > vaimage->height) {
392 mtx_unlock(&drv->mutex);
393 return VA_STATUS_ERROR_INVALID_PARAMETER;
394 }
395
396 img_buf = handle_table_get(drv->htab, vaimage->buf);
397 if (!img_buf) {
398 mtx_unlock(&drv->mutex);
399 return VA_STATUS_ERROR_INVALID_BUFFER;
400 }
401
402 format = VaFourccToPipeFormat(vaimage->format.fourcc);
403 if (format == PIPE_FORMAT_NONE) {
404 mtx_unlock(&drv->mutex);
405 return VA_STATUS_ERROR_OPERATION_FAILED;
406 }
407
408 if (format != surf->buffer->buffer_format) {
409 /* support NV12 to YV12 and IYUV conversion now only */
410 if ((format == PIPE_FORMAT_YV12 &&
411 surf->buffer->buffer_format == PIPE_FORMAT_NV12) ||
412 (format == PIPE_FORMAT_IYUV &&
413 surf->buffer->buffer_format == PIPE_FORMAT_NV12))
414 convert = true;
415 else {
416 mtx_unlock(&drv->mutex);
417 return VA_STATUS_ERROR_OPERATION_FAILED;
418 }
419 }
420
421 views = surf->buffer->get_sampler_view_planes(surf->buffer);
422 if (!views) {
423 mtx_unlock(&drv->mutex);
424 return VA_STATUS_ERROR_OPERATION_FAILED;
425 }
426
427 for (i = 0; i < vaimage->num_planes; i++) {
428 data[i] = img_buf->data + vaimage->offsets[i];
429 pitches[i] = vaimage->pitches[i];
430 }
431 if (vaimage->format.fourcc == VA_FOURCC('I','4','2','0')) {
432 void *tmp_d;
433 unsigned tmp_p;
434 tmp_d = data[1];
435 data[1] = data[2];
436 data[2] = tmp_d;
437 tmp_p = pitches[1];
438 pitches[1] = pitches[2];
439 pitches[2] = tmp_p;
440 }
441
442 for (i = 0; i < vaimage->num_planes; i++) {
443 unsigned box_w = align(width, 2);
444 unsigned box_h = align(height, 2);
445 unsigned box_x = x & ~1;
446 unsigned box_y = y & ~1;
447 if (!views[i]) continue;
448 vl_video_buffer_adjust_size(&box_w, &box_h, i,
449 surf->templat.chroma_format,
450 surf->templat.interlaced);
451 vl_video_buffer_adjust_size(&box_x, &box_y, i,
452 surf->templat.chroma_format,
453 surf->templat.interlaced);
454 for (j = 0; j < views[i]->texture->array_size; ++j) {
455 struct pipe_box box = {box_x, box_y, j, box_w, box_h, 1};
456 struct pipe_transfer *transfer;
457 uint8_t *map;
458 map = drv->pipe->transfer_map(drv->pipe, views[i]->texture, 0,
459 PIPE_TRANSFER_READ, &box, &transfer);
460 if (!map) {
461 mtx_unlock(&drv->mutex);
462 return VA_STATUS_ERROR_OPERATION_FAILED;
463 }
464
465 if (i == 1 && convert) {
466 u_copy_nv12_to_yv12(data, pitches, i, j,
467 transfer->stride, views[i]->texture->array_size,
468 map, box.width, box.height);
469 } else {
470 util_copy_rect(data[i] + pitches[i] * j,
471 views[i]->texture->format,
472 pitches[i] * views[i]->texture->array_size, 0, 0,
473 box.width, box.height, map, transfer->stride, 0, 0);
474 }
475 pipe_transfer_unmap(drv->pipe, transfer);
476 }
477 }
478 mtx_unlock(&drv->mutex);
479
480 return VA_STATUS_SUCCESS;
481 }
482
483 VAStatus
484 vlVaPutImage(VADriverContextP ctx, VASurfaceID surface, VAImageID image,
485 int src_x, int src_y, unsigned int src_width, unsigned int src_height,
486 int dest_x, int dest_y, unsigned int dest_width, unsigned int dest_height)
487 {
488 vlVaDriver *drv;
489 vlVaSurface *surf;
490 vlVaBuffer *img_buf;
491 VAImage *vaimage;
492 struct pipe_sampler_view **views;
493 enum pipe_format format;
494 void *data[3];
495 unsigned pitches[3], i, j;
496
497 if (!ctx)
498 return VA_STATUS_ERROR_INVALID_CONTEXT;
499
500 drv = VL_VA_DRIVER(ctx);
501 mtx_lock(&drv->mutex);
502
503 surf = handle_table_get(drv->htab, surface);
504 if (!surf || !surf->buffer) {
505 mtx_unlock(&drv->mutex);
506 return VA_STATUS_ERROR_INVALID_SURFACE;
507 }
508
509 vaimage = handle_table_get(drv->htab, image);
510 if (!vaimage) {
511 mtx_unlock(&drv->mutex);
512 return VA_STATUS_ERROR_INVALID_IMAGE;
513 }
514
515 img_buf = handle_table_get(drv->htab, vaimage->buf);
516 if (!img_buf) {
517 mtx_unlock(&drv->mutex);
518 return VA_STATUS_ERROR_INVALID_BUFFER;
519 }
520
521 if (img_buf->derived_surface.resource) {
522 /* Attempting to transfer derived image to surface */
523 mtx_unlock(&drv->mutex);
524 return VA_STATUS_ERROR_UNIMPLEMENTED;
525 }
526
527 format = VaFourccToPipeFormat(vaimage->format.fourcc);
528
529 if (format == PIPE_FORMAT_NONE) {
530 mtx_unlock(&drv->mutex);
531 return VA_STATUS_ERROR_OPERATION_FAILED;
532 }
533
534 if ((format != surf->buffer->buffer_format) &&
535 ((format != PIPE_FORMAT_YV12) || (surf->buffer->buffer_format != PIPE_FORMAT_NV12)) &&
536 ((format != PIPE_FORMAT_IYUV) || (surf->buffer->buffer_format != PIPE_FORMAT_NV12))) {
537 struct pipe_video_buffer *tmp_buf;
538
539 surf->templat.buffer_format = format;
540 if (format == PIPE_FORMAT_YUYV || format == PIPE_FORMAT_UYVY ||
541 format == PIPE_FORMAT_B8G8R8A8_UNORM || format == PIPE_FORMAT_B8G8R8X8_UNORM ||
542 format == PIPE_FORMAT_R8G8B8A8_UNORM || format == PIPE_FORMAT_R8G8B8X8_UNORM)
543 surf->templat.interlaced = false;
544 tmp_buf = drv->pipe->create_video_buffer(drv->pipe, &surf->templat);
545
546 if (!tmp_buf) {
547 mtx_unlock(&drv->mutex);
548 return VA_STATUS_ERROR_ALLOCATION_FAILED;
549 }
550
551 surf->buffer->destroy(surf->buffer);
552 surf->buffer = tmp_buf;
553 }
554
555 views = surf->buffer->get_sampler_view_planes(surf->buffer);
556 if (!views) {
557 mtx_unlock(&drv->mutex);
558 return VA_STATUS_ERROR_OPERATION_FAILED;
559 }
560
561 for (i = 0; i < vaimage->num_planes; i++) {
562 data[i] = img_buf->data + vaimage->offsets[i];
563 pitches[i] = vaimage->pitches[i];
564 }
565 if (vaimage->format.fourcc == VA_FOURCC('I','4','2','0')) {
566 void *tmp_d;
567 unsigned tmp_p;
568 tmp_d = data[1];
569 data[1] = data[2];
570 data[2] = tmp_d;
571 tmp_p = pitches[1];
572 pitches[1] = pitches[2];
573 pitches[2] = tmp_p;
574 }
575
576 for (i = 0; i < vaimage->num_planes; ++i) {
577 unsigned width, height;
578 struct pipe_resource *tex;
579
580 if (!views[i]) continue;
581 tex = views[i]->texture;
582
583 vlVaVideoSurfaceSize(surf, i, &width, &height);
584 for (j = 0; j < tex->array_size; ++j) {
585 struct pipe_box dst_box = {0, 0, j, width, height, 1};
586
587 if (((format == PIPE_FORMAT_YV12) || (format == PIPE_FORMAT_IYUV))
588 && (surf->buffer->buffer_format == PIPE_FORMAT_NV12)
589 && i == 1) {
590 struct pipe_transfer *transfer = NULL;
591 uint8_t *map = NULL;
592
593 map = drv->pipe->transfer_map(drv->pipe,
594 tex,
595 0,
596 PIPE_TRANSFER_WRITE |
597 PIPE_TRANSFER_DISCARD_RANGE,
598 &dst_box, &transfer);
599 if (map == NULL) {
600 mtx_unlock(&drv->mutex);
601 return VA_STATUS_ERROR_OPERATION_FAILED;
602 }
603
604 u_copy_nv12_from_yv12((const void * const*) data, pitches, i, j,
605 transfer->stride, tex->array_size,
606 map, dst_box.width, dst_box.height);
607 pipe_transfer_unmap(drv->pipe, transfer);
608 } else {
609 drv->pipe->texture_subdata(drv->pipe, tex, 0,
610 PIPE_TRANSFER_WRITE, &dst_box,
611 data[i] + pitches[i] * j,
612 pitches[i] * views[i]->texture->array_size, 0);
613 }
614 }
615 }
616 mtx_unlock(&drv->mutex);
617
618 return VA_STATUS_SUCCESS;
619 }