Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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 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 void
  417. vlVdpDecoderFixVC1Startcode(uint32_t *num_buffers, const void *buffers[], unsigned sizes[])
  418. {
  419.    static const uint8_t vc1_startcode[] = { 0x00, 0x00, 0x01, 0x0D };
  420.    struct vl_vlc vlc;
  421.    unsigned i;
  422.  
  423.    /* search the first 64 bytes for a startcode */
  424.    vl_vlc_init(&vlc, *num_buffers, buffers, sizes);
  425.    while (vl_vlc_search_byte(&vlc, 64*8, 0x00) && vl_vlc_bits_left(&vlc) >= 32) {
  426.       uint32_t value = vl_vlc_peekbits(&vlc, 32);
  427.       if (value == 0x0000010D ||
  428.           value == 0x0000010C ||
  429.           value == 0x0000010B)
  430.          return;
  431.       vl_vlc_eatbits(&vlc, 8);
  432.    }
  433.  
  434.    /* none found, ok add one manually */
  435.    VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Manually adding VC-1 startcode\n");
  436.    for (i = *num_buffers; i > 0; --i) {
  437.       buffers[i] = buffers[i - 1];
  438.       sizes[i] = sizes[i - 1];
  439.    }
  440.    ++(*num_buffers);
  441.    buffers[0] = vc1_startcode;
  442.    sizes[0] = 4;
  443. }
  444.  
  445. /**
  446.  * Decode a compressed field/frame and render the result into a VdpVideoSurface.
  447.  */
  448. VdpStatus
  449. vlVdpDecoderRender(VdpDecoder decoder,
  450.                    VdpVideoSurface target,
  451.                    VdpPictureInfo const *picture_info,
  452.                    uint32_t bitstream_buffer_count,
  453.                    VdpBitstreamBuffer const *bitstream_buffers)
  454. {
  455.    const void * buffers[bitstream_buffer_count + 1];
  456.    unsigned sizes[bitstream_buffer_count + 1];
  457.    vlVdpDecoder *vldecoder;
  458.    vlVdpSurface *vlsurf;
  459.    VdpStatus ret;
  460.    struct pipe_screen *screen;
  461.    struct pipe_video_codec *dec;
  462.    bool buffer_support[2];
  463.    unsigned i;
  464.    struct pipe_h264_sps sps = {};
  465.    struct pipe_h264_pps pps = { &sps };
  466.    union {
  467.       struct pipe_picture_desc base;
  468.       struct pipe_mpeg12_picture_desc mpeg12;
  469.       struct pipe_mpeg4_picture_desc mpeg4;
  470.       struct pipe_vc1_picture_desc vc1;
  471.       struct pipe_h264_picture_desc h264;
  472.    } desc;
  473.  
  474.    if (!(picture_info && bitstream_buffers))
  475.       return VDP_STATUS_INVALID_POINTER;
  476.  
  477.    vldecoder = (vlVdpDecoder *)vlGetDataHTAB(decoder);
  478.    if (!vldecoder)
  479.       return VDP_STATUS_INVALID_HANDLE;
  480.    dec = vldecoder->decoder;
  481.    screen = dec->context->screen;
  482.  
  483.    vlsurf = (vlVdpSurface *)vlGetDataHTAB(target);
  484.    if (!vlsurf)
  485.       return VDP_STATUS_INVALID_HANDLE;
  486.  
  487.    if (vlsurf->device != vldecoder->device)
  488.       return VDP_STATUS_HANDLE_DEVICE_MISMATCH;
  489.  
  490.    if (vlsurf->video_buffer != NULL && vlsurf->video_buffer->chroma_format != dec->chroma_format)
  491.       // TODO: Recreate decoder with correct chroma
  492.       return VDP_STATUS_INVALID_CHROMA_TYPE;
  493.  
  494.    buffer_support[0] = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
  495.                                                PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE);
  496.    buffer_support[1] = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
  497.                                                PIPE_VIDEO_CAP_SUPPORTS_INTERLACED);
  498.  
  499.    if (vlsurf->video_buffer == NULL ||
  500.        !screen->is_video_format_supported(screen, vlsurf->video_buffer->buffer_format,
  501.                                           dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM) ||
  502.        !buffer_support[vlsurf->video_buffer->interlaced]) {
  503.  
  504.       pipe_mutex_lock(vlsurf->device->mutex);
  505.  
  506.       /* destroy the old one */
  507.       if (vlsurf->video_buffer)
  508.          vlsurf->video_buffer->destroy(vlsurf->video_buffer);
  509.  
  510.       /* set the buffer format to the prefered one */
  511.       vlsurf->templat.buffer_format = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
  512.                                                               PIPE_VIDEO_CAP_PREFERED_FORMAT);
  513.  
  514.       /* also set interlacing to decoders preferences */
  515.       vlsurf->templat.interlaced = screen->get_video_param(screen, dec->profile, PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
  516.                                                            PIPE_VIDEO_CAP_PREFERS_INTERLACED);
  517.  
  518.       /* and recreate the video buffer */
  519.       vlsurf->video_buffer = dec->context->create_video_buffer(dec->context, &vlsurf->templat);
  520.  
  521.       /* still no luck? get me out of here... */
  522.       if (!vlsurf->video_buffer) {
  523.          pipe_mutex_unlock(vlsurf->device->mutex);
  524.          return VDP_STATUS_NO_IMPLEMENTATION;
  525.       }
  526.       vlVdpVideoSurfaceClear(vlsurf);
  527.       pipe_mutex_unlock(vlsurf->device->mutex);
  528.    }
  529.  
  530.    for (i = 0; i < bitstream_buffer_count; ++i) {
  531.       buffers[i] = bitstream_buffers[i].bitstream;
  532.       sizes[i] = bitstream_buffers[i].bitstream_bytes;
  533.    }
  534.  
  535.    memset(&desc, 0, sizeof(desc));
  536.    desc.base.profile = dec->profile;
  537.    switch (u_reduce_video_profile(dec->profile)) {
  538.    case PIPE_VIDEO_FORMAT_MPEG12:
  539.       ret = vlVdpDecoderRenderMpeg12(&desc.mpeg12, (VdpPictureInfoMPEG1Or2 *)picture_info);
  540.       break;
  541.    case PIPE_VIDEO_FORMAT_MPEG4:
  542.       ret = vlVdpDecoderRenderMpeg4(&desc.mpeg4, (VdpPictureInfoMPEG4Part2 *)picture_info);
  543.       break;
  544.    case PIPE_VIDEO_FORMAT_VC1:
  545.       if (dec->profile == PIPE_VIDEO_PROFILE_VC1_ADVANCED)
  546.          vlVdpDecoderFixVC1Startcode(&bitstream_buffer_count, buffers, sizes);
  547.       ret = vlVdpDecoderRenderVC1(&desc.vc1, (VdpPictureInfoVC1 *)picture_info);
  548.       break;
  549.    case PIPE_VIDEO_FORMAT_MPEG4_AVC:
  550.       desc.h264.pps = &pps;
  551.       ret = vlVdpDecoderRenderH264(&desc.h264, (VdpPictureInfoH264 *)picture_info);
  552.       break;
  553.    default:
  554.       return VDP_STATUS_INVALID_DECODER_PROFILE;
  555.    }
  556.  
  557.    if (ret != VDP_STATUS_OK)
  558.       return ret;
  559.  
  560.    pipe_mutex_lock(vldecoder->mutex);
  561.    dec->begin_frame(dec, vlsurf->video_buffer, &desc.base);
  562.    dec->decode_bitstream(dec, vlsurf->video_buffer, &desc.base, bitstream_buffer_count, buffers, sizes);
  563.    dec->end_frame(dec, vlsurf->video_buffer, &desc.base);
  564.    pipe_mutex_unlock(vldecoder->mutex);
  565.    return ret;
  566. }
  567.