1 /**************************************************************************
3 * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4 * Copyright 2014 Advanced Micro Devices, Inc.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
29 #include "pipe/p_screen.h"
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"
36 #include "vl/vl_winsys.h"
38 #include "va_private.h"
40 static const VAImageFormat formats
[VL_VA_MAX_IMAGE_FORMATS
] =
42 {VA_FOURCC('N','V','1','2')},
43 {VA_FOURCC('I','4','2','0')},
44 {VA_FOURCC('Y','V','1','2')},
45 {VA_FOURCC('Y','U','Y','V')},
46 {VA_FOURCC('U','Y','V','Y')},
47 {VA_FOURCC('B','G','R','A')}
51 vlVaVideoSurfaceSize(vlVaSurface
*p_surf
, int component
,
52 unsigned *width
, unsigned *height
)
54 *width
= p_surf
->templat
.width
;
55 *height
= p_surf
->templat
.height
;
58 if (p_surf
->templat
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
61 } else if (p_surf
->templat
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
)
64 if (p_surf
->templat
.interlaced
)
69 vlVaQueryImageFormats(VADriverContextP ctx
, VAImageFormat
*format_list
, int *num_formats
)
71 struct pipe_screen
*pscreen
;
72 enum pipe_format format
;
76 return VA_STATUS_ERROR_INVALID_CONTEXT
;
78 if (!(format_list
&& num_formats
))
79 return VA_STATUS_ERROR_INVALID_PARAMETER
;
82 pscreen
= VL_VA_PSCREEN(ctx
);
83 for (i
= 0; i
< VL_VA_MAX_IMAGE_FORMATS
; ++i
) {
84 format
= YCbCrToPipe(formats
[i
].fourcc
);
85 if (pscreen
->is_video_format_supported(pscreen
, format
,
86 PIPE_VIDEO_PROFILE_UNKNOWN
,
87 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
))
88 format_list
[(*num_formats
)++] = formats
[i
];
91 return VA_STATUS_SUCCESS
;
95 vlVaCreateImage(VADriverContextP ctx
, VAImageFormat
*format
, int width
, int height
, VAImage
*image
)
103 return VA_STATUS_ERROR_INVALID_CONTEXT
;
105 if (!(format
&& image
&& width
&& height
))
106 return VA_STATUS_ERROR_INVALID_PARAMETER
;
108 drv
= VL_VA_DRIVER(ctx
);
110 img
= CALLOC(1, sizeof(VAImage
));
112 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
113 img
->image_id
= handle_table_add(drv
->htab
, img
);
115 img
->format
= *format
;
117 img
->height
= height
;
121 switch (format
->fourcc
) {
122 case VA_FOURCC('N','V','1','2'):
127 img
->offsets
[1] = w
* h
;
128 img
->data_size
= w
* h
* 3 / 2;
131 case VA_FOURCC('I','4','2','0'):
132 case VA_FOURCC('Y','V','1','2'):
136 img
->pitches
[1] = w
/ 2;
137 img
->offsets
[1] = w
* h
;
138 img
->pitches
[2] = w
/ 2;
139 img
->offsets
[2] = w
* h
* 5 / 4;
140 img
->data_size
= w
* h
* 3 / 2;
143 case VA_FOURCC('U','Y','V','Y'):
144 case VA_FOURCC('Y','U','Y','V'):
146 img
->pitches
[0] = w
* 2;
148 img
->data_size
= w
* h
* 2;
151 case VA_FOURCC('B','G','R','A'):
153 img
->pitches
[0] = w
* 4;
155 img
->data_size
= w
* h
* 4;
159 return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT
;
162 status
= vlVaCreateBuffer(ctx
, 0, VAImageBufferType
,
163 align(img
->data_size
, 16),
165 if (status
!= VA_STATUS_SUCCESS
)
173 vlVaDeriveImage(VADriverContextP ctx
, VASurfaceID surface
, VAImage
*image
)
176 return VA_STATUS_ERROR_INVALID_CONTEXT
;
178 return VA_STATUS_ERROR_UNIMPLEMENTED
;
182 vlVaDestroyImage(VADriverContextP ctx
, VAImageID image
)
187 return VA_STATUS_ERROR_INVALID_CONTEXT
;
189 vaimage
= handle_table_get(VL_VA_DRIVER(ctx
)->htab
, image
);
191 return VA_STATUS_ERROR_INVALID_IMAGE
;
193 handle_table_remove(VL_VA_DRIVER(ctx
)->htab
, image
);
195 return vlVaDestroyBuffer(ctx
, vaimage
->buf
);
199 vlVaSetImagePalette(VADriverContextP ctx
, VAImageID image
, unsigned char *palette
)
202 return VA_STATUS_ERROR_INVALID_CONTEXT
;
204 return VA_STATUS_ERROR_UNIMPLEMENTED
;
208 vlVaGetImage(VADriverContextP ctx
, VASurfaceID surface
, int x
, int y
,
209 unsigned int width
, unsigned int height
, VAImageID image
)
215 struct pipe_sampler_view
**views
;
216 enum pipe_format format
;
217 bool convert
= false;
219 unsigned pitches
[3], i
, j
;
222 return VA_STATUS_ERROR_INVALID_CONTEXT
;
224 drv
= VL_VA_DRIVER(ctx
);
226 surf
= handle_table_get(drv
->htab
, surface
);
227 if (!surf
|| !surf
->buffer
)
228 return VA_STATUS_ERROR_INVALID_SURFACE
;
230 vaimage
= handle_table_get(drv
->htab
, image
);
232 return VA_STATUS_ERROR_INVALID_IMAGE
;
234 img_buf
= handle_table_get(drv
->htab
, vaimage
->buf
);
236 return VA_STATUS_ERROR_INVALID_BUFFER
;
238 format
= YCbCrToPipe(vaimage
->format
.fourcc
);
239 if (format
== PIPE_FORMAT_NONE
)
240 return VA_STATUS_ERROR_OPERATION_FAILED
;
242 if (format
!= surf
->buffer
->buffer_format
) {
243 /* support NV12 to YV12 conversion now only */
244 if (format
== PIPE_FORMAT_YV12
&&
245 surf
->buffer
->buffer_format
== PIPE_FORMAT_NV12
)
248 return VA_STATUS_ERROR_OPERATION_FAILED
;
251 views
= surf
->buffer
->get_sampler_view_planes(surf
->buffer
);
253 return VA_STATUS_ERROR_OPERATION_FAILED
;
255 for (i
= 0; i
< vaimage
->num_planes
; i
++) {
256 data
[i
] = img_buf
->data
+ vaimage
->offsets
[i
];
257 pitches
[i
] = vaimage
->pitches
[i
];
259 if (vaimage
->format
.fourcc
== VA_FOURCC('I','4','2','0')) {
266 pitches
[1] = pitches
[2];
270 for (i
= 0; i
< vaimage
->num_planes
; i
++) {
271 unsigned width
, height
;
272 if (!views
[i
]) continue;
273 vlVaVideoSurfaceSize(surf
, i
, &width
, &height
);
274 for (j
= 0; j
< views
[i
]->texture
->array_size
; ++j
) {
275 struct pipe_box box
= {0, 0, j
, width
, height
, 1};
276 struct pipe_transfer
*transfer
;
278 map
= drv
->pipe
->transfer_map(drv
->pipe
, views
[i
]->texture
, 0,
279 PIPE_TRANSFER_READ
, &box
, &transfer
);
281 return VA_STATUS_ERROR_OPERATION_FAILED
;
283 if (i
== 1 && convert
) {
284 u_copy_nv12_to_yv12(data
, pitches
, i
, j
,
285 transfer
->stride
, views
[i
]->texture
->array_size
,
286 map
, box
.width
, box
.height
);
288 util_copy_rect(data
[i
] + pitches
[i
] * j
,
289 views
[i
]->texture
->format
,
290 pitches
[i
] * views
[i
]->texture
->array_size
, 0, 0,
291 box
.width
, box
.height
, map
, transfer
->stride
, 0, 0);
293 pipe_transfer_unmap(drv
->pipe
, transfer
);
297 return VA_STATUS_SUCCESS
;
301 vlVaPutImage(VADriverContextP ctx
, VASurfaceID surface
, VAImageID image
,
302 int src_x
, int src_y
, unsigned int src_width
, unsigned int src_height
,
303 int dest_x
, int dest_y
, unsigned int dest_width
, unsigned int dest_height
)
309 struct pipe_sampler_view
**views
;
310 enum pipe_format format
;
312 unsigned pitches
[3], i
, j
;
315 return VA_STATUS_ERROR_INVALID_CONTEXT
;
317 drv
= VL_VA_DRIVER(ctx
);
319 surf
= handle_table_get(drv
->htab
, surface
);
320 if (!surf
|| !surf
->buffer
)
321 return VA_STATUS_ERROR_INVALID_SURFACE
;
323 vaimage
= handle_table_get(drv
->htab
, image
);
325 return VA_STATUS_ERROR_INVALID_IMAGE
;
327 img_buf
= handle_table_get(drv
->htab
, vaimage
->buf
);
329 return VA_STATUS_ERROR_INVALID_BUFFER
;
331 format
= YCbCrToPipe(vaimage
->format
.fourcc
);
332 if (format
== PIPE_FORMAT_NONE
)
333 return VA_STATUS_ERROR_OPERATION_FAILED
;
335 if (surf
->buffer
== NULL
|| format
!= surf
->buffer
->buffer_format
) {
337 surf
->buffer
->destroy(surf
->buffer
);
338 surf
->templat
.buffer_format
= format
;
339 surf
->buffer
= drv
->pipe
->create_video_buffer(drv
->pipe
, &surf
->templat
);
341 return VA_STATUS_ERROR_ALLOCATION_FAILED
;
344 views
= surf
->buffer
->get_sampler_view_planes(surf
->buffer
);
346 return VA_STATUS_ERROR_OPERATION_FAILED
;
348 for (i
= 0; i
< vaimage
->num_planes
; i
++) {
349 data
[i
] = img_buf
->data
+ vaimage
->offsets
[i
];
350 pitches
[i
] = vaimage
->pitches
[i
];
352 if (vaimage
->format
.fourcc
== VA_FOURCC('I','4','2','0')) {
359 pitches
[1] = pitches
[2];
363 for (i
= 0; i
< vaimage
->num_planes
; ++i
) {
364 unsigned width
, height
;
365 if (!views
[i
]) continue;
366 vlVaVideoSurfaceSize(surf
, i
, &width
, &height
);
367 for (j
= 0; j
< views
[i
]->texture
->array_size
; ++j
) {
368 struct pipe_box dst_box
= {0, 0, j
, width
, height
, 1};
369 drv
->pipe
->transfer_inline_write(drv
->pipe
, views
[i
]->texture
, 0,
370 PIPE_TRANSFER_WRITE
, &dst_box
,
371 data
[i
] + pitches
[i
] * j
,
372 pitches
[i
] * views
[i
]->texture
->array_size
, 0);
376 return VA_STATUS_SUCCESS
;