vdpau: implement vlVdpVideoSurfacePutBitsYCbCr
[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_video_context.h>
32 #include <pipe/p_state.h>
33
34 #include <util/u_memory.h>
35 #include <util/u_debug.h>
36
37 #include "vdpau_private.h"
38
39 VdpStatus
40 vlVdpVideoSurfaceCreate(VdpDevice device, VdpChromaType chroma_type,
41 uint32_t width, uint32_t height,
42 VdpVideoSurface *surface)
43 {
44 vlVdpSurface *p_surf;
45 VdpStatus ret;
46
47 _debug_printf("[VDPAU] Creating a surface\n");
48
49 if (!(width && height)) {
50 ret = VDP_STATUS_INVALID_SIZE;
51 goto inv_size;
52 }
53
54 if (!vlCreateHTAB()) {
55 ret = VDP_STATUS_RESOURCES;
56 goto no_htab;
57 }
58
59 p_surf = CALLOC(1, sizeof(p_surf));
60 if (!p_surf) {
61 ret = VDP_STATUS_RESOURCES;
62 goto no_res;
63 }
64
65 vlVdpDevice *dev = vlGetDataHTAB(device);
66 if (!dev) {
67 ret = VDP_STATUS_INVALID_HANDLE;
68 goto inv_device;
69 }
70
71 p_surf->device = dev;
72 p_surf->video_buffer = dev->context->vpipe->create_buffer(dev->context->vpipe,
73 PIPE_FORMAT_YV12, // most common used
74 ChromaToPipe(chroma_type),
75 width, height);
76
77 *surface = vlAddDataHTAB(p_surf);
78 if (*surface == 0) {
79 ret = VDP_STATUS_ERROR;
80 goto no_handle;
81 }
82
83 return VDP_STATUS_OK;
84
85 no_handle:
86 p_surf->video_buffer->destroy(p_surf->video_buffer);
87
88 inv_device:
89 FREE(p_surf);
90
91 no_res:
92 no_htab:
93 inv_size:
94 return ret;
95 }
96
97 VdpStatus
98 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface)
99 {
100 vlVdpSurface *p_surf;
101
102 p_surf = (vlVdpSurface *)vlGetDataHTAB((vlHandle)surface);
103 if (!p_surf)
104 return VDP_STATUS_INVALID_HANDLE;
105
106 if (p_surf->video_buffer)
107 p_surf->video_buffer->destroy(p_surf->video_buffer);
108
109 FREE(p_surf);
110 return VDP_STATUS_OK;
111 }
112
113 VdpStatus
114 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface,
115 VdpChromaType *chroma_type,
116 uint32_t *width, uint32_t *height)
117 {
118 if (!(width && height && chroma_type))
119 return VDP_STATUS_INVALID_POINTER;
120
121 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
122 if (!p_surf)
123 return VDP_STATUS_INVALID_HANDLE;
124
125 *width = p_surf->video_buffer->width;
126 *height = p_surf->video_buffer->height;
127 *chroma_type = PipeToChroma(p_surf->video_buffer->chroma_format);
128
129 return VDP_STATUS_OK;
130 }
131
132 VdpStatus
133 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface,
134 VdpYCbCrFormat destination_ycbcr_format,
135 void *const *destination_data,
136 uint32_t const *destination_pitches)
137 {
138 if (!vlCreateHTAB())
139 return VDP_STATUS_RESOURCES;
140
141 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
142 if (!p_surf)
143 return VDP_STATUS_INVALID_HANDLE;
144
145 //if (!p_surf->psurface)
146 // return VDP_STATUS_RESOURCES;
147
148 //return VDP_STATUS_OK;
149 return VDP_STATUS_NO_IMPLEMENTATION;
150 }
151
152 VdpStatus
153 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface,
154 VdpYCbCrFormat source_ycbcr_format,
155 void const *const *source_data,
156 uint32_t const *source_pitches)
157 {
158 enum pipe_format pformat = FormatToPipe(source_ycbcr_format);
159 struct pipe_video_context *context;
160 struct pipe_sampler_view **sampler_views;
161 unsigned i;
162
163 if (!vlCreateHTAB())
164 return VDP_STATUS_RESOURCES;
165
166 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
167 if (!p_surf)
168 return VDP_STATUS_INVALID_HANDLE;
169
170 context = p_surf->device->context->vpipe;
171 if (!context)
172 return VDP_STATUS_INVALID_HANDLE;
173
174 if (p_surf->video_buffer == NULL || pformat != p_surf->video_buffer->buffer_format) {
175 assert(0); // TODO Recreate resource
176 return VDP_STATUS_NO_IMPLEMENTATION;
177 }
178
179 sampler_views = p_surf->video_buffer->get_sampler_views(p_surf->video_buffer);
180 if (!sampler_views)
181 return VDP_STATUS_RESOURCES;
182
183 for (i = 0; i < 3; ++i) { //TODO put nr of planes into util format
184 struct pipe_sampler_view *sv = sampler_views[i];
185 struct pipe_box dst_box = { 0, 0, sv->texture->width0, sv->texture->height0 };
186 context->upload_sampler(context, sv, &dst_box, source_data[i], source_pitches[i], 0, 0);
187 }
188
189 return VDP_STATUS_OK;
190 }