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 p_surf
= CALLOC(1, sizeof(vlVdpSurface
));
59 ret
= VDP_STATUS_RESOURCES
;
63 vlVdpDevice
*dev
= vlGetDataHTAB(device
);
65 ret
= VDP_STATUS_INVALID_HANDLE
;
72 pipe_mutex_lock(dev
->mutex
);
73 memset(&p_surf
->templat
, 0, sizeof(p_surf
->templat
));
74 p_surf
->templat
.buffer_format
= pipe
->screen
->get_video_param
77 PIPE_VIDEO_PROFILE_UNKNOWN
,
78 PIPE_VIDEO_CAP_PREFERED_FORMAT
80 p_surf
->templat
.chroma_format
= ChromaToPipe(chroma_type
);
81 p_surf
->templat
.width
= width
;
82 p_surf
->templat
.height
= height
;
83 p_surf
->templat
.interlaced
= pipe
->screen
->get_video_param
86 PIPE_VIDEO_PROFILE_UNKNOWN
,
87 PIPE_VIDEO_CAP_PREFERS_INTERLACED
89 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
90 vlVdpVideoSurfaceClear(p_surf
);
91 pipe_mutex_unlock(dev
->mutex
);
93 *surface
= vlAddDataHTAB(p_surf
);
95 ret
= VDP_STATUS_ERROR
;
102 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
113 * Destroy a VdpVideoSurface.
116 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface
)
118 vlVdpSurface
*p_surf
;
120 p_surf
= (vlVdpSurface
*)vlGetDataHTAB((vlHandle
)surface
);
122 return VDP_STATUS_INVALID_HANDLE
;
124 pipe_mutex_lock(p_surf
->device
->mutex
);
125 if (p_surf
->video_buffer
)
126 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
127 pipe_mutex_unlock(p_surf
->device
->mutex
);
129 vlRemoveDataHTAB(surface
);
132 return VDP_STATUS_OK
;
136 * Retrieve the parameters used to create a VdpVideoSurface.
139 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface
,
140 VdpChromaType
*chroma_type
,
141 uint32_t *width
, uint32_t *height
)
143 if (!(width
&& height
&& chroma_type
))
144 return VDP_STATUS_INVALID_POINTER
;
146 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
148 return VDP_STATUS_INVALID_HANDLE
;
150 if (p_surf
->video_buffer
) {
151 *width
= p_surf
->video_buffer
->width
;
152 *height
= p_surf
->video_buffer
->height
;
153 *chroma_type
= PipeToChroma(p_surf
->video_buffer
->chroma_format
);
155 *width
= p_surf
->templat
.width
;
156 *height
= p_surf
->templat
.height
;
157 *chroma_type
= PipeToChroma(p_surf
->templat
.chroma_format
);
160 return VDP_STATUS_OK
;
164 vlVdpVideoSurfaceSize(vlVdpSurface
*p_surf
, int component
,
165 unsigned *width
, unsigned *height
)
167 *width
= p_surf
->templat
.width
;
168 *height
= p_surf
->templat
.height
;
171 if (p_surf
->templat
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
174 } else if (p_surf
->templat
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
) {
178 if (p_surf
->templat
.interlaced
)
183 * Copy image data from a VdpVideoSurface to application memory in a specified
187 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface
,
188 VdpYCbCrFormat destination_ycbcr_format
,
189 void *const *destination_data
,
190 uint32_t const *destination_pitches
)
192 vlVdpSurface
*vlsurface
;
193 struct pipe_context
*pipe
;
194 enum pipe_format format
;
195 struct pipe_sampler_view
**sampler_views
;
198 vlsurface
= vlGetDataHTAB(surface
);
200 return VDP_STATUS_INVALID_HANDLE
;
202 pipe
= vlsurface
->device
->context
;
204 return VDP_STATUS_INVALID_HANDLE
;
206 format
= FormatYCBCRToPipe(destination_ycbcr_format
);
207 if (format
== PIPE_FORMAT_NONE
)
208 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT
;
210 if (vlsurface
->video_buffer
== NULL
|| format
!= vlsurface
->video_buffer
->buffer_format
)
211 return VDP_STATUS_NO_IMPLEMENTATION
; /* TODO We don't support conversion (yet) */
213 pipe_mutex_lock(vlsurface
->device
->mutex
);
214 sampler_views
= vlsurface
->video_buffer
->get_sampler_view_planes(vlsurface
->video_buffer
);
215 if (!sampler_views
) {
216 pipe_mutex_unlock(vlsurface
->device
->mutex
);
217 return VDP_STATUS_RESOURCES
;
220 for (i
= 0; i
< 3; ++i
) {
221 unsigned width
, height
;
222 struct pipe_sampler_view
*sv
= sampler_views
[i
];
225 vlVdpVideoSurfaceSize(vlsurface
, i
, &width
, &height
);
227 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
228 struct pipe_box box
= {
232 struct pipe_transfer
*transfer
;
235 map
= pipe
->transfer_map(pipe
, sv
->texture
, 0,
236 PIPE_TRANSFER_READ
, &box
, &transfer
);
238 pipe_mutex_unlock(vlsurface
->device
->mutex
);
239 return VDP_STATUS_RESOURCES
;
242 util_copy_rect(destination_data
[i
] + destination_pitches
[i
] * j
, sv
->texture
->format
,
243 destination_pitches
[i
] * sv
->texture
->array_size
, 0, 0,
244 box
.width
, box
.height
, map
, transfer
->stride
, 0, 0);
246 pipe_transfer_unmap(pipe
, transfer
);
249 pipe_mutex_unlock(vlsurface
->device
->mutex
);
251 return VDP_STATUS_OK
;
255 * Copy image data from application memory in a specific YCbCr format to
259 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface
,
260 VdpYCbCrFormat source_ycbcr_format
,
261 void const *const *source_data
,
262 uint32_t const *source_pitches
)
264 enum pipe_format pformat
= FormatYCBCRToPipe(source_ycbcr_format
);
265 struct pipe_context
*pipe
;
266 struct pipe_sampler_view
**sampler_views
;
269 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
271 return VDP_STATUS_INVALID_HANDLE
;
273 pipe
= p_surf
->device
->context
;
275 return VDP_STATUS_INVALID_HANDLE
;
277 pipe_mutex_lock(p_surf
->device
->mutex
);
278 if (p_surf
->video_buffer
== NULL
|| pformat
!= p_surf
->video_buffer
->buffer_format
) {
280 /* destroy the old one */
281 if (p_surf
->video_buffer
)
282 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
284 /* adjust the template parameters */
285 p_surf
->templat
.buffer_format
= pformat
;
287 /* and try to create the video buffer with the new format */
288 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
290 /* stil no luck? ok forget it we don't support it */
291 if (!p_surf
->video_buffer
) {
292 pipe_mutex_unlock(p_surf
->device
->mutex
);
293 return VDP_STATUS_NO_IMPLEMENTATION
;
295 vlVdpVideoSurfaceClear(p_surf
);
298 sampler_views
= p_surf
->video_buffer
->get_sampler_view_planes(p_surf
->video_buffer
);
299 if (!sampler_views
) {
300 pipe_mutex_unlock(p_surf
->device
->mutex
);
301 return VDP_STATUS_RESOURCES
;
304 for (i
= 0; i
< 3; ++i
) {
305 unsigned width
, height
;
306 struct pipe_sampler_view
*sv
= sampler_views
[i
];
307 if (!sv
|| !source_pitches
[i
]) continue;
309 vlVdpVideoSurfaceSize(p_surf
, i
, &width
, &height
);
311 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
312 struct pipe_box dst_box
= {
317 pipe
->transfer_inline_write(pipe
, sv
->texture
, 0,
318 PIPE_TRANSFER_WRITE
, &dst_box
,
319 source_data
[i
] + source_pitches
[i
] * j
,
320 source_pitches
[i
] * sv
->texture
->array_size
,
324 pipe_mutex_unlock(p_surf
->device
->mutex
);
326 return VDP_STATUS_OK
;
330 * Helper function to initially clear the VideoSurface after (re-)creation
333 vlVdpVideoSurfaceClear(vlVdpSurface
*vlsurf
)
335 struct pipe_context
*pipe
= vlsurf
->device
->context
;
336 struct pipe_surface
**surfaces
;
339 if (!vlsurf
->video_buffer
)
342 surfaces
= vlsurf
->video_buffer
->get_surfaces(vlsurf
->video_buffer
);
343 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
344 union pipe_color_union c
= {};
349 if (i
> !!vlsurf
->templat
.interlaced
)
350 c
.f
[0] = c
.f
[1] = c
.f
[2] = c
.f
[3] = 0.5f
;
352 pipe
->clear_render_target(pipe
, surfaces
[i
], &c
, 0, 0,
353 surfaces
[i
]->width
, surfaces
[i
]->height
);
355 pipe
->flush(pipe
, NULL
, 0);