state_trackers/vdpau: Add num_slices to mpeg12 picture structure
[mesa/nouveau-pmpeg.git] / src / gallium / state_trackers / vdpau / decode.c
blob7689ce27793646e05b1c617e311bc0721dd085c9
1 /**************************************************************************
3 * Copyright 2010 Thomas Balling Sørensen.
4 * All Rights Reserved.
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:
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.
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.
26 **************************************************************************/
28 #include "util/u_memory.h"
29 #include "util/u_math.h"
30 #include "util/u_debug.h"
32 #include "vdpau_private.h"
34 /**
35 * Create a VdpDecoder.
37 VdpStatus
38 vlVdpDecoderCreate(VdpDevice device,
39 VdpDecoderProfile profile,
40 uint32_t width, uint32_t height,
41 uint32_t max_references,
42 VdpDecoder *decoder)
44 enum pipe_video_profile p_profile;
45 struct pipe_context *pipe;
46 vlVdpDevice *dev;
47 vlVdpDecoder *vldecoder;
48 VdpStatus ret;
49 unsigned i;
51 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Creating decoder\n");
53 if (!decoder)
54 return VDP_STATUS_INVALID_POINTER;
56 if (!(width && height))
57 return VDP_STATUS_INVALID_VALUE;
59 p_profile = ProfileToPipe(profile);
60 if (p_profile == PIPE_VIDEO_PROFILE_UNKNOWN)
61 return VDP_STATUS_INVALID_DECODER_PROFILE;
63 dev = vlGetDataHTAB(device);
64 if (!dev)
65 return VDP_STATUS_INVALID_HANDLE;
67 pipe = dev->context->pipe;
69 vldecoder = CALLOC(1,sizeof(vlVdpDecoder));
70 if (!vldecoder)
71 return VDP_STATUS_RESOURCES;
73 vldecoder->device = dev;
75 vldecoder->decoder = pipe->create_video_decoder
77 pipe, p_profile,
78 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
79 PIPE_VIDEO_CHROMA_FORMAT_420,
80 width, height, max_references
83 if (!vldecoder->decoder) {
84 ret = VDP_STATUS_ERROR;
85 goto error_decoder;
88 vldecoder->num_buffers = pipe->screen->get_video_param
90 pipe->screen, p_profile,
91 PIPE_VIDEO_CAP_NUM_BUFFERS_DESIRED
93 vldecoder->cur_buffer = 0;
95 vldecoder->buffers = CALLOC(vldecoder->num_buffers, sizeof(void*));
96 if (!vldecoder->buffers)
97 goto error_alloc_buffers;
99 for (i = 0; i < vldecoder->num_buffers; ++i) {
100 vldecoder->buffers[i] = vldecoder->decoder->create_buffer(vldecoder->decoder);
101 if (!vldecoder->buffers[i]) {
102 ret = VDP_STATUS_ERROR;
103 goto error_create_buffers;
107 *decoder = vlAddDataHTAB(vldecoder);
108 if (*decoder == 0) {
109 ret = VDP_STATUS_ERROR;
110 goto error_handle;
113 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoder created succesfully\n");
115 return VDP_STATUS_OK;
117 error_handle:
118 error_create_buffers:
120 for (i = 0; i < vldecoder->num_buffers; ++i)
121 if (vldecoder->buffers[i])
122 vldecoder->decoder->destroy_buffer(vldecoder->decoder, vldecoder->buffers[i]);
124 FREE(vldecoder->buffers);
126 error_alloc_buffers:
128 vldecoder->decoder->destroy(vldecoder->decoder);
130 error_decoder:
131 FREE(vldecoder);
132 return ret;
136 * Destroy a VdpDecoder.
138 VdpStatus
139 vlVdpDecoderDestroy(VdpDecoder decoder)
141 vlVdpDecoder *vldecoder;
142 unsigned i;
144 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Destroying decoder\n");
146 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
147 if (!vldecoder)
148 return VDP_STATUS_INVALID_HANDLE;
150 for (i = 0; i < vldecoder->num_buffers; ++i)
151 if (vldecoder->buffers[i])
152 vldecoder->decoder->destroy_buffer(vldecoder->decoder, vldecoder->buffers[i]);
154 FREE(vldecoder->buffers);
156 vldecoder->decoder->destroy(vldecoder->decoder);
158 FREE(vldecoder);
160 return VDP_STATUS_OK;
164 * Retrieve the parameters used to create a VdpBitmapSurface.
166 VdpStatus
167 vlVdpDecoderGetParameters(VdpDecoder decoder,
168 VdpDecoderProfile *profile,
169 uint32_t *width,
170 uint32_t *height)
172 vlVdpDecoder *vldecoder;
174 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoder get parameters called\n");
176 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
177 if (!vldecoder)
178 return VDP_STATUS_INVALID_HANDLE;
180 *profile = PipeToProfile(vldecoder->decoder->profile);
181 *width = vldecoder->decoder->width;
182 *height = vldecoder->decoder->height;
184 return VDP_STATUS_OK;
188 * Decode a mpeg 1/2 video.
190 static VdpStatus
191 vlVdpDecoderRenderMpeg12(struct pipe_video_decoder *decoder,
192 VdpPictureInfoMPEG1Or2 *picture_info,
193 uint32_t bitstream_buffer_count,
194 VdpBitstreamBuffer const *bitstream_buffers)
196 struct pipe_mpeg12_picture_desc picture;
197 struct pipe_mpeg12_quant_matrix quant;
198 struct pipe_video_buffer *ref_frames[2];
199 unsigned i;
201 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding MPEG12\n");
203 i = 0;
205 /* if surfaces equals VDP_STATUS_INVALID_HANDLE, they are not used */
206 if (picture_info->forward_reference != VDP_INVALID_HANDLE) {
207 ref_frames[i] = ((vlVdpSurface *)vlGetDataHTAB(picture_info->forward_reference))->video_buffer;
208 if (!ref_frames[i])
209 return VDP_STATUS_INVALID_HANDLE;
210 ++i;
213 if (picture_info->backward_reference != VDP_INVALID_HANDLE) {
214 ref_frames[i] = ((vlVdpSurface *)vlGetDataHTAB(picture_info->backward_reference))->video_buffer;
215 if (!ref_frames[i])
216 return VDP_STATUS_INVALID_HANDLE;
217 ++i;
220 decoder->set_reference_frames(decoder, ref_frames, i);
222 memset(&picture, 0, sizeof(picture));
223 picture.base.profile = decoder->profile;
224 picture.picture_coding_type = picture_info->picture_coding_type;
225 picture.picture_structure = picture_info->picture_structure;
226 picture.frame_pred_frame_dct = picture_info->frame_pred_frame_dct;
227 picture.q_scale_type = picture_info->q_scale_type;
228 picture.alternate_scan = picture_info->alternate_scan;
229 picture.intra_vlc_format = picture_info->intra_vlc_format;
230 picture.concealment_motion_vectors = picture_info->concealment_motion_vectors;
231 picture.intra_dc_precision = picture_info->intra_dc_precision;
232 picture.f_code[0][0] = picture_info->f_code[0][0] - 1;
233 picture.f_code[0][1] = picture_info->f_code[0][1] - 1;
234 picture.f_code[1][0] = picture_info->f_code[1][0] - 1;
235 picture.f_code[1][1] = picture_info->f_code[1][1] - 1;
236 picture.num_slices = picture_info->slice_count;
238 decoder->set_picture_parameters(decoder, &picture.base);
240 memset(&quant, 0, sizeof(quant));
241 quant.base.codec = PIPE_VIDEO_CODEC_MPEG12;
242 quant.intra_matrix = picture_info->intra_quantizer_matrix;
243 quant.non_intra_matrix = picture_info->non_intra_quantizer_matrix;
245 decoder->set_quant_matrix(decoder, &quant.base);
247 decoder->begin_frame(decoder);
249 for (i = 0; i < bitstream_buffer_count; ++i)
250 decoder->decode_bitstream(decoder, bitstream_buffers[i].bitstream_bytes,
251 bitstream_buffers[i].bitstream);
253 decoder->end_frame(decoder);
255 return VDP_STATUS_OK;
259 * Decode a compressed field/frame and render the result into a VdpVideoSurface.
261 VdpStatus
262 vlVdpDecoderRender(VdpDecoder decoder,
263 VdpVideoSurface target,
264 VdpPictureInfo const *picture_info,
265 uint32_t bitstream_buffer_count,
266 VdpBitstreamBuffer const *bitstream_buffers)
268 vlVdpDecoder *vldecoder;
269 vlVdpSurface *vlsurf;
271 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding\n");
273 if (!(picture_info && bitstream_buffers))
274 return VDP_STATUS_INVALID_POINTER;
276 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
277 if (!vldecoder)
278 return VDP_STATUS_INVALID_HANDLE;
280 vlsurf = (vlVdpSurface *)vlGetDataHTAB(target);
281 if (!vlsurf)
282 return VDP_STATUS_INVALID_HANDLE;
284 if (vlsurf->device != vldecoder->device)
285 return VDP_STATUS_HANDLE_DEVICE_MISMATCH;
287 if (vlsurf->video_buffer->chroma_format != vldecoder->decoder->chroma_format)
288 // TODO: Recreate decoder with correct chroma
289 return VDP_STATUS_INVALID_CHROMA_TYPE;
291 // TODO: Right now only mpeg 1 & 2 videos are supported.
292 switch (vldecoder->decoder->profile) {
293 case PIPE_VIDEO_PROFILE_MPEG1:
294 case PIPE_VIDEO_PROFILE_MPEG2_SIMPLE:
295 case PIPE_VIDEO_PROFILE_MPEG2_MAIN:
296 ++vldecoder->cur_buffer;
297 vldecoder->cur_buffer %= vldecoder->num_buffers;
299 vldecoder->decoder->set_decode_buffer(vldecoder->decoder, vldecoder->buffers[vldecoder->cur_buffer]);
300 vldecoder->decoder->set_decode_target(vldecoder->decoder, vlsurf->video_buffer);
302 return vlVdpDecoderRenderMpeg12(vldecoder->decoder, (VdpPictureInfoMPEG1Or2 *)picture_info,
303 bitstream_buffer_count, bitstream_buffers);
304 break;
306 default:
307 return VDP_STATUS_INVALID_DECODER_PROFILE;