vdpau/vl 422 chroma width/height mix up
[mesa.git] / src / gallium / state_trackers / vdpau / surface.c
1 /**************************************************************************
2 *
3 * Copyright 2010 Thomas Balling Sørensen.
4 * Copyright 2011 Christian König.
5 * All Rights Reserved.
6 *
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:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
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.
26 *
27 **************************************************************************/
28
29 #include <assert.h>
30
31 #include "pipe/p_state.h"
32
33 #include "util/u_memory.h"
34 #include "util/u_debug.h"
35 #include "util/u_rect.h"
36 #include "vl/vl_defines.h"
37
38 #include "vdpau_private.h"
39
40 /**
41 * Create a VdpVideoSurface.
42 */
43 VdpStatus
44 vlVdpVideoSurfaceCreate(VdpDevice device, VdpChromaType chroma_type,
45 uint32_t width, uint32_t height,
46 VdpVideoSurface *surface)
47 {
48 struct pipe_context *pipe;
49 vlVdpSurface *p_surf;
50 VdpStatus ret;
51
52 if (!(width && height)) {
53 ret = VDP_STATUS_INVALID_SIZE;
54 goto inv_size;
55 }
56
57 p_surf = CALLOC(1, sizeof(vlVdpSurface));
58 if (!p_surf) {
59 ret = VDP_STATUS_RESOURCES;
60 goto no_res;
61 }
62
63 vlVdpDevice *dev = vlGetDataHTAB(device);
64 if (!dev) {
65 ret = VDP_STATUS_INVALID_HANDLE;
66 goto inv_device;
67 }
68
69 p_surf->device = dev;
70 pipe = dev->context;
71
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
75 (
76 pipe->screen,
77 PIPE_VIDEO_PROFILE_UNKNOWN,
78 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
79 PIPE_VIDEO_CAP_PREFERED_FORMAT
80 );
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
85 (
86 pipe->screen,
87 PIPE_VIDEO_PROFILE_UNKNOWN,
88 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
89 PIPE_VIDEO_CAP_PREFERS_INTERLACED
90 );
91 p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
92 vlVdpVideoSurfaceClear(p_surf);
93 pipe_mutex_unlock(dev->mutex);
94
95 *surface = vlAddDataHTAB(p_surf);
96 if (*surface == 0) {
97 ret = VDP_STATUS_ERROR;
98 goto no_handle;
99 }
100
101 return VDP_STATUS_OK;
102
103 no_handle:
104 p_surf->video_buffer->destroy(p_surf->video_buffer);
105
106 inv_device:
107 FREE(p_surf);
108
109 no_res:
110 inv_size:
111 return ret;
112 }
113
114 /**
115 * Destroy a VdpVideoSurface.
116 */
117 VdpStatus
118 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface)
119 {
120 vlVdpSurface *p_surf;
121
122 p_surf = (vlVdpSurface *)vlGetDataHTAB((vlHandle)surface);
123 if (!p_surf)
124 return VDP_STATUS_INVALID_HANDLE;
125
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);
130
131 vlRemoveDataHTAB(surface);
132 FREE(p_surf);
133
134 return VDP_STATUS_OK;
135 }
136
137 /**
138 * Retrieve the parameters used to create a VdpVideoSurface.
139 */
140 VdpStatus
141 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface,
142 VdpChromaType *chroma_type,
143 uint32_t *width, uint32_t *height)
144 {
145 if (!(width && height && chroma_type))
146 return VDP_STATUS_INVALID_POINTER;
147
148 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
149 if (!p_surf)
150 return VDP_STATUS_INVALID_HANDLE;
151
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);
156 } else {
157 *width = p_surf->templat.width;
158 *height = p_surf->templat.height;
159 *chroma_type = PipeToChroma(p_surf->templat.chroma_format);
160 }
161
162 return VDP_STATUS_OK;
163 }
164
165 static void
166 vlVdpVideoSurfaceSize(vlVdpSurface *p_surf, int component,
167 unsigned *width, unsigned *height)
168 {
169 *width = p_surf->templat.width;
170 *height = p_surf->templat.height;
171
172 if (component > 0) {
173 if (p_surf->templat.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
174 *width /= 2;
175 *height /= 2;
176 } else if (p_surf->templat.chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
177 *width /= 2;
178 }
179 }
180 if (p_surf->templat.interlaced)
181 *height /= 2;
182 }
183
184 /**
185 * Copy image data from a VdpVideoSurface to application memory in a specified
186 * YCbCr format.
187 */
188 VdpStatus
189 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface,
190 VdpYCbCrFormat destination_ycbcr_format,
191 void *const *destination_data,
192 uint32_t const *destination_pitches)
193 {
194 vlVdpSurface *vlsurface;
195 struct pipe_context *pipe;
196 enum pipe_format format;
197 struct pipe_sampler_view **sampler_views;
198 unsigned i, j;
199
200 vlsurface = vlGetDataHTAB(surface);
201 if (!vlsurface)
202 return VDP_STATUS_INVALID_HANDLE;
203
204 pipe = vlsurface->device->context;
205 if (!pipe)
206 return VDP_STATUS_INVALID_HANDLE;
207
208 format = FormatYCBCRToPipe(destination_ycbcr_format);
209 if (format == PIPE_FORMAT_NONE)
210 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT;
211
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) */
214
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;
220 }
221
222 for (i = 0; i < 3; ++i) {
223 unsigned width, height;
224 struct pipe_sampler_view *sv = sampler_views[i];
225 if (!sv) continue;
226
227 vlVdpVideoSurfaceSize(vlsurface, i, &width, &height);
228
229 for (j = 0; j < sv->texture->array_size; ++j) {
230 struct pipe_box box = {
231 0, 0, j,
232 width, height, 1
233 };
234 struct pipe_transfer *transfer;
235 uint8_t *map;
236
237 map = pipe->transfer_map(pipe, sv->texture, 0,
238 PIPE_TRANSFER_READ, &box, &transfer);
239 if (!map) {
240 pipe_mutex_unlock(vlsurface->device->mutex);
241 return VDP_STATUS_RESOURCES;
242 }
243
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);
247
248 pipe_transfer_unmap(pipe, transfer);
249 }
250 }
251 pipe_mutex_unlock(vlsurface->device->mutex);
252
253 return VDP_STATUS_OK;
254 }
255
256 /**
257 * Copy image data from application memory in a specific YCbCr format to
258 * a VdpVideoSurface.
259 */
260 VdpStatus
261 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface,
262 VdpYCbCrFormat source_ycbcr_format,
263 void const *const *source_data,
264 uint32_t const *source_pitches)
265 {
266 enum pipe_format pformat = FormatYCBCRToPipe(source_ycbcr_format);
267 struct pipe_context *pipe;
268 struct pipe_sampler_view **sampler_views;
269 unsigned i, j;
270
271 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
272 if (!p_surf)
273 return VDP_STATUS_INVALID_HANDLE;
274
275 pipe = p_surf->device->context;
276 if (!pipe)
277 return VDP_STATUS_INVALID_HANDLE;
278
279 pipe_mutex_lock(p_surf->device->mutex);
280 if (p_surf->video_buffer == NULL || pformat != p_surf->video_buffer->buffer_format) {
281
282 /* destroy the old one */
283 if (p_surf->video_buffer)
284 p_surf->video_buffer->destroy(p_surf->video_buffer);
285
286 /* adjust the template parameters */
287 p_surf->templat.buffer_format = pformat;
288
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);
291
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;
296 }
297 vlVdpVideoSurfaceClear(p_surf);
298 }
299
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;
304 }
305
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;
310
311 vlVdpVideoSurfaceSize(p_surf, i, &width, &height);
312
313 for (j = 0; j < sv->texture->array_size; ++j) {
314 struct pipe_box dst_box = {
315 0, 0, j,
316 width, height, 1
317 };
318
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,
323 0);
324 }
325 }
326 pipe_mutex_unlock(p_surf->device->mutex);
327
328 return VDP_STATUS_OK;
329 }
330
331 /**
332 * Helper function to initially clear the VideoSurface after (re-)creation
333 */
334 void
335 vlVdpVideoSurfaceClear(vlVdpSurface *vlsurf)
336 {
337 struct pipe_context *pipe = vlsurf->device->context;
338 struct pipe_surface **surfaces;
339 unsigned i;
340
341 if (!vlsurf->video_buffer)
342 return;
343
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 = {};
347
348 if (!surfaces[i])
349 continue;
350
351 if (i > !!vlsurf->templat.interlaced)
352 c.f[0] = c.f[1] = c.f[2] = c.f[3] = 0.5f;
353
354 pipe->clear_render_target(pipe, surfaces[i], &c, 0, 0,
355 surfaces[i]->width, surfaces[i]->height);
356 }
357 pipe->flush(pipe, NULL, 0);
358 }