Merge commit '0bfdcce4d42a6e654c00ea5f9237dc987626457f'
authorJames Almer <jamrial@gmail.com>
Thu, 23 Mar 2017 23:02:11 +0000 (20:02 -0300)
committerJames Almer <jamrial@gmail.com>
Thu, 23 Mar 2017 23:02:11 +0000 (20:02 -0300)
* commit '0bfdcce4d42a6e654c00ea5f9237dc987626457f':
  hevc: move the SliceType enum to hevc.h

Merged-by: James Almer <jamrial@gmail.com>
1  2 
libavcodec/hevc.h
libavcodec/hevc_cabac.c
libavcodec/hevc_mvs.c
libavcodec/hevc_parser.c
libavcodec/hevc_refs.c
libavcodec/hevcdec.c
libavcodec/hevcdec.h
libavcodec/vaapi_encode_h265.c
libavcodec/vaapi_hevc.c

Simple merge
@@@ -25,7 -25,7 +25,8 @@@
  #include "libavutil/common.h"
  
  #include "cabac_functions.h"
 +#include "hevc_data.h"
+ #include "hevc.h"
  #include "hevcdec.h"
  
  #define CABAC_MAX_BIN 31
@@@ -314,9 -351,15 +315,9 @@@ static void derive_spatial_merge_candid
  
      const int xB2    = x0 - 1;
      const int yB2    = y0 - 1;
 -    const int xB2_pu = xB2 >> s->ps.sps->log2_min_pu_size;
 -    const int yB2_pu = yB2 >> s->ps.sps->log2_min_pu_size;
  
-     const int nb_refs = (s->sh.slice_type == P_SLICE) ?
+     const int nb_refs = (s->sh.slice_type == HEVC_SLICE_P) ?
                          s->sh.nb_refs[0] : FFMIN(s->sh.nb_refs[0], s->sh.nb_refs[1]);
 -    int check_MER   = 1;
 -    int check_MER_1 = 1;
  
      int zero_idx = 0;
  
      nb_orig_merge_cand = nb_merge_cand;
  
      // combined bi-predictive merge candidates  (applies for B slices)
-     if (s->sh.slice_type == B_SLICE && nb_orig_merge_cand > 1 &&
+     if (s->sh.slice_type == HEVC_SLICE_B && nb_orig_merge_cand > 1 &&
          nb_orig_merge_cand < s->sh.max_num_merge_cand) {
 -        int comb_idx;
 +        int comb_idx = 0;
  
          for (comb_idx = 0; nb_merge_cand < s->sh.max_num_merge_cand &&
                             comb_idx < nb_orig_merge_cand * (nb_orig_merge_cand - 1); comb_idx++) {
  
      // append Zero motion vector candidates
      while (nb_merge_cand < s->sh.max_num_merge_cand) {
-         mergecandlist[nb_merge_cand].pred_flag    = PF_L0 + ((s->sh.slice_type == B_SLICE) << 1);
 -        mergecandlist[nb_merge_cand].pred_flag[0] = 1;
 -        mergecandlist[nb_merge_cand].pred_flag[1] = s->sh.slice_type == HEVC_SLICE_B;
++        mergecandlist[nb_merge_cand].pred_flag    = PF_L0 + ((s->sh.slice_type == HEVC_SLICE_B) << 1);
          AV_ZERO32(mergecandlist[nb_merge_cand].mv + 0);
          AV_ZERO32(mergecandlist[nb_merge_cand].mv + 1);
 -        mergecandlist[nb_merge_cand].is_intra     = 0;
          mergecandlist[nb_merge_cand].ref_idx[0]   = zero_idx < nb_refs ? zero_idx : 0;
          mergecandlist[nb_merge_cand].ref_idx[1]   = zero_idx < nb_refs ? zero_idx : 0;
  
@@@ -175,236 -163,7 +175,236 @@@ static int hevc_find_frame_end(AVCodecP
      return END_NOT_FOUND;
  }
  
 -static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
 +#if ADVANCED_PARSER
 +/**
 + * Parse NAL units of found picture and decode some basic information.
 + *
 + * @param s parser context.
 + * @param avctx codec context.
 + * @param buf buffer with field/frame data.
 + * @param buf_size size of the buffer.
 + */
 +static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
 +                           int buf_size, AVCodecContext *avctx)
 +{
 +    HEVCParserContext *ctx = s->priv_data;
 +    HEVCContext       *h   = &ctx->h;
 +    GetBitContext      *gb;
 +    SliceHeader        *sh = &h->sh;
 +    HEVCParamSets *ps = &h->ps;
 +    H2645Packet   *pkt = &ctx->pkt;
 +    const uint8_t *buf_end = buf + buf_size;
 +    int state = -1, i;
 +    H2645NAL *nal;
 +    int is_global = buf == avctx->extradata;
 +
 +    if (!h->HEVClc)
 +        h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
 +    if (!h->HEVClc)
 +        return AVERROR(ENOMEM);
 +
 +    gb = &h->HEVClc->gb;
 +
 +    /* set some sane default values */
 +    s->pict_type         = AV_PICTURE_TYPE_I;
 +    s->key_frame         = 0;
 +    s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
 +
 +    h->avctx = avctx;
 +
 +    ff_hevc_reset_sei(h);
 +
 +    if (!buf_size)
 +        return 0;
 +
 +    if (pkt->nals_allocated < 1) {
 +        H2645NAL *tmp = av_realloc_array(pkt->nals, 1, sizeof(*tmp));
 +        if (!tmp)
 +            return AVERROR(ENOMEM);
 +        pkt->nals = tmp;
 +        memset(pkt->nals, 0, sizeof(*tmp));
 +        pkt->nals_allocated = 1;
 +    }
 +
 +    nal = &pkt->nals[0];
 +
 +    for (;;) {
 +        int src_length, consumed;
 +        int ret;
 +        int num = 0, den = 0;
 +        buf = avpriv_find_start_code(buf, buf_end, &state);
 +        if (--buf + 2 >= buf_end)
 +            break;
 +        src_length = buf_end - buf;
 +
 +        h->nal_unit_type = (*buf >> 1) & 0x3f;
 +        h->temporal_id   = (*(buf + 1) & 0x07) - 1;
 +        if (h->nal_unit_type <= HEVC_NAL_CRA_NUT) {
 +            // Do not walk the whole buffer just to decode slice segment header
 +            if (src_length > 20)
 +                src_length = 20;
 +        }
 +
 +        consumed = ff_h2645_extract_rbsp(buf, src_length, nal, 1);
 +        if (consumed < 0)
 +            return consumed;
 +
 +        ret = init_get_bits8(gb, nal->data + 2, nal->size);
 +        if (ret < 0)
 +            return ret;
 +
 +        switch (h->nal_unit_type) {
 +        case HEVC_NAL_VPS:
 +            ff_hevc_decode_nal_vps(gb, avctx, ps);
 +            break;
 +        case HEVC_NAL_SPS:
 +            ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
 +            break;
 +        case HEVC_NAL_PPS:
 +            ff_hevc_decode_nal_pps(gb, avctx, ps);
 +            break;
 +        case HEVC_NAL_SEI_PREFIX:
 +        case HEVC_NAL_SEI_SUFFIX:
 +            ff_hevc_decode_nal_sei(h);
 +            break;
 +        case HEVC_NAL_TRAIL_N:
 +        case HEVC_NAL_TRAIL_R:
 +        case HEVC_NAL_TSA_N:
 +        case HEVC_NAL_TSA_R:
 +        case HEVC_NAL_STSA_N:
 +        case HEVC_NAL_STSA_R:
 +        case HEVC_NAL_RADL_N:
 +        case HEVC_NAL_RADL_R:
 +        case HEVC_NAL_RASL_N:
 +        case HEVC_NAL_RASL_R:
 +        case HEVC_NAL_BLA_W_LP:
 +        case HEVC_NAL_BLA_W_RADL:
 +        case HEVC_NAL_BLA_N_LP:
 +        case HEVC_NAL_IDR_W_RADL:
 +        case HEVC_NAL_IDR_N_LP:
 +        case HEVC_NAL_CRA_NUT:
 +
 +            if (is_global) {
 +                av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", h->nal_unit_type);
 +                return AVERROR_INVALIDDATA;
 +            }
 +
 +            sh->first_slice_in_pic_flag = get_bits1(gb);
 +            s->picture_structure = h->picture_struct;
 +            s->field_order = h->picture_struct;
 +
 +            if (IS_IRAP(h)) {
 +                s->key_frame = 1;
 +                sh->no_output_of_prior_pics_flag = get_bits1(gb);
 +            }
 +
 +            sh->pps_id = get_ue_golomb(gb);
 +            if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
 +                av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
 +                return AVERROR_INVALIDDATA;
 +            }
 +            ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
 +
 +            if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
 +                av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
 +                return AVERROR_INVALIDDATA;
 +            }
 +            if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
 +                ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
 +                ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
 +            }
 +
 +            s->coded_width  = ps->sps->width;
 +            s->coded_height = ps->sps->height;
 +            s->width        = ps->sps->output_width;
 +            s->height       = ps->sps->output_height;
 +            s->format       = ps->sps->pix_fmt;
 +            avctx->profile  = ps->sps->ptl.general_ptl.profile_idc;
 +            avctx->level    = ps->sps->ptl.general_ptl.level_idc;
 +
 +            if (ps->vps->vps_timing_info_present_flag) {
 +                num = ps->vps->vps_num_units_in_tick;
 +                den = ps->vps->vps_time_scale;
 +            } else if (ps->sps->vui.vui_timing_info_present_flag) {
 +                num = ps->sps->vui.vui_num_units_in_tick;
 +                den = ps->sps->vui.vui_time_scale;
 +            }
 +
 +            if (num != 0 && den != 0)
 +                av_reduce(&avctx->framerate.den, &avctx->framerate.num,
 +                          num, den, 1 << 30);
 +
 +            if (!sh->first_slice_in_pic_flag) {
 +                int slice_address_length;
 +
 +                if (ps->pps->dependent_slice_segments_enabled_flag)
 +                    sh->dependent_slice_segment_flag = get_bits1(gb);
 +                else
 +                    sh->dependent_slice_segment_flag = 0;
 +
 +                slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
 +                                                      ps->sps->ctb_height);
 +                sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
 +                if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
 +                    av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
 +                           sh->slice_segment_addr);
 +                    return AVERROR_INVALIDDATA;
 +                }
 +            } else
 +                sh->dependent_slice_segment_flag = 0;
 +
 +            if (sh->dependent_slice_segment_flag)
 +                break;
 +
 +            for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
 +                skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
 +
 +            sh->slice_type = get_ue_golomb(gb);
-             if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
-                   sh->slice_type == B_SLICE)) {
++            if (!(sh->slice_type == HEVC_SLICE_I || sh->slice_type == HEVC_SLICE_P ||
++                  sh->slice_type == HEVC_SLICE_B)) {
 +                av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
 +                       sh->slice_type);
 +                return AVERROR_INVALIDDATA;
 +            }
-             s->pict_type = sh->slice_type == B_SLICE ? AV_PICTURE_TYPE_B :
-                            sh->slice_type == P_SLICE ? AV_PICTURE_TYPE_P :
++            s->pict_type = sh->slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
++                           sh->slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
 +                                                       AV_PICTURE_TYPE_I;
 +
 +            if (ps->pps->output_flag_present_flag)
 +                sh->pic_output_flag = get_bits1(gb);
 +
 +            if (ps->sps->separate_colour_plane_flag)
 +                sh->colour_plane_id = get_bits(gb, 2);
 +
 +            if (!IS_IDR(h)) {
 +                sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
 +                s->output_picture_number = h->poc = ff_hevc_compute_poc(h, sh->pic_order_cnt_lsb);
 +            } else
 +                s->output_picture_number = h->poc = 0;
 +
 +            if (h->temporal_id == 0 &&
 +                h->nal_unit_type != HEVC_NAL_TRAIL_N &&
 +                h->nal_unit_type != HEVC_NAL_TSA_N &&
 +                h->nal_unit_type != HEVC_NAL_STSA_N &&
 +                h->nal_unit_type != HEVC_NAL_RADL_N &&
 +                h->nal_unit_type != HEVC_NAL_RASL_N &&
 +                h->nal_unit_type != HEVC_NAL_RADL_R &&
 +                h->nal_unit_type != HEVC_NAL_RASL_R)
 +                h->pocTid0 = h->poc;
 +
 +            return 0; /* no need to evaluate the rest */
 +        }
 +        buf += consumed;
 +    }
 +    /* didn't find a picture! */
 +    if (!is_global)
 +        av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
 +    return -1;
 +}
 +#endif
 +
 +static int hevc_parse(AVCodecParserContext *s,
 +                      AVCodecContext *avctx,
                        const uint8_t **poutbuf, int *poutbuf_size,
                        const uint8_t *buf, int buf_size)
  {
Simple merge
@@@ -1345,11 -1509,11 +1345,11 @@@ static void luma_mc_uni(HEVCContext *s
      ptrdiff_t srcstride  = ref->linesize[0];
      int pic_width        = s->ps.sps->width;
      int pic_height       = s->ps.sps->height;
 -
 -    int mx         = mv->x & 3;
 -    int my         = mv->y & 3;
 -    int extra_left = ff_hevc_qpel_extra_before[mx];
 -    int extra_top  = ff_hevc_qpel_extra_before[my];
 +    int mx               = mv->x & 3;
 +    int my               = mv->y & 3;
-     int weight_flag      = (s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
-                            (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag);
++    int weight_flag      = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
++                           (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
 +    int idx              = ff_hevc_pel_weight[block_w];
  
      x_off += mv->x >> 2;
      y_off += mv->y >> 2;
          src = lc->edge_emu_buffer + buf_offset;
          srcstride = edge_emu_stride;
      }
 -    s->hevcdsp.put_hevc_qpel[!!my][!!mx][pred_idx](dst, dststride, src, srcstride,
 -                                                   block_h, mx, my, lc->mc_buffer);
 +
 +    if (!weight_flag)
 +        s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
 +                                                      block_h, mx, my, block_w);
 +    else
 +        s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
 +                                                        block_h, s->sh.luma_log2_weight_denom,
 +                                                        luma_weight, luma_offset, mx, my, block_w);
 +}
 +
 +/**
 + * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
 + *
 + * @param s HEVC decoding context
 + * @param dst target buffer for block data at block position
 + * @param dststride stride of the dst buffer
 + * @param ref0 reference picture0 buffer at origin (0, 0)
 + * @param mv0 motion vector0 (relative to block position) to get pixel data from
 + * @param x_off horizontal position of block from origin (0, 0)
 + * @param y_off vertical position of block from origin (0, 0)
 + * @param block_w width of block
 + * @param block_h height of block
 + * @param ref1 reference picture1 buffer at origin (0, 0)
 + * @param mv1 motion vector1 (relative to block position) to get pixel data from
 + * @param current_mv current motion vector structure
 + */
 + static void luma_mc_bi(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
 +                       AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
 +                       int block_w, int block_h, AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
 +{
 +    HEVCLocalContext *lc = s->HEVClc;
 +    ptrdiff_t src0stride  = ref0->linesize[0];
 +    ptrdiff_t src1stride  = ref1->linesize[0];
 +    int pic_width        = s->ps.sps->width;
 +    int pic_height       = s->ps.sps->height;
 +    int mx0              = mv0->x & 3;
 +    int my0              = mv0->y & 3;
 +    int mx1              = mv1->x & 3;
 +    int my1              = mv1->y & 3;
-     int weight_flag      = (s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
-                            (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag);
++    int weight_flag      = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
++                           (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
 +    int x_off0           = x_off + (mv0->x >> 2);
 +    int y_off0           = y_off + (mv0->y >> 2);
 +    int x_off1           = x_off + (mv1->x >> 2);
 +    int y_off1           = y_off + (mv1->y >> 2);
 +    int idx              = ff_hevc_pel_weight[block_w];
 +
 +    uint8_t *src0  = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
 +    uint8_t *src1  = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
 +
 +    if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
 +        x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
 +        y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
 +        const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
 +        int offset     = QPEL_EXTRA_BEFORE * src0stride       + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
 +        int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
 +
 +        s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
 +                                 edge_emu_stride, src0stride,
 +                                 block_w + QPEL_EXTRA,
 +                                 block_h + QPEL_EXTRA,
 +                                 x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
 +                                 pic_width, pic_height);
 +        src0 = lc->edge_emu_buffer + buf_offset;
 +        src0stride = edge_emu_stride;
 +    }
 +
 +    if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
 +        x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
 +        y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
 +        const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
 +        int offset     = QPEL_EXTRA_BEFORE * src1stride       + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
 +        int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
 +
 +        s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
 +                                 edge_emu_stride, src1stride,
 +                                 block_w + QPEL_EXTRA,
 +                                 block_h + QPEL_EXTRA,
 +                                 x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
 +                                 pic_width, pic_height);
 +        src1 = lc->edge_emu_buffer2 + buf_offset;
 +        src1stride = edge_emu_stride;
 +    }
 +
 +    s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
 +                                                block_h, mx0, my0, block_w);
 +    if (!weight_flag)
 +        s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
 +                                                       block_h, mx1, my1, block_w);
 +    else
 +        s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
 +                                                         block_h, s->sh.luma_log2_weight_denom,
 +                                                         s->sh.luma_weight_l0[current_mv->ref_idx[0]],
 +                                                         s->sh.luma_weight_l1[current_mv->ref_idx[1]],
 +                                                         s->sh.luma_offset_l0[current_mv->ref_idx[0]],
 +                                                         s->sh.luma_offset_l1[current_mv->ref_idx[1]],
 +                                                         mx1, my1, block_w);
 +
  }
  
  /**
   * @param y_off vertical position of block from origin (0, 0)
   * @param block_w width of block
   * @param block_h height of block
 + * @param chroma_weight weighting factor applied to the chroma prediction
 + * @param chroma_offset additive offset applied to the chroma prediction value
   */
 -static void chroma_mc(HEVCContext *s, int16_t *dst1, int16_t *dst2,
 -                      ptrdiff_t dststride, AVFrame *ref, const Mv *mv,
 -                      int x_off, int y_off, int block_w, int block_h, int pred_idx)
 +
 +static void chroma_mc_uni(HEVCContext *s, uint8_t *dst0,
 +                          ptrdiff_t dststride, uint8_t *src0, ptrdiff_t srcstride, int reflist,
 +                          int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int chroma_weight, int chroma_offset)
  {
 -    HEVCLocalContext *lc = &s->HEVClc;
 -    uint8_t *src1        = ref->data[1];
 -    uint8_t *src2        = ref->data[2];
 -    ptrdiff_t src1stride = ref->linesize[1];
 -    ptrdiff_t src2stride = ref->linesize[2];
 -    int pic_width        = s->ps.sps->width >> 1;
 -    int pic_height       = s->ps.sps->height >> 1;
 -
 -    int mx = mv->x & 7;
 -    int my = mv->y & 7;
 -
 -    x_off += mv->x >> 3;
 -    y_off += mv->y >> 3;
 -    src1  += y_off * src1stride + (x_off * (1 << s->ps.sps->pixel_shift));
 -    src2  += y_off * src2stride + (x_off * (1 << s->ps.sps->pixel_shift));
 +    HEVCLocalContext *lc = s->HEVClc;
 +    int pic_width        = s->ps.sps->width >> s->ps.sps->hshift[1];
 +    int pic_height       = s->ps.sps->height >> s->ps.sps->vshift[1];
 +    const Mv *mv         = &current_mv->mv[reflist];
-     int weight_flag      = (s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
-                            (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag);
++    int weight_flag      = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
++                           (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
 +    int idx              = ff_hevc_pel_weight[block_w];
 +    int hshift           = s->ps.sps->hshift[1];
 +    int vshift           = s->ps.sps->vshift[1];
 +    intptr_t mx          = av_mod_uintp2(mv->x, 2 + hshift);
 +    intptr_t my          = av_mod_uintp2(mv->y, 2 + vshift);
 +    intptr_t _mx         = mx << (1 - hshift);
 +    intptr_t _my         = my << (1 - vshift);
 +
 +    x_off += mv->x >> (2 + hshift);
 +    y_off += mv->y >> (2 + vshift);
 +    src0  += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
  
      if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
          x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
          y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
 -        const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
 +        const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
 +        int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << s->ps.sps->pixel_shift));
 +        int buf_offset0 = EPEL_EXTRA_BEFORE *
 +                          (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
 +        s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
 +                                 edge_emu_stride, srcstride,
 +                                 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
 +                                 x_off - EPEL_EXTRA_BEFORE,
 +                                 y_off - EPEL_EXTRA_BEFORE,
 +                                 pic_width, pic_height);
 +
 +        src0 = lc->edge_emu_buffer + buf_offset0;
 +        srcstride = edge_emu_stride;
 +    }
 +    if (!weight_flag)
 +        s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
 +                                                  block_h, _mx, _my, block_w);
 +    else
 +        s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
 +                                                        block_h, s->sh.chroma_log2_weight_denom,
 +                                                        chroma_weight, chroma_offset, _mx, _my, block_w);
 +}
 +
 +/**
 + * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
 + *
 + * @param s HEVC decoding context
 + * @param dst target buffer for block data at block position
 + * @param dststride stride of the dst buffer
 + * @param ref0 reference picture0 buffer at origin (0, 0)
 + * @param mv0 motion vector0 (relative to block position) to get pixel data from
 + * @param x_off horizontal position of block from origin (0, 0)
 + * @param y_off vertical position of block from origin (0, 0)
 + * @param block_w width of block
 + * @param block_h height of block
 + * @param ref1 reference picture1 buffer at origin (0, 0)
 + * @param mv1 motion vector1 (relative to block position) to get pixel data from
 + * @param current_mv current motion vector structure
 + * @param cidx chroma component(cb, cr)
 + */
 +static void chroma_mc_bi(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, AVFrame *ref0, AVFrame *ref1,
 +                         int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int cidx)
 +{
 +    HEVCLocalContext *lc = s->HEVClc;
 +    uint8_t *src1        = ref0->data[cidx+1];
 +    uint8_t *src2        = ref1->data[cidx+1];
 +    ptrdiff_t src1stride = ref0->linesize[cidx+1];
 +    ptrdiff_t src2stride = ref1->linesize[cidx+1];
-     int weight_flag      = (s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
-                            (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag);
++    int weight_flag      = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
++                           (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
 +    int pic_width        = s->ps.sps->width >> s->ps.sps->hshift[1];
 +    int pic_height       = s->ps.sps->height >> s->ps.sps->vshift[1];
 +    Mv *mv0              = &current_mv->mv[0];
 +    Mv *mv1              = &current_mv->mv[1];
 +    int hshift = s->ps.sps->hshift[1];
 +    int vshift = s->ps.sps->vshift[1];
 +
 +    intptr_t mx0 = av_mod_uintp2(mv0->x, 2 + hshift);
 +    intptr_t my0 = av_mod_uintp2(mv0->y, 2 + vshift);
 +    intptr_t mx1 = av_mod_uintp2(mv1->x, 2 + hshift);
 +    intptr_t my1 = av_mod_uintp2(mv1->y, 2 + vshift);
 +    intptr_t _mx0 = mx0 << (1 - hshift);
 +    intptr_t _my0 = my0 << (1 - vshift);
 +    intptr_t _mx1 = mx1 << (1 - hshift);
 +    intptr_t _my1 = my1 << (1 - vshift);
 +
 +    int x_off0 = x_off + (mv0->x >> (2 + hshift));
 +    int y_off0 = y_off + (mv0->y >> (2 + vshift));
 +    int x_off1 = x_off + (mv1->x >> (2 + hshift));
 +    int y_off1 = y_off + (mv1->y >> (2 + vshift));
 +    int idx = ff_hevc_pel_weight[block_w];
 +    src1  += y_off0 * src1stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
 +    src2  += y_off1 * src2stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
 +
 +    if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
 +        x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
 +        y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
 +        const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
          int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->ps.sps->pixel_shift));
          int buf_offset1 = EPEL_EXTRA_BEFORE *
                            (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
@@@ -1661,8 -1632,7 +1661,8 @@@ static void hevc_luma_mv_mvp_mode(HEVCC
      int mvp_flag;
  
      ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
-     if (s->sh.slice_type == B_SLICE)
 +    mv->pred_flag = 0;
+     if (s->sh.slice_type == HEVC_SLICE_B)
          inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
  
      if (inter_pred_idc != PRED_L1) {
Simple merge
Simple merge
index 6dd35db,0000000..69b8e47
mode 100644,000000..100644
--- /dev/null
@@@ -1,439 -1,0 +1,439 @@@
-     if (sh->slice_type == I_SLICE ||
-         (sh->slice_type == P_SLICE && !h->ps.pps->weighted_pred_flag) ||
-         (sh->slice_type == B_SLICE && !h->ps.pps->weighted_bipred_flag))
 +/*
 + * HEVC HW decode acceleration through VA API
 + *
 + * Copyright (C) 2015 Timo Rothenpieler <timo@rothenpieler.org>
 + *
 + * This file is part of FFmpeg.
 + *
 + * FFmpeg is free software; you can redistribute it and/or
 + * modify it under the terms of the GNU Lesser General Public
 + * License as published by the Free Software Foundation; either
 + * version 2.1 of the License, or (at your option) any later version.
 + *
 + * FFmpeg is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 + * Lesser General Public License for more details.
 + *
 + * You should have received a copy of the GNU Lesser General Public
 + * License along with FFmpeg; if not, write to the Free Software
 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 + */
 +
 +#include <va/va.h>
 +#include <va/va_dec_hevc.h>
 +
 +#include "avcodec.h"
 +#include "hevcdec.h"
 +#include "hwaccel.h"
 +#include "vaapi_decode.h"
 +
 +typedef struct VAAPIDecodePictureHEVC {
 +    VAPictureParameterBufferHEVC pic_param;
 +    VASliceParameterBufferHEVC last_slice_param;
 +    const uint8_t *last_buffer;
 +    size_t         last_size;
 +
 +    VAAPIDecodePicture pic;
 +} VAAPIDecodePictureHEVC;
 +
 +static void init_vaapi_pic(VAPictureHEVC *va_pic)
 +{
 +    va_pic->picture_id    = VA_INVALID_ID;
 +    va_pic->flags         = VA_PICTURE_HEVC_INVALID;
 +    va_pic->pic_order_cnt = 0;
 +}
 +
 +static void fill_vaapi_pic(VAPictureHEVC *va_pic, const HEVCFrame *pic, int rps_type)
 +{
 +    va_pic->picture_id    = ff_vaapi_get_surface_id(pic->frame);
 +    va_pic->pic_order_cnt = pic->poc;
 +    va_pic->flags         = rps_type;
 +
 +    if (pic->flags & HEVC_FRAME_FLAG_LONG_REF)
 +        va_pic->flags |= VA_PICTURE_HEVC_LONG_TERM_REFERENCE;
 +
 +    if (pic->frame->interlaced_frame) {
 +        va_pic->flags |= VA_PICTURE_HEVC_FIELD_PIC;
 +
 +        if (!pic->frame->top_field_first)
 +            va_pic->flags |= VA_PICTURE_HEVC_BOTTOM_FIELD;
 +    }
 +}
 +
 +static int find_frame_rps_type(const HEVCContext *h, const HEVCFrame *pic)
 +{
 +    VASurfaceID pic_surf = ff_vaapi_get_surface_id(pic->frame);
 +    int i;
 +
 +    for (i = 0; i < h->rps[ST_CURR_BEF].nb_refs; i++) {
 +        if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_BEF].ref[i]->frame))
 +            return VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE;
 +    }
 +
 +    for (i = 0; i < h->rps[ST_CURR_AFT].nb_refs; i++) {
 +        if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_AFT].ref[i]->frame))
 +            return VA_PICTURE_HEVC_RPS_ST_CURR_AFTER;
 +    }
 +
 +    for (i = 0; i < h->rps[LT_CURR].nb_refs; i++) {
 +        if (pic_surf == ff_vaapi_get_surface_id(h->rps[LT_CURR].ref[i]->frame))
 +            return VA_PICTURE_HEVC_RPS_LT_CURR;
 +    }
 +
 +    return 0;
 +}
 +
 +static void fill_vaapi_reference_frames(const HEVCContext *h, VAPictureParameterBufferHEVC *pp)
 +{
 +    const HEVCFrame *current_picture = h->ref;
 +    int i, j, rps_type;
 +
 +    for (i = 0, j = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); i++) {
 +        const HEVCFrame *frame = NULL;
 +
 +        while (!frame && j < FF_ARRAY_ELEMS(h->DPB)) {
 +            if (&h->DPB[j] != current_picture && (h->DPB[j].flags & (HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF)))
 +                frame = &h->DPB[j];
 +            j++;
 +        }
 +
 +        init_vaapi_pic(&pp->ReferenceFrames[i]);
 +
 +        if (frame) {
 +            rps_type = find_frame_rps_type(h, frame);
 +            fill_vaapi_pic(&pp->ReferenceFrames[i], frame, rps_type);
 +        }
 +    }
 +}
 +
 +static int vaapi_hevc_start_frame(AVCodecContext          *avctx,
 +                                  av_unused const uint8_t *buffer,
 +                                  av_unused uint32_t       size)
 +{
 +    const HEVCContext        *h = avctx->priv_data;
 +    VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private;
 +    const HEVCSPS          *sps = h->ps.sps;
 +    const HEVCPPS          *pps = h->ps.pps;
 +
 +    const ScalingList *scaling_list = NULL;
 +    int err, i;
 +
 +    pic->pic.output_surface = ff_vaapi_get_surface_id(h->ref->frame);
 +
 +    pic->pic_param = (VAPictureParameterBufferHEVC) {
 +        .pic_fields.value                             = 0,
 +        .slice_parsing_fields.value                   = 0,
 +        .pic_width_in_luma_samples                    = sps->width,
 +        .pic_height_in_luma_samples                   = sps->height,
 +        .log2_min_luma_coding_block_size_minus3       = sps->log2_min_cb_size - 3,
 +        .sps_max_dec_pic_buffering_minus1             = sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering - 1,
 +        .log2_diff_max_min_luma_coding_block_size     = sps->log2_diff_max_min_coding_block_size,
 +        .log2_min_transform_block_size_minus2         = sps->log2_min_tb_size - 2,
 +        .log2_diff_max_min_transform_block_size       = sps->log2_max_trafo_size  - sps->log2_min_tb_size,
 +        .max_transform_hierarchy_depth_inter          = sps->max_transform_hierarchy_depth_inter,
 +        .max_transform_hierarchy_depth_intra          = sps->max_transform_hierarchy_depth_intra,
 +        .num_short_term_ref_pic_sets                  = sps->nb_st_rps,
 +        .num_long_term_ref_pic_sps                    = sps->num_long_term_ref_pics_sps,
 +        .num_ref_idx_l0_default_active_minus1         = pps->num_ref_idx_l0_default_active - 1,
 +        .num_ref_idx_l1_default_active_minus1         = pps->num_ref_idx_l1_default_active - 1,
 +        .init_qp_minus26                              = pps->pic_init_qp_minus26,
 +        .pps_cb_qp_offset                             = pps->cb_qp_offset,
 +        .pps_cr_qp_offset                             = pps->cr_qp_offset,
 +        .pcm_sample_bit_depth_luma_minus1             = sps->pcm.bit_depth - 1,
 +        .pcm_sample_bit_depth_chroma_minus1           = sps->pcm.bit_depth_chroma - 1,
 +        .log2_min_pcm_luma_coding_block_size_minus3   = sps->pcm.log2_min_pcm_cb_size - 3,
 +        .log2_diff_max_min_pcm_luma_coding_block_size = sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size,
 +        .diff_cu_qp_delta_depth                       = pps->diff_cu_qp_delta_depth,
 +        .pps_beta_offset_div2                         = pps->beta_offset / 2,
 +        .pps_tc_offset_div2                           = pps->tc_offset / 2,
 +        .log2_parallel_merge_level_minus2             = pps->log2_parallel_merge_level - 2,
 +        .bit_depth_luma_minus8                        = sps->bit_depth - 8,
 +        .bit_depth_chroma_minus8                      = sps->bit_depth - 8,
 +        .log2_max_pic_order_cnt_lsb_minus4            = sps->log2_max_poc_lsb - 4,
 +        .num_extra_slice_header_bits                  = pps->num_extra_slice_header_bits,
 +        .pic_fields.bits = {
 +            .chroma_format_idc                          = sps->chroma_format_idc,
 +            .tiles_enabled_flag                         = pps->tiles_enabled_flag,
 +            .separate_colour_plane_flag                 = sps->separate_colour_plane_flag,
 +            .pcm_enabled_flag                           = sps->pcm_enabled_flag,
 +            .scaling_list_enabled_flag                  = sps->scaling_list_enable_flag,
 +            .transform_skip_enabled_flag                = pps->transform_skip_enabled_flag,
 +            .amp_enabled_flag                           = sps->amp_enabled_flag,
 +            .strong_intra_smoothing_enabled_flag        = sps->sps_strong_intra_smoothing_enable_flag,
 +            .sign_data_hiding_enabled_flag              = pps->sign_data_hiding_flag,
 +            .constrained_intra_pred_flag                = pps->constrained_intra_pred_flag,
 +            .cu_qp_delta_enabled_flag                   = pps->cu_qp_delta_enabled_flag,
 +            .weighted_pred_flag                         = pps->weighted_pred_flag,
 +            .weighted_bipred_flag                       = pps->weighted_bipred_flag,
 +            .transquant_bypass_enabled_flag             = pps->transquant_bypass_enable_flag,
 +            .entropy_coding_sync_enabled_flag           = pps->entropy_coding_sync_enabled_flag,
 +            .pps_loop_filter_across_slices_enabled_flag = pps->seq_loop_filter_across_slices_enabled_flag,
 +            .loop_filter_across_tiles_enabled_flag      = pps->loop_filter_across_tiles_enabled_flag,
 +            .pcm_loop_filter_disabled_flag              = sps->pcm.loop_filter_disable_flag,
 +        },
 +        .slice_parsing_fields.bits = {
 +            .lists_modification_present_flag             = pps->lists_modification_present_flag,
 +            .long_term_ref_pics_present_flag             = sps->long_term_ref_pics_present_flag,
 +            .sps_temporal_mvp_enabled_flag               = sps->sps_temporal_mvp_enabled_flag,
 +            .cabac_init_present_flag                     = pps->cabac_init_present_flag,
 +            .output_flag_present_flag                    = pps->output_flag_present_flag,
 +            .dependent_slice_segments_enabled_flag       = pps->dependent_slice_segments_enabled_flag,
 +            .pps_slice_chroma_qp_offsets_present_flag    = pps->pic_slice_level_chroma_qp_offsets_present_flag,
 +            .sample_adaptive_offset_enabled_flag         = sps->sao_enabled,
 +            .deblocking_filter_override_enabled_flag     = pps->deblocking_filter_override_enabled_flag,
 +            .pps_disable_deblocking_filter_flag          = pps->disable_dbf,
 +            .slice_segment_header_extension_present_flag = pps->slice_header_extension_present_flag,
 +            .RapPicFlag                                  = IS_IRAP(h),
 +            .IdrPicFlag                                  = IS_IDR(h),
 +            .IntraPicFlag                                = IS_IRAP(h),
 +        },
 +    };
 +
 +    fill_vaapi_pic(&pic->pic_param.CurrPic, h->ref, 0);
 +    fill_vaapi_reference_frames(h, &pic->pic_param);
 +
 +    if (pps->tiles_enabled_flag) {
 +        pic->pic_param.num_tile_columns_minus1 = pps->num_tile_columns - 1;
 +        pic->pic_param.num_tile_rows_minus1    = pps->num_tile_rows - 1;
 +
 +        for (i = 0; i < pps->num_tile_columns; i++)
 +            pic->pic_param.column_width_minus1[i] = pps->column_width[i] - 1;
 +
 +        for (i = 0; i < pps->num_tile_rows; i++)
 +            pic->pic_param.row_height_minus1[i] = pps->row_height[i] - 1;
 +    }
 +
 +    if (h->sh.short_term_ref_pic_set_sps_flag == 0 && h->sh.short_term_rps) {
 +        pic->pic_param.st_rps_bits = h->sh.short_term_ref_pic_set_size;
 +    } else {
 +        pic->pic_param.st_rps_bits = 0;
 +    }
 +
 +    err = ff_vaapi_decode_make_param_buffer(avctx, &pic->pic,
 +                                            VAPictureParameterBufferType,
 +                                            &pic->pic_param, sizeof(pic->pic_param));
 +    if (err < 0)
 +        goto fail;
 +
 +    if (pps->scaling_list_data_present_flag)
 +        scaling_list = &pps->scaling_list;
 +    else if (sps->scaling_list_enable_flag)
 +        scaling_list = &sps->scaling_list;
 +
 +    if (scaling_list) {
 +        VAIQMatrixBufferHEVC iq_matrix;
 +        int j;
 +
 +        for (i = 0; i < 6; i++) {
 +            for (j = 0; j < 16; j++)
 +                iq_matrix.ScalingList4x4[i][j] = scaling_list->sl[0][i][j];
 +            for (j = 0; j < 64; j++) {
 +                iq_matrix.ScalingList8x8[i][j]   = scaling_list->sl[1][i][j];
 +                iq_matrix.ScalingList16x16[i][j] = scaling_list->sl[2][i][j];
 +                if (i < 2)
 +                    iq_matrix.ScalingList32x32[i][j] = scaling_list->sl[3][i * 3][j];
 +            }
 +            iq_matrix.ScalingListDC16x16[i] = scaling_list->sl_dc[0][i];
 +            if (i < 2)
 +                iq_matrix.ScalingListDC32x32[i] = scaling_list->sl_dc[1][i * 3];
 +        }
 +
 +        err = ff_vaapi_decode_make_param_buffer(avctx, &pic->pic,
 +                                                VAIQMatrixBufferType,
 +                                                &iq_matrix, sizeof(iq_matrix));
 +        if (err < 0)
 +            goto fail;
 +    }
 +
 +    return 0;
 +
 +fail:
 +    ff_vaapi_decode_cancel(avctx, &pic->pic);
 +    return err;
 +}
 +
 +static int vaapi_hevc_end_frame(AVCodecContext *avctx)
 +{
 +    const HEVCContext        *h = avctx->priv_data;
 +    VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private;
 +    int ret;
 +
 +    if (pic->last_size) {
 +        pic->last_slice_param.LongSliceFlags.fields.LastSliceOfPic = 1;
 +        ret = ff_vaapi_decode_make_slice_buffer(avctx, &pic->pic,
 +                                                &pic->last_slice_param, sizeof(pic->last_slice_param),
 +                                                pic->last_buffer, pic->last_size);
 +        if (ret < 0)
 +            goto fail;
 +    }
 +
 +
 +    ret = ff_vaapi_decode_issue(avctx, &pic->pic);
 +    if (ret < 0)
 +        goto fail;
 +
 +    return 0;
 +fail:
 +    ff_vaapi_decode_cancel(avctx, &pic->pic);
 +    return ret;
 +}
 +
 +static void fill_pred_weight_table(const HEVCContext *h,
 +                                   const SliceHeader *sh,
 +                                   VASliceParameterBufferHEVC *slice_param)
 +{
 +    int i;
 +
 +    memset(slice_param->delta_luma_weight_l0,   0, sizeof(slice_param->delta_luma_weight_l0));
 +    memset(slice_param->delta_luma_weight_l1,   0, sizeof(slice_param->delta_luma_weight_l1));
 +    memset(slice_param->luma_offset_l0,         0, sizeof(slice_param->luma_offset_l0));
 +    memset(slice_param->luma_offset_l1,         0, sizeof(slice_param->luma_offset_l1));
 +    memset(slice_param->delta_chroma_weight_l0, 0, sizeof(slice_param->delta_chroma_weight_l0));
 +    memset(slice_param->delta_chroma_weight_l1, 0, sizeof(slice_param->delta_chroma_weight_l1));
 +    memset(slice_param->ChromaOffsetL0,         0, sizeof(slice_param->ChromaOffsetL0));
 +    memset(slice_param->ChromaOffsetL1,         0, sizeof(slice_param->ChromaOffsetL1));
 +
 +    slice_param->delta_chroma_log2_weight_denom = 0;
 +    slice_param->luma_log2_weight_denom         = 0;
 +
-     if (sh->slice_type == B_SLICE) {
++    if (sh->slice_type == HEVC_SLICE_I ||
++        (sh->slice_type == HEVC_SLICE_P && !h->ps.pps->weighted_pred_flag) ||
++        (sh->slice_type == HEVC_SLICE_B && !h->ps.pps->weighted_bipred_flag))
 +        return;
 +
 +    slice_param->luma_log2_weight_denom = sh->luma_log2_weight_denom;
 +
 +    if (h->ps.sps->chroma_format_idc) {
 +        slice_param->delta_chroma_log2_weight_denom = sh->chroma_log2_weight_denom - sh->luma_log2_weight_denom;
 +    }
 +
 +    for (i = 0; i < 15 && i < sh->nb_refs[L0]; i++) {
 +        slice_param->delta_luma_weight_l0[i] = sh->luma_weight_l0[i] - (1 << sh->luma_log2_weight_denom);
 +        slice_param->luma_offset_l0[i] = sh->luma_offset_l0[i];
 +        slice_param->delta_chroma_weight_l0[i][0] = sh->chroma_weight_l0[i][0] - (1 << sh->chroma_log2_weight_denom);
 +        slice_param->delta_chroma_weight_l0[i][1] = sh->chroma_weight_l0[i][1] - (1 << sh->chroma_log2_weight_denom);
 +        slice_param->ChromaOffsetL0[i][0] = sh->chroma_offset_l0[i][0];
 +        slice_param->ChromaOffsetL0[i][1] = sh->chroma_offset_l0[i][1];
 +    }
 +
-     int nb_list = (sh->slice_type == B_SLICE) ?
-                   2 : (sh->slice_type == I_SLICE ? 0 : 1);
++    if (sh->slice_type == HEVC_SLICE_B) {
 +        for (i = 0; i < 15 && i < sh->nb_refs[L1]; i++) {
 +            slice_param->delta_luma_weight_l1[i] = sh->luma_weight_l1[i] - (1 << sh->luma_log2_weight_denom);
 +            slice_param->luma_offset_l1[i] = sh->luma_offset_l1[i];
 +            slice_param->delta_chroma_weight_l1[i][0] = sh->chroma_weight_l1[i][0] - (1 << sh->chroma_log2_weight_denom);
 +            slice_param->delta_chroma_weight_l1[i][1] = sh->chroma_weight_l1[i][1] - (1 << sh->chroma_log2_weight_denom);
 +            slice_param->ChromaOffsetL1[i][0] = sh->chroma_offset_l1[i][0];
 +            slice_param->ChromaOffsetL1[i][1] = sh->chroma_offset_l1[i][1];
 +        }
 +    }
 +}
 +
 +static uint8_t get_ref_pic_index(const HEVCContext *h, const HEVCFrame *frame)
 +{
 +    VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private;
 +    VAPictureParameterBufferHEVC *pp = &pic->pic_param;
 +    uint8_t i;
 +
 +    if (!frame)
 +        return 0xff;
 +
 +    for (i = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); i++) {
 +        VASurfaceID pid = pp->ReferenceFrames[i].picture_id;
 +        int poc = pp->ReferenceFrames[i].pic_order_cnt;
 +        if (pid != VA_INVALID_ID && pid == ff_vaapi_get_surface_id(frame->frame) && poc == frame->poc)
 +            return i;
 +    }
 +
 +    return 0xff;
 +}
 +
 +static int vaapi_hevc_decode_slice(AVCodecContext *avctx,
 +                                   const uint8_t  *buffer,
 +                                   uint32_t        size)
 +{
 +    const HEVCContext        *h = avctx->priv_data;
 +    const SliceHeader       *sh = &h->sh;
 +    VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private;
 +
-         .five_minus_max_num_merge_cand = sh->slice_type == I_SLICE ? 0 : 5 - sh->max_num_merge_cand,
++    int nb_list = (sh->slice_type == HEVC_SLICE_B) ?
++                  2 : (sh->slice_type == HEVC_SLICE_I ? 0 : 1);
 +
 +    int err, i, list_idx;
 +
 +    if (!sh->first_slice_in_pic_flag) {
 +        err = ff_vaapi_decode_make_slice_buffer(avctx, &pic->pic,
 +                                                &pic->last_slice_param, sizeof(pic->last_slice_param),
 +                                                pic->last_buffer, pic->last_size);
 +        pic->last_buffer = NULL;
 +        pic->last_size   = 0;
 +        if (err) {
 +            ff_vaapi_decode_cancel(avctx, &pic->pic);
 +            return err;
 +        }
 +    }
 +
 +    pic->last_slice_param = (VASliceParameterBufferHEVC) {
 +        .slice_data_size               = size,
 +        .slice_data_offset             = 0,
 +        .slice_data_flag               = VA_SLICE_DATA_FLAG_ALL,
 +        /* Add 1 to the bits count here to account for the byte_alignment bit, which
 +         * always is at least one bit and not accounted for otherwise. */
 +        .slice_data_byte_offset        = (get_bits_count(&h->HEVClc->gb) + 1 + 7) / 8,
 +        .slice_segment_address         = sh->slice_segment_addr,
 +        .slice_qp_delta                = sh->slice_qp_delta,
 +        .slice_cb_qp_offset            = sh->slice_cb_qp_offset,
 +        .slice_cr_qp_offset            = sh->slice_cr_qp_offset,
 +        .slice_beta_offset_div2        = sh->beta_offset / 2,
 +        .slice_tc_offset_div2          = sh->tc_offset / 2,
 +        .collocated_ref_idx            = sh->slice_temporal_mvp_enabled_flag ? sh->collocated_ref_idx : 0xFF,
++        .five_minus_max_num_merge_cand = sh->slice_type == HEVC_SLICE_I ? 0 : 5 - sh->max_num_merge_cand,
 +        .num_ref_idx_l0_active_minus1  = sh->nb_refs[L0] ? sh->nb_refs[L0] - 1 : 0,
 +        .num_ref_idx_l1_active_minus1  = sh->nb_refs[L1] ? sh->nb_refs[L1] - 1 : 0,
 +
 +        .LongSliceFlags.fields = {
 +            .dependent_slice_segment_flag                 = sh->dependent_slice_segment_flag,
 +            .slice_type                                   = sh->slice_type,
 +            .color_plane_id                               = sh->colour_plane_id,
 +            .mvd_l1_zero_flag                             = sh->mvd_l1_zero_flag,
 +            .cabac_init_flag                              = sh->cabac_init_flag,
 +            .slice_temporal_mvp_enabled_flag              = sh->slice_temporal_mvp_enabled_flag,
 +            .slice_deblocking_filter_disabled_flag        = sh->disable_deblocking_filter_flag,
 +            .collocated_from_l0_flag                      = sh->collocated_list == L0 ? 1 : 0,
 +            .slice_loop_filter_across_slices_enabled_flag = sh->slice_loop_filter_across_slices_enabled_flag,
 +            .slice_sao_luma_flag                          = sh->slice_sample_adaptive_offset_flag[0],
 +            .slice_sao_chroma_flag                        = sh->slice_sample_adaptive_offset_flag[1],
 +        },
 +    };
 +
 +    memset(pic->last_slice_param.RefPicList, 0xFF, sizeof(pic->last_slice_param.RefPicList));
 +
 +    for (list_idx = 0; list_idx < nb_list; list_idx++) {
 +        RefPicList *rpl = &h->ref->refPicList[list_idx];
 +
 +        for (i = 0; i < rpl->nb_refs; i++)
 +            pic->last_slice_param.RefPicList[list_idx][i] = get_ref_pic_index(h, rpl->ref[i]);
 +    }
 +
 +    fill_pred_weight_table(h, sh, &pic->last_slice_param);
 +
 +    pic->last_buffer = buffer;
 +    pic->last_size   = size;
 +
 +    return 0;
 +}
 +
 +AVHWAccel ff_hevc_vaapi_hwaccel = {
 +    .name                 = "hevc_vaapi",
 +    .type                 = AVMEDIA_TYPE_VIDEO,
 +    .id                   = AV_CODEC_ID_HEVC,
 +    .pix_fmt              = AV_PIX_FMT_VAAPI,
 +    .start_frame          = vaapi_hevc_start_frame,
 +    .end_frame            = vaapi_hevc_end_frame,
 +    .decode_slice         = vaapi_hevc_decode_slice,
 +    .frame_priv_data_size = sizeof(VAAPIDecodePictureHEVC),
 +    .init                 = ff_vaapi_decode_init,
 +    .uninit               = ff_vaapi_decode_uninit,
 +    .priv_data_size       = sizeof(VAAPIDecodeContext),
 +    .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
 +};