Home | History | Annotate | Download | only in vdpau
      1 /**************************************************************************
      2  *
      3  * Copyright 2010 Thomas Balling Srensen.
      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 VMWARE 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 "vl/vl_vlc.h"
     34 
     35 #include "vdpau_private.h"
     36 
     37 /**
     38  * Create a VdpDecoder.
     39  */
     40 VdpStatus
     41 vlVdpDecoderCreate(VdpDevice device,
     42                    VdpDecoderProfile profile,
     43                    uint32_t width, uint32_t height,
     44                    uint32_t max_references,
     45                    VdpDecoder *decoder)
     46 {
     47    struct pipe_video_codec templat = {};
     48    struct pipe_context *pipe;
     49    struct pipe_screen *screen;
     50    vlVdpDevice *dev;
     51    vlVdpDecoder *vldecoder;
     52    VdpStatus ret;
     53    bool supported;
     54    uint32_t maxwidth, maxheight;
     55 
     56    if (!decoder)
     57       return VDP_STATUS_INVALID_POINTER;
     58    *decoder = 0;
     59 
     60    if (!(width && height))
     61       return VDP_STATUS_INVALID_VALUE;
     62 
     63    templat.profile = ProfileToPipe(profile);
     64    if (templat.profile == PIPE_VIDEO_PROFILE_UNKNOWN)
     65       return VDP_STATUS_INVALID_DECODER_PROFILE;
     66 
     67    dev = vlGetDataHTAB(device);
     68    if (!dev)
     69       return VDP_STATUS_INVALID_HANDLE;
     70 
     71    pipe = dev->context;
     72    screen = dev->vscreen->pscreen;
     73 
     74    pipe_mutex_lock(dev->mutex);
     75 
     76    supported = screen->get_video_param
     77    (
     78       screen,
     79       templat.profile,
     80       PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
     81       PIPE_VIDEO_CAP_SUPPORTED
     82    );
     83    if (!supported) {
     84       pipe_mutex_unlock(dev->mutex);
     85       return VDP_STATUS_INVALID_DECODER_PROFILE;
     86    }
     87 
     88    maxwidth = screen->get_video_param
     89    (
     90       screen,
     91       templat.profile,
     92       PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
     93       PIPE_VIDEO_CAP_MAX_WIDTH
     94    );
     95    maxheight = screen->get_video_param
     96    (
     97       screen,
     98       templat.profile,
     99       PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
    100       PIPE_VIDEO_CAP_MAX_HEIGHT
    101    );
    102    if (width > maxwidth || height > maxheight) {
    103       pipe_mutex_unlock(dev->mutex);
    104       return VDP_STATUS_INVALID_SIZE;
    105    }
    106 
    107    vldecoder = CALLOC(1,sizeof(vlVdpDecoder));
    108    if (!vldecoder) {
    109       pipe_mutex_unlock(dev->mutex);
    110       return VDP_STATUS_RESOURCES;
    111    }
    112 
    113    DeviceReference(&vldecoder->device, dev);
    114 
    115    templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
    116    templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
    117    templat.width = width;
    118    templat.height = height;
    119    templat.max_references = max_references;
    120 
    121    if (u_reduce_video_profile(templat.profile) ==
    122        PIPE_VIDEO_FORMAT_MPEG4_AVC)
    123       templat.level = u_get_h264_level(templat.width, templat.height,
    124                             &templat.max_references);
    125 
    126    vldecoder->decoder = pipe->create_video_codec(pipe, &templat);
    127 
    128    if (!vldecoder->decoder) {
    129       ret = VDP_STATUS_ERROR;
    130       goto error_decoder;
    131    }
    132 
    133    *decoder = vlAddDataHTAB(vldecoder);
    134    if (*decoder == 0) {
    135       ret = VDP_STATUS_ERROR;
    136       goto error_handle;
    137    }
    138 
    139    pipe_mutex_init(vldecoder->mutex);
    140    pipe_mutex_unlock(dev->mutex);
    141 
    142    return VDP_STATUS_OK;
    143 
    144 error_handle:
    145    vldecoder->decoder->destroy(vldecoder->decoder);
    146 
    147 error_decoder:
    148    pipe_mutex_unlock(dev->mutex);
    149    DeviceReference(&vldecoder->device, NULL);
    150    FREE(vldecoder);
    151    return ret;
    152 }
    153 
    154 /**
    155  * Destroy a VdpDecoder.
    156  */
    157 VdpStatus
    158 vlVdpDecoderDestroy(VdpDecoder decoder)
    159 {
    160    vlVdpDecoder *vldecoder;
    161 
    162    vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
    163    if (!vldecoder)
    164       return VDP_STATUS_INVALID_HANDLE;
    165 
    166    pipe_mutex_lock(vldecoder->mutex);
    167    vldecoder->decoder->destroy(vldecoder->decoder);
    168    pipe_mutex_unlock(vldecoder->mutex);
    169    pipe_mutex_destroy(vldecoder->mutex);
    170 
    171    vlRemoveDataHTAB(decoder);
    172    DeviceReference(&vldecoder->device, NULL);
    173    FREE(vldecoder);
    174 
    175    return VDP_STATUS_OK;
    176 }
    177 
    178 /**
    179  * Retrieve the parameters used to create a VdpDecoder.
    180  */
    181 VdpStatus
    182 vlVdpDecoderGetParameters(VdpDecoder decoder,
    183                           VdpDecoderProfile *profile,
    184                           uint32_t *width,
    185                           uint32_t *height)
    186 {
    187    vlVdpDecoder *vldecoder;
    188 
    189    vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
    190    if (!vldecoder)
    191       return VDP_STATUS_INVALID_HANDLE;
    192 
    193    *profile = PipeToProfile(vldecoder->decoder->profile);
    194    *width = vldecoder->decoder->width;
    195    *height = vldecoder->decoder->height;
    196 
    197    return VDP_STATUS_OK;
    198 }
    199 
    200 static VdpStatus
    201 vlVdpGetReferenceFrame(VdpVideoSurface handle, struct pipe_video_buffer **ref_frame)
    202 {
    203    vlVdpSurface *surface;
    204 
    205    /* if surfaces equals VDP_STATUS_INVALID_HANDLE, they are not used */
    206    if (handle ==  VDP_INVALID_HANDLE) {
    207       *ref_frame = NULL;
    208       return VDP_STATUS_OK;
    209    }
    210 
    211    surface = vlGetDataHTAB(handle);
    212    if (!surface)
    213       return VDP_STATUS_INVALID_HANDLE;
    214 
    215    *ref_frame = surface->video_buffer;
    216    if (!*ref_frame)
    217          return VDP_STATUS_INVALID_HANDLE;
    218 
    219    return VDP_STATUS_OK;
    220 }
    221 
    222 /**
    223  * Decode a mpeg 1/2 video.
    224  */
    225 static VdpStatus
    226 vlVdpDecoderRenderMpeg12(struct pipe_mpeg12_picture_desc *picture,
    227                          VdpPictureInfoMPEG1Or2 *picture_info)
    228 {
    229    VdpStatus r;
    230 
    231    VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding MPEG12\n");
    232 
    233    r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]);
    234    if (r != VDP_STATUS_OK)
    235       return r;
    236 
    237    r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]);
    238    if (r != VDP_STATUS_OK)
    239       return r;
    240 
    241    picture->picture_coding_type = picture_info->picture_coding_type;
    242    picture->picture_structure = picture_info->picture_structure;
    243    picture->frame_pred_frame_dct = picture_info->frame_pred_frame_dct;
    244    picture->q_scale_type = picture_info->q_scale_type;
    245    picture->alternate_scan = picture_info->alternate_scan;
    246    picture->intra_vlc_format = picture_info->intra_vlc_format;
    247    picture->concealment_motion_vectors = picture_info->concealment_motion_vectors;
    248    picture->intra_dc_precision = picture_info->intra_dc_precision;
    249    picture->f_code[0][0] = picture_info->f_code[0][0] - 1;
    250    picture->f_code[0][1] = picture_info->f_code[0][1] - 1;
    251    picture->f_code[1][0] = picture_info->f_code[1][0] - 1;
    252    picture->f_code[1][1] = picture_info->f_code[1][1] - 1;
    253    picture->num_slices = picture_info->slice_count;
    254    picture->top_field_first = picture_info->top_field_first;
    255    picture->full_pel_forward_vector = picture_info->full_pel_forward_vector;
    256    picture->full_pel_backward_vector = picture_info->full_pel_backward_vector;
    257    picture->intra_matrix = picture_info->intra_quantizer_matrix;
    258    picture->non_intra_matrix = picture_info->non_intra_quantizer_matrix;
    259 
    260    return VDP_STATUS_OK;
    261 }
    262 
    263 /**
    264  * Decode a mpeg 4 video.
    265  */
    266 static VdpStatus
    267 vlVdpDecoderRenderMpeg4(struct pipe_mpeg4_picture_desc *picture,
    268                         VdpPictureInfoMPEG4Part2 *picture_info)
    269 {
    270    VdpStatus r;
    271    unsigned i;
    272 
    273    VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding MPEG4\n");
    274 
    275    r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]);
    276    if (r != VDP_STATUS_OK)
    277       return r;
    278 
    279    r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]);
    280    if (r != VDP_STATUS_OK)
    281       return r;
    282 
    283    for (i = 0; i < 2; ++i) {
    284       picture->trd[i] = picture_info->trd[i];
    285       picture->trb[i] = picture_info->trb[i];
    286    }
    287    picture->vop_time_increment_resolution = picture_info->vop_time_increment_resolution;
    288    picture->vop_coding_type = picture_info->vop_coding_type;
    289    picture->vop_fcode_forward = picture_info->vop_fcode_forward;
    290    picture->vop_fcode_backward = picture_info->vop_fcode_backward;
    291    picture->resync_marker_disable = picture_info->resync_marker_disable;
    292    picture->interlaced = picture_info->interlaced;
    293    picture->quant_type = picture_info->quant_type;
    294    picture->quarter_sample = picture_info->quarter_sample;
    295    picture->short_video_header = picture_info->short_video_header;
    296    picture->rounding_control = picture_info->rounding_control;
    297    picture->alternate_vertical_scan_flag = picture_info->alternate_vertical_scan_flag;
    298    picture->top_field_first = picture_info->top_field_first;
    299    picture->intra_matrix = picture_info->intra_quantizer_matrix;
    300    picture->non_intra_matrix = picture_info->non_intra_quantizer_matrix;
    301 
    302    return VDP_STATUS_OK;
    303 }
    304 
    305 static VdpStatus
    306 vlVdpDecoderRenderVC1(struct pipe_vc1_picture_desc *picture,
    307                       VdpPictureInfoVC1 *picture_info)
    308 {
    309    VdpStatus r;
    310 
    311    VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding VC-1\n");
    312 
    313    r = vlVdpGetReferenceFrame(picture_info->forward_reference, &picture->ref[0]);
    314    if (r != VDP_STATUS_OK)
    315       return r;
    316 
    317    r = vlVdpGetReferenceFrame(picture_info->backward_reference, &picture->ref[1]);
    318    if (r != VDP_STATUS_OK)
    319       return r;
    320 
    321    picture->slice_count = picture_info->slice_count;
    322    picture->picture_type = picture_info->picture_type;
    323    picture->frame_coding_mode = picture_info->frame_coding_mode;
    324    picture->postprocflag = picture_info->postprocflag;
    325    picture->pulldown = picture_info->pulldown;
    326    picture->interlace = picture_info->interlace;
    327    picture->tfcntrflag = picture_info->tfcntrflag;
    328    picture->finterpflag = picture_info->finterpflag;
    329    picture->psf = picture_info->psf;
    330    picture->dquant = picture_info->dquant;
    331    picture->panscan_flag = picture_info->panscan_flag;
    332    picture->refdist_flag = picture_info->refdist_flag;
    333    picture->quantizer = picture_info->quantizer;
    334    picture->extended_mv = picture_info->extended_mv;
    335    picture->extended_dmv = picture_info->extended_dmv;
    336    picture->overlap = picture_info->overlap;
    337    picture->vstransform = picture_info->vstransform;
    338    picture->loopfilter = picture_info->loopfilter;
    339    picture->fastuvmc = picture_info->fastuvmc;
    340    picture->range_mapy_flag = picture_info->range_mapy_flag;
    341    picture->range_mapy = picture_info->range_mapy;
    342    picture->range_mapuv_flag = picture_info->range_mapuv_flag;
    343    picture->range_mapuv = picture_info->range_mapuv;
    344    picture->multires = picture_info->multires;
    345    picture->syncmarker = picture_info->syncmarker;
    346    picture->rangered = picture_info->rangered;
    347    picture->maxbframes = picture_info->maxbframes;
    348    picture->deblockEnable = picture_info->deblockEnable;
    349    picture->pquant = picture_info->pquant;
    350 
    351    return VDP_STATUS_OK;
    352 }
    353 
    354 static VdpStatus
    355 vlVdpDecoderRenderH264(struct pipe_h264_picture_desc *picture,
    356                        VdpPictureInfoH264 *picture_info)
    357 {
    358    unsigned i;
    359 
    360    VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Decoding H264\n");
    361 
    362    picture->pps->sps->mb_adaptive_frame_field_flag = picture_info->mb_adaptive_frame_field_flag;
    363    picture->pps->sps->frame_mbs_only_flag = picture_info->frame_mbs_only_flag;
    364    picture->pps->sps->log2_max_frame_num_minus4 = picture_info->log2_max_frame_num_minus4;
    365    picture->pps->sps->pic_order_cnt_type = picture_info->pic_order_cnt_type;
    366    picture->pps->sps->log2_max_pic_order_cnt_lsb_minus4 = picture_info->log2_max_pic_order_cnt_lsb_minus4;
    367    picture->pps->sps->delta_pic_order_always_zero_flag = picture_info->delta_pic_order_always_zero_flag;
    368    picture->pps->sps->direct_8x8_inference_flag = picture_info->direct_8x8_inference_flag;
    369 
    370    picture->pps->transform_8x8_mode_flag = picture_info->transform_8x8_mode_flag;
    371    picture->pps->chroma_qp_index_offset = picture_info->chroma_qp_index_offset;
    372    picture->pps->second_chroma_qp_index_offset = picture_info->second_chroma_qp_index_offset;
    373    picture->pps->pic_init_qp_minus26 = picture_info->pic_init_qp_minus26;
    374    picture->pps->entropy_coding_mode_flag = picture_info->entropy_coding_mode_flag;
    375    picture->pps->deblocking_filter_control_present_flag = picture_info->deblocking_filter_control_present_flag;
    376    picture->pps->redundant_pic_cnt_present_flag = picture_info->redundant_pic_cnt_present_flag;
    377    picture->pps->constrained_intra_pred_flag = picture_info->constrained_intra_pred_flag;
    378    picture->pps->weighted_pred_flag = picture_info->weighted_pred_flag;
    379    picture->pps->weighted_bipred_idc = picture_info->weighted_bipred_idc;
    380    picture->pps->bottom_field_pic_order_in_frame_present_flag = picture_info->pic_order_present_flag;
    381    memcpy(picture->pps->ScalingList4x4, picture_info->scaling_lists_4x4, 6*16);
    382    memcpy(picture->pps->ScalingList8x8, picture_info->scaling_lists_8x8, 2*64);
    383 
    384    picture->slice_count = picture_info->slice_count;
    385    picture->field_order_cnt[0] = picture_info->field_order_cnt[0];
    386    picture->field_order_cnt[1] = picture_info->field_order_cnt[1];
    387    picture->is_reference = picture_info->is_reference;
    388    picture->frame_num = picture_info->frame_num;
    389    picture->field_pic_flag = picture_info->field_pic_flag;
    390    picture->bottom_field_flag = picture_info->bottom_field_flag;
    391    picture->num_ref_frames = picture_info->num_ref_frames;
    392 
    393    picture->num_ref_idx_l0_active_minus1 = picture_info->num_ref_idx_l0_active_minus1;
    394    picture->num_ref_idx_l1_active_minus1 = picture_info->num_ref_idx_l1_active_minus1;
    395 
    396    for (i = 0; i < 16; ++i) {
    397       VdpStatus ret = vlVdpGetReferenceFrame
    398       (
    399          picture_info->referenceFrames[i].surface,
    400          &picture->ref[i]
    401       );
    402       if (ret != VDP_STATUS_OK)
    403          return ret;
    404 
    405       picture->is_long_term[i] = picture_info->referenceFrames[i].is_long_term;
    406       picture->top_is_reference[i] = picture_info->referenceFrames[i].top_is_reference;
    407       picture->bottom_is_reference[i] = picture_info->referenceFrames[i].bottom_is_reference;
    408       picture->field_order_cnt_list[i][0] = picture_info->referenceFrames[i].field_order_cnt[0];
    409       picture->field_order_cnt_list[i][1] = picture_info->referenceFrames[i].field_order_cnt[1];
    410       picture->frame_num_list[i] = picture_info->referenceFrames[i].frame_idx;
    411    }
    412 
    413    return VDP_STATUS_OK;
    414 }
    415 
    416 static VdpStatus
    417 vlVdpDecoderRenderH265(struct pipe_h265_picture_desc *picture,
    418                        VdpPictureInfoHEVC *picture_info)
    419 {
    420    unsigned i;
    421 
    422    picture->pps->sps->chroma_format_idc = picture_info->chroma_format_idc;
    423    picture->pps->sps->separate_colour_plane_flag = picture_info->separate_colour_plane_flag;
    424    picture->pps->sps->pic_width_in_luma_samples = picture_info->pic_width_in_luma_samples;
    425    picture->pps->sps->pic_height_in_luma_samples = picture_info->pic_height_in_luma_samples;
    426    picture->pps->sps->bit_depth_luma_minus8 = picture_info->bit_depth_luma_minus8;
    427    picture->pps->sps->bit_depth_chroma_minus8 = picture_info->bit_depth_chroma_minus8;
    428    picture->pps->sps->log2_max_pic_order_cnt_lsb_minus4 = picture_info->log2_max_pic_order_cnt_lsb_minus4;
    429    picture->pps->sps->sps_max_dec_pic_buffering_minus1 = picture_info->sps_max_dec_pic_buffering_minus1;
    430    picture->pps->sps->log2_min_luma_coding_block_size_minus3 = picture_info->log2_min_luma_coding_block_size_minus3;
    431    picture->pps->sps->log2_diff_max_min_luma_coding_block_size = picture_info->log2_diff_max_min_luma_coding_block_size;
    432    picture->pps->sps->log2_min_transform_block_size_minus2 = picture_info->log2_min_transform_block_size_minus2;
    433    picture->pps->sps->log2_diff_max_min_transform_block_size = picture_info->log2_diff_max_min_transform_block_size;
    434    picture->pps->sps->max_transform_hierarchy_depth_inter = picture_info->max_transform_hierarchy_depth_inter;
    435    picture->pps->sps->max_transform_hierarchy_depth_intra = picture_info->max_transform_hierarchy_depth_intra;
    436    picture->pps->sps->scaling_list_enabled_flag = picture_info->scaling_list_enabled_flag;
    437    memcpy(picture->pps->sps->ScalingList4x4, picture_info->ScalingList4x4, 6*16);
    438    memcpy(picture->pps->sps->ScalingList8x8, picture_info->ScalingList8x8, 6*64);
    439    memcpy(picture->pps->sps->ScalingList16x16, picture_info->ScalingList16x16, 6*64);
    440    memcpy(picture->pps->sps->ScalingList32x32, picture_info->ScalingList32x32, 2*64);
    441    memcpy(picture->pps->sps->ScalingListDCCoeff16x16, picture_info->ScalingListDCCoeff16x16, 6);
    442    memcpy(picture->pps->sps->ScalingListDCCoeff32x32, picture_info->ScalingListDCCoeff32x32, 2);
    443    picture->pps->sps->amp_enabled_flag = picture_info->amp_enabled_flag;
    444    picture->pps->sps->sample_adaptive_offset_enabled_flag = picture_info->sample_adaptive_offset_enabled_flag;
    445    picture->pps->sps->pcm_enabled_flag = picture_info->pcm_enabled_flag;
    446    picture->pps->sps->pcm_sample_bit_depth_luma_minus1 = picture_info->pcm_sample_bit_depth_luma_minus1;
    447    picture->pps->sps->pcm_sample_bit_depth_chroma_minus1 = picture_info->pcm_sample_bit_depth_chroma_minus1;
    448    picture->pps->sps->log2_min_pcm_luma_coding_block_size_minus3 = picture_info->log2_min_pcm_luma_coding_block_size_minus3;
    449    picture->pps->sps->log2_diff_max_min_pcm_luma_coding_block_size = picture_info->log2_diff_max_min_pcm_luma_coding_block_size;
    450    picture->pps->sps->pcm_loop_filter_disabled_flag = picture_info->pcm_loop_filter_disabled_flag;
    451    picture->pps->sps->num_short_term_ref_pic_sets = picture_info->num_short_term_ref_pic_sets;
    452    picture->pps->sps->long_term_ref_pics_present_flag = picture_info->long_term_ref_pics_present_flag;
    453    picture->pps->sps->num_long_term_ref_pics_sps = picture_info->num_long_term_ref_pics_sps;
    454    picture->pps->sps->sps_temporal_mvp_enabled_flag = picture_info->sps_temporal_mvp_enabled_flag;
    455    picture->pps->sps->strong_intra_smoothing_enabled_flag = picture_info->strong_intra_smoothing_enabled_flag;
    456 
    457    picture->pps->dependent_slice_segments_enabled_flag = picture_info->dependent_slice_segments_enabled_flag;
    458    picture->pps->output_flag_present_flag = picture_info->output_flag_present_flag;
    459    picture->pps->num_extra_slice_header_bits = picture_info->num_extra_slice_header_bits;
    460    picture->pps->sign_data_hiding_enabled_flag = picture_info->sign_data_hiding_enabled_flag;
    461    picture->pps->cabac_init_present_flag = picture_info->cabac_init_present_flag;
    462    picture->pps->num_ref_idx_l0_default_active_minus1 = picture_info->num_ref_idx_l0_default_active_minus1;
    463    picture->pps->num_ref_idx_l1_default_active_minus1 = picture_info->num_ref_idx_l1_default_active_minus1;
    464    picture->pps->init_qp_minus26 = picture_info->init_qp_minus26;
    465    picture->pps->constrained_intra_pred_flag = picture_info->constrained_intra_pred_flag;
    466    picture->pps->transform_skip_enabled_flag = picture_info->transform_skip_enabled_flag;
    467    picture->pps->cu_qp_delta_enabled_flag = picture_info->cu_qp_delta_enabled_flag;
    468    picture->pps->diff_cu_qp_delta_depth = picture_info->diff_cu_qp_delta_depth;
    469    picture->pps->pps_cb_qp_offset = picture_info->pps_cb_qp_offset;
    470    picture->pps->pps_cr_qp_offset = picture_info->pps_cr_qp_offset;
    471    picture->pps->pps_slice_chroma_qp_offsets_present_flag = picture_info->pps_slice_chroma_qp_offsets_present_flag;
    472    picture->pps->weighted_pred_flag = picture_info->weighted_pred_flag;
    473    picture->pps->weighted_bipred_flag = picture_info->weighted_bipred_flag;
    474    picture->pps->transquant_bypass_enabled_flag = picture_info->transquant_bypass_enabled_flag;
    475    picture->pps->tiles_enabled_flag = picture_info->tiles_enabled_flag;
    476    picture->pps->entropy_coding_sync_enabled_flag = picture_info->entropy_coding_sync_enabled_flag;
    477    picture->pps->num_tile_columns_minus1 = picture_info->num_tile_columns_minus1;
    478    picture->pps->num_tile_rows_minus1 = picture_info->num_tile_rows_minus1;
    479    picture->pps->uniform_spacing_flag = picture_info->uniform_spacing_flag;
    480    memcpy(picture->pps->column_width_minus1, picture_info->column_width_minus1, 20 * 2);
    481    memcpy(picture->pps->row_height_minus1, picture_info->row_height_minus1, 22 * 2);
    482    picture->pps->loop_filter_across_tiles_enabled_flag = picture_info->loop_filter_across_tiles_enabled_flag;
    483    picture->pps->pps_loop_filter_across_slices_enabled_flag = picture_info->pps_loop_filter_across_slices_enabled_flag;
    484    picture->pps->deblocking_filter_control_present_flag = picture_info->deblocking_filter_control_present_flag;
    485    picture->pps->deblocking_filter_override_enabled_flag = picture_info->deblocking_filter_override_enabled_flag;
    486    picture->pps->pps_deblocking_filter_disabled_flag = picture_info->pps_deblocking_filter_disabled_flag;
    487    picture->pps->pps_beta_offset_div2 = picture_info->pps_beta_offset_div2;
    488    picture->pps->pps_tc_offset_div2 = picture_info->pps_tc_offset_div2;
    489    picture->pps->lists_modification_present_flag = picture_info->lists_modification_present_flag;
    490    picture->pps->log2_parallel_merge_level_minus2 = picture_info->log2_parallel_merge_level_minus2;
    491    picture->pps->slice_segment_header_extension_present_flag = picture_info->slice_segment_header_extension_present_flag;
    492 
    493    picture->IDRPicFlag = picture_info->IDRPicFlag;
    494    picture->RAPPicFlag = picture_info->RAPPicFlag;
    495    picture->CurrRpsIdx = picture_info->CurrRpsIdx;
    496    picture->NumPocTotalCurr = picture_info->NumPocTotalCurr;
    497    picture->NumDeltaPocsOfRefRpsIdx = picture_info->NumDeltaPocsOfRefRpsIdx;
    498    picture->NumShortTermPictureSliceHeaderBits = picture_info->NumShortTermPictureSliceHeaderBits;
    499    picture->NumLongTermPictureSliceHeaderBits = picture_info->NumLongTermPictureSliceHeaderBits;
    500    picture->CurrPicOrderCntVal = picture_info->CurrPicOrderCntVal;
    501 
    502    for (i = 0; i < 16; ++i) {
    503       VdpStatus ret = vlVdpGetReferenceFrame
    504       (
    505          picture_info->RefPics[i],
    506          &picture->ref[i]
    507       );
    508       if (ret != VDP_STATUS_OK)
    509          return ret;
    510 
    511       picture->PicOrderCntVal[i] = picture_info->PicOrderCntVal[i];
    512       picture->IsLongTerm[i] = picture_info->IsLongTerm[i];
    513    }
    514 
    515    picture->NumPocStCurrBefore = picture_info->NumPocStCurrBefore;
    516    picture->NumPocStCurrAfter = picture_info->NumPocStCurrAfter;
    517    picture->NumPocLtCurr = picture_info->NumPocLtCurr;
    518    memcpy(picture->RefPicSetStCurrBefore, picture_info->RefPicSetStCurrBefore, 8);
    519    memcpy(picture->RefPicSetStCurrAfter, picture_info->RefPicSetStCurrAfter, 8);
    520    memcpy(picture->RefPicSetLtCurr, picture_info->RefPicSetLtCurr, 8);
    521    picture->UseRefPicList = false;
    522 
    523    return VDP_STATUS_OK;
    524 }
    525 
    526 static void
    527 vlVdpDecoderFixVC1Startcode(uint32_t *num_buffers, const void *buffers[], unsigned sizes[])
    528 {
    529    static const uint8_t vc1_startcode[] = { 0x00, 0x00, 0x01, 0x0D };
    530    struct vl_vlc vlc;
    531    unsigned i;
    532 
    533    /* search the first 64 bytes for a startcode */
    534    vl_vlc_init(&vlc, *num_buffers, buffers, sizes);
    535    while (vl_vlc_search_byte(&vlc, 64*8, 0x00) && vl_vlc_bits_left(&vlc) >= 32) {
    536       uint32_t value = vl_vlc_peekbits(&vlc, 32);
    537       if (value == 0x0000010D ||
    538           value == 0x0000010C ||
    539           value == 0x0000010B)
    540          return;
    541       vl_vlc_eatbits(&vlc, 8);
    542    }
    543 
    544    /* none found, ok add one manually */
    545    VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Manually adding VC-1 startcode\n");
    546    for (i = *num_buffers; i > 0; --i) {
    547       buffers[i] = buffers[i - 1];
    548       sizes[i] = sizes[i - 1];
    549    }
    550    ++(*num_buffers);
    551    buffers[0] = vc1_startcode;
    552    sizes[0] = 4;
    553 }
    554 
    555 /**
    556  * Decode a compressed field/frame and render the result into a VdpVideoSurface.
    557  */
    558 VdpStatus
    559 vlVdpDecoderRender(VdpDecoder decoder,
    560                    VdpVideoSurface target,
    561                    VdpPictureInfo const *picture_info,
    562                    uint32_t bitstream_buffer_count,
    563                    VdpBitstreamBuffer const *bitstream_buffers)
    564 {
    565    const void * buffers[bitstream_buffer_count + 1];
    566    unsigned sizes[bitstream_buffer_count + 1];
    567    vlVdpDecoder *vldecoder;
    568    vlVdpSurface *vlsurf;
    569    VdpStatus ret;
    570    struct pipe_screen *screen;
    571    struct pipe_video_codec *dec;
    572    bool buffer_support[2];
    573    unsigned i;
    574    struct pipe_h264_sps sps_h264 = {};
    575    struct pipe_h264_pps pps_h264 = { &sps_h264 };
    576    struct pipe_h265_sps sps_h265 = {};
    577    struct pipe_h265_pps pps_h265 = { &sps_h265 };
    578    union {
    579       struct pipe_picture_desc base;
    580       struct pipe_mpeg12_picture_desc mpeg12;
    581       struct pipe_mpeg4_picture_desc mpeg4;
    582       struct pipe_vc1_picture_desc vc1;
    583       struct pipe_h264_picture_desc h264;
    584       struct pipe_h265_picture_desc h265;
    585    } desc;
    586 
    587    if (!(picture_info && bitstream_buffers))
    588       return VDP_STATUS_INVALID_POINTER;
    589 
    590    vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
    591    if (!vldecoder)
    592       return VDP_STATUS_INVALID_HANDLE;
    593    dec = vldecoder->decoder;
    594    screen = dec->context->screen;
    595 
    596    vlsurf = (vlVdpSurface *)vlGetDataHTAB(target);
    597    if (!vlsurf)
    598       return VDP_STATUS_INVALID_HANDLE;
    599 
    600    if (vlsurf->device != vldecoder->device)
    601       return VDP_STATUS_HANDLE_DEVICE_MISMATCH;
    602 
    603    if (vlsurf->video_buffer != NULL && vlsurf->video_buffer->chroma_format != dec->chroma_format)
    604       // TODO: Recreate decoder with correct chroma
    605       return VDP_STATUS_INVALID_CHROMA_TYPE;
    606 
    607    buffer_support[0] = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
    608                                                PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE);
    609    buffer_support[1] = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
    610                                                PIPE_VIDEO_CAP_SUPPORTS_INTERLACED);
    611 
    612    if (vlsurf->video_buffer == NULL ||
    613        !screen->is_video_format_supported(screen, vlsurf->video_buffer->buffer_format,
    614                                           dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM) ||
    615        !buffer_support[vlsurf->video_buffer->interlaced]) {
    616 
    617       pipe_mutex_lock(vlsurf->device->mutex);
    618 
    619       /* destroy the old one */
    620       if (vlsurf->video_buffer)
    621          vlsurf->video_buffer->destroy(vlsurf->video_buffer);
    622 
    623       /* set the buffer format to the prefered one */
    624       vlsurf->templat.buffer_format = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
    625                                                               PIPE_VIDEO_CAP_PREFERED_FORMAT);
    626 
    627       /* also set interlacing to decoders preferences */
    628       vlsurf->templat.interlaced = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
    629                                                            PIPE_VIDEO_CAP_PREFERS_INTERLACED);
    630 
    631       /* and recreate the video buffer */
    632       vlsurf->video_buffer = dec->context->create_video_buffer(dec->context, &vlsurf->templat);
    633 
    634       /* still no luck? get me out of here... */
    635       if (!vlsurf->video_buffer) {
    636          pipe_mutex_unlock(vlsurf->device->mutex);
    637          return VDP_STATUS_NO_IMPLEMENTATION;
    638       }
    639       vlVdpVideoSurfaceClear(vlsurf);
    640       pipe_mutex_unlock(vlsurf->device->mutex);
    641    }
    642 
    643    for (i = 0; i < bitstream_buffer_count; ++i) {
    644       buffers[i] = bitstream_buffers[i].bitstream;
    645       sizes[i] = bitstream_buffers[i].bitstream_bytes;
    646    }
    647 
    648    memset(&desc, 0, sizeof(desc));
    649    desc.base.profile = dec->profile;
    650    switch (u_reduce_video_profile(dec->profile)) {
    651    case PIPE_VIDEO_FORMAT_MPEG12:
    652       ret = vlVdpDecoderRenderMpeg12(&desc.mpeg12, (VdpPictureInfoMPEG1Or2 *)picture_info);
    653       break;
    654    case PIPE_VIDEO_FORMAT_MPEG4:
    655       ret = vlVdpDecoderRenderMpeg4(&desc.mpeg4, (VdpPictureInfoMPEG4Part2 *)picture_info);
    656       break;
    657    case PIPE_VIDEO_FORMAT_VC1:
    658       if (dec->profile == PIPE_VIDEO_PROFILE_VC1_ADVANCED)
    659          vlVdpDecoderFixVC1Startcode(&bitstream_buffer_count, buffers, sizes);
    660       ret = vlVdpDecoderRenderVC1(&desc.vc1, (VdpPictureInfoVC1 *)picture_info);
    661       break;
    662    case PIPE_VIDEO_FORMAT_MPEG4_AVC:
    663       desc.h264.pps = &pps_h264;
    664       ret = vlVdpDecoderRenderH264(&desc.h264, (VdpPictureInfoH264 *)picture_info);
    665       break;
    666    case PIPE_VIDEO_FORMAT_HEVC:
    667       desc.h265.pps = &pps_h265;
    668       ret = vlVdpDecoderRenderH265(&desc.h265, (VdpPictureInfoHEVC *)picture_info);
    669       break;
    670    default:
    671       return VDP_STATUS_INVALID_DECODER_PROFILE;
    672    }
    673 
    674    if (ret != VDP_STATUS_OK)
    675       return ret;
    676 
    677    pipe_mutex_lock(vldecoder->mutex);
    678    dec->begin_frame(dec, vlsurf->video_buffer, &desc.base);
    679    dec->decode_bitstream(dec, vlsurf->video_buffer, &desc.base, bitstream_buffer_count, buffers, sizes);
    680    dec->end_frame(dec, vlsurf->video_buffer, &desc.base);
    681    pipe_mutex_unlock(vldecoder->mutex);
    682    return ret;
    683 }
    684