1 /**************************************************************************
3 * Copyright 2010 Thomas Balling Sørensen.
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
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"
35 * Create a VdpDecoder.
38 vlVdpDecoderCreate(VdpDevice device
,
39 VdpDecoderProfile profile
,
40 uint32_t width
, uint32_t height
,
41 uint32_t max_references
,
44 enum pipe_video_profile p_profile
;
45 struct pipe_context
*pipe
;
46 struct pipe_screen
*screen
;
48 vlVdpDecoder
*vldecoder
;
53 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Creating decoder\n");
56 return VDP_STATUS_INVALID_POINTER
;
59 if (!(width
&& height
))
60 return VDP_STATUS_INVALID_VALUE
;
62 p_profile
= ProfileToPipe(profile
);
63 if (p_profile
== PIPE_VIDEO_PROFILE_UNKNOWN
)
64 return VDP_STATUS_INVALID_DECODER_PROFILE
;
66 dev
= vlGetDataHTAB(device
);
68 return VDP_STATUS_INVALID_HANDLE
;
70 pipe
= dev
->context
->pipe
;
71 screen
= dev
->vscreen
->pscreen
;
72 supported
= screen
->get_video_param
76 PIPE_VIDEO_CAP_SUPPORTED
79 return VDP_STATUS_INVALID_DECODER_PROFILE
;
81 vldecoder
= CALLOC(1,sizeof(vlVdpDecoder
));
83 return VDP_STATUS_RESOURCES
;
85 vldecoder
->device
= dev
;
87 vldecoder
->decoder
= pipe
->create_video_decoder
90 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
,
91 PIPE_VIDEO_CHROMA_FORMAT_420
,
92 width
, height
, max_references
95 if (!vldecoder
->decoder
) {
96 ret
= VDP_STATUS_ERROR
;
100 vldecoder
->num_buffers
= pipe
->screen
->get_video_param
102 pipe
->screen
, p_profile
,
103 PIPE_VIDEO_CAP_NUM_BUFFERS_DESIRED
105 vldecoder
->cur_buffer
= 0;
107 vldecoder
->buffers
= CALLOC(vldecoder
->num_buffers
, sizeof(void*));
108 if (!vldecoder
->buffers
)
109 goto error_alloc_buffers
;
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
;
119 *decoder
= vlAddDataHTAB(vldecoder
);
121 ret
= VDP_STATUS_ERROR
;
125 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoder created succesfully\n");
127 return VDP_STATUS_OK
;
130 error_create_buffers
:
132 for (i
= 0; i
< vldecoder
->num_buffers
; ++i
)
133 if (vldecoder
->buffers
[i
])
134 vldecoder
->decoder
->destroy_buffer(vldecoder
->decoder
, vldecoder
->buffers
[i
]);
136 FREE(vldecoder
->buffers
);
140 vldecoder
->decoder
->destroy(vldecoder
->decoder
);
148 * Destroy a VdpDecoder.
151 vlVdpDecoderDestroy(VdpDecoder decoder
)
153 vlVdpDecoder
*vldecoder
;
156 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Destroying decoder\n");
158 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
160 return VDP_STATUS_INVALID_HANDLE
;
162 for (i
= 0; i
< vldecoder
->num_buffers
; ++i
)
163 if (vldecoder
->buffers
[i
])
164 vldecoder
->decoder
->destroy_buffer(vldecoder
->decoder
, vldecoder
->buffers
[i
]);
166 FREE(vldecoder
->buffers
);
168 vldecoder
->decoder
->destroy(vldecoder
->decoder
);
172 return VDP_STATUS_OK
;
176 * Retrieve the parameters used to create a VdpBitmapSurface.
179 vlVdpDecoderGetParameters(VdpDecoder decoder
,
180 VdpDecoderProfile
*profile
,
184 vlVdpDecoder
*vldecoder
;
186 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoder get parameters called\n");
188 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
190 return VDP_STATUS_INVALID_HANDLE
;
192 *profile
= PipeToProfile(vldecoder
->decoder
->profile
);
193 *width
= vldecoder
->decoder
->width
;
194 *height
= vldecoder
->decoder
->height
;
196 return VDP_STATUS_OK
;
200 * Decode a mpeg 1/2 video.
203 vlVdpDecoderRenderMpeg12(struct pipe_video_decoder
*decoder
,
204 VdpPictureInfoMPEG1Or2
*picture_info
,
205 uint32_t bitstream_buffer_count
,
206 VdpBitstreamBuffer
const *bitstream_buffers
)
208 struct pipe_mpeg12_picture_desc picture
;
209 struct pipe_mpeg12_quant_matrix quant
;
210 struct pipe_video_buffer
*ref_frames
[2];
213 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding MPEG12\n");
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
;
221 return VDP_STATUS_INVALID_HANDLE
;
225 if (picture_info
->backward_reference
!= VDP_INVALID_HANDLE
) {
226 ref_frames
[i
] = ((vlVdpSurface
*)vlGetDataHTAB(picture_info
->backward_reference
))->video_buffer
;
228 return VDP_STATUS_INVALID_HANDLE
;
232 decoder
->set_reference_frames(decoder
, ref_frames
, i
);
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
;
250 decoder
->set_picture_parameters(decoder
, &picture
.base
);
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
;
257 decoder
->set_quant_matrix(decoder
, &quant
.base
);
259 decoder
->begin_frame(decoder
);
261 for (i
= 0; i
< bitstream_buffer_count
; ++i
)
262 decoder
->decode_bitstream(decoder
, bitstream_buffers
[i
].bitstream_bytes
,
263 bitstream_buffers
[i
].bitstream
);
265 decoder
->end_frame(decoder
);
267 return VDP_STATUS_OK
;
271 * Decode a compressed field/frame and render the result into a VdpVideoSurface.
274 vlVdpDecoderRender(VdpDecoder decoder
,
275 VdpVideoSurface target
,
276 VdpPictureInfo
const *picture_info
,
277 uint32_t bitstream_buffer_count
,
278 VdpBitstreamBuffer
const *bitstream_buffers
)
280 vlVdpDecoder
*vldecoder
;
281 vlVdpSurface
*vlsurf
;
283 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding\n");
285 if (!(picture_info
&& bitstream_buffers
))
286 return VDP_STATUS_INVALID_POINTER
;
288 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
290 return VDP_STATUS_INVALID_HANDLE
;
292 vlsurf
= (vlVdpSurface
*)vlGetDataHTAB(target
);
294 return VDP_STATUS_INVALID_HANDLE
;
296 if (vlsurf
->device
!= vldecoder
->device
)
297 return VDP_STATUS_HANDLE_DEVICE_MISMATCH
;
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
;
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
;
311 vldecoder
->decoder
->set_decode_buffer(vldecoder
->decoder
, vldecoder
->buffers
[vldecoder
->cur_buffer
]);
312 vldecoder
->decoder
->set_decode_target(vldecoder
->decoder
, vlsurf
->video_buffer
);
314 return vlVdpDecoderRenderMpeg12(vldecoder
->decoder
, (VdpPictureInfoMPEG1Or2
*)picture_info
,
315 bitstream_buffer_count
, bitstream_buffers
);
319 return VDP_STATUS_INVALID_DECODER_PROFILE
;