state_trackers/vdpau: Test if profile is supported first before trying to create...
[mesa.git] / src / gallium / state_trackers / vdpau / decode.c
1 /**************************************************************************
2 *
3 * Copyright 2010 Thomas Balling Sørensen.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/u_memory.h"
29 #include "util/u_math.h"
30 #include "util/u_debug.h"
31
32 #include "vdpau_private.h"
33
34 /**
35 * Create a VdpDecoder.
36 */
37 VdpStatus
38 vlVdpDecoderCreate(VdpDevice device,
39 VdpDecoderProfile profile,
40 uint32_t width, uint32_t height,
41 uint32_t max_references,
42 VdpDecoder *decoder)
43 {
44 enum pipe_video_profile p_profile;
45 struct pipe_context *pipe;
46 struct pipe_screen *screen;
47 vlVdpDevice *dev;
48 vlVdpDecoder *vldecoder;
49 VdpStatus ret;
50 unsigned i;
51 bool supported;
52
53 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Creating decoder\n");
54
55 if (!decoder)
56 return VDP_STATUS_INVALID_POINTER;
57 *decoder = 0;
58
59 if (!(width && height))
60 return VDP_STATUS_INVALID_VALUE;
61
62 p_profile = ProfileToPipe(profile);
63 if (p_profile == PIPE_VIDEO_PROFILE_UNKNOWN)
64 return VDP_STATUS_INVALID_DECODER_PROFILE;
65
66 dev = vlGetDataHTAB(device);
67 if (!dev)
68 return VDP_STATUS_INVALID_HANDLE;
69
70 pipe = dev->context->pipe;
71 screen = dev->vscreen->pscreen;
72 supported = screen->get_video_param
73 (
74 screen,
75 p_profile,
76 PIPE_VIDEO_CAP_SUPPORTED
77 );
78 if (!supported)
79 return VDP_STATUS_INVALID_DECODER_PROFILE;
80
81 vldecoder = CALLOC(1,sizeof(vlVdpDecoder));
82 if (!vldecoder)
83 return VDP_STATUS_RESOURCES;
84
85 vldecoder->device = dev;
86
87 vldecoder->decoder = pipe->create_video_decoder
88 (
89 pipe, p_profile,
90 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
91 PIPE_VIDEO_CHROMA_FORMAT_420,
92 width, height, max_references
93 );
94
95 if (!vldecoder->decoder) {
96 ret = VDP_STATUS_ERROR;
97 goto error_decoder;
98 }
99
100 vldecoder->num_buffers = pipe->screen->get_video_param
101 (
102 pipe->screen, p_profile,
103 PIPE_VIDEO_CAP_NUM_BUFFERS_DESIRED
104 );
105 vldecoder->cur_buffer = 0;
106
107 vldecoder->buffers = CALLOC(vldecoder->num_buffers, sizeof(void*));
108 if (!vldecoder->buffers)
109 goto error_alloc_buffers;
110
111 for (i = 0; i < vldecoder->num_buffers; ++i) {
112 vldecoder->buffers[i] = vldecoder->decoder->create_buffer(vldecoder->decoder);
113 if (!vldecoder->buffers[i]) {
114 ret = VDP_STATUS_ERROR;
115 goto error_create_buffers;
116 }
117 }
118
119 *decoder = vlAddDataHTAB(vldecoder);
120 if (*decoder == 0) {
121 ret = VDP_STATUS_ERROR;
122 goto error_handle;
123 }
124
125 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoder created succesfully\n");
126
127 return VDP_STATUS_OK;
128
129 error_handle:
130 error_create_buffers:
131
132 for (i = 0; i < vldecoder->num_buffers; ++i)
133 if (vldecoder->buffers[i])
134 vldecoder->decoder->destroy_buffer(vldecoder->decoder, vldecoder->buffers[i]);
135
136 FREE(vldecoder->buffers);
137
138 error_alloc_buffers:
139
140 vldecoder->decoder->destroy(vldecoder->decoder);
141
142 error_decoder:
143 FREE(vldecoder);
144 return ret;
145 }
146
147 /**
148 * Destroy a VdpDecoder.
149 */
150 VdpStatus
151 vlVdpDecoderDestroy(VdpDecoder decoder)
152 {
153 vlVdpDecoder *vldecoder;
154 unsigned i;
155
156 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Destroying decoder\n");
157
158 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
159 if (!vldecoder)
160 return VDP_STATUS_INVALID_HANDLE;
161
162 for (i = 0; i < vldecoder->num_buffers; ++i)
163 if (vldecoder->buffers[i])
164 vldecoder->decoder->destroy_buffer(vldecoder->decoder, vldecoder->buffers[i]);
165
166 FREE(vldecoder->buffers);
167
168 vldecoder->decoder->destroy(vldecoder->decoder);
169
170 FREE(vldecoder);
171
172 return VDP_STATUS_OK;
173 }
174
175 /**
176 * Retrieve the parameters used to create a VdpBitmapSurface.
177 */
178 VdpStatus
179 vlVdpDecoderGetParameters(VdpDecoder decoder,
180 VdpDecoderProfile *profile,
181 uint32_t *width,
182 uint32_t *height)
183 {
184 vlVdpDecoder *vldecoder;
185
186 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoder get parameters called\n");
187
188 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
189 if (!vldecoder)
190 return VDP_STATUS_INVALID_HANDLE;
191
192 *profile = PipeToProfile(vldecoder->decoder->profile);
193 *width = vldecoder->decoder->width;
194 *height = vldecoder->decoder->height;
195
196 return VDP_STATUS_OK;
197 }
198
199 /**
200 * Decode a mpeg 1/2 video.
201 */
202 static VdpStatus
203 vlVdpDecoderRenderMpeg12(struct pipe_video_decoder *decoder,
204 VdpPictureInfoMPEG1Or2 *picture_info,
205 uint32_t bitstream_buffer_count,
206 VdpBitstreamBuffer const *bitstream_buffers)
207 {
208 struct pipe_mpeg12_picture_desc picture;
209 struct pipe_mpeg12_quant_matrix quant;
210 struct pipe_video_buffer *ref_frames[2];
211 unsigned i;
212
213 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding MPEG12\n");
214
215 i = 0;
216
217 /* if surfaces equals VDP_STATUS_INVALID_HANDLE, they are not used */
218 if (picture_info->forward_reference != VDP_INVALID_HANDLE) {
219 ref_frames[i] = ((vlVdpSurface *)vlGetDataHTAB(picture_info->forward_reference))->video_buffer;
220 if (!ref_frames[i])
221 return VDP_STATUS_INVALID_HANDLE;
222 ++i;
223 }
224
225 if (picture_info->backward_reference != VDP_INVALID_HANDLE) {
226 ref_frames[i] = ((vlVdpSurface *)vlGetDataHTAB(picture_info->backward_reference))->video_buffer;
227 if (!ref_frames[i])
228 return VDP_STATUS_INVALID_HANDLE;
229 ++i;
230 }
231
232 decoder->set_reference_frames(decoder, ref_frames, i);
233
234 memset(&picture, 0, sizeof(picture));
235 picture.base.profile = decoder->profile;
236 picture.picture_coding_type = picture_info->picture_coding_type;
237 picture.picture_structure = picture_info->picture_structure;
238 picture.frame_pred_frame_dct = picture_info->frame_pred_frame_dct;
239 picture.q_scale_type = picture_info->q_scale_type;
240 picture.alternate_scan = picture_info->alternate_scan;
241 picture.intra_vlc_format = picture_info->intra_vlc_format;
242 picture.concealment_motion_vectors = picture_info->concealment_motion_vectors;
243 picture.intra_dc_precision = picture_info->intra_dc_precision;
244 picture.f_code[0][0] = picture_info->f_code[0][0] - 1;
245 picture.f_code[0][1] = picture_info->f_code[0][1] - 1;
246 picture.f_code[1][0] = picture_info->f_code[1][0] - 1;
247 picture.f_code[1][1] = picture_info->f_code[1][1] - 1;
248 picture.num_slices = picture_info->slice_count;
249
250 decoder->set_picture_parameters(decoder, &picture.base);
251
252 memset(&quant, 0, sizeof(quant));
253 quant.base.codec = PIPE_VIDEO_CODEC_MPEG12;
254 quant.intra_matrix = picture_info->intra_quantizer_matrix;
255 quant.non_intra_matrix = picture_info->non_intra_quantizer_matrix;
256
257 decoder->set_quant_matrix(decoder, &quant.base);
258
259 decoder->begin_frame(decoder);
260
261 for (i = 0; i < bitstream_buffer_count; ++i)
262 decoder->decode_bitstream(decoder, bitstream_buffers[i].bitstream_bytes,
263 bitstream_buffers[i].bitstream);
264
265 decoder->end_frame(decoder);
266
267 return VDP_STATUS_OK;
268 }
269
270 /**
271 * Decode a compressed field/frame and render the result into a VdpVideoSurface.
272 */
273 VdpStatus
274 vlVdpDecoderRender(VdpDecoder decoder,
275 VdpVideoSurface target,
276 VdpPictureInfo const *picture_info,
277 uint32_t bitstream_buffer_count,
278 VdpBitstreamBuffer const *bitstream_buffers)
279 {
280 vlVdpDecoder *vldecoder;
281 vlVdpSurface *vlsurf;
282
283 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding\n");
284
285 if (!(picture_info && bitstream_buffers))
286 return VDP_STATUS_INVALID_POINTER;
287
288 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
289 if (!vldecoder)
290 return VDP_STATUS_INVALID_HANDLE;
291
292 vlsurf = (vlVdpSurface *)vlGetDataHTAB(target);
293 if (!vlsurf)
294 return VDP_STATUS_INVALID_HANDLE;
295
296 if (vlsurf->device != vldecoder->device)
297 return VDP_STATUS_HANDLE_DEVICE_MISMATCH;
298
299 if (vlsurf->video_buffer->chroma_format != vldecoder->decoder->chroma_format)
300 // TODO: Recreate decoder with correct chroma
301 return VDP_STATUS_INVALID_CHROMA_TYPE;
302
303 // TODO: Right now only mpeg 1 & 2 videos are supported.
304 switch (vldecoder->decoder->profile) {
305 case PIPE_VIDEO_PROFILE_MPEG1:
306 case PIPE_VIDEO_PROFILE_MPEG2_SIMPLE:
307 case PIPE_VIDEO_PROFILE_MPEG2_MAIN:
308 ++vldecoder->cur_buffer;
309 vldecoder->cur_buffer %= vldecoder->num_buffers;
310
311 vldecoder->decoder->set_decode_buffer(vldecoder->decoder, vldecoder->buffers[vldecoder->cur_buffer]);
312 vldecoder->decoder->set_decode_target(vldecoder->decoder, vlsurf->video_buffer);
313
314 return vlVdpDecoderRenderMpeg12(vldecoder->decoder, (VdpPictureInfoMPEG1Or2 *)picture_info,
315 bitstream_buffer_count, bitstream_buffers);
316 break;
317
318 default:
319 return VDP_STATUS_INVALID_DECODER_PROFILE;
320 }
321 }