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 vlVdpDecoderCreate(VdpDevice device
,
36 VdpDecoderProfile profile
,
37 uint32_t width
, uint32_t height
,
38 uint32_t max_references
,
41 enum pipe_video_profile p_profile
;
42 struct pipe_context
*pipe
;
44 vlVdpDecoder
*vldecoder
;
48 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Creating decoder\n");
51 return VDP_STATUS_INVALID_POINTER
;
53 if (!(width
&& height
))
54 return VDP_STATUS_INVALID_VALUE
;
56 p_profile
= ProfileToPipe(profile
);
57 if (p_profile
== PIPE_VIDEO_PROFILE_UNKNOWN
)
58 return VDP_STATUS_INVALID_DECODER_PROFILE
;
60 dev
= vlGetDataHTAB(device
);
62 return VDP_STATUS_INVALID_HANDLE
;
64 pipe
= dev
->context
->pipe
;
66 vldecoder
= CALLOC(1,sizeof(vlVdpDecoder
));
68 return VDP_STATUS_RESOURCES
;
70 vldecoder
->device
= dev
;
72 // TODO: Define max_references. Used mainly for H264
73 vldecoder
->decoder
= pipe
->create_video_decoder
76 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
,
77 PIPE_VIDEO_CHROMA_FORMAT_420
,
80 if (!vldecoder
->decoder
) {
81 ret
= VDP_STATUS_ERROR
;
85 vldecoder
->cur_buffer
= 0;
87 for (i
= 0; i
< VL_NUM_DECODE_BUFFERS
; ++i
) {
88 vldecoder
->buffer
[i
] = vldecoder
->decoder
->create_buffer(vldecoder
->decoder
);
89 if (!vldecoder
->buffer
[i
]) {
90 ret
= VDP_STATUS_ERROR
;
95 *decoder
= vlAddDataHTAB(vldecoder
);
97 ret
= VDP_STATUS_ERROR
;
101 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoder created succesfully\n");
103 return VDP_STATUS_OK
;
108 for (i
= 0; i
< VL_NUM_DECODE_BUFFERS
; ++i
)
109 if (vldecoder
->buffer
[i
])
110 vldecoder
->decoder
->destroy_buffer(vldecoder
->decoder
, vldecoder
->buffer
[i
]);
112 vldecoder
->decoder
->destroy(vldecoder
->decoder
);
120 vlVdpDecoderDestroy(VdpDecoder decoder
)
122 vlVdpDecoder
*vldecoder
;
125 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Destroying decoder\n");
127 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
129 return VDP_STATUS_INVALID_HANDLE
;
131 for (i
= 0; i
< VL_NUM_DECODE_BUFFERS
; ++i
)
132 if (vldecoder
->buffer
[i
])
133 vldecoder
->decoder
->destroy_buffer(vldecoder
->decoder
, vldecoder
->buffer
[i
]);
135 vldecoder
->decoder
->destroy(vldecoder
->decoder
);
139 return VDP_STATUS_OK
;
143 vlVdpDecoderGetParameters(VdpDecoder decoder
,
144 VdpDecoderProfile
*profile
,
148 vlVdpDecoder
*vldecoder
;
150 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] decoder get parameters called\n");
152 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
154 return VDP_STATUS_INVALID_HANDLE
;
156 *profile
= PipeToProfile(vldecoder
->decoder
->profile
);
157 *width
= vldecoder
->decoder
->width
;
158 *height
= vldecoder
->decoder
->height
;
160 return VDP_STATUS_OK
;
164 vlVdpDecoderRenderMpeg12(struct pipe_video_decoder
*decoder
,
165 VdpPictureInfoMPEG1Or2
*picture_info
,
166 uint32_t bitstream_buffer_count
,
167 VdpBitstreamBuffer
const *bitstream_buffers
)
169 struct pipe_mpeg12_picture_desc picture
;
170 struct pipe_video_buffer
*ref_frames
[2];
171 uint8_t intra_quantizer_matrix
[64];
172 unsigned num_ycbcr_blocks
[3] = { 0, 0, 0 };
175 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding MPEG2\n");
179 /* if surfaces equals VDP_STATUS_INVALID_HANDLE, they are not used */
180 if (picture_info
->forward_reference
!= VDP_INVALID_HANDLE
) {
181 ref_frames
[i
] = ((vlVdpSurface
*)vlGetDataHTAB(picture_info
->forward_reference
))->video_buffer
;
183 return VDP_STATUS_INVALID_HANDLE
;
187 if (picture_info
->backward_reference
!= VDP_INVALID_HANDLE
) {
188 ref_frames
[i
] = ((vlVdpSurface
*)vlGetDataHTAB(picture_info
->backward_reference
))->video_buffer
;
190 return VDP_STATUS_INVALID_HANDLE
;
194 decoder
->set_reference_frames(decoder
, ref_frames
, i
);
196 memset(&picture
, 0, sizeof(picture
));
197 picture
.base
.profile
= decoder
->profile
;
198 picture
.picture_coding_type
= picture_info
->picture_coding_type
;
199 picture
.picture_structure
= picture_info
->picture_structure
;
200 picture
.frame_pred_frame_dct
= picture_info
->frame_pred_frame_dct
;
201 picture
.q_scale_type
= picture_info
->q_scale_type
;
202 picture
.alternate_scan
= picture_info
->alternate_scan
;
203 picture
.intra_vlc_format
= picture_info
->intra_vlc_format
;
204 picture
.concealment_motion_vectors
= picture_info
->concealment_motion_vectors
;
205 picture
.f_code
[0][0] = picture_info
->f_code
[0][0] - 1;
206 picture
.f_code
[0][1] = picture_info
->f_code
[0][1] - 1;
207 picture
.f_code
[1][0] = picture_info
->f_code
[1][0] - 1;
208 picture
.f_code
[1][1] = picture_info
->f_code
[1][1] - 1;
210 decoder
->set_picture_parameters(decoder
, &picture
.base
);
212 memcpy(intra_quantizer_matrix
, picture_info
->intra_quantizer_matrix
, sizeof(intra_quantizer_matrix
));
213 intra_quantizer_matrix
[0] = 1 << (7 - picture_info
->intra_dc_precision
);
214 decoder
->set_quant_matrix(decoder
, intra_quantizer_matrix
, picture_info
->non_intra_quantizer_matrix
);
216 decoder
->begin_frame(decoder
);
218 for (i
= 0; i
< bitstream_buffer_count
; ++i
)
219 decoder
->decode_bitstream(decoder
, bitstream_buffers
[i
].bitstream_bytes
,
220 bitstream_buffers
[i
].bitstream
, num_ycbcr_blocks
);
222 decoder
->end_frame(decoder
, num_ycbcr_blocks
);
224 return VDP_STATUS_OK
;
228 vlVdpDecoderRender(VdpDecoder decoder
,
229 VdpVideoSurface target
,
230 VdpPictureInfo
const *picture_info
,
231 uint32_t bitstream_buffer_count
,
232 VdpBitstreamBuffer
const *bitstream_buffers
)
234 vlVdpDecoder
*vldecoder
;
235 vlVdpSurface
*vlsurf
;
237 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding\n");
239 if (!(picture_info
&& bitstream_buffers
))
240 return VDP_STATUS_INVALID_POINTER
;
242 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
244 return VDP_STATUS_INVALID_HANDLE
;
246 vlsurf
= (vlVdpSurface
*)vlGetDataHTAB(target
);
248 return VDP_STATUS_INVALID_HANDLE
;
250 if (vlsurf
->device
!= vldecoder
->device
)
251 return VDP_STATUS_HANDLE_DEVICE_MISMATCH
;
253 if (vlsurf
->video_buffer
->chroma_format
!= vldecoder
->decoder
->chroma_format
)
254 // TODO: Recreate decoder with correct chroma
255 return VDP_STATUS_INVALID_CHROMA_TYPE
;
257 // TODO: Right now only mpeg 1 & 2 is supported.
258 switch (vldecoder
->decoder
->profile
) {
259 case PIPE_VIDEO_PROFILE_MPEG1
:
260 case PIPE_VIDEO_PROFILE_MPEG2_SIMPLE
:
261 case PIPE_VIDEO_PROFILE_MPEG2_MAIN
:
262 ++vldecoder
->cur_buffer
;
263 vldecoder
->cur_buffer
%= VL_NUM_DECODE_BUFFERS
;
265 vldecoder
->decoder
->set_decode_buffer(vldecoder
->decoder
, vldecoder
->buffer
[vldecoder
->cur_buffer
]);
266 vldecoder
->decoder
->set_decode_target(vldecoder
->decoder
, vlsurf
->video_buffer
);
268 return vlVdpDecoderRenderMpeg12(vldecoder
->decoder
, (VdpPictureInfoMPEG1Or2
*)picture_info
,
269 bitstream_buffer_count
, bitstream_buffers
);
273 return VDP_STATUS_INVALID_DECODER_PROFILE
;