1 /**************************************************************************
3 * Copyright 2010 Thomas Balling Sørensen.
4 * Copyright 2011 Christian König.
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 VMWARE AND/OR ITS SUPPLIERS 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 **************************************************************************/
31 #include "pipe/p_state.h"
33 #include "util/u_memory.h"
34 #include "util/u_debug.h"
35 #include "util/u_rect.h"
36 #include "util/u_surface.h"
37 #include "vl/vl_defines.h"
39 #include "vdpau_private.h"
41 enum getbits_conversion
{
43 CONVERSION_NV12_TO_YV12
,
44 CONVERSION_YV12_TO_NV12
,
45 CONVERSION_SWAP_YUYV_UYVY
,
49 * Create a VdpVideoSurface.
52 vlVdpVideoSurfaceCreate(VdpDevice device
, VdpChromaType chroma_type
,
53 uint32_t width
, uint32_t height
,
54 VdpVideoSurface
*surface
)
56 struct pipe_context
*pipe
;
60 if (!(width
&& height
)) {
61 ret
= VDP_STATUS_INVALID_SIZE
;
65 p_surf
= CALLOC(1, sizeof(vlVdpSurface
));
67 ret
= VDP_STATUS_RESOURCES
;
71 vlVdpDevice
*dev
= vlGetDataHTAB(device
);
73 ret
= VDP_STATUS_INVALID_HANDLE
;
80 pipe_mutex_lock(dev
->mutex
);
81 memset(&p_surf
->templat
, 0, sizeof(p_surf
->templat
));
82 p_surf
->templat
.buffer_format
= pipe
->screen
->get_video_param
85 PIPE_VIDEO_PROFILE_UNKNOWN
,
86 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
,
87 PIPE_VIDEO_CAP_PREFERED_FORMAT
89 p_surf
->templat
.chroma_format
= ChromaToPipe(chroma_type
);
90 p_surf
->templat
.width
= width
;
91 p_surf
->templat
.height
= height
;
92 p_surf
->templat
.interlaced
= pipe
->screen
->get_video_param
95 PIPE_VIDEO_PROFILE_UNKNOWN
,
96 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
,
97 PIPE_VIDEO_CAP_PREFERS_INTERLACED
99 if (p_surf
->templat
.buffer_format
!= PIPE_FORMAT_NONE
)
100 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
102 /* do not mandate early allocation of a video buffer */
103 vlVdpVideoSurfaceClear(p_surf
);
104 pipe_mutex_unlock(dev
->mutex
);
106 *surface
= vlAddDataHTAB(p_surf
);
108 ret
= VDP_STATUS_ERROR
;
112 return VDP_STATUS_OK
;
115 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
126 * Destroy a VdpVideoSurface.
129 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface
)
131 vlVdpSurface
*p_surf
;
133 p_surf
= (vlVdpSurface
*)vlGetDataHTAB((vlHandle
)surface
);
135 return VDP_STATUS_INVALID_HANDLE
;
137 pipe_mutex_lock(p_surf
->device
->mutex
);
138 if (p_surf
->video_buffer
)
139 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
140 pipe_mutex_unlock(p_surf
->device
->mutex
);
142 vlRemoveDataHTAB(surface
);
145 return VDP_STATUS_OK
;
149 * Retrieve the parameters used to create a VdpVideoSurface.
152 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface
,
153 VdpChromaType
*chroma_type
,
154 uint32_t *width
, uint32_t *height
)
156 if (!(width
&& height
&& chroma_type
))
157 return VDP_STATUS_INVALID_POINTER
;
159 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
161 return VDP_STATUS_INVALID_HANDLE
;
163 if (p_surf
->video_buffer
) {
164 *width
= p_surf
->video_buffer
->width
;
165 *height
= p_surf
->video_buffer
->height
;
166 *chroma_type
= PipeToChroma(p_surf
->video_buffer
->chroma_format
);
168 *width
= p_surf
->templat
.width
;
169 *height
= p_surf
->templat
.height
;
170 *chroma_type
= PipeToChroma(p_surf
->templat
.chroma_format
);
173 return VDP_STATUS_OK
;
177 vlVdpVideoSurfaceSize(vlVdpSurface
*p_surf
, int component
,
178 unsigned *width
, unsigned *height
)
180 *width
= p_surf
->templat
.width
;
181 *height
= p_surf
->templat
.height
;
184 if (p_surf
->templat
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
187 } else if (p_surf
->templat
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
) {
191 if (p_surf
->templat
.interlaced
)
196 vlVdpCopyNV12ToYV12(void *const *destination_data
,
197 uint32_t const *destination_pitches
,
198 int src_plane
, int src_field
,
199 int src_stride
, int num_fields
,
201 int width
, int height
)
204 unsigned u_stride
= destination_pitches
[2] * num_fields
;
205 unsigned v_stride
= destination_pitches
[1] * num_fields
;
206 uint8_t *u_dst
= (uint8_t *)destination_data
[2] + destination_pitches
[2] * src_field
;
207 uint8_t *v_dst
= (uint8_t *)destination_data
[1] + destination_pitches
[1] * src_field
;
210 for (y
= 0; y
< height
; y
++) {
211 for (x
= 0; x
< width
; x
++) {
213 v_dst
[x
] = src
[2*x
+1];
222 vlVdpCopyYV12ToNV12(void *const *destination_data
,
223 uint32_t const *destination_pitches
,
224 int src_plane
, int src_field
,
225 int src_stride
, int num_fields
,
227 int width
, int height
)
230 unsigned offset
= 2 - src_plane
;
231 unsigned stride
= destination_pitches
[1] * num_fields
;
232 uint8_t *dst
= (uint8_t *)destination_data
[1] + destination_pitches
[1] * src_field
;
235 for (y
= 0; y
< height
; y
++) {
236 for (x
= 0; x
< 2 * width
; x
+= 2) {
237 dst
[x
+offset
] = src
[x
>>1];
245 vlVdpCopySwap422Packed(void *const *destination_data
,
246 uint32_t const *destination_pitches
,
247 int src_plane
, int src_field
,
248 int src_stride
, int num_fields
,
250 int width
, int height
)
253 unsigned stride
= destination_pitches
[0] * num_fields
;
254 uint8_t *dst
= (uint8_t *)destination_data
[0] + destination_pitches
[0] * src_field
;
257 for (y
= 0; y
< height
; y
++) {
258 for (x
= 0; x
< 4 * width
; x
+= 4) {
270 * Copy image data from a VdpVideoSurface to application memory in a specified
274 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface
,
275 VdpYCbCrFormat destination_ycbcr_format
,
276 void *const *destination_data
,
277 uint32_t const *destination_pitches
)
279 vlVdpSurface
*vlsurface
;
280 struct pipe_context
*pipe
;
281 enum pipe_format format
, buffer_format
;
282 struct pipe_sampler_view
**sampler_views
;
283 enum getbits_conversion conversion
= CONVERSION_NONE
;
286 vlsurface
= vlGetDataHTAB(surface
);
288 return VDP_STATUS_INVALID_HANDLE
;
290 pipe
= vlsurface
->device
->context
;
292 return VDP_STATUS_INVALID_HANDLE
;
294 format
= FormatYCBCRToPipe(destination_ycbcr_format
);
295 if (format
== PIPE_FORMAT_NONE
)
296 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT
;
298 if (vlsurface
->video_buffer
== NULL
)
299 return VDP_STATUS_INVALID_VALUE
;
301 buffer_format
= vlsurface
->video_buffer
->buffer_format
;
302 if (format
!= buffer_format
) {
303 if (format
== PIPE_FORMAT_YV12
&& buffer_format
== PIPE_FORMAT_NV12
)
304 conversion
= CONVERSION_NV12_TO_YV12
;
305 else if (format
== PIPE_FORMAT_NV12
&& buffer_format
== PIPE_FORMAT_YV12
)
306 conversion
= CONVERSION_YV12_TO_NV12
;
307 else if ((format
== PIPE_FORMAT_YUYV
&& buffer_format
== PIPE_FORMAT_UYVY
) ||
308 (format
== PIPE_FORMAT_UYVY
&& buffer_format
== PIPE_FORMAT_YUYV
))
309 conversion
= CONVERSION_SWAP_YUYV_UYVY
;
311 return VDP_STATUS_NO_IMPLEMENTATION
;
314 pipe_mutex_lock(vlsurface
->device
->mutex
);
315 sampler_views
= vlsurface
->video_buffer
->get_sampler_view_planes(vlsurface
->video_buffer
);
316 if (!sampler_views
) {
317 pipe_mutex_unlock(vlsurface
->device
->mutex
);
318 return VDP_STATUS_RESOURCES
;
321 for (i
= 0; i
< 3; ++i
) {
322 unsigned width
, height
;
323 struct pipe_sampler_view
*sv
= sampler_views
[i
];
326 vlVdpVideoSurfaceSize(vlsurface
, i
, &width
, &height
);
328 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
329 struct pipe_box box
= {
333 struct pipe_transfer
*transfer
;
336 map
= pipe
->transfer_map(pipe
, sv
->texture
, 0,
337 PIPE_TRANSFER_READ
, &box
, &transfer
);
339 pipe_mutex_unlock(vlsurface
->device
->mutex
);
340 return VDP_STATUS_RESOURCES
;
343 if (conversion
== CONVERSION_NV12_TO_YV12
&& i
== 1) {
344 vlVdpCopyNV12ToYV12(destination_data
, destination_pitches
,
345 i
, j
, transfer
->stride
, sv
->texture
->array_size
,
346 map
, box
.width
, box
.height
);
347 } else if (conversion
== CONVERSION_YV12_TO_NV12
&& i
> 0) {
348 vlVdpCopyYV12ToNV12(destination_data
, destination_pitches
,
349 i
, j
, transfer
->stride
, sv
->texture
->array_size
,
350 map
, box
.width
, box
.height
);
351 } else if (conversion
== CONVERSION_SWAP_YUYV_UYVY
) {
352 vlVdpCopySwap422Packed(destination_data
, destination_pitches
,
353 i
, j
, transfer
->stride
, sv
->texture
->array_size
,
354 map
, box
.width
, box
.height
);
356 util_copy_rect(destination_data
[i
] + destination_pitches
[i
] * j
, sv
->texture
->format
,
357 destination_pitches
[i
] * sv
->texture
->array_size
, 0, 0,
358 box
.width
, box
.height
, map
, transfer
->stride
, 0, 0);
361 pipe_transfer_unmap(pipe
, transfer
);
364 pipe_mutex_unlock(vlsurface
->device
->mutex
);
366 return VDP_STATUS_OK
;
370 * Copy image data from application memory in a specific YCbCr format to
374 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface
,
375 VdpYCbCrFormat source_ycbcr_format
,
376 void const *const *source_data
,
377 uint32_t const *source_pitches
)
379 enum pipe_format pformat
= FormatYCBCRToPipe(source_ycbcr_format
);
380 struct pipe_context
*pipe
;
381 struct pipe_sampler_view
**sampler_views
;
384 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
386 return VDP_STATUS_INVALID_HANDLE
;
388 pipe
= p_surf
->device
->context
;
390 return VDP_STATUS_INVALID_HANDLE
;
392 pipe_mutex_lock(p_surf
->device
->mutex
);
393 if (p_surf
->video_buffer
== NULL
|| pformat
!= p_surf
->video_buffer
->buffer_format
) {
395 /* destroy the old one */
396 if (p_surf
->video_buffer
)
397 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
399 /* adjust the template parameters */
400 p_surf
->templat
.buffer_format
= pformat
;
402 /* and try to create the video buffer with the new format */
403 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
405 /* stil no luck? ok forget it we don't support it */
406 if (!p_surf
->video_buffer
) {
407 pipe_mutex_unlock(p_surf
->device
->mutex
);
408 return VDP_STATUS_NO_IMPLEMENTATION
;
410 vlVdpVideoSurfaceClear(p_surf
);
413 sampler_views
= p_surf
->video_buffer
->get_sampler_view_planes(p_surf
->video_buffer
);
414 if (!sampler_views
) {
415 pipe_mutex_unlock(p_surf
->device
->mutex
);
416 return VDP_STATUS_RESOURCES
;
419 for (i
= 0; i
< 3; ++i
) {
420 unsigned width
, height
;
421 struct pipe_sampler_view
*sv
= sampler_views
[i
];
422 if (!sv
|| !source_pitches
[i
]) continue;
424 vlVdpVideoSurfaceSize(p_surf
, i
, &width
, &height
);
426 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
427 struct pipe_box dst_box
= {
432 pipe
->transfer_inline_write(pipe
, sv
->texture
, 0,
433 PIPE_TRANSFER_WRITE
, &dst_box
,
434 source_data
[i
] + source_pitches
[i
] * j
,
435 source_pitches
[i
] * sv
->texture
->array_size
,
439 pipe_mutex_unlock(p_surf
->device
->mutex
);
441 return VDP_STATUS_OK
;
445 * Helper function to initially clear the VideoSurface after (re-)creation
448 vlVdpVideoSurfaceClear(vlVdpSurface
*vlsurf
)
450 struct pipe_context
*pipe
= vlsurf
->device
->context
;
451 struct pipe_surface
**surfaces
;
454 if (!vlsurf
->video_buffer
)
457 surfaces
= vlsurf
->video_buffer
->get_surfaces(vlsurf
->video_buffer
);
458 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
459 union pipe_color_union c
= {};
464 if (i
> !!vlsurf
->templat
.interlaced
)
465 c
.f
[0] = c
.f
[1] = c
.f
[2] = c
.f
[3] = 0.5f
;
467 pipe
->clear_render_target(pipe
, surfaces
[i
], &c
, 0, 0,
468 surfaces
[i
]->width
, surfaces
[i
]->height
);
470 pipe
->flush(pipe
, NULL
, 0);
474 * Interop to mesa state tracker
476 struct pipe_video_buffer
*vlVdpVideoSurfaceGallium(VdpVideoSurface surface
)
478 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
482 pipe_mutex_lock(p_surf
->device
->mutex
);
483 if (p_surf
->video_buffer
== NULL
) {
484 struct pipe_context
*pipe
= p_surf
->device
->context
;
486 /* try to create a video buffer if we don't already have one */
487 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
489 pipe_mutex_unlock(p_surf
->device
->mutex
);
491 return p_surf
->video_buffer
;