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"
31 #include "util/u_video.h"
33 #include "vdpau_private.h"
36 * Create a VdpDecoder.
39 vlVdpDecoderCreate(VdpDevice device
,
40 VdpDecoderProfile profile
,
41 uint32_t width
, uint32_t height
,
42 uint32_t max_references
,
45 enum pipe_video_profile p_profile
;
46 struct pipe_context
*pipe
;
47 struct pipe_screen
*screen
;
49 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
,
96 if (!vldecoder
->decoder
) {
97 ret
= VDP_STATUS_ERROR
;
101 *decoder
= vlAddDataHTAB(vldecoder
);
103 ret
= VDP_STATUS_ERROR
;
107 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoder created succesfully\n");
109 return VDP_STATUS_OK
;
113 vldecoder
->decoder
->destroy(vldecoder
->decoder
);
121 * Destroy a VdpDecoder.
124 vlVdpDecoderDestroy(VdpDecoder decoder
)
126 vlVdpDecoder
*vldecoder
;
128 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Destroying decoder\n");
130 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
132 return VDP_STATUS_INVALID_HANDLE
;
134 vldecoder
->decoder
->destroy(vldecoder
->decoder
);
138 return VDP_STATUS_OK
;
142 * Retrieve the parameters used to create a VdpBitmapSurface.
145 vlVdpDecoderGetParameters(VdpDecoder decoder
,
146 VdpDecoderProfile
*profile
,
150 vlVdpDecoder
*vldecoder
;
152 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoder get parameters called\n");
154 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
156 return VDP_STATUS_INVALID_HANDLE
;
158 *profile
= PipeToProfile(vldecoder
->decoder
->profile
);
159 *width
= vldecoder
->decoder
->width
;
160 *height
= vldecoder
->decoder
->height
;
162 return VDP_STATUS_OK
;
166 vlVdpGetReferenceFrame(VdpVideoSurface handle
, struct pipe_video_buffer
**ref_frame
)
168 vlVdpSurface
*surface
;
170 /* if surfaces equals VDP_STATUS_INVALID_HANDLE, they are not used */
171 if (handle
== VDP_INVALID_HANDLE
) {
173 return VDP_STATUS_OK
;
176 surface
= vlGetDataHTAB(handle
);
178 return VDP_STATUS_INVALID_HANDLE
;
180 *ref_frame
= surface
->video_buffer
;
182 return VDP_STATUS_INVALID_HANDLE
;
184 return VDP_STATUS_OK
;
188 * Decode a mpeg 1/2 video.
191 vlVdpDecoderRenderMpeg12(struct pipe_mpeg12_picture_desc
*picture
,
192 VdpPictureInfoMPEG1Or2
*picture_info
)
196 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding MPEG12\n");
198 r
= vlVdpGetReferenceFrame(picture_info
->forward_reference
, &picture
->ref
[0]);
199 if (r
!= VDP_STATUS_OK
)
202 r
= vlVdpGetReferenceFrame(picture_info
->backward_reference
, &picture
->ref
[1]);
203 if (r
!= VDP_STATUS_OK
)
206 picture
->picture_coding_type
= picture_info
->picture_coding_type
;
207 picture
->picture_structure
= picture_info
->picture_structure
;
208 picture
->frame_pred_frame_dct
= picture_info
->frame_pred_frame_dct
;
209 picture
->q_scale_type
= picture_info
->q_scale_type
;
210 picture
->alternate_scan
= picture_info
->alternate_scan
;
211 picture
->intra_vlc_format
= picture_info
->intra_vlc_format
;
212 picture
->concealment_motion_vectors
= picture_info
->concealment_motion_vectors
;
213 picture
->intra_dc_precision
= picture_info
->intra_dc_precision
;
214 picture
->f_code
[0][0] = picture_info
->f_code
[0][0] - 1;
215 picture
->f_code
[0][1] = picture_info
->f_code
[0][1] - 1;
216 picture
->f_code
[1][0] = picture_info
->f_code
[1][0] - 1;
217 picture
->f_code
[1][1] = picture_info
->f_code
[1][1] - 1;
218 picture
->num_slices
= picture_info
->slice_count
;
219 picture
->top_field_first
= picture_info
->top_field_first
;
220 picture
->full_pel_forward_vector
= picture_info
->full_pel_forward_vector
;
221 picture
->full_pel_backward_vector
= picture_info
->full_pel_backward_vector
;
222 picture
->intra_matrix
= picture_info
->intra_quantizer_matrix
;
223 picture
->non_intra_matrix
= picture_info
->non_intra_quantizer_matrix
;
225 return VDP_STATUS_OK
;
229 * Decode a mpeg 4 video.
232 vlVdpDecoderRenderMpeg4(struct pipe_mpeg4_picture_desc
*picture
,
233 VdpPictureInfoMPEG4Part2
*picture_info
)
238 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding MPEG4\n");
240 r
= vlVdpGetReferenceFrame(picture_info
->forward_reference
, &picture
->ref
[0]);
241 if (r
!= VDP_STATUS_OK
)
244 r
= vlVdpGetReferenceFrame(picture_info
->backward_reference
, &picture
->ref
[1]);
245 if (r
!= VDP_STATUS_OK
)
248 for (i
= 0; i
< 2; ++i
) {
249 picture
->trd
[i
] = picture_info
->trd
[i
];
250 picture
->trb
[i
] = picture_info
->trb
[i
];
252 picture
->vop_time_increment_resolution
= picture_info
->vop_time_increment_resolution
;
253 picture
->vop_coding_type
= picture_info
->vop_coding_type
;
254 picture
->vop_fcode_forward
= picture_info
->vop_fcode_forward
;
255 picture
->vop_fcode_backward
= picture_info
->vop_fcode_backward
;
256 picture
->resync_marker_disable
= picture_info
->resync_marker_disable
;
257 picture
->interlaced
= picture_info
->interlaced
;
258 picture
->quant_type
= picture_info
->quant_type
;
259 picture
->quarter_sample
= picture_info
->quarter_sample
;
260 picture
->short_video_header
= picture_info
->short_video_header
;
261 picture
->rounding_control
= picture_info
->rounding_control
;
262 picture
->alternate_vertical_scan_flag
= picture_info
->alternate_vertical_scan_flag
;
263 picture
->top_field_first
= picture_info
->top_field_first
;
264 picture
->intra_matrix
= picture_info
->intra_quantizer_matrix
;
265 picture
->non_intra_matrix
= picture_info
->non_intra_quantizer_matrix
;
267 return VDP_STATUS_OK
;
271 vlVdpDecoderRenderVC1(struct pipe_vc1_picture_desc
*picture
,
272 VdpPictureInfoVC1
*picture_info
)
276 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding VC-1\n");
278 r
= vlVdpGetReferenceFrame(picture_info
->forward_reference
, &picture
->ref
[0]);
279 if (r
!= VDP_STATUS_OK
)
282 r
= vlVdpGetReferenceFrame(picture_info
->backward_reference
, &picture
->ref
[1]);
283 if (r
!= VDP_STATUS_OK
)
286 picture
->slice_count
= picture_info
->slice_count
;
287 picture
->picture_type
= picture_info
->picture_type
;
288 picture
->frame_coding_mode
= picture_info
->frame_coding_mode
;
289 picture
->postprocflag
= picture_info
->postprocflag
;
290 picture
->pulldown
= picture_info
->pulldown
;
291 picture
->interlace
= picture_info
->interlace
;
292 picture
->tfcntrflag
= picture_info
->tfcntrflag
;
293 picture
->finterpflag
= picture_info
->finterpflag
;
294 picture
->psf
= picture_info
->psf
;
295 picture
->dquant
= picture_info
->dquant
;
296 picture
->panscan_flag
= picture_info
->panscan_flag
;
297 picture
->refdist_flag
= picture_info
->refdist_flag
;
298 picture
->quantizer
= picture_info
->quantizer
;
299 picture
->extended_mv
= picture_info
->extended_mv
;
300 picture
->extended_dmv
= picture_info
->extended_dmv
;
301 picture
->overlap
= picture_info
->overlap
;
302 picture
->vstransform
= picture_info
->vstransform
;
303 picture
->loopfilter
= picture_info
->loopfilter
;
304 picture
->fastuvmc
= picture_info
->fastuvmc
;
305 picture
->range_mapy_flag
= picture_info
->range_mapy_flag
;
306 picture
->range_mapy
= picture_info
->range_mapy
;
307 picture
->range_mapuv_flag
= picture_info
->range_mapuv_flag
;
308 picture
->range_mapuv
= picture_info
->range_mapuv
;
309 picture
->multires
= picture_info
->multires
;
310 picture
->syncmarker
= picture_info
->syncmarker
;
311 picture
->rangered
= picture_info
->rangered
;
312 picture
->maxbframes
= picture_info
->maxbframes
;
313 picture
->deblockEnable
= picture_info
->deblockEnable
;
314 picture
->pquant
= picture_info
->pquant
;
316 return VDP_STATUS_OK
;
320 vlVdpDecoderRenderH264(struct pipe_h264_picture_desc
*picture
,
321 VdpPictureInfoH264
*picture_info
)
325 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding H264\n");
327 picture
->slice_count
= picture_info
->slice_count
;
328 picture
->field_order_cnt
[0] = picture_info
->field_order_cnt
[0];
329 picture
->field_order_cnt
[1] = picture_info
->field_order_cnt
[1];
330 picture
->is_reference
= picture_info
->is_reference
;
331 picture
->frame_num
= picture_info
->frame_num
;
332 picture
->field_pic_flag
= picture_info
->field_pic_flag
;
333 picture
->bottom_field_flag
= picture_info
->bottom_field_flag
;
334 picture
->num_ref_frames
= picture_info
->num_ref_frames
;
335 picture
->mb_adaptive_frame_field_flag
= picture_info
->mb_adaptive_frame_field_flag
;
336 picture
->constrained_intra_pred_flag
= picture_info
->constrained_intra_pred_flag
;
337 picture
->weighted_pred_flag
= picture_info
->weighted_pred_flag
;
338 picture
->weighted_bipred_idc
= picture_info
->weighted_bipred_idc
;
339 picture
->frame_mbs_only_flag
= picture_info
->frame_mbs_only_flag
;
340 picture
->transform_8x8_mode_flag
= picture_info
->transform_8x8_mode_flag
;
341 picture
->chroma_qp_index_offset
= picture_info
->chroma_qp_index_offset
;
342 picture
->second_chroma_qp_index_offset
= picture_info
->second_chroma_qp_index_offset
;
343 picture
->pic_init_qp_minus26
= picture_info
->pic_init_qp_minus26
;
344 picture
->num_ref_idx_l0_active_minus1
= picture_info
->num_ref_idx_l0_active_minus1
;
345 picture
->num_ref_idx_l1_active_minus1
= picture_info
->num_ref_idx_l1_active_minus1
;
346 picture
->log2_max_frame_num_minus4
= picture_info
->log2_max_frame_num_minus4
;
347 picture
->pic_order_cnt_type
= picture_info
->pic_order_cnt_type
;
348 picture
->log2_max_pic_order_cnt_lsb_minus4
= picture_info
->log2_max_pic_order_cnt_lsb_minus4
;
349 picture
->delta_pic_order_always_zero_flag
= picture_info
->delta_pic_order_always_zero_flag
;
350 picture
->direct_8x8_inference_flag
= picture_info
->direct_8x8_inference_flag
;
351 picture
->entropy_coding_mode_flag
= picture_info
->entropy_coding_mode_flag
;
352 picture
->pic_order_present_flag
= picture_info
->pic_order_present_flag
;
353 picture
->deblocking_filter_control_present_flag
= picture_info
->deblocking_filter_control_present_flag
;
354 picture
->redundant_pic_cnt_present_flag
= picture_info
->redundant_pic_cnt_present_flag
;
356 memcpy(picture
->scaling_lists_4x4
, picture_info
->scaling_lists_4x4
, 6*16);
357 memcpy(picture
->scaling_lists_8x8
, picture_info
->scaling_lists_8x8
, 2*64);
359 for (i
= 0; i
< 16; ++i
) {
360 VdpStatus ret
= vlVdpGetReferenceFrame
362 picture_info
->referenceFrames
[i
].surface
,
365 if (ret
!= VDP_STATUS_OK
)
368 picture
->is_long_term
[i
] = picture_info
->referenceFrames
[i
].is_long_term
;
369 picture
->top_is_reference
[i
] = picture_info
->referenceFrames
[i
].top_is_reference
;
370 picture
->bottom_is_reference
[i
] = picture_info
->referenceFrames
[i
].bottom_is_reference
;
371 picture
->field_order_cnt_list
[i
][0] = picture_info
->referenceFrames
[i
].field_order_cnt
[0];
372 picture
->field_order_cnt_list
[i
][1] = picture_info
->referenceFrames
[i
].field_order_cnt
[1];
373 picture
->frame_num_list
[i
] = picture_info
->referenceFrames
[i
].frame_idx
;
376 return VDP_STATUS_OK
;
380 * Decode a compressed field/frame and render the result into a VdpVideoSurface.
383 vlVdpDecoderRender(VdpDecoder decoder
,
384 VdpVideoSurface target
,
385 VdpPictureInfo
const *picture_info
,
386 uint32_t bitstream_buffer_count
,
387 VdpBitstreamBuffer
const *bitstream_buffers
)
389 const void * buffers
[bitstream_buffer_count
];
390 unsigned sizes
[bitstream_buffer_count
];
391 vlVdpDecoder
*vldecoder
;
392 vlVdpSurface
*vlsurf
;
394 struct pipe_video_decoder
*dec
;
397 struct pipe_picture_desc base
;
398 struct pipe_mpeg12_picture_desc mpeg12
;
399 struct pipe_mpeg4_picture_desc mpeg4
;
400 struct pipe_vc1_picture_desc vc1
;
401 struct pipe_h264_picture_desc h264
;
404 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding\n");
406 if (!(picture_info
&& bitstream_buffers
))
407 return VDP_STATUS_INVALID_POINTER
;
409 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
411 return VDP_STATUS_INVALID_HANDLE
;
412 dec
= vldecoder
->decoder
;
414 vlsurf
= (vlVdpSurface
*)vlGetDataHTAB(target
);
416 return VDP_STATUS_INVALID_HANDLE
;
418 if (vlsurf
->device
!= vldecoder
->device
)
419 return VDP_STATUS_HANDLE_DEVICE_MISMATCH
;
421 if (vlsurf
->video_buffer
->chroma_format
!= dec
->chroma_format
)
422 // TODO: Recreate decoder with correct chroma
423 return VDP_STATUS_INVALID_CHROMA_TYPE
;
425 memset(&desc
, 0, sizeof(desc
));
426 desc
.base
.profile
= dec
->profile
;
427 switch (u_reduce_video_profile(dec
->profile
)) {
428 case PIPE_VIDEO_CODEC_MPEG12
:
429 ret
= vlVdpDecoderRenderMpeg12(&desc
.mpeg12
, (VdpPictureInfoMPEG1Or2
*)picture_info
);
431 case PIPE_VIDEO_CODEC_MPEG4
:
432 ret
= vlVdpDecoderRenderMpeg4(&desc
.mpeg4
, (VdpPictureInfoMPEG4Part2
*)picture_info
);
434 case PIPE_VIDEO_CODEC_VC1
:
435 ret
= vlVdpDecoderRenderVC1(&desc
.vc1
, (VdpPictureInfoVC1
*)picture_info
);
437 case PIPE_VIDEO_CODEC_MPEG4_AVC
:
438 ret
= vlVdpDecoderRenderH264(&desc
.h264
, (VdpPictureInfoH264
*)picture_info
);
441 return VDP_STATUS_INVALID_DECODER_PROFILE
;
443 if (ret
!= VDP_STATUS_OK
)
446 for (i
= 0; i
< bitstream_buffer_count
; ++i
) {
447 buffers
[i
] = bitstream_buffers
[i
].bitstream
;
448 sizes
[i
] = bitstream_buffers
[i
].bitstream_bytes
;
451 dec
->begin_frame(dec
, vlsurf
->video_buffer
, &desc
.base
);
452 dec
->decode_bitstream(dec
, vlsurf
->video_buffer
, &desc
.base
, bitstream_buffer_count
, buffers
, sizes
);
453 dec
->end_frame(dec
, vlsurf
->video_buffer
, &desc
.base
);