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
;
47 vlVdpDecoder
*vldecoder
;
51 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Creating decoder\n");
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
);
65 return VDP_STATUS_INVALID_HANDLE
;
67 pipe
= dev
->context
->pipe
;
69 vldecoder
= CALLOC(1,sizeof(vlVdpDecoder
));
71 return VDP_STATUS_RESOURCES
;
73 vldecoder
->device
= dev
;
75 vldecoder
->decoder
= pipe
->create_video_decoder
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
;
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
);
109 ret
= VDP_STATUS_ERROR
;
113 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoder created succesfully\n");
115 return VDP_STATUS_OK
;
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
);
128 vldecoder
->decoder
->destroy(vldecoder
->decoder
);
136 * Destroy a VdpDecoder.
139 vlVdpDecoderDestroy(VdpDecoder decoder
)
141 vlVdpDecoder
*vldecoder
;
144 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Destroying decoder\n");
146 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
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
);
160 return VDP_STATUS_OK
;
164 * Retrieve the parameters used to create a VdpBitmapSurface.
167 vlVdpDecoderGetParameters(VdpDecoder decoder
,
168 VdpDecoderProfile
*profile
,
172 vlVdpDecoder
*vldecoder
;
174 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoder get parameters called\n");
176 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
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.
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];
201 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding MPEG12\n");
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
;
209 return VDP_STATUS_INVALID_HANDLE
;
213 if (picture_info
->backward_reference
!= VDP_INVALID_HANDLE
) {
214 ref_frames
[i
] = ((vlVdpSurface
*)vlGetDataHTAB(picture_info
->backward_reference
))->video_buffer
;
216 return VDP_STATUS_INVALID_HANDLE
;
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.
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
);
278 return VDP_STATUS_INVALID_HANDLE
;
280 vlsurf
= (vlVdpSurface
*)vlGetDataHTAB(target
);
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
);
307 return VDP_STATUS_INVALID_DECODER_PROFILE
;