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_ENTRYPOINT_BITSTREAM
,
79 PIPE_VIDEO_CAP_PREFERED_FORMAT
81 p_surf
->templat
.chroma_format
= ChromaToPipe(chroma_type
);
82 p_surf
->templat
.width
= width
;
83 p_surf
->templat
.height
= height
;
84 p_surf
->templat
.interlaced
= pipe
->screen
->get_video_param
87 PIPE_VIDEO_PROFILE_UNKNOWN
,
88 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
,
89 PIPE_VIDEO_CAP_PREFERS_INTERLACED
91 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
92 vlVdpVideoSurfaceClear(p_surf
);
93 pipe_mutex_unlock(dev
->mutex
);
95 *surface
= vlAddDataHTAB(p_surf
);
97 ret
= VDP_STATUS_ERROR
;
101 return VDP_STATUS_OK
;
104 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
115 * Destroy a VdpVideoSurface.
118 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface
)
120 vlVdpSurface
*p_surf
;
122 p_surf
= (vlVdpSurface
*)vlGetDataHTAB((vlHandle
)surface
);
124 return VDP_STATUS_INVALID_HANDLE
;
126 pipe_mutex_lock(p_surf
->device
->mutex
);
127 if (p_surf
->video_buffer
)
128 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
129 pipe_mutex_unlock(p_surf
->device
->mutex
);
131 vlRemoveDataHTAB(surface
);
134 return VDP_STATUS_OK
;
138 * Retrieve the parameters used to create a VdpVideoSurface.
141 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface
,
142 VdpChromaType
*chroma_type
,
143 uint32_t *width
, uint32_t *height
)
145 if (!(width
&& height
&& chroma_type
))
146 return VDP_STATUS_INVALID_POINTER
;
148 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
150 return VDP_STATUS_INVALID_HANDLE
;
152 if (p_surf
->video_buffer
) {
153 *width
= p_surf
->video_buffer
->width
;
154 *height
= p_surf
->video_buffer
->height
;
155 *chroma_type
= PipeToChroma(p_surf
->video_buffer
->chroma_format
);
157 *width
= p_surf
->templat
.width
;
158 *height
= p_surf
->templat
.height
;
159 *chroma_type
= PipeToChroma(p_surf
->templat
.chroma_format
);
162 return VDP_STATUS_OK
;
166 vlVdpVideoSurfaceSize(vlVdpSurface
*p_surf
, int component
,
167 unsigned *width
, unsigned *height
)
169 *width
= p_surf
->templat
.width
;
170 *height
= p_surf
->templat
.height
;
173 if (p_surf
->templat
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
176 } else if (p_surf
->templat
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
) {
180 if (p_surf
->templat
.interlaced
)
185 * Copy image data from a VdpVideoSurface to application memory in a specified
189 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface
,
190 VdpYCbCrFormat destination_ycbcr_format
,
191 void *const *destination_data
,
192 uint32_t const *destination_pitches
)
194 vlVdpSurface
*vlsurface
;
195 struct pipe_context
*pipe
;
196 enum pipe_format format
;
197 struct pipe_sampler_view
**sampler_views
;
200 vlsurface
= vlGetDataHTAB(surface
);
202 return VDP_STATUS_INVALID_HANDLE
;
204 pipe
= vlsurface
->device
->context
;
206 return VDP_STATUS_INVALID_HANDLE
;
208 format
= FormatYCBCRToPipe(destination_ycbcr_format
);
209 if (format
== PIPE_FORMAT_NONE
)
210 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT
;
212 if (vlsurface
->video_buffer
== NULL
|| format
!= vlsurface
->video_buffer
->buffer_format
)
213 return VDP_STATUS_NO_IMPLEMENTATION
; /* TODO We don't support conversion (yet) */
215 pipe_mutex_lock(vlsurface
->device
->mutex
);
216 sampler_views
= vlsurface
->video_buffer
->get_sampler_view_planes(vlsurface
->video_buffer
);
217 if (!sampler_views
) {
218 pipe_mutex_unlock(vlsurface
->device
->mutex
);
219 return VDP_STATUS_RESOURCES
;
222 for (i
= 0; i
< 3; ++i
) {
223 unsigned width
, height
;
224 struct pipe_sampler_view
*sv
= sampler_views
[i
];
227 vlVdpVideoSurfaceSize(vlsurface
, i
, &width
, &height
);
229 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
230 struct pipe_box box
= {
234 struct pipe_transfer
*transfer
;
237 map
= pipe
->transfer_map(pipe
, sv
->texture
, 0,
238 PIPE_TRANSFER_READ
, &box
, &transfer
);
240 pipe_mutex_unlock(vlsurface
->device
->mutex
);
241 return VDP_STATUS_RESOURCES
;
244 util_copy_rect(destination_data
[i
] + destination_pitches
[i
] * j
, sv
->texture
->format
,
245 destination_pitches
[i
] * sv
->texture
->array_size
, 0, 0,
246 box
.width
, box
.height
, map
, transfer
->stride
, 0, 0);
248 pipe_transfer_unmap(pipe
, transfer
);
251 pipe_mutex_unlock(vlsurface
->device
->mutex
);
253 return VDP_STATUS_OK
;
257 * Copy image data from application memory in a specific YCbCr format to
261 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface
,
262 VdpYCbCrFormat source_ycbcr_format
,
263 void const *const *source_data
,
264 uint32_t const *source_pitches
)
266 enum pipe_format pformat
= FormatYCBCRToPipe(source_ycbcr_format
);
267 struct pipe_context
*pipe
;
268 struct pipe_sampler_view
**sampler_views
;
271 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
273 return VDP_STATUS_INVALID_HANDLE
;
275 pipe
= p_surf
->device
->context
;
277 return VDP_STATUS_INVALID_HANDLE
;
279 pipe_mutex_lock(p_surf
->device
->mutex
);
280 if (p_surf
->video_buffer
== NULL
|| pformat
!= p_surf
->video_buffer
->buffer_format
) {
282 /* destroy the old one */
283 if (p_surf
->video_buffer
)
284 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
286 /* adjust the template parameters */
287 p_surf
->templat
.buffer_format
= pformat
;
289 /* and try to create the video buffer with the new format */
290 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
292 /* stil no luck? ok forget it we don't support it */
293 if (!p_surf
->video_buffer
) {
294 pipe_mutex_unlock(p_surf
->device
->mutex
);
295 return VDP_STATUS_NO_IMPLEMENTATION
;
297 vlVdpVideoSurfaceClear(p_surf
);
300 sampler_views
= p_surf
->video_buffer
->get_sampler_view_planes(p_surf
->video_buffer
);
301 if (!sampler_views
) {
302 pipe_mutex_unlock(p_surf
->device
->mutex
);
303 return VDP_STATUS_RESOURCES
;
306 for (i
= 0; i
< 3; ++i
) {
307 unsigned width
, height
;
308 struct pipe_sampler_view
*sv
= sampler_views
[i
];
309 if (!sv
|| !source_pitches
[i
]) continue;
311 vlVdpVideoSurfaceSize(p_surf
, i
, &width
, &height
);
313 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
314 struct pipe_box dst_box
= {
319 pipe
->transfer_inline_write(pipe
, sv
->texture
, 0,
320 PIPE_TRANSFER_WRITE
, &dst_box
,
321 source_data
[i
] + source_pitches
[i
] * j
,
322 source_pitches
[i
] * sv
->texture
->array_size
,
326 pipe_mutex_unlock(p_surf
->device
->mutex
);
328 return VDP_STATUS_OK
;
332 * Helper function to initially clear the VideoSurface after (re-)creation
335 vlVdpVideoSurfaceClear(vlVdpSurface
*vlsurf
)
337 struct pipe_context
*pipe
= vlsurf
->device
->context
;
338 struct pipe_surface
**surfaces
;
341 if (!vlsurf
->video_buffer
)
344 surfaces
= vlsurf
->video_buffer
->get_surfaces(vlsurf
->video_buffer
);
345 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
346 union pipe_color_union c
= {};
351 if (i
> !!vlsurf
->templat
.interlaced
)
352 c
.f
[0] = c
.f
[1] = c
.f
[2] = c
.f
[3] = 0.5f
;
354 pipe
->clear_render_target(pipe
, surfaces
[i
], &c
, 0, 0,
355 surfaces
[i
]->width
, surfaces
[i
]->height
);
357 pipe
->flush(pipe
, NULL
, 0);