Merge commit '4abe3b049d987420eb891f74a35af2cebbf52144'
authorClément Bœsch <u@pkh.me>
Thu, 23 Mar 2017 15:48:41 +0000 (16:48 +0100)
committerClément Bœsch <u@pkh.me>
Thu, 23 Mar 2017 15:48:41 +0000 (16:48 +0100)
* commit '4abe3b049d987420eb891f74a35af2cebbf52144':
  hevc: rename hevc.[ch] to hevcdec.[ch]

Merged-by: Clément Bœsch <u@pkh.me>
25 files changed:
1  2 
libavcodec/Makefile
libavcodec/dxva2_hevc.c
libavcodec/h2645_parse.c
libavcodec/hevc_cabac.c
libavcodec/hevc_data.c
libavcodec/hevc_filter.c
libavcodec/hevc_mp4toannexb_bsf.c
libavcodec/hevc_mvs.c
libavcodec/hevc_parse.c
libavcodec/hevc_parse.h
libavcodec/hevc_parser.c
libavcodec/hevc_ps.c
libavcodec/hevc_ps_enc.c
libavcodec/hevc_refs.c
libavcodec/hevc_sei.c
libavcodec/hevcdec.c
libavcodec/hevcdec.h
libavcodec/hevcdsp_template.c
libavcodec/hevcpred.c
libavcodec/qsvenc_hevc.c
libavcodec/vaapi_encode_h265.c
libavcodec/vaapi_hevc.c
libavcodec/vdpau_hevc.c
libavformat/hevc.c
libavformat/hevcdec.c

@@@ -331,16 -262,12 +331,16 @@@ OBJS-$(CONFIG_H264_OMX_ENCODER)        
  OBJS-$(CONFIG_H264_QSV_DECODER)        += qsvdec_h2645.o
  OBJS-$(CONFIG_H264_QSV_ENCODER)        += qsvenc_h264.o
  OBJS-$(CONFIG_H264_VAAPI_ENCODER)      += vaapi_encode_h264.o vaapi_encode_h26x.o
 +OBJS-$(CONFIG_H264_VIDEOTOOLBOX_ENCODER) += videotoolboxenc.o
  OBJS-$(CONFIG_HAP_DECODER)             += hapdec.o hap.o
  OBJS-$(CONFIG_HAP_ENCODER)             += hapenc.o hap.o
- OBJS-$(CONFIG_HEVC_DECODER)            += hevc.o hevc_mvs.o hevc_ps.o hevc_sei.o \
+ OBJS-$(CONFIG_HEVC_DECODER)            += hevcdec.o hevc_mvs.o hevc_ps.o hevc_sei.o \
                                            hevc_cabac.o hevc_refs.o hevcpred.o    \
                                            hevcdsp.o hevc_filter.o h2645_parse.o hevc_data.o
 +OBJS-$(CONFIG_HEVC_CUVID_DECODER)      += cuvid.o
 +OBJS-$(CONFIG_HEVC_MEDIACODEC_DECODER) += mediacodecdec.o hevc_parse.o
  OBJS-$(CONFIG_HEVC_NVENC_ENCODER)      += nvenc_hevc.o
 +OBJS-$(CONFIG_NVENC_HEVC_ENCODER)      += nvenc_hevc.o
  OBJS-$(CONFIG_HEVC_QSV_DECODER)        += qsvdec_h2645.o
  OBJS-$(CONFIG_HEVC_QSV_ENCODER)        += qsvenc_hevc.o hevc_ps_enc.o h2645_parse.o
  OBJS-$(CONFIG_HEVC_VAAPI_ENCODER)      += vaapi_encode_h265.o vaapi_encode_h26x.o
Simple merge
@@@ -26,7 -26,6 +26,7 @@@
  #include "libavutil/intreadwrite.h"
  #include "libavutil/mem.h"
  
- #include "hevc.h"
++#include "hevcdec.h"
  #include "h2645_parse.h"
  
  int ff_h2645_extract_rbsp(const uint8_t *src, int length,
Simple merge
Simple merge
  #include "libavutil/internal.h"
  
  #include "cabac_functions.h"
- #include "hevc.h"
+ #include "hevcdec.h"
  
 +#include "bit_depth_template.c"
 +
  #define LUMA 0
  #define CB 1
  #define CR 2
Simple merge
Simple merge
index cf04bc2,0000000..d6bf872
mode 100644,000000..100644
--- /dev/null
@@@ -1,134 -1,0 +1,134 @@@
- #include "hevc.h"
 +/*
 + * 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 "bytestream.h"
 +#include "h2645_parse.h"
++#include "hevcdec.h"
 +#include "hevc_parse.h"
 +
 +static int hevc_decode_nal_units(const uint8_t *buf, int buf_size, HEVCParamSets *ps,
 +                                 int is_nalff, int nal_length_size, void *logctx)
 +{
 +    int i;
 +    int ret = 0;
 +    H2645Packet pkt = { 0 };
 +
 +    ret = ff_h2645_packet_split(&pkt, buf, buf_size, logctx, is_nalff, nal_length_size, AV_CODEC_ID_HEVC, 1);
 +    if (ret < 0) {
 +        goto done;
 +    }
 +
 +    for (i = 0; i < pkt.nb_nals; i++) {
 +        H2645NAL *nal = &pkt.nals[i];
 +
 +        /* ignore everything except parameter sets and VCL NALUs */
 +        switch (nal->type) {
 +        case NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, logctx, ps);    break;
 +        case NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, logctx, ps, 1); break;
 +        case NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, logctx, ps);    break;
 +        case NAL_TRAIL_R:
 +        case NAL_TRAIL_N:
 +        case NAL_TSA_N:
 +        case NAL_TSA_R:
 +        case NAL_STSA_N:
 +        case NAL_STSA_R:
 +        case NAL_BLA_W_LP:
 +        case NAL_BLA_W_RADL:
 +        case NAL_BLA_N_LP:
 +        case NAL_IDR_W_RADL:
 +        case NAL_IDR_N_LP:
 +        case NAL_CRA_NUT:
 +        case NAL_RADL_N:
 +        case NAL_RADL_R:
 +        case NAL_RASL_N:
 +        case NAL_RASL_R:
 +            av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", nal->type);
 +            ret = AVERROR_INVALIDDATA;
 +            goto done;
 +            break;
 +        }
 +    }
 +
 +done:
 +    ff_h2645_packet_uninit(&pkt);
 +    return ret;
 +}
 +
 +int ff_hevc_decode_extradata(const uint8_t *data, int size, HEVCParamSets *ps,
 +                             int *is_nalff, int *nal_length_size,
 +                             int err_recognition, void *logctx)
 +{
 +    int ret = 0;
 +    GetByteContext gb;
 +
 +    bytestream2_init(&gb, data, size);
 +
 +    if (size > 3 && (data[0] || data[1] || data[2] > 1)) {
 +        /* It seems the extradata is encoded as hvcC format.
 +         * Temporarily, we support configurationVersion==0 until 14496-15 3rd
 +         * is finalized. When finalized, configurationVersion will be 1 and we
 +         * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
 +        int i, j, num_arrays, nal_len_size;
 +
 +        *is_nalff = 1;
 +
 +        bytestream2_skip(&gb, 21);
 +        nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
 +        num_arrays   = bytestream2_get_byte(&gb);
 +
 +        /* nal units in the hvcC always have length coded with 2 bytes,
 +         * so put a fake nal_length_size = 2 while parsing them */
 +        *nal_length_size = 2;
 +
 +        /* Decode nal units from hvcC. */
 +        for (i = 0; i < num_arrays; i++) {
 +            int type = bytestream2_get_byte(&gb) & 0x3f;
 +            int cnt  = bytestream2_get_be16(&gb);
 +
 +            for (j = 0; j < cnt; j++) {
 +                // +2 for the nal size field
 +                int nalsize = bytestream2_peek_be16(&gb) + 2;
 +                if (bytestream2_get_bytes_left(&gb) < nalsize) {
 +                    av_log(logctx, AV_LOG_ERROR,
 +                           "Invalid NAL unit size in extradata.\n");
 +                    return AVERROR_INVALIDDATA;
 +                }
 +
 +                ret = hevc_decode_nal_units(gb.buffer, nalsize, ps, *is_nalff, *nal_length_size, logctx);
 +                if (ret < 0) {
 +                    av_log(logctx, AV_LOG_ERROR,
 +                           "Decoding nal unit %d %d from hvcC failed\n",
 +                           type, i);
 +                    return ret;
 +                }
 +                bytestream2_skip(&gb, nalsize);
 +            }
 +        }
 +
 +        /* Now store right nal length size, that will be used to parse
 +         * all other nals */
 +        *nal_length_size = nal_len_size;
 +    } else {
 +        *is_nalff = 0;
 +        ret = hevc_decode_nal_units(data, size, ps, *is_nalff, *nal_length_size, logctx);
 +        if (ret < 0)
 +            return ret;
 +    }
 +
 +    return ret;
 +}
index 877356a,0000000..39bd23e
mode 100644,000000..100644
--- /dev/null
@@@ -1,33 -1,0 +1,33 @@@
- #include "hevc.h"
 +/*
 + * 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
 + */
 +
 +/**
 + * @file
 + * H.265 parser code
 + */
 +
 +#ifndef AVCODEC_HEVC_PARSE_H
 +#define AVCODEC_HEVC_PARSE_H
 +
++#include "hevcdec.h"
 +
 +int ff_hevc_decode_extradata(const uint8_t *data, int size, HEVCParamSets *ps,
 +                             int *is_nalff, int *nal_length_size,
 +                             int err_recognition, void *logctx);
 +
 +#endif /* AVCODEC_HEVC_PARSE_H */
Simple merge
@@@ -24,8 -24,9 +24,8 @@@
   */
  
  #include "libavutil/imgutils.h"
 -
  #include "golomb.h"
- #include "hevc.h"
+ #include "hevcdec.h"
  
  static const uint8_t default_scaling_list_intra[] = {
      16, 16, 16, 16, 17, 18, 21, 24,
Simple merge
Simple merge
Simple merge
  #include "bytestream.h"
  #include "cabac_functions.h"
  #include "golomb.h"
- #include "hevc.h"
+ #include "hevcdec.h"
  #include "profiles.h"
  
 -const uint8_t ff_hevc_qpel_extra_before[4] = { 0, 3, 3, 3 };
 -const uint8_t ff_hevc_qpel_extra_after[4]  = { 0, 4, 4, 4 };
 -const uint8_t ff_hevc_qpel_extra[4]        = { 0, 7, 7, 7 };
 -
 -static const uint8_t scan_1x1[1] = { 0 };
 -
 -static const uint8_t horiz_scan2x2_x[4] = { 0, 1, 0, 1 };
 -
 -static const uint8_t horiz_scan2x2_y[4] = { 0, 0, 1, 1 };
 -
 -static const uint8_t horiz_scan4x4_x[16] = {
 -    0, 1, 2, 3,
 -    0, 1, 2, 3,
 -    0, 1, 2, 3,
 -    0, 1, 2, 3,
 -};
 -
 -static const uint8_t horiz_scan4x4_y[16] = {
 -    0, 0, 0, 0,
 -    1, 1, 1, 1,
 -    2, 2, 2, 2,
 -    3, 3, 3, 3,
 -};
 -
 -static const uint8_t horiz_scan8x8_inv[8][8] = {
 -    {  0,  1,  2,  3, 16, 17, 18, 19, },
 -    {  4,  5,  6,  7, 20, 21, 22, 23, },
 -    {  8,  9, 10, 11, 24, 25, 26, 27, },
 -    { 12, 13, 14, 15, 28, 29, 30, 31, },
 -    { 32, 33, 34, 35, 48, 49, 50, 51, },
 -    { 36, 37, 38, 39, 52, 53, 54, 55, },
 -    { 40, 41, 42, 43, 56, 57, 58, 59, },
 -    { 44, 45, 46, 47, 60, 61, 62, 63, },
 -};
 -
 -static const uint8_t diag_scan2x2_x[4] = { 0, 0, 1, 1 };
 -
 -static const uint8_t diag_scan2x2_y[4] = { 0, 1, 0, 1 };
 -
 -static const uint8_t diag_scan2x2_inv[2][2] = {
 -    { 0, 2, },
 -    { 1, 3, },
 -};
 -
 -static const uint8_t diag_scan4x4_inv[4][4] = {
 -    { 0,  2,  5,  9, },
 -    { 1,  4,  8, 12, },
 -    { 3,  7, 11, 14, },
 -    { 6, 10, 13, 15, },
 -};
 -
 -static const uint8_t diag_scan8x8_inv[8][8] = {
 -    {  0,  2,  5,  9, 14, 20, 27, 35, },
 -    {  1,  4,  8, 13, 19, 26, 34, 42, },
 -    {  3,  7, 12, 18, 25, 33, 41, 48, },
 -    {  6, 11, 17, 24, 32, 40, 47, 53, },
 -    { 10, 16, 23, 31, 39, 46, 52, 57, },
 -    { 15, 22, 30, 38, 45, 51, 56, 60, },
 -    { 21, 29, 37, 44, 50, 55, 59, 62, },
 -    { 28, 36, 43, 49, 54, 58, 61, 63, },
 -};
 +const uint8_t ff_hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 };
  
  /**
   * NOTE: Each function hls_foo correspond to the function foo in the
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   */
  
- #ifndef AVCODEC_HEVC_H
- #define AVCODEC_HEVC_H
+ #ifndef AVCODEC_HEVCDEC_H
+ #define AVCODEC_HEVCDEC_H
  
 -#include <stddef.h>
 -#include <stdint.h>
 +#include <stdatomic.h>
  
  #include "libavutil/buffer.h"
  #include "libavutil/md5.h"
   */
  
  #include "get_bits.h"
- #include "hevc.h"
+ #include "hevcdec.h"
  
  #include "bit_depth_template.c"
 +#include "hevcdsp.h"
  
 -static void FUNC(put_pcm)(uint8_t *_dst, ptrdiff_t stride, int size,
 +static void FUNC(put_pcm)(uint8_t *_dst, ptrdiff_t stride, int width, int height,
                            GetBitContext *gb, int pcm_bit_depth)
  {
      int x, y;
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   */
  
- #include "hevc.h"
+ #include "hevcdec.h"
  
 +#include "hevcpred.h"
 +
  #define BIT_DEPTH 8
  #include "hevcpred_template.c"
  #undef BIT_DEPTH
Simple merge
Simple merge
index 34d3279,0000000..6dd35db
mode 100644,000000..100644
--- /dev/null
@@@ -1,439 -1,0 +1,439 @@@
- #include "hevc.h"
 +/*
 + * 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 == I_SLICE ||
 +        (sh->slice_type == P_SLICE && !h->ps.pps->weighted_pred_flag) ||
 +        (sh->slice_type == B_SLICE && !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];
 +    }
 +
 +    if (sh->slice_type == B_SLICE) {
 +        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;
 +
 +    int nb_list = (sh->slice_type == B_SLICE) ?
 +                  2 : (sh->slice_type == I_SLICE ? 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 == I_SLICE ? 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,
 +};
@@@ -24,8 -24,7 +24,8 @@@
  
  #include "avcodec.h"
  #include "internal.h"
- #include "hevc.h"
+ #include "hevcdec.h"
 +#include "hwaccel.h"
  #include "vdpau.h"
  #include "vdpau_internal.h"
  
Simple merge
Simple merge