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 TUNGSTEN GRAPHICS 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 "vl/vl_defines.h"
38 #include "vdpau_private.h"
41 * Create a VdpVideoSurface.
44 vlVdpVideoSurfaceCreate(VdpDevice device
, VdpChromaType chroma_type
,
45 uint32_t width
, uint32_t height
,
46 VdpVideoSurface
*surface
)
48 struct pipe_context
*pipe
;
52 if (!(width
&& height
)) {
53 ret
= VDP_STATUS_INVALID_SIZE
;
57 if (!vlCreateHTAB()) {
58 ret
= VDP_STATUS_RESOURCES
;
62 p_surf
= CALLOC(1, sizeof(vlVdpSurface
));
64 ret
= VDP_STATUS_RESOURCES
;
68 vlVdpDevice
*dev
= vlGetDataHTAB(device
);
70 ret
= VDP_STATUS_INVALID_HANDLE
;
77 pipe_mutex_lock(dev
->mutex
);
78 memset(&p_surf
->templat
, 0, sizeof(p_surf
->templat
));
79 p_surf
->templat
.buffer_format
= pipe
->screen
->get_video_param
82 PIPE_VIDEO_PROFILE_UNKNOWN
,
83 PIPE_VIDEO_CAP_PREFERED_FORMAT
85 p_surf
->templat
.chroma_format
= ChromaToPipe(chroma_type
);
86 p_surf
->templat
.width
= width
;
87 p_surf
->templat
.height
= height
;
88 p_surf
->templat
.interlaced
= pipe
->screen
->get_video_param
91 PIPE_VIDEO_PROFILE_UNKNOWN
,
92 PIPE_VIDEO_CAP_PREFERS_INTERLACED
94 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
95 vlVdpVideoSurfaceClear(p_surf
);
96 pipe_mutex_unlock(dev
->mutex
);
98 *surface
= vlAddDataHTAB(p_surf
);
100 ret
= VDP_STATUS_ERROR
;
104 return VDP_STATUS_OK
;
107 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
119 * Destroy a VdpVideoSurface.
122 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface
)
124 vlVdpSurface
*p_surf
;
126 p_surf
= (vlVdpSurface
*)vlGetDataHTAB((vlHandle
)surface
);
128 return VDP_STATUS_INVALID_HANDLE
;
130 pipe_mutex_lock(p_surf
->device
->mutex
);
131 if (p_surf
->video_buffer
)
132 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
133 pipe_mutex_unlock(p_surf
->device
->mutex
);
136 return VDP_STATUS_OK
;
140 * Retrieve the parameters used to create a VdpVideoSurface.
143 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface
,
144 VdpChromaType
*chroma_type
,
145 uint32_t *width
, uint32_t *height
)
147 if (!(width
&& height
&& chroma_type
))
148 return VDP_STATUS_INVALID_POINTER
;
150 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
152 return VDP_STATUS_INVALID_HANDLE
;
154 if (p_surf
->video_buffer
) {
155 *width
= p_surf
->video_buffer
->width
;
156 *height
= p_surf
->video_buffer
->height
;
157 *chroma_type
= PipeToChroma(p_surf
->video_buffer
->chroma_format
);
159 *width
= p_surf
->templat
.width
;
160 *height
= p_surf
->templat
.height
;
161 *chroma_type
= PipeToChroma(p_surf
->templat
.chroma_format
);
164 return VDP_STATUS_OK
;
168 vlVdpVideoSurfaceSize(vlVdpSurface
*p_surf
, int component
,
169 unsigned *width
, unsigned *height
)
171 *width
= p_surf
->templat
.width
;
172 *height
= p_surf
->templat
.height
;
175 if (p_surf
->templat
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
178 } else if (p_surf
->templat
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
) {
181 if (p_surf
->templat
.interlaced
)
187 * Copy image data from a VdpVideoSurface to application memory in a specified
191 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface
,
192 VdpYCbCrFormat destination_ycbcr_format
,
193 void *const *destination_data
,
194 uint32_t const *destination_pitches
)
196 vlVdpSurface
*vlsurface
;
197 struct pipe_context
*pipe
;
198 enum pipe_format format
;
199 struct pipe_sampler_view
**sampler_views
;
202 vlsurface
= vlGetDataHTAB(surface
);
204 return VDP_STATUS_INVALID_HANDLE
;
206 pipe
= vlsurface
->device
->context
;
208 return VDP_STATUS_INVALID_HANDLE
;
210 format
= FormatYCBCRToPipe(destination_ycbcr_format
);
211 if (format
== PIPE_FORMAT_NONE
)
212 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT
;
214 if (vlsurface
->video_buffer
== NULL
|| format
!= vlsurface
->video_buffer
->buffer_format
)
215 return VDP_STATUS_NO_IMPLEMENTATION
; /* TODO We don't support conversion (yet) */
217 pipe_mutex_lock(vlsurface
->device
->mutex
);
218 sampler_views
= vlsurface
->video_buffer
->get_sampler_view_planes(vlsurface
->video_buffer
);
219 if (!sampler_views
) {
220 pipe_mutex_unlock(vlsurface
->device
->mutex
);
221 return VDP_STATUS_RESOURCES
;
224 for (i
= 0; i
< 3; ++i
) {
225 unsigned width
, height
;
226 struct pipe_sampler_view
*sv
= sampler_views
[i
];
229 vlVdpVideoSurfaceSize(vlsurface
, i
, &width
, &height
);
231 for (j
= 0; j
< sv
->texture
->depth0
; ++j
) {
232 struct pipe_box box
= {
236 struct pipe_transfer
*transfer
;
239 map
= pipe
->transfer_map(pipe
, sv
->texture
, 0,
240 PIPE_TRANSFER_READ
, &box
, &transfer
);
242 pipe_mutex_unlock(vlsurface
->device
->mutex
);
243 return VDP_STATUS_RESOURCES
;
246 util_copy_rect(destination_data
[i
] + destination_pitches
[i
] * j
, sv
->texture
->format
,
247 destination_pitches
[i
] * sv
->texture
->depth0
, 0, 0,
248 box
.width
, box
.height
, map
, transfer
->stride
, 0, 0);
250 pipe_transfer_unmap(pipe
, transfer
);
253 pipe_mutex_unlock(vlsurface
->device
->mutex
);
255 return VDP_STATUS_OK
;
259 * Copy image data from application memory in a specific YCbCr format to
263 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface
,
264 VdpYCbCrFormat source_ycbcr_format
,
265 void const *const *source_data
,
266 uint32_t const *source_pitches
)
268 enum pipe_format pformat
= FormatYCBCRToPipe(source_ycbcr_format
);
269 struct pipe_context
*pipe
;
270 struct pipe_sampler_view
**sampler_views
;
274 return VDP_STATUS_RESOURCES
;
276 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
278 return VDP_STATUS_INVALID_HANDLE
;
280 pipe
= p_surf
->device
->context
;
282 return VDP_STATUS_INVALID_HANDLE
;
284 pipe_mutex_lock(p_surf
->device
->mutex
);
285 if (p_surf
->video_buffer
== NULL
|| pformat
!= p_surf
->video_buffer
->buffer_format
) {
287 /* destroy the old one */
288 if (p_surf
->video_buffer
)
289 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
291 /* adjust the template parameters */
292 p_surf
->templat
.buffer_format
= pformat
;
294 /* and try to create the video buffer with the new format */
295 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
297 /* stil no luck? ok forget it we don't support it */
298 if (!p_surf
->video_buffer
) {
299 pipe_mutex_unlock(p_surf
->device
->mutex
);
300 return VDP_STATUS_NO_IMPLEMENTATION
;
302 vlVdpVideoSurfaceClear(p_surf
);
305 sampler_views
= p_surf
->video_buffer
->get_sampler_view_planes(p_surf
->video_buffer
);
306 if (!sampler_views
) {
307 pipe_mutex_unlock(p_surf
->device
->mutex
);
308 return VDP_STATUS_RESOURCES
;
311 for (i
= 0; i
< 3; ++i
) {
312 unsigned width
, height
;
313 struct pipe_sampler_view
*sv
= sampler_views
[i
];
314 if (!sv
|| !source_pitches
[i
]) continue;
316 vlVdpVideoSurfaceSize(p_surf
, i
, &width
, &height
);
318 for (j
= 0; j
< sv
->texture
->depth0
; ++j
) {
319 struct pipe_box dst_box
= {
324 pipe
->transfer_inline_write(pipe
, sv
->texture
, 0,
325 PIPE_TRANSFER_WRITE
, &dst_box
,
326 source_data
[i
] + source_pitches
[i
] * j
,
327 source_pitches
[i
] * sv
->texture
->depth0
,
331 pipe_mutex_unlock(p_surf
->device
->mutex
);
333 return VDP_STATUS_OK
;
337 * Helper function to initially clear the VideoSurface after (re-)creation
340 vlVdpVideoSurfaceClear(vlVdpSurface
*vlsurf
)
342 struct pipe_context
*pipe
= vlsurf
->device
->context
;
343 struct pipe_surface
**surfaces
;
346 if (!vlsurf
->video_buffer
)
349 surfaces
= vlsurf
->video_buffer
->get_surfaces(vlsurf
->video_buffer
);
350 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
351 union pipe_color_union c
= {};
356 if (i
> !!vlsurf
->templat
.interlaced
)
357 c
.f
[0] = c
.f
[1] = c
.f
[2] = c
.f
[3] = 0.5f
;
359 pipe
->clear_render_target(pipe
, surfaces
[i
], &c
, 0, 0,
360 surfaces
[i
]->width
, surfaces
[i
]->height
);
362 pipe
->flush(pipe
, NULL
);