decode.c revision 020a6f6cd8c3c8632f68e1f47ba3c63f2315e47e
1/************************************************************************** 2 * 3 * Copyright 2010 Thomas Balling Sørensen. 4 * All Rights Reserved. 5 * 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: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 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. 25 * 26 **************************************************************************/ 27 28#include "util/u_memory.h" 29#include "util/u_math.h" 30#include "util/u_debug.h" 31#include "util/u_video.h" 32 33#include "vdpau_private.h" 34 35/** 36 * Create a VdpDecoder. 37 */ 38VdpStatus 39vlVdpDecoderCreate(VdpDevice device, 40 VdpDecoderProfile profile, 41 uint32_t width, uint32_t height, 42 uint32_t max_references, 43 VdpDecoder *decoder) 44{ 45 enum pipe_video_profile p_profile; 46 struct pipe_context *pipe; 47 struct pipe_screen *screen; 48 vlVdpDevice *dev; 49 vlVdpDecoder *vldecoder; 50 VdpStatus ret; 51 bool supported; 52 53 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Creating decoder\n"); 54 55 if (!decoder) 56 return VDP_STATUS_INVALID_POINTER; 57 *decoder = 0; 58 59 if (!(width && height)) 60 return VDP_STATUS_INVALID_VALUE; 61 62 p_profile = ProfileToPipe(profile); 63 if (p_profile == PIPE_VIDEO_PROFILE_UNKNOWN) 64 return VDP_STATUS_INVALID_DECODER_PROFILE; 65 66 dev = vlGetDataHTAB(device); 67 if (!dev) 68 return VDP_STATUS_INVALID_HANDLE; 69 70 pipe = dev->context->pipe; 71 screen = dev->vscreen->pscreen; 72 supported = screen->get_video_param 73 ( 74 screen, 75 p_profile, 76 PIPE_VIDEO_CAP_SUPPORTED 77 ); 78 if (!supported) 79 return VDP_STATUS_INVALID_DECODER_PROFILE; 80 81 vldecoder = CALLOC(1,sizeof(vlVdpDecoder)); 82 if (!vldecoder) 83 return VDP_STATUS_RESOURCES; 84 85 vldecoder->device = dev; 86 87 vldecoder->decoder = pipe->create_video_decoder 88 ( 89 pipe, p_profile, 90 PIPE_VIDEO_ENTRYPOINT_BITSTREAM, 91 PIPE_VIDEO_CHROMA_FORMAT_420, 92 width, height, max_references, 93 false 94 ); 95 96 if (!vldecoder->decoder) { 97 ret = VDP_STATUS_ERROR; 98 goto error_decoder; 99 } 100 101 *decoder = vlAddDataHTAB(vldecoder); 102 if (*decoder == 0) { 103 ret = VDP_STATUS_ERROR; 104 goto error_handle; 105 } 106 107 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoder created succesfully\n"); 108 109 return VDP_STATUS_OK; 110 111error_handle: 112 113 vldecoder->decoder->destroy(vldecoder->decoder); 114 115error_decoder: 116 FREE(vldecoder); 117 return ret; 118} 119 120/** 121 * Destroy a VdpDecoder. 122 */ 123VdpStatus 124vlVdpDecoderDestroy(VdpDecoder decoder) 125{ 126 vlVdpDecoder *vldecoder; 127 128 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Destroying decoder\n"); 129 130 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder); 131 if (!vldecoder) 132 return VDP_STATUS_INVALID_HANDLE; 133 134 vldecoder->decoder->destroy(vldecoder->decoder); 135 136 FREE(vldecoder); 137 138 return VDP_STATUS_OK; 139} 140 141/** 142 * Retrieve the parameters used to create a VdpBitmapSurface. 143 */ 144VdpStatus 145vlVdpDecoderGetParameters(VdpDecoder decoder, 146 VdpDecoderProfile *profile, 147 uint32_t *width, 148 uint32_t *height) 149{ 150 vlVdpDecoder *vldecoder; 151 152 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoder get parameters called\n"); 153 154 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder); 155 if (!vldecoder) 156 return VDP_STATUS_INVALID_HANDLE; 157 158 *profile = PipeToProfile(vldecoder->decoder->profile); 159 *width = vldecoder->decoder->width; 160 *height = vldecoder->decoder->height; 161 162 return VDP_STATUS_OK; 163} 164 165static VdpStatus 166vlVdpGetReferenceFrame(VdpVideoSurface handle, struct pipe_video_buffer **ref_frame) 167{ 168 vlVdpSurface *surface; 169 170 /* if surfaces equals VDP_STATUS_INVALID_HANDLE, they are not used */ 171 if (handle == VDP_INVALID_HANDLE) { 172 *ref_frame = NULL; 173 return VDP_STATUS_OK; 174 } 175 176 surface = vlGetDataHTAB(handle); 177 if (!surface) 178 return VDP_STATUS_INVALID_HANDLE; 179 180 *ref_frame = surface->video_buffer; 181 if (!*ref_frame) 182 return VDP_STATUS_INVALID_HANDLE; 183 184 return VDP_STATUS_OK; 185} 186 187/** 188 * Decode a mpeg 1/2 video. 189 */ 190static VdpStatus 191vlVdpDecoderRenderMpeg12(struct pipe_mpeg12_picture_desc *picture, 192 VdpPictureInfoMPEG1Or2 *picture_info) 193{ 194 VdpStatus r; 195 196 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding MPEG12\n"); 197 198 r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]); 199 if (r != VDP_STATUS_OK) 200 return r; 201 202 r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]); 203 if (r != VDP_STATUS_OK) 204 return r; 205 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; 224 225 return VDP_STATUS_OK; 226} 227 228/** 229 * Decode a mpeg 4 video. 230 */ 231static VdpStatus 232vlVdpDecoderRenderMpeg4(struct pipe_mpeg4_picture_desc *picture, 233 VdpPictureInfoMPEG4Part2 *picture_info) 234{ 235 VdpStatus r; 236 unsigned i; 237 238 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding MPEG4\n"); 239 240 r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]); 241 if (r != VDP_STATUS_OK) 242 return r; 243 244 r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]); 245 if (r != VDP_STATUS_OK) 246 return r; 247 248 for (i = 0; i < 2; ++i) { 249 picture->trd[i] = picture_info->trd[i]; 250 picture->trb[i] = picture_info->trb[i]; 251 } 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; 266 267 return VDP_STATUS_OK; 268} 269 270static VdpStatus 271vlVdpDecoderRenderVC1(struct pipe_vc1_picture_desc *picture, 272 VdpPictureInfoVC1 *picture_info) 273{ 274 VdpStatus r; 275 276 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding VC-1\n"); 277 278 r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]); 279 if (r != VDP_STATUS_OK) 280 return r; 281 282 r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]); 283 if (r != VDP_STATUS_OK) 284 return r; 285 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; 315 316 return VDP_STATUS_OK; 317} 318 319static VdpStatus 320vlVdpDecoderRenderH264(struct pipe_h264_picture_desc *picture, 321 VdpPictureInfoH264 *picture_info) 322{ 323 unsigned i; 324 325 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding H264\n"); 326 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; 355 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); 358 359 for (i = 0; i < 16; ++i) { 360 VdpStatus ret = vlVdpGetReferenceFrame 361 ( 362 picture_info->referenceFrames[i].surface, 363 &picture->ref[i] 364 ); 365 if (ret != VDP_STATUS_OK) 366 return ret; 367 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; 374 } 375 376 return VDP_STATUS_OK; 377} 378 379/** 380 * Decode a compressed field/frame and render the result into a VdpVideoSurface. 381 */ 382VdpStatus 383vlVdpDecoderRender(VdpDecoder decoder, 384 VdpVideoSurface target, 385 VdpPictureInfo const *picture_info, 386 uint32_t bitstream_buffer_count, 387 VdpBitstreamBuffer const *bitstream_buffers) 388{ 389 const void * buffers[bitstream_buffer_count]; 390 unsigned sizes[bitstream_buffer_count]; 391 vlVdpDecoder *vldecoder; 392 vlVdpSurface *vlsurf; 393 VdpStatus ret; 394 struct pipe_screen *screen; 395 struct pipe_video_decoder *dec; 396 unsigned i; 397 union { 398 struct pipe_picture_desc base; 399 struct pipe_mpeg12_picture_desc mpeg12; 400 struct pipe_mpeg4_picture_desc mpeg4; 401 struct pipe_vc1_picture_desc vc1; 402 struct pipe_h264_picture_desc h264; 403 } desc; 404 405 VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding\n"); 406 407 if (!(picture_info && bitstream_buffers)) 408 return VDP_STATUS_INVALID_POINTER; 409 410 vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder); 411 if (!vldecoder) 412 return VDP_STATUS_INVALID_HANDLE; 413 dec = vldecoder->decoder; 414 screen = dec->context->screen; 415 416 vlsurf = (vlVdpSurface *)vlGetDataHTAB(target); 417 if (!vlsurf) 418 return VDP_STATUS_INVALID_HANDLE; 419 420 if (vlsurf->device != vldecoder->device) 421 return VDP_STATUS_HANDLE_DEVICE_MISMATCH; 422 423 if (vlsurf->video_buffer != NULL && vlsurf->video_buffer->chroma_format != dec->chroma_format) 424 // TODO: Recreate decoder with correct chroma 425 return VDP_STATUS_INVALID_CHROMA_TYPE; 426 427 if (vlsurf->video_buffer == NULL || 428 !screen->is_video_format_supported(screen, vlsurf->video_buffer->buffer_format, dec->profile)) { 429 430 /* destroy the old one */ 431 if (vlsurf->video_buffer) 432 vlsurf->video_buffer->destroy(vlsurf->video_buffer); 433 434 /* set the buffer format to the prefered one */ 435 vlsurf->templat.buffer_format = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_CAP_PREFERED_FORMAT); 436 437 /* and recreate the video buffer */ 438 vlsurf->video_buffer = dec->context->create_video_buffer(dec->context, &vlsurf->templat); 439 440 /* still no luck? get me out of here... */ 441 if (!vlsurf->video_buffer) 442 return VDP_STATUS_NO_IMPLEMENTATION; 443 } 444 445 memset(&desc, 0, sizeof(desc)); 446 desc.base.profile = dec->profile; 447 switch (u_reduce_video_profile(dec->profile)) { 448 case PIPE_VIDEO_CODEC_MPEG12: 449 ret = vlVdpDecoderRenderMpeg12(&desc.mpeg12, (VdpPictureInfoMPEG1Or2 *)picture_info); 450 break; 451 case PIPE_VIDEO_CODEC_MPEG4: 452 ret = vlVdpDecoderRenderMpeg4(&desc.mpeg4, (VdpPictureInfoMPEG4Part2 *)picture_info); 453 break; 454 case PIPE_VIDEO_CODEC_VC1: 455 ret = vlVdpDecoderRenderVC1(&desc.vc1, (VdpPictureInfoVC1 *)picture_info); 456 break; 457 case PIPE_VIDEO_CODEC_MPEG4_AVC: 458 ret = vlVdpDecoderRenderH264(&desc.h264, (VdpPictureInfoH264 *)picture_info); 459 break; 460 default: 461 return VDP_STATUS_INVALID_DECODER_PROFILE; 462 } 463 if (ret != VDP_STATUS_OK) 464 return ret; 465 466 for (i = 0; i < bitstream_buffer_count; ++i) { 467 buffers[i] = bitstream_buffers[i].bitstream; 468 sizes[i] = bitstream_buffers[i].bitstream_bytes; 469 } 470 471 dec->begin_frame(dec, vlsurf->video_buffer, &desc.base); 472 dec->decode_bitstream(dec, vlsurf->video_buffer, &desc.base, bitstream_buffer_count, buffers, sizes); 473 dec->end_frame(dec, vlsurf->video_buffer, &desc.base); 474 return ret; 475} 476