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 if (p_surf
->templat
.buffer_format
!= PIPE_FORMAT_NONE
)
92 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
94 /* do not mandate early allocation of a video buffer */
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
);
118 * Destroy a VdpVideoSurface.
121 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface
)
123 vlVdpSurface
*p_surf
;
125 p_surf
= (vlVdpSurface
*)vlGetDataHTAB((vlHandle
)surface
);
127 return VDP_STATUS_INVALID_HANDLE
;
129 pipe_mutex_lock(p_surf
->device
->mutex
);
130 if (p_surf
->video_buffer
)
131 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
132 pipe_mutex_unlock(p_surf
->device
->mutex
);
134 vlRemoveDataHTAB(surface
);
137 return VDP_STATUS_OK
;
141 * Retrieve the parameters used to create a VdpVideoSurface.
144 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface
,
145 VdpChromaType
*chroma_type
,
146 uint32_t *width
, uint32_t *height
)
148 if (!(width
&& height
&& chroma_type
))
149 return VDP_STATUS_INVALID_POINTER
;
151 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
153 return VDP_STATUS_INVALID_HANDLE
;
155 if (p_surf
->video_buffer
) {
156 *width
= p_surf
->video_buffer
->width
;
157 *height
= p_surf
->video_buffer
->height
;
158 *chroma_type
= PipeToChroma(p_surf
->video_buffer
->chroma_format
);
160 *width
= p_surf
->templat
.width
;
161 *height
= p_surf
->templat
.height
;
162 *chroma_type
= PipeToChroma(p_surf
->templat
.chroma_format
);
165 return VDP_STATUS_OK
;
169 vlVdpVideoSurfaceSize(vlVdpSurface
*p_surf
, int component
,
170 unsigned *width
, unsigned *height
)
172 *width
= p_surf
->templat
.width
;
173 *height
= p_surf
->templat
.height
;
176 if (p_surf
->templat
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
179 } else if (p_surf
->templat
.chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
) {
183 if (p_surf
->templat
.interlaced
)
188 * Copy image data from a VdpVideoSurface to application memory in a specified
192 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface
,
193 VdpYCbCrFormat destination_ycbcr_format
,
194 void *const *destination_data
,
195 uint32_t const *destination_pitches
)
197 vlVdpSurface
*vlsurface
;
198 struct pipe_context
*pipe
;
199 enum pipe_format format
;
200 struct pipe_sampler_view
**sampler_views
;
203 vlsurface
= vlGetDataHTAB(surface
);
205 return VDP_STATUS_INVALID_HANDLE
;
207 pipe
= vlsurface
->device
->context
;
209 return VDP_STATUS_INVALID_HANDLE
;
211 format
= FormatYCBCRToPipe(destination_ycbcr_format
);
212 if (format
== PIPE_FORMAT_NONE
)
213 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT
;
215 if (vlsurface
->video_buffer
== NULL
|| format
!= vlsurface
->video_buffer
->buffer_format
)
216 return VDP_STATUS_NO_IMPLEMENTATION
; /* TODO We don't support conversion (yet) */
218 pipe_mutex_lock(vlsurface
->device
->mutex
);
219 sampler_views
= vlsurface
->video_buffer
->get_sampler_view_planes(vlsurface
->video_buffer
);
220 if (!sampler_views
) {
221 pipe_mutex_unlock(vlsurface
->device
->mutex
);
222 return VDP_STATUS_RESOURCES
;
225 for (i
= 0; i
< 3; ++i
) {
226 unsigned width
, height
;
227 struct pipe_sampler_view
*sv
= sampler_views
[i
];
230 vlVdpVideoSurfaceSize(vlsurface
, i
, &width
, &height
);
232 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
233 struct pipe_box box
= {
237 struct pipe_transfer
*transfer
;
240 map
= pipe
->transfer_map(pipe
, sv
->texture
, 0,
241 PIPE_TRANSFER_READ
, &box
, &transfer
);
243 pipe_mutex_unlock(vlsurface
->device
->mutex
);
244 return VDP_STATUS_RESOURCES
;
247 util_copy_rect(destination_data
[i
] + destination_pitches
[i
] * j
, sv
->texture
->format
,
248 destination_pitches
[i
] * sv
->texture
->array_size
, 0, 0,
249 box
.width
, box
.height
, map
, transfer
->stride
, 0, 0);
251 pipe_transfer_unmap(pipe
, transfer
);
254 pipe_mutex_unlock(vlsurface
->device
->mutex
);
256 return VDP_STATUS_OK
;
260 * Copy image data from application memory in a specific YCbCr format to
264 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface
,
265 VdpYCbCrFormat source_ycbcr_format
,
266 void const *const *source_data
,
267 uint32_t const *source_pitches
)
269 enum pipe_format pformat
= FormatYCBCRToPipe(source_ycbcr_format
);
270 struct pipe_context
*pipe
;
271 struct pipe_sampler_view
**sampler_views
;
274 vlVdpSurface
*p_surf
= vlGetDataHTAB(surface
);
276 return VDP_STATUS_INVALID_HANDLE
;
278 pipe
= p_surf
->device
->context
;
280 return VDP_STATUS_INVALID_HANDLE
;
282 pipe_mutex_lock(p_surf
->device
->mutex
);
283 if (p_surf
->video_buffer
== NULL
|| pformat
!= p_surf
->video_buffer
->buffer_format
) {
285 /* destroy the old one */
286 if (p_surf
->video_buffer
)
287 p_surf
->video_buffer
->destroy(p_surf
->video_buffer
);
289 /* adjust the template parameters */
290 p_surf
->templat
.buffer_format
= pformat
;
292 /* and try to create the video buffer with the new format */
293 p_surf
->video_buffer
= pipe
->create_video_buffer(pipe
, &p_surf
->templat
);
295 /* stil no luck? ok forget it we don't support it */
296 if (!p_surf
->video_buffer
) {
297 pipe_mutex_unlock(p_surf
->device
->mutex
);
298 return VDP_STATUS_NO_IMPLEMENTATION
;
300 vlVdpVideoSurfaceClear(p_surf
);
303 sampler_views
= p_surf
->video_buffer
->get_sampler_view_planes(p_surf
->video_buffer
);
304 if (!sampler_views
) {
305 pipe_mutex_unlock(p_surf
->device
->mutex
);
306 return VDP_STATUS_RESOURCES
;
309 for (i
= 0; i
< 3; ++i
) {
310 unsigned width
, height
;
311 struct pipe_sampler_view
*sv
= sampler_views
[i
];
312 if (!sv
|| !source_pitches
[i
]) continue;
314 vlVdpVideoSurfaceSize(p_surf
, i
, &width
, &height
);
316 for (j
= 0; j
< sv
->texture
->array_size
; ++j
) {
317 struct pipe_box dst_box
= {
322 pipe
->transfer_inline_write(pipe
, sv
->texture
, 0,
323 PIPE_TRANSFER_WRITE
, &dst_box
,
324 source_data
[i
] + source_pitches
[i
] * j
,
325 source_pitches
[i
] * sv
->texture
->array_size
,
329 pipe_mutex_unlock(p_surf
->device
->mutex
);
331 return VDP_STATUS_OK
;
335 * Helper function to initially clear the VideoSurface after (re-)creation
338 vlVdpVideoSurfaceClear(vlVdpSurface
*vlsurf
)
340 struct pipe_context
*pipe
= vlsurf
->device
->context
;
341 struct pipe_surface
**surfaces
;
344 if (!vlsurf
->video_buffer
)
347 surfaces
= vlsurf
->video_buffer
->get_surfaces(vlsurf
->video_buffer
);
348 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
) {
349 union pipe_color_union c
= {};
354 if (i
> !!vlsurf
->templat
.interlaced
)
355 c
.f
[0] = c
.f
[1] = c
.f
[2] = c
.f
[3] = 0.5f
;
357 pipe
->clear_render_target(pipe
, surfaces
[i
], &c
, 0, 0,
358 surfaces
[i
]->width
, surfaces
[i
]->height
);
360 pipe
->flush(pipe
, NULL
, 0);