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
;
54 return VDP_STATUS_INVALID_POINTER
;
57 if (!(width
&& height
))
58 return VDP_STATUS_INVALID_VALUE
;
60 p_profile
= ProfileToPipe(profile
);
61 if (p_profile
== PIPE_VIDEO_PROFILE_UNKNOWN
)
62 return VDP_STATUS_INVALID_DECODER_PROFILE
;
64 dev
= vlGetDataHTAB(device
);
66 return VDP_STATUS_INVALID_HANDLE
;
69 screen
= dev
->vscreen
->pscreen
;
70 supported
= screen
->get_video_param
74 PIPE_VIDEO_CAP_SUPPORTED
77 return VDP_STATUS_INVALID_DECODER_PROFILE
;
79 vldecoder
= CALLOC(1,sizeof(vlVdpDecoder
));
81 return VDP_STATUS_RESOURCES
;
83 vldecoder
->device
= dev
;
85 vldecoder
->decoder
= pipe
->create_video_decoder
88 PIPE_VIDEO_ENTRYPOINT_BITSTREAM
,
89 PIPE_VIDEO_CHROMA_FORMAT_420
,
90 width
, height
, max_references
,
94 if (!vldecoder
->decoder
) {
95 ret
= VDP_STATUS_ERROR
;
99 *decoder
= vlAddDataHTAB(vldecoder
);
101 ret
= VDP_STATUS_ERROR
;
105 return VDP_STATUS_OK
;
109 vldecoder
->decoder
->destroy(vldecoder
->decoder
);
117 * Destroy a VdpDecoder.
120 vlVdpDecoderDestroy(VdpDecoder decoder
)
122 vlVdpDecoder
*vldecoder
;
124 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
126 return VDP_STATUS_INVALID_HANDLE
;
128 vldecoder
->decoder
->destroy(vldecoder
->decoder
);
132 return VDP_STATUS_OK
;
136 * Retrieve the parameters used to create a VdpBitmapSurface.
139 vlVdpDecoderGetParameters(VdpDecoder decoder
,
140 VdpDecoderProfile
*profile
,
144 vlVdpDecoder
*vldecoder
;
146 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
148 return VDP_STATUS_INVALID_HANDLE
;
150 *profile
= PipeToProfile(vldecoder
->decoder
->profile
);
151 *width
= vldecoder
->decoder
->width
;
152 *height
= vldecoder
->decoder
->height
;
154 return VDP_STATUS_OK
;
158 vlVdpGetReferenceFrame(VdpVideoSurface handle
, struct pipe_video_buffer
**ref_frame
)
160 vlVdpSurface
*surface
;
162 /* if surfaces equals VDP_STATUS_INVALID_HANDLE, they are not used */
163 if (handle
== VDP_INVALID_HANDLE
) {
165 return VDP_STATUS_OK
;
168 surface
= vlGetDataHTAB(handle
);
170 return VDP_STATUS_INVALID_HANDLE
;
172 *ref_frame
= surface
->video_buffer
;
174 return VDP_STATUS_INVALID_HANDLE
;
176 return VDP_STATUS_OK
;
180 * Decode a mpeg 1/2 video.
183 vlVdpDecoderRenderMpeg12(struct pipe_mpeg12_picture_desc
*picture
,
184 VdpPictureInfoMPEG1Or2
*picture_info
)
188 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding MPEG12\n");
190 r
= vlVdpGetReferenceFrame(picture_info
->forward_reference
, &picture
->ref
[0]);
191 if (r
!= VDP_STATUS_OK
)
194 r
= vlVdpGetReferenceFrame(picture_info
->backward_reference
, &picture
->ref
[1]);
195 if (r
!= VDP_STATUS_OK
)
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
->intra_dc_precision
= picture_info
->intra_dc_precision
;
206 picture
->f_code
[0][0] = picture_info
->f_code
[0][0] - 1;
207 picture
->f_code
[0][1] = picture_info
->f_code
[0][1] - 1;
208 picture
->f_code
[1][0] = picture_info
->f_code
[1][0] - 1;
209 picture
->f_code
[1][1] = picture_info
->f_code
[1][1] - 1;
210 picture
->num_slices
= picture_info
->slice_count
;
211 picture
->top_field_first
= picture_info
->top_field_first
;
212 picture
->full_pel_forward_vector
= picture_info
->full_pel_forward_vector
;
213 picture
->full_pel_backward_vector
= picture_info
->full_pel_backward_vector
;
214 picture
->intra_matrix
= picture_info
->intra_quantizer_matrix
;
215 picture
->non_intra_matrix
= picture_info
->non_intra_quantizer_matrix
;
217 return VDP_STATUS_OK
;
221 * Decode a mpeg 4 video.
224 vlVdpDecoderRenderMpeg4(struct pipe_mpeg4_picture_desc
*picture
,
225 VdpPictureInfoMPEG4Part2
*picture_info
)
230 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding MPEG4\n");
232 r
= vlVdpGetReferenceFrame(picture_info
->forward_reference
, &picture
->ref
[0]);
233 if (r
!= VDP_STATUS_OK
)
236 r
= vlVdpGetReferenceFrame(picture_info
->backward_reference
, &picture
->ref
[1]);
237 if (r
!= VDP_STATUS_OK
)
240 for (i
= 0; i
< 2; ++i
) {
241 picture
->trd
[i
] = picture_info
->trd
[i
];
242 picture
->trb
[i
] = picture_info
->trb
[i
];
244 picture
->vop_time_increment_resolution
= picture_info
->vop_time_increment_resolution
;
245 picture
->vop_coding_type
= picture_info
->vop_coding_type
;
246 picture
->vop_fcode_forward
= picture_info
->vop_fcode_forward
;
247 picture
->vop_fcode_backward
= picture_info
->vop_fcode_backward
;
248 picture
->resync_marker_disable
= picture_info
->resync_marker_disable
;
249 picture
->interlaced
= picture_info
->interlaced
;
250 picture
->quant_type
= picture_info
->quant_type
;
251 picture
->quarter_sample
= picture_info
->quarter_sample
;
252 picture
->short_video_header
= picture_info
->short_video_header
;
253 picture
->rounding_control
= picture_info
->rounding_control
;
254 picture
->alternate_vertical_scan_flag
= picture_info
->alternate_vertical_scan_flag
;
255 picture
->top_field_first
= picture_info
->top_field_first
;
256 picture
->intra_matrix
= picture_info
->intra_quantizer_matrix
;
257 picture
->non_intra_matrix
= picture_info
->non_intra_quantizer_matrix
;
259 return VDP_STATUS_OK
;
263 vlVdpDecoderRenderVC1(struct pipe_vc1_picture_desc
*picture
,
264 VdpPictureInfoVC1
*picture_info
)
268 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding VC-1\n");
270 r
= vlVdpGetReferenceFrame(picture_info
->forward_reference
, &picture
->ref
[0]);
271 if (r
!= VDP_STATUS_OK
)
274 r
= vlVdpGetReferenceFrame(picture_info
->backward_reference
, &picture
->ref
[1]);
275 if (r
!= VDP_STATUS_OK
)
278 picture
->slice_count
= picture_info
->slice_count
;
279 picture
->picture_type
= picture_info
->picture_type
;
280 picture
->frame_coding_mode
= picture_info
->frame_coding_mode
;
281 picture
->postprocflag
= picture_info
->postprocflag
;
282 picture
->pulldown
= picture_info
->pulldown
;
283 picture
->interlace
= picture_info
->interlace
;
284 picture
->tfcntrflag
= picture_info
->tfcntrflag
;
285 picture
->finterpflag
= picture_info
->finterpflag
;
286 picture
->psf
= picture_info
->psf
;
287 picture
->dquant
= picture_info
->dquant
;
288 picture
->panscan_flag
= picture_info
->panscan_flag
;
289 picture
->refdist_flag
= picture_info
->refdist_flag
;
290 picture
->quantizer
= picture_info
->quantizer
;
291 picture
->extended_mv
= picture_info
->extended_mv
;
292 picture
->extended_dmv
= picture_info
->extended_dmv
;
293 picture
->overlap
= picture_info
->overlap
;
294 picture
->vstransform
= picture_info
->vstransform
;
295 picture
->loopfilter
= picture_info
->loopfilter
;
296 picture
->fastuvmc
= picture_info
->fastuvmc
;
297 picture
->range_mapy_flag
= picture_info
->range_mapy_flag
;
298 picture
->range_mapy
= picture_info
->range_mapy
;
299 picture
->range_mapuv_flag
= picture_info
->range_mapuv_flag
;
300 picture
->range_mapuv
= picture_info
->range_mapuv
;
301 picture
->multires
= picture_info
->multires
;
302 picture
->syncmarker
= picture_info
->syncmarker
;
303 picture
->rangered
= picture_info
->rangered
;
304 picture
->maxbframes
= picture_info
->maxbframes
;
305 picture
->deblockEnable
= picture_info
->deblockEnable
;
306 picture
->pquant
= picture_info
->pquant
;
308 return VDP_STATUS_OK
;
312 vlVdpDecoderRenderH264(struct pipe_h264_picture_desc
*picture
,
313 VdpPictureInfoH264
*picture_info
)
317 VDPAU_MSG(VDPAU_TRACE
, "[VDPAU] Decoding H264\n");
319 picture
->slice_count
= picture_info
->slice_count
;
320 picture
->field_order_cnt
[0] = picture_info
->field_order_cnt
[0];
321 picture
->field_order_cnt
[1] = picture_info
->field_order_cnt
[1];
322 picture
->is_reference
= picture_info
->is_reference
;
323 picture
->frame_num
= picture_info
->frame_num
;
324 picture
->field_pic_flag
= picture_info
->field_pic_flag
;
325 picture
->bottom_field_flag
= picture_info
->bottom_field_flag
;
326 picture
->num_ref_frames
= picture_info
->num_ref_frames
;
327 picture
->mb_adaptive_frame_field_flag
= picture_info
->mb_adaptive_frame_field_flag
;
328 picture
->constrained_intra_pred_flag
= picture_info
->constrained_intra_pred_flag
;
329 picture
->weighted_pred_flag
= picture_info
->weighted_pred_flag
;
330 picture
->weighted_bipred_idc
= picture_info
->weighted_bipred_idc
;
331 picture
->frame_mbs_only_flag
= picture_info
->frame_mbs_only_flag
;
332 picture
->transform_8x8_mode_flag
= picture_info
->transform_8x8_mode_flag
;
333 picture
->chroma_qp_index_offset
= picture_info
->chroma_qp_index_offset
;
334 picture
->second_chroma_qp_index_offset
= picture_info
->second_chroma_qp_index_offset
;
335 picture
->pic_init_qp_minus26
= picture_info
->pic_init_qp_minus26
;
336 picture
->num_ref_idx_l0_active_minus1
= picture_info
->num_ref_idx_l0_active_minus1
;
337 picture
->num_ref_idx_l1_active_minus1
= picture_info
->num_ref_idx_l1_active_minus1
;
338 picture
->log2_max_frame_num_minus4
= picture_info
->log2_max_frame_num_minus4
;
339 picture
->pic_order_cnt_type
= picture_info
->pic_order_cnt_type
;
340 picture
->log2_max_pic_order_cnt_lsb_minus4
= picture_info
->log2_max_pic_order_cnt_lsb_minus4
;
341 picture
->delta_pic_order_always_zero_flag
= picture_info
->delta_pic_order_always_zero_flag
;
342 picture
->direct_8x8_inference_flag
= picture_info
->direct_8x8_inference_flag
;
343 picture
->entropy_coding_mode_flag
= picture_info
->entropy_coding_mode_flag
;
344 picture
->pic_order_present_flag
= picture_info
->pic_order_present_flag
;
345 picture
->deblocking_filter_control_present_flag
= picture_info
->deblocking_filter_control_present_flag
;
346 picture
->redundant_pic_cnt_present_flag
= picture_info
->redundant_pic_cnt_present_flag
;
348 memcpy(picture
->scaling_lists_4x4
, picture_info
->scaling_lists_4x4
, 6*16);
349 memcpy(picture
->scaling_lists_8x8
, picture_info
->scaling_lists_8x8
, 2*64);
351 for (i
= 0; i
< 16; ++i
) {
352 VdpStatus ret
= vlVdpGetReferenceFrame
354 picture_info
->referenceFrames
[i
].surface
,
357 if (ret
!= VDP_STATUS_OK
)
360 picture
->is_long_term
[i
] = picture_info
->referenceFrames
[i
].is_long_term
;
361 picture
->top_is_reference
[i
] = picture_info
->referenceFrames
[i
].top_is_reference
;
362 picture
->bottom_is_reference
[i
] = picture_info
->referenceFrames
[i
].bottom_is_reference
;
363 picture
->field_order_cnt_list
[i
][0] = picture_info
->referenceFrames
[i
].field_order_cnt
[0];
364 picture
->field_order_cnt_list
[i
][1] = picture_info
->referenceFrames
[i
].field_order_cnt
[1];
365 picture
->frame_num_list
[i
] = picture_info
->referenceFrames
[i
].frame_idx
;
368 return VDP_STATUS_OK
;
372 * Decode a compressed field/frame and render the result into a VdpVideoSurface.
375 vlVdpDecoderRender(VdpDecoder decoder
,
376 VdpVideoSurface target
,
377 VdpPictureInfo
const *picture_info
,
378 uint32_t bitstream_buffer_count
,
379 VdpBitstreamBuffer
const *bitstream_buffers
)
381 const void * buffers
[bitstream_buffer_count
];
382 unsigned sizes
[bitstream_buffer_count
];
383 vlVdpDecoder
*vldecoder
;
384 vlVdpSurface
*vlsurf
;
386 struct pipe_screen
*screen
;
387 struct pipe_video_decoder
*dec
;
388 bool buffer_support
[2];
391 struct pipe_picture_desc base
;
392 struct pipe_mpeg12_picture_desc mpeg12
;
393 struct pipe_mpeg4_picture_desc mpeg4
;
394 struct pipe_vc1_picture_desc vc1
;
395 struct pipe_h264_picture_desc h264
;
398 if (!(picture_info
&& bitstream_buffers
))
399 return VDP_STATUS_INVALID_POINTER
;
401 vldecoder
= (vlVdpDecoder
*)vlGetDataHTAB(decoder
);
403 return VDP_STATUS_INVALID_HANDLE
;
404 dec
= vldecoder
->decoder
;
405 screen
= dec
->context
->screen
;
407 vlsurf
= (vlVdpSurface
*)vlGetDataHTAB(target
);
409 return VDP_STATUS_INVALID_HANDLE
;
411 if (vlsurf
->device
!= vldecoder
->device
)
412 return VDP_STATUS_HANDLE_DEVICE_MISMATCH
;
414 if (vlsurf
->video_buffer
!= NULL
&& vlsurf
->video_buffer
->chroma_format
!= dec
->chroma_format
)
415 // TODO: Recreate decoder with correct chroma
416 return VDP_STATUS_INVALID_CHROMA_TYPE
;
418 buffer_support
[0] = screen
->get_video_param(screen
, dec
->profile
, PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE
);
419 buffer_support
[1] = screen
->get_video_param(screen
, dec
->profile
, PIPE_VIDEO_CAP_SUPPORTS_INTERLACED
);
421 if (vlsurf
->video_buffer
== NULL
||
422 !screen
->is_video_format_supported(screen
, vlsurf
->video_buffer
->buffer_format
, dec
->profile
) ||
423 buffer_support
[vlsurf
->video_buffer
->interlaced
]) {
425 /* destroy the old one */
426 if (vlsurf
->video_buffer
)
427 vlsurf
->video_buffer
->destroy(vlsurf
->video_buffer
);
429 /* set the buffer format to the prefered one */
430 vlsurf
->templat
.buffer_format
= screen
->get_video_param(screen
, dec
->profile
, PIPE_VIDEO_CAP_PREFERED_FORMAT
);
432 /* also set interlacing to decoders preferences */
433 vlsurf
->templat
.interlaced
= screen
->get_video_param(screen
, dec
->profile
, PIPE_VIDEO_CAP_PREFERS_INTERLACED
);
435 /* and recreate the video buffer */
436 vlsurf
->video_buffer
= dec
->context
->create_video_buffer(dec
->context
, &vlsurf
->templat
);
438 /* still no luck? get me out of here... */
439 if (!vlsurf
->video_buffer
)
440 return VDP_STATUS_NO_IMPLEMENTATION
;
443 memset(&desc
, 0, sizeof(desc
));
444 desc
.base
.profile
= dec
->profile
;
445 switch (u_reduce_video_profile(dec
->profile
)) {
446 case PIPE_VIDEO_CODEC_MPEG12
:
447 ret
= vlVdpDecoderRenderMpeg12(&desc
.mpeg12
, (VdpPictureInfoMPEG1Or2
*)picture_info
);
449 case PIPE_VIDEO_CODEC_MPEG4
:
450 ret
= vlVdpDecoderRenderMpeg4(&desc
.mpeg4
, (VdpPictureInfoMPEG4Part2
*)picture_info
);
452 case PIPE_VIDEO_CODEC_VC1
:
453 ret
= vlVdpDecoderRenderVC1(&desc
.vc1
, (VdpPictureInfoVC1
*)picture_info
);
455 case PIPE_VIDEO_CODEC_MPEG4_AVC
:
456 ret
= vlVdpDecoderRenderH264(&desc
.h264
, (VdpPictureInfoH264
*)picture_info
);
459 return VDP_STATUS_INVALID_DECODER_PROFILE
;
461 if (ret
!= VDP_STATUS_OK
)
464 for (i
= 0; i
< bitstream_buffer_count
; ++i
) {
465 buffers
[i
] = bitstream_buffers
[i
].bitstream
;
466 sizes
[i
] = bitstream_buffers
[i
].bitstream_bytes
;
469 dec
->begin_frame(dec
, vlsurf
->video_buffer
, &desc
.base
);
470 dec
->decode_bitstream(dec
, vlsurf
->video_buffer
, &desc
.base
, bitstream_buffer_count
, buffers
, sizes
);
471 dec
->end_frame(dec
, vlsurf
->video_buffer
, &desc
.base
);