Path: blob/21.2-virgl/src/gallium/frontends/vdpau/decode.c
4565 views
/**************************************************************************1*2* Copyright 2010 Thomas Balling Sørensen.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include "util/u_memory.h"28#include "util/u_math.h"29#include "util/u_debug.h"30#include "util/u_video.h"3132#include "vl/vl_vlc.h"3334#include "vdpau_private.h"3536/**37* Create a VdpDecoder.38*/39VdpStatus40vlVdpDecoderCreate(VdpDevice device,41VdpDecoderProfile profile,42uint32_t width, uint32_t height,43uint32_t max_references,44VdpDecoder *decoder)45{46struct pipe_video_codec templat = {};47struct pipe_context *pipe;48struct pipe_screen *screen;49vlVdpDevice *dev;50vlVdpDecoder *vldecoder;51VdpStatus ret;52bool supported;53uint32_t maxwidth, maxheight;5455if (!decoder)56return VDP_STATUS_INVALID_POINTER;57*decoder = 0;5859if (!(width && height))60return VDP_STATUS_INVALID_VALUE;6162templat.profile = ProfileToPipe(profile);63if (templat.profile == PIPE_VIDEO_PROFILE_UNKNOWN)64return VDP_STATUS_INVALID_DECODER_PROFILE;6566dev = vlGetDataHTAB(device);67if (!dev)68return VDP_STATUS_INVALID_HANDLE;6970pipe = dev->context;71screen = dev->vscreen->pscreen;7273mtx_lock(&dev->mutex);7475supported = screen->get_video_param76(77screen,78templat.profile,79PIPE_VIDEO_ENTRYPOINT_BITSTREAM,80PIPE_VIDEO_CAP_SUPPORTED81);82if (!supported) {83mtx_unlock(&dev->mutex);84return VDP_STATUS_INVALID_DECODER_PROFILE;85}8687maxwidth = screen->get_video_param88(89screen,90templat.profile,91PIPE_VIDEO_ENTRYPOINT_BITSTREAM,92PIPE_VIDEO_CAP_MAX_WIDTH93);94maxheight = screen->get_video_param95(96screen,97templat.profile,98PIPE_VIDEO_ENTRYPOINT_BITSTREAM,99PIPE_VIDEO_CAP_MAX_HEIGHT100);101if (width > maxwidth || height > maxheight) {102mtx_unlock(&dev->mutex);103return VDP_STATUS_INVALID_SIZE;104}105106vldecoder = CALLOC(1,sizeof(vlVdpDecoder));107if (!vldecoder) {108mtx_unlock(&dev->mutex);109return VDP_STATUS_RESOURCES;110}111112DeviceReference(&vldecoder->device, dev);113114templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;115templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;116templat.width = width;117templat.height = height;118templat.max_references = max_references;119120if (u_reduce_video_profile(templat.profile) ==121PIPE_VIDEO_FORMAT_MPEG4_AVC)122templat.level = u_get_h264_level(templat.width, templat.height,123&templat.max_references);124125vldecoder->decoder = pipe->create_video_codec(pipe, &templat);126127if (!vldecoder->decoder) {128ret = VDP_STATUS_ERROR;129goto error_decoder;130}131132*decoder = vlAddDataHTAB(vldecoder);133if (*decoder == 0) {134ret = VDP_STATUS_ERROR;135goto error_handle;136}137138(void) mtx_init(&vldecoder->mutex, mtx_plain);139mtx_unlock(&dev->mutex);140141return VDP_STATUS_OK;142143error_handle:144vldecoder->decoder->destroy(vldecoder->decoder);145146error_decoder:147mtx_unlock(&dev->mutex);148DeviceReference(&vldecoder->device, NULL);149FREE(vldecoder);150return ret;151}152153/**154* Destroy a VdpDecoder.155*/156VdpStatus157vlVdpDecoderDestroy(VdpDecoder decoder)158{159vlVdpDecoder *vldecoder;160161vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);162if (!vldecoder)163return VDP_STATUS_INVALID_HANDLE;164165mtx_lock(&vldecoder->mutex);166vldecoder->decoder->destroy(vldecoder->decoder);167mtx_unlock(&vldecoder->mutex);168mtx_destroy(&vldecoder->mutex);169170vlRemoveDataHTAB(decoder);171DeviceReference(&vldecoder->device, NULL);172FREE(vldecoder);173174return VDP_STATUS_OK;175}176177/**178* Retrieve the parameters used to create a VdpDecoder.179*/180VdpStatus181vlVdpDecoderGetParameters(VdpDecoder decoder,182VdpDecoderProfile *profile,183uint32_t *width,184uint32_t *height)185{186vlVdpDecoder *vldecoder;187188vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);189if (!vldecoder)190return VDP_STATUS_INVALID_HANDLE;191192*profile = PipeToProfile(vldecoder->decoder->profile);193*width = vldecoder->decoder->width;194*height = vldecoder->decoder->height;195196return VDP_STATUS_OK;197}198199static VdpStatus200vlVdpGetReferenceFrame(VdpVideoSurface handle, struct pipe_video_buffer **ref_frame)201{202vlVdpSurface *surface;203204/* if surfaces equals VDP_STATUS_INVALID_HANDLE, they are not used */205if (handle == VDP_INVALID_HANDLE) {206*ref_frame = NULL;207return VDP_STATUS_OK;208}209210surface = vlGetDataHTAB(handle);211if (!surface)212return VDP_STATUS_INVALID_HANDLE;213214*ref_frame = surface->video_buffer;215if (!*ref_frame)216return VDP_STATUS_INVALID_HANDLE;217218return VDP_STATUS_OK;219}220221/**222* Decode a mpeg 1/2 video.223*/224static VdpStatus225vlVdpDecoderRenderMpeg12(struct pipe_mpeg12_picture_desc *picture,226VdpPictureInfoMPEG1Or2 *picture_info)227{228VdpStatus r;229230VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding MPEG12\n");231232r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]);233if (r != VDP_STATUS_OK)234return r;235236r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]);237if (r != VDP_STATUS_OK)238return r;239240picture->picture_coding_type = picture_info->picture_coding_type;241picture->picture_structure = picture_info->picture_structure;242picture->frame_pred_frame_dct = picture_info->frame_pred_frame_dct;243picture->q_scale_type = picture_info->q_scale_type;244picture->alternate_scan = picture_info->alternate_scan;245picture->intra_vlc_format = picture_info->intra_vlc_format;246picture->concealment_motion_vectors = picture_info->concealment_motion_vectors;247picture->intra_dc_precision = picture_info->intra_dc_precision;248picture->f_code[0][0] = picture_info->f_code[0][0] - 1;249picture->f_code[0][1] = picture_info->f_code[0][1] - 1;250picture->f_code[1][0] = picture_info->f_code[1][0] - 1;251picture->f_code[1][1] = picture_info->f_code[1][1] - 1;252picture->num_slices = picture_info->slice_count;253picture->top_field_first = picture_info->top_field_first;254picture->full_pel_forward_vector = picture_info->full_pel_forward_vector;255picture->full_pel_backward_vector = picture_info->full_pel_backward_vector;256picture->intra_matrix = picture_info->intra_quantizer_matrix;257picture->non_intra_matrix = picture_info->non_intra_quantizer_matrix;258259return VDP_STATUS_OK;260}261262/**263* Decode a mpeg 4 video.264*/265static VdpStatus266vlVdpDecoderRenderMpeg4(struct pipe_mpeg4_picture_desc *picture,267VdpPictureInfoMPEG4Part2 *picture_info)268{269VdpStatus r;270unsigned i;271272VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding MPEG4\n");273274r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]);275if (r != VDP_STATUS_OK)276return r;277278r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]);279if (r != VDP_STATUS_OK)280return r;281282for (i = 0; i < 2; ++i) {283picture->trd[i] = picture_info->trd[i];284picture->trb[i] = picture_info->trb[i];285}286picture->vop_time_increment_resolution = picture_info->vop_time_increment_resolution;287picture->vop_coding_type = picture_info->vop_coding_type;288picture->vop_fcode_forward = picture_info->vop_fcode_forward;289picture->vop_fcode_backward = picture_info->vop_fcode_backward;290picture->resync_marker_disable = picture_info->resync_marker_disable;291picture->interlaced = picture_info->interlaced;292picture->quant_type = picture_info->quant_type;293picture->quarter_sample = picture_info->quarter_sample;294picture->short_video_header = picture_info->short_video_header;295picture->rounding_control = picture_info->rounding_control;296picture->alternate_vertical_scan_flag = picture_info->alternate_vertical_scan_flag;297picture->top_field_first = picture_info->top_field_first;298picture->intra_matrix = picture_info->intra_quantizer_matrix;299picture->non_intra_matrix = picture_info->non_intra_quantizer_matrix;300301return VDP_STATUS_OK;302}303304static VdpStatus305vlVdpDecoderRenderVC1(struct pipe_vc1_picture_desc *picture,306VdpPictureInfoVC1 *picture_info)307{308VdpStatus r;309310VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding VC-1\n");311312r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]);313if (r != VDP_STATUS_OK)314return r;315316r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]);317if (r != VDP_STATUS_OK)318return r;319320picture->slice_count = picture_info->slice_count;321picture->picture_type = picture_info->picture_type;322picture->frame_coding_mode = picture_info->frame_coding_mode;323picture->postprocflag = picture_info->postprocflag;324picture->pulldown = picture_info->pulldown;325picture->interlace = picture_info->interlace;326picture->tfcntrflag = picture_info->tfcntrflag;327picture->finterpflag = picture_info->finterpflag;328picture->psf = picture_info->psf;329picture->dquant = picture_info->dquant;330picture->panscan_flag = picture_info->panscan_flag;331picture->refdist_flag = picture_info->refdist_flag;332picture->quantizer = picture_info->quantizer;333picture->extended_mv = picture_info->extended_mv;334picture->extended_dmv = picture_info->extended_dmv;335picture->overlap = picture_info->overlap;336picture->vstransform = picture_info->vstransform;337picture->loopfilter = picture_info->loopfilter;338picture->fastuvmc = picture_info->fastuvmc;339picture->range_mapy_flag = picture_info->range_mapy_flag;340picture->range_mapy = picture_info->range_mapy;341picture->range_mapuv_flag = picture_info->range_mapuv_flag;342picture->range_mapuv = picture_info->range_mapuv;343picture->multires = picture_info->multires;344picture->syncmarker = picture_info->syncmarker;345picture->rangered = picture_info->rangered;346picture->maxbframes = picture_info->maxbframes;347picture->deblockEnable = picture_info->deblockEnable;348picture->pquant = picture_info->pquant;349350return VDP_STATUS_OK;351}352353static VdpStatus354vlVdpDecoderRenderH264(struct pipe_h264_picture_desc *picture,355VdpPictureInfoH264 *picture_info)356{357unsigned i;358359VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding H264\n");360361picture->pps->sps->mb_adaptive_frame_field_flag = picture_info->mb_adaptive_frame_field_flag;362picture->pps->sps->frame_mbs_only_flag = picture_info->frame_mbs_only_flag;363picture->pps->sps->log2_max_frame_num_minus4 = picture_info->log2_max_frame_num_minus4;364picture->pps->sps->pic_order_cnt_type = picture_info->pic_order_cnt_type;365picture->pps->sps->log2_max_pic_order_cnt_lsb_minus4 = picture_info->log2_max_pic_order_cnt_lsb_minus4;366picture->pps->sps->delta_pic_order_always_zero_flag = picture_info->delta_pic_order_always_zero_flag;367picture->pps->sps->direct_8x8_inference_flag = picture_info->direct_8x8_inference_flag;368369picture->pps->transform_8x8_mode_flag = picture_info->transform_8x8_mode_flag;370picture->pps->chroma_qp_index_offset = picture_info->chroma_qp_index_offset;371picture->pps->second_chroma_qp_index_offset = picture_info->second_chroma_qp_index_offset;372picture->pps->pic_init_qp_minus26 = picture_info->pic_init_qp_minus26;373picture->pps->entropy_coding_mode_flag = picture_info->entropy_coding_mode_flag;374picture->pps->deblocking_filter_control_present_flag = picture_info->deblocking_filter_control_present_flag;375picture->pps->redundant_pic_cnt_present_flag = picture_info->redundant_pic_cnt_present_flag;376picture->pps->constrained_intra_pred_flag = picture_info->constrained_intra_pred_flag;377picture->pps->weighted_pred_flag = picture_info->weighted_pred_flag;378picture->pps->weighted_bipred_idc = picture_info->weighted_bipred_idc;379picture->pps->bottom_field_pic_order_in_frame_present_flag = picture_info->pic_order_present_flag;380memcpy(picture->pps->ScalingList4x4, picture_info->scaling_lists_4x4, 6*16);381memcpy(picture->pps->ScalingList8x8, picture_info->scaling_lists_8x8, 2*64);382383picture->slice_count = picture_info->slice_count;384picture->field_order_cnt[0] = picture_info->field_order_cnt[0];385picture->field_order_cnt[1] = picture_info->field_order_cnt[1];386picture->is_reference = picture_info->is_reference;387picture->frame_num = picture_info->frame_num;388picture->field_pic_flag = picture_info->field_pic_flag;389picture->bottom_field_flag = picture_info->bottom_field_flag;390picture->num_ref_frames = picture_info->num_ref_frames;391392picture->num_ref_idx_l0_active_minus1 = picture_info->num_ref_idx_l0_active_minus1;393picture->num_ref_idx_l1_active_minus1 = picture_info->num_ref_idx_l1_active_minus1;394395for (i = 0; i < 16; ++i) {396VdpStatus ret = vlVdpGetReferenceFrame397(398picture_info->referenceFrames[i].surface,399&picture->ref[i]400);401if (ret != VDP_STATUS_OK)402return ret;403404picture->is_long_term[i] = picture_info->referenceFrames[i].is_long_term;405picture->top_is_reference[i] = picture_info->referenceFrames[i].top_is_reference;406picture->bottom_is_reference[i] = picture_info->referenceFrames[i].bottom_is_reference;407picture->field_order_cnt_list[i][0] = picture_info->referenceFrames[i].field_order_cnt[0];408picture->field_order_cnt_list[i][1] = picture_info->referenceFrames[i].field_order_cnt[1];409picture->frame_num_list[i] = picture_info->referenceFrames[i].frame_idx;410}411412return VDP_STATUS_OK;413}414415static VdpStatus416vlVdpDecoderRenderH265(struct pipe_h265_picture_desc *picture,417VdpPictureInfoHEVC *picture_info)418{419unsigned i;420421picture->pps->sps->chroma_format_idc = picture_info->chroma_format_idc;422picture->pps->sps->separate_colour_plane_flag = picture_info->separate_colour_plane_flag;423picture->pps->sps->pic_width_in_luma_samples = picture_info->pic_width_in_luma_samples;424picture->pps->sps->pic_height_in_luma_samples = picture_info->pic_height_in_luma_samples;425picture->pps->sps->bit_depth_luma_minus8 = picture_info->bit_depth_luma_minus8;426picture->pps->sps->bit_depth_chroma_minus8 = picture_info->bit_depth_chroma_minus8;427picture->pps->sps->log2_max_pic_order_cnt_lsb_minus4 = picture_info->log2_max_pic_order_cnt_lsb_minus4;428picture->pps->sps->sps_max_dec_pic_buffering_minus1 = picture_info->sps_max_dec_pic_buffering_minus1;429picture->pps->sps->log2_min_luma_coding_block_size_minus3 = picture_info->log2_min_luma_coding_block_size_minus3;430picture->pps->sps->log2_diff_max_min_luma_coding_block_size = picture_info->log2_diff_max_min_luma_coding_block_size;431picture->pps->sps->log2_min_transform_block_size_minus2 = picture_info->log2_min_transform_block_size_minus2;432picture->pps->sps->log2_diff_max_min_transform_block_size = picture_info->log2_diff_max_min_transform_block_size;433picture->pps->sps->max_transform_hierarchy_depth_inter = picture_info->max_transform_hierarchy_depth_inter;434picture->pps->sps->max_transform_hierarchy_depth_intra = picture_info->max_transform_hierarchy_depth_intra;435picture->pps->sps->scaling_list_enabled_flag = picture_info->scaling_list_enabled_flag;436memcpy(picture->pps->sps->ScalingList4x4, picture_info->ScalingList4x4, 6*16);437memcpy(picture->pps->sps->ScalingList8x8, picture_info->ScalingList8x8, 6*64);438memcpy(picture->pps->sps->ScalingList16x16, picture_info->ScalingList16x16, 6*64);439memcpy(picture->pps->sps->ScalingList32x32, picture_info->ScalingList32x32, 2*64);440memcpy(picture->pps->sps->ScalingListDCCoeff16x16, picture_info->ScalingListDCCoeff16x16, 6);441memcpy(picture->pps->sps->ScalingListDCCoeff32x32, picture_info->ScalingListDCCoeff32x32, 2);442picture->pps->sps->amp_enabled_flag = picture_info->amp_enabled_flag;443picture->pps->sps->sample_adaptive_offset_enabled_flag = picture_info->sample_adaptive_offset_enabled_flag;444picture->pps->sps->pcm_enabled_flag = picture_info->pcm_enabled_flag;445picture->pps->sps->pcm_sample_bit_depth_luma_minus1 = picture_info->pcm_sample_bit_depth_luma_minus1;446picture->pps->sps->pcm_sample_bit_depth_chroma_minus1 = picture_info->pcm_sample_bit_depth_chroma_minus1;447picture->pps->sps->log2_min_pcm_luma_coding_block_size_minus3 = picture_info->log2_min_pcm_luma_coding_block_size_minus3;448picture->pps->sps->log2_diff_max_min_pcm_luma_coding_block_size = picture_info->log2_diff_max_min_pcm_luma_coding_block_size;449picture->pps->sps->pcm_loop_filter_disabled_flag = picture_info->pcm_loop_filter_disabled_flag;450picture->pps->sps->num_short_term_ref_pic_sets = picture_info->num_short_term_ref_pic_sets;451picture->pps->sps->long_term_ref_pics_present_flag = picture_info->long_term_ref_pics_present_flag;452picture->pps->sps->num_long_term_ref_pics_sps = picture_info->num_long_term_ref_pics_sps;453picture->pps->sps->sps_temporal_mvp_enabled_flag = picture_info->sps_temporal_mvp_enabled_flag;454picture->pps->sps->strong_intra_smoothing_enabled_flag = picture_info->strong_intra_smoothing_enabled_flag;455456picture->pps->dependent_slice_segments_enabled_flag = picture_info->dependent_slice_segments_enabled_flag;457picture->pps->output_flag_present_flag = picture_info->output_flag_present_flag;458picture->pps->num_extra_slice_header_bits = picture_info->num_extra_slice_header_bits;459picture->pps->sign_data_hiding_enabled_flag = picture_info->sign_data_hiding_enabled_flag;460picture->pps->cabac_init_present_flag = picture_info->cabac_init_present_flag;461picture->pps->num_ref_idx_l0_default_active_minus1 = picture_info->num_ref_idx_l0_default_active_minus1;462picture->pps->num_ref_idx_l1_default_active_minus1 = picture_info->num_ref_idx_l1_default_active_minus1;463picture->pps->init_qp_minus26 = picture_info->init_qp_minus26;464picture->pps->constrained_intra_pred_flag = picture_info->constrained_intra_pred_flag;465picture->pps->transform_skip_enabled_flag = picture_info->transform_skip_enabled_flag;466picture->pps->cu_qp_delta_enabled_flag = picture_info->cu_qp_delta_enabled_flag;467picture->pps->diff_cu_qp_delta_depth = picture_info->diff_cu_qp_delta_depth;468picture->pps->pps_cb_qp_offset = picture_info->pps_cb_qp_offset;469picture->pps->pps_cr_qp_offset = picture_info->pps_cr_qp_offset;470picture->pps->pps_slice_chroma_qp_offsets_present_flag = picture_info->pps_slice_chroma_qp_offsets_present_flag;471picture->pps->weighted_pred_flag = picture_info->weighted_pred_flag;472picture->pps->weighted_bipred_flag = picture_info->weighted_bipred_flag;473picture->pps->transquant_bypass_enabled_flag = picture_info->transquant_bypass_enabled_flag;474picture->pps->tiles_enabled_flag = picture_info->tiles_enabled_flag;475picture->pps->entropy_coding_sync_enabled_flag = picture_info->entropy_coding_sync_enabled_flag;476picture->pps->num_tile_columns_minus1 = picture_info->num_tile_columns_minus1;477picture->pps->num_tile_rows_minus1 = picture_info->num_tile_rows_minus1;478picture->pps->uniform_spacing_flag = picture_info->uniform_spacing_flag;479memcpy(picture->pps->column_width_minus1, picture_info->column_width_minus1, 20 * 2);480memcpy(picture->pps->row_height_minus1, picture_info->row_height_minus1, 22 * 2);481picture->pps->loop_filter_across_tiles_enabled_flag = picture_info->loop_filter_across_tiles_enabled_flag;482picture->pps->pps_loop_filter_across_slices_enabled_flag = picture_info->pps_loop_filter_across_slices_enabled_flag;483picture->pps->deblocking_filter_control_present_flag = picture_info->deblocking_filter_control_present_flag;484picture->pps->deblocking_filter_override_enabled_flag = picture_info->deblocking_filter_override_enabled_flag;485picture->pps->pps_deblocking_filter_disabled_flag = picture_info->pps_deblocking_filter_disabled_flag;486picture->pps->pps_beta_offset_div2 = picture_info->pps_beta_offset_div2;487picture->pps->pps_tc_offset_div2 = picture_info->pps_tc_offset_div2;488picture->pps->lists_modification_present_flag = picture_info->lists_modification_present_flag;489picture->pps->log2_parallel_merge_level_minus2 = picture_info->log2_parallel_merge_level_minus2;490picture->pps->slice_segment_header_extension_present_flag = picture_info->slice_segment_header_extension_present_flag;491492picture->IDRPicFlag = picture_info->IDRPicFlag;493picture->RAPPicFlag = picture_info->RAPPicFlag;494picture->CurrRpsIdx = picture_info->CurrRpsIdx;495picture->NumPocTotalCurr = picture_info->NumPocTotalCurr;496picture->NumDeltaPocsOfRefRpsIdx = picture_info->NumDeltaPocsOfRefRpsIdx;497picture->NumShortTermPictureSliceHeaderBits = picture_info->NumShortTermPictureSliceHeaderBits;498picture->NumLongTermPictureSliceHeaderBits = picture_info->NumLongTermPictureSliceHeaderBits;499picture->CurrPicOrderCntVal = picture_info->CurrPicOrderCntVal;500501for (i = 0; i < 16; ++i) {502VdpStatus ret = vlVdpGetReferenceFrame503(504picture_info->RefPics[i],505&picture->ref[i]506);507if (ret != VDP_STATUS_OK)508return ret;509510picture->PicOrderCntVal[i] = picture_info->PicOrderCntVal[i];511picture->IsLongTerm[i] = picture_info->IsLongTerm[i];512}513514picture->NumPocStCurrBefore = picture_info->NumPocStCurrBefore;515picture->NumPocStCurrAfter = picture_info->NumPocStCurrAfter;516picture->NumPocLtCurr = picture_info->NumPocLtCurr;517memcpy(picture->RefPicSetStCurrBefore, picture_info->RefPicSetStCurrBefore, 8);518memcpy(picture->RefPicSetStCurrAfter, picture_info->RefPicSetStCurrAfter, 8);519memcpy(picture->RefPicSetLtCurr, picture_info->RefPicSetLtCurr, 8);520picture->UseRefPicList = false;521picture->UseStRpsBits = false;522523return VDP_STATUS_OK;524}525526static void527vlVdpDecoderFixVC1Startcode(uint32_t *num_buffers, const void *buffers[], unsigned sizes[])528{529static const uint8_t vc1_startcode[] = { 0x00, 0x00, 0x01, 0x0D };530struct vl_vlc vlc = {};531unsigned i;532533/* search the first 64 bytes for a startcode */534vl_vlc_init(&vlc, *num_buffers, buffers, sizes);535while (vl_vlc_search_byte(&vlc, 64*8, 0x00) && vl_vlc_bits_left(&vlc) >= 32) {536uint32_t value = vl_vlc_peekbits(&vlc, 32);537if (value == 0x0000010D ||538value == 0x0000010C ||539value == 0x0000010B)540return;541vl_vlc_eatbits(&vlc, 8);542}543544/* none found, ok add one manually */545VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Manually adding VC-1 startcode\n");546for (i = *num_buffers; i > 0; --i) {547buffers[i] = buffers[i - 1];548sizes[i] = sizes[i - 1];549}550++(*num_buffers);551buffers[0] = vc1_startcode;552sizes[0] = 4;553}554555/**556* Decode a compressed field/frame and render the result into a VdpVideoSurface.557*/558VdpStatus559vlVdpDecoderRender(VdpDecoder decoder,560VdpVideoSurface target,561VdpPictureInfo const *picture_info,562uint32_t bitstream_buffer_count,563VdpBitstreamBuffer const *bitstream_buffers)564{565const void * buffers[bitstream_buffer_count + 1];566unsigned sizes[bitstream_buffer_count + 1];567vlVdpDecoder *vldecoder;568vlVdpSurface *vlsurf;569VdpStatus ret;570struct pipe_screen *screen;571struct pipe_video_codec *dec;572bool buffer_support[2];573unsigned i;574struct pipe_h264_sps sps_h264 = {};575struct pipe_h264_pps pps_h264 = { &sps_h264 };576struct pipe_h265_sps sps_h265 = {};577struct pipe_h265_pps pps_h265 = { &sps_h265 };578union {579struct pipe_picture_desc base;580struct pipe_mpeg12_picture_desc mpeg12;581struct pipe_mpeg4_picture_desc mpeg4;582struct pipe_vc1_picture_desc vc1;583struct pipe_h264_picture_desc h264;584struct pipe_h265_picture_desc h265;585} desc;586587if (!(picture_info && bitstream_buffers))588return VDP_STATUS_INVALID_POINTER;589590vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);591if (!vldecoder)592return VDP_STATUS_INVALID_HANDLE;593dec = vldecoder->decoder;594screen = dec->context->screen;595596vlsurf = (vlVdpSurface *)vlGetDataHTAB(target);597if (!vlsurf)598return VDP_STATUS_INVALID_HANDLE;599600if (vlsurf->device != vldecoder->device)601return VDP_STATUS_HANDLE_DEVICE_MISMATCH;602603if (vlsurf->video_buffer != NULL &&604pipe_format_to_chroma_format(vlsurf->video_buffer->buffer_format) != dec->chroma_format)605// TODO: Recreate decoder with correct chroma606return VDP_STATUS_INVALID_CHROMA_TYPE;607608buffer_support[0] = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,609PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE);610buffer_support[1] = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,611PIPE_VIDEO_CAP_SUPPORTS_INTERLACED);612613if (vlsurf->video_buffer == NULL ||614!screen->is_video_format_supported(screen, vlsurf->video_buffer->buffer_format,615dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM) ||616!buffer_support[vlsurf->video_buffer->interlaced]) {617618mtx_lock(&vlsurf->device->mutex);619620/* destroy the old one */621if (vlsurf->video_buffer)622vlsurf->video_buffer->destroy(vlsurf->video_buffer);623624/* set the buffer format to the prefered one */625vlsurf->templat.buffer_format = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,626PIPE_VIDEO_CAP_PREFERED_FORMAT);627628/* also set interlacing to decoders preferences */629vlsurf->templat.interlaced = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,630PIPE_VIDEO_CAP_PREFERS_INTERLACED);631632/* and recreate the video buffer */633vlsurf->video_buffer = dec->context->create_video_buffer(dec->context, &vlsurf->templat);634635/* still no luck? get me out of here... */636if (!vlsurf->video_buffer) {637mtx_unlock(&vlsurf->device->mutex);638return VDP_STATUS_NO_IMPLEMENTATION;639}640vlVdpVideoSurfaceClear(vlsurf);641mtx_unlock(&vlsurf->device->mutex);642}643644for (i = 0; i < bitstream_buffer_count; ++i) {645buffers[i] = bitstream_buffers[i].bitstream;646sizes[i] = bitstream_buffers[i].bitstream_bytes;647}648649memset(&desc, 0, sizeof(desc));650desc.base.profile = dec->profile;651switch (u_reduce_video_profile(dec->profile)) {652case PIPE_VIDEO_FORMAT_MPEG12:653ret = vlVdpDecoderRenderMpeg12(&desc.mpeg12, (VdpPictureInfoMPEG1Or2 *)picture_info);654break;655case PIPE_VIDEO_FORMAT_MPEG4:656ret = vlVdpDecoderRenderMpeg4(&desc.mpeg4, (VdpPictureInfoMPEG4Part2 *)picture_info);657break;658case PIPE_VIDEO_FORMAT_VC1:659if (dec->profile == PIPE_VIDEO_PROFILE_VC1_ADVANCED)660vlVdpDecoderFixVC1Startcode(&bitstream_buffer_count, buffers, sizes);661ret = vlVdpDecoderRenderVC1(&desc.vc1, (VdpPictureInfoVC1 *)picture_info);662break;663case PIPE_VIDEO_FORMAT_MPEG4_AVC:664desc.h264.pps = &pps_h264;665ret = vlVdpDecoderRenderH264(&desc.h264, (VdpPictureInfoH264 *)picture_info);666break;667case PIPE_VIDEO_FORMAT_HEVC:668desc.h265.pps = &pps_h265;669ret = vlVdpDecoderRenderH265(&desc.h265, (VdpPictureInfoHEVC *)picture_info);670break;671default:672return VDP_STATUS_INVALID_DECODER_PROFILE;673}674675if (ret != VDP_STATUS_OK)676return ret;677678mtx_lock(&vldecoder->mutex);679dec->begin_frame(dec, vlsurf->video_buffer, &desc.base);680dec->decode_bitstream(dec, vlsurf->video_buffer, &desc.base, bitstream_buffer_count, buffers, sizes);681dec->end_frame(dec, vlsurf->video_buffer, &desc.base);682mtx_unlock(&vldecoder->mutex);683return ret;684}685686687