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 "util/u_video.h"
38 #include "vl/vl_defines.h"
40 #include "vdpau_private.h"
42 enum getbits_conversion
{
44 CONVERSION_NV12_TO_YV12
,
45 CONVERSION_YV12_TO_NV12
,
46 CONVERSION_SWAP_YUYV_UYVY
,
50 * Create a VdpVideoSurface.
53 vlVdpVideoSurfaceCreate(VdpDevice device
, VdpChromaType chroma_type
,
54 uint32_t width
, uint32_t height
,
55 VdpVideoSurface
*surface
)
57 struct pipe_context
*pipe
;
61 if (!(width
&& height
)) {
62 ret
= VDP_STATUS_INVALID_SIZE
;
66 p_surf
= CALLOC(1, sizeof(vlVdpSurface
));
68 ret
= VDP_STATUS_RESOURCES
;
72 vlVdpDevice
*dev
= vlGetDataHTAB(device
);
74 ret
= VDP_STATUS_INVALID_HANDLE
;
78 DeviceReference(&p_surf
->device
, dev
);
81 pipe_mutex_lock(dev
->mutex
);
82 memset(&p_surf
->templat
, 0, sizeof(p_surf
->templat
));
83 p_surf
->templat
.buffer_format
= pipe
->screen
->get_video_param
86 PIPE_VIDEO_PROFILE_UNKNOWN
,
87 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
,
88 PIPE_VIDEO_CAP_PREFERED_FORMAT
90 p_surf
->templat
.chroma_format
= ChromaToPipe(chroma_type
);
91 p_surf
->templat
.width
= width
;
92 p_surf
->templat
.height
= height
;
93 p_surf
->templat
.interlaced
= pipe
->screen
->get_video_param
96 PIPE_VIDEO_PROFILE_UNKNOWN
,
97 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
,
98 PIPE_VIDEO_CAP_PREFERS_INTERLACED
100 if (p_surf
->templat
.buffer_format
!= PIPE_FORMAT_NONE
)
101 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
103 /* do not mandate early allocation of a video buffer */
104 vlVdpVideoSurfaceClear(p_surf
);
105 pipe_mutex_unlock(dev
->mutex
);
107 *surface
= vlAddDataHTAB(p_surf
);
109 ret
= VDP_STATUS_ERROR
;
113 return VDP_STATUS_OK
;
116 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
119 DeviceReference(&p_surf
->device
, NULL
);
128 * Destroy a VdpVideoSurface.
131 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface
)
133 vlVdpSurface
*p_surf
;
135 p_surf
= (vlVdpSurface
*)vlGetDataHTAB((vlHandle
)surface
);
137 return VDP_STATUS_INVALID_HANDLE
;
139 pipe_mutex_lock(p_surf
->device
->mutex
);
140 if (p_surf
->video_buffer
)
141 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
142 pipe_mutex_unlock(p_surf
->device
->mutex
);
144 vlRemoveDataHTAB(surface
);
145 DeviceReference(&p_surf
->device
, NULL
);
148 return VDP_STATUS_OK
;
152 * Retrieve the parameters used to create a VdpVideoSurface.
155 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface
,
156 VdpChromaType
*chroma_type
,
157 uint32_t *width
, uint32_t *height
)
159 if (!(width
&& height
&& chroma_type
))
160 return VDP_STATUS_INVALID_POINTER
;
162 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
164 return VDP_STATUS_INVALID_HANDLE
;
166 if (p_surf
->video_buffer
) {
167 *width
= p_surf
->video_buffer
->width
;
168 *height
= p_surf
->video_buffer
->height
;
169 *chroma_type
= PipeToChroma(p_surf
->video_buffer
->chroma_format
);
171 *width
= p_surf
->templat
.width
;
172 *height
= p_surf
->templat
.height
;
173 *chroma_type
= PipeToChroma(p_surf
->templat
.chroma_format
);
176 return VDP_STATUS_OK
;
180 vlVdpVideoSurfaceSize(vlVdpSurface
*p_surf
, int component
,
181 unsigned *width
, unsigned *height
)
183 *width
= p_surf
->templat
.width
;
184 *height
= p_surf
->templat
.height
;
186 vl_video_buffer_adjust_size(width
, height
, component
,
187 p_surf
->templat
.chroma_format
,
188 p_surf
->templat
.interlaced
);
192 * Copy image data from a VdpVideoSurface to application memory in a specified
196 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface
,
197 VdpYCbCrFormat destination_ycbcr_format
,
198 void *const *destination_data
,
199 uint32_t const *destination_pitches
)
201 vlVdpSurface
*vlsurface
;
202 struct pipe_context
*pipe
;
203 enum pipe_format format
, buffer_format
;
204 struct pipe_sampler_view
**sampler_views
;
205 enum getbits_conversion conversion
= CONVERSION_NONE
;
208 vlsurface
= vlGetDataHTAB(surface
);
210 return VDP_STATUS_INVALID_HANDLE
;
212 pipe
= vlsurface
->device
->context
;
214 return VDP_STATUS_INVALID_HANDLE
;
216 format
= FormatYCBCRToPipe(destination_ycbcr_format
);
217 if (format
== PIPE_FORMAT_NONE
)
218 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT
;
220 if (vlsurface
->video_buffer
== NULL
)
221 return VDP_STATUS_INVALID_VALUE
;
223 buffer_format
= vlsurface
->video_buffer
->buffer_format
;
224 if (format
!= buffer_format
) {
225 if (format
== PIPE_FORMAT_YV12
&& buffer_format
== PIPE_FORMAT_NV12
)
226 conversion
= CONVERSION_NV12_TO_YV12
;
227 else if (format
== PIPE_FORMAT_NV12
&& buffer_format
== PIPE_FORMAT_YV12
)
228 conversion
= CONVERSION_YV12_TO_NV12
;
229 else if ((format
== PIPE_FORMAT_YUYV
&& buffer_format
== PIPE_FORMAT_UYVY
) ||
230 (format
== PIPE_FORMAT_UYVY
&& buffer_format
== PIPE_FORMAT_YUYV
))
231 conversion
= CONVERSION_SWAP_YUYV_UYVY
;
233 return VDP_STATUS_NO_IMPLEMENTATION
;
236 pipe_mutex_lock(vlsurface
->device
->mutex
);
237 sampler_views
= vlsurface
->video_buffer
->get_sampler_view_planes(vlsurface
->video_buffer
);
238 if (!sampler_views
) {
239 pipe_mutex_unlock(vlsurface
->device
->mutex
);
240 return VDP_STATUS_RESOURCES
;
243 for (i
= 0; i
< 3; ++i
) {
244 unsigned width
, height
;
245 struct pipe_sampler_view
*sv
= sampler_views
[i
];
248 vlVdpVideoSurfaceSize(vlsurface
, i
, &width
, &height
);
250 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
251 struct pipe_box box
= {
255 struct pipe_transfer
*transfer
;
258 map
= pipe
->transfer_map(pipe
, sv
->texture
, 0,
259 PIPE_TRANSFER_READ
, &box
, &transfer
);
261 pipe_mutex_unlock(vlsurface
->device
->mutex
);
262 return VDP_STATUS_RESOURCES
;
265 if (conversion
== CONVERSION_NV12_TO_YV12
&& i
== 1) {
266 u_copy_nv12_to_yv12(destination_data
, destination_pitches
,
267 i
, j
, transfer
->stride
, sv
->texture
->array_size
,
268 map
, box
.width
, box
.height
);
269 } else if (conversion
== CONVERSION_YV12_TO_NV12
&& i
> 0) {
270 u_copy_yv12_to_nv12(destination_data
, destination_pitches
,
271 i
, j
, transfer
->stride
, sv
->texture
->array_size
,
272 map
, box
.width
, box
.height
);
273 } else if (conversion
== CONVERSION_SWAP_YUYV_UYVY
) {
274 u_copy_swap422_packed(destination_data
, destination_pitches
,
275 i
, j
, transfer
->stride
, sv
->texture
->array_size
,
276 map
, box
.width
, box
.height
);
278 util_copy_rect(destination_data
[i
] + destination_pitches
[i
] * j
, sv
->texture
->format
,
279 destination_pitches
[i
] * sv
->texture
->array_size
, 0, 0,
280 box
.width
, box
.height
, map
, transfer
->stride
, 0, 0);
283 pipe_transfer_unmap(pipe
, transfer
);
286 pipe_mutex_unlock(vlsurface
->device
->mutex
);
288 return VDP_STATUS_OK
;
292 * Copy image data from application memory in a specific YCbCr format to
296 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface
,
297 VdpYCbCrFormat source_ycbcr_format
,
298 void const *const *source_data
,
299 uint32_t const *source_pitches
)
301 enum pipe_format pformat
= FormatYCBCRToPipe(source_ycbcr_format
);
302 struct pipe_context
*pipe
;
303 struct pipe_sampler_view
**sampler_views
;
306 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
308 return VDP_STATUS_INVALID_HANDLE
;
310 pipe
= p_surf
->device
->context
;
312 return VDP_STATUS_INVALID_HANDLE
;
314 pipe_mutex_lock(p_surf
->device
->mutex
);
315 if (p_surf
->video_buffer
== NULL
|| pformat
!= p_surf
->video_buffer
->buffer_format
) {
317 /* destroy the old one */
318 if (p_surf
->video_buffer
)
319 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
321 /* adjust the template parameters */
322 p_surf
->templat
.buffer_format
= pformat
;
324 /* and try to create the video buffer with the new format */
325 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
327 /* stil no luck? ok forget it we don't support it */
328 if (!p_surf
->video_buffer
) {
329 pipe_mutex_unlock(p_surf
->device
->mutex
);
330 return VDP_STATUS_NO_IMPLEMENTATION
;
332 vlVdpVideoSurfaceClear(p_surf
);
335 sampler_views
= p_surf
->video_buffer
->get_sampler_view_planes(p_surf
->video_buffer
);
336 if (!sampler_views
) {
337 pipe_mutex_unlock(p_surf
->device
->mutex
);
338 return VDP_STATUS_RESOURCES
;
341 for (i
= 0; i
< 3; ++i
) {
342 unsigned width
, height
;
343 struct pipe_sampler_view
*sv
= sampler_views
[i
];
344 if (!sv
|| !source_pitches
[i
]) continue;
346 vlVdpVideoSurfaceSize(p_surf
, i
, &width
, &height
);
348 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
349 struct pipe_box dst_box
= {
354 pipe
->transfer_inline_write(pipe
, sv
->texture
, 0,
355 PIPE_TRANSFER_WRITE
, &dst_box
,
356 source_data
[i
] + source_pitches
[i
] * j
,
357 source_pitches
[i
] * sv
->texture
->array_size
,
361 pipe_mutex_unlock(p_surf
->device
->mutex
);
363 return VDP_STATUS_OK
;
367 * Helper function to initially clear the VideoSurface after (re-)creation
370 vlVdpVideoSurfaceClear(vlVdpSurface
*vlsurf
)
372 struct pipe_context
*pipe
= vlsurf
->device
->context
;
373 struct pipe_surface
**surfaces
;
376 if (!vlsurf
->video_buffer
)
379 surfaces
= vlsurf
->video_buffer
->get_surfaces(vlsurf
->video_buffer
);
380 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
381 union pipe_color_union c
= {};
386 if (i
> !!vlsurf
->templat
.interlaced
)
387 c
.f
[0] = c
.f
[1] = c
.f
[2] = c
.f
[3] = 0.5f
;
389 pipe
->clear_render_target(pipe
, surfaces
[i
], &c
, 0, 0,
390 surfaces
[i
]->width
, surfaces
[i
]->height
);
392 pipe
->flush(pipe
, NULL
, 0);
396 * Interop to mesa state tracker
398 struct pipe_video_buffer
*vlVdpVideoSurfaceGallium(VdpVideoSurface surface
)
400 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
404 pipe_mutex_lock(p_surf
->device
->mutex
);
405 if (p_surf
->video_buffer
== NULL
) {
406 struct pipe_context
*pipe
= p_surf
->device
->context
;
408 /* try to create a video buffer if we don't already have one */
409 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
411 pipe_mutex_unlock(p_surf
->device
->mutex
);
413 return p_surf
->video_buffer
;