Merge commit '064698d381e1e7790f21b0199a8930ea04e2e942'
authorMichael Niedermayer <michaelni@gmx.at>
Fri, 1 Nov 2013 15:14:39 +0000 (16:14 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Fri, 1 Nov 2013 16:13:00 +0000 (17:13 +0100)
* commit '064698d381e1e7790f21b0199a8930ea04e2e942':
  Add HEVC decoder

Conflicts:
Changelog
libavcodec/Makefile
libavcodec/allcodecs.c
libavcodec/hevc.c
libavcodec/hevc.h
libavcodec/hevc_cabac.c
libavcodec/hevc_filter.c
libavcodec/hevc_mvs.c
libavcodec/hevc_parser.c
libavcodec/hevc_ps.c
libavcodec/hevc_refs.c
libavcodec/hevc_sei.c
libavcodec/hevcdsp.c
libavcodec/hevcdsp_template.c
libavcodec/hevcpred.c
libavcodec/hevcpred_template.c
libavcodec/version.h

cosmetics from hevc.h & hevc_ps.c mostly merged, other files left as they where in ffmpeg.

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
doc/general.texi
libavcodec/avcodec.h
libavcodec/codec_desc.c
libavcodec/hevc.c
libavcodec/hevc.h
libavcodec/hevc_cabac.c
libavcodec/hevc_ps.c
libavcodec/utils.c
libavcodec/version.h

Simple merge
@@@ -273,38 -271,11 +273,39 @@@ enum AVCodecID 
      AV_CODEC_ID_MSS2,
      AV_CODEC_ID_VP9,
      AV_CODEC_ID_AIC,
 -    AV_CODEC_ID_ESCAPE130,
 -    AV_CODEC_ID_G2M,
 -    AV_CODEC_ID_WEBP,
 +    AV_CODEC_ID_ESCAPE130_DEPRECATED,
 +    AV_CODEC_ID_G2M_DEPRECATED,
 +    AV_CODEC_ID_WEBP_DEPRECATED,
      AV_CODEC_ID_HNM4_VIDEO,
 -    AV_CODEC_ID_HEVC,
++    AV_CODEC_ID_HEVC_DEPRECATED,
 +
 +    AV_CODEC_ID_BRENDER_PIX= MKBETAG('B','P','I','X'),
 +    AV_CODEC_ID_Y41P       = MKBETAG('Y','4','1','P'),
 +    AV_CODEC_ID_ESCAPE130  = MKBETAG('E','1','3','0'),
 +    AV_CODEC_ID_EXR        = MKBETAG('0','E','X','R'),
 +    AV_CODEC_ID_AVRP       = MKBETAG('A','V','R','P'),
 +
 +    AV_CODEC_ID_012V       = MKBETAG('0','1','2','V'),
 +    AV_CODEC_ID_G2M        = MKBETAG( 0 ,'G','2','M'),
 +    AV_CODEC_ID_AVUI       = MKBETAG('A','V','U','I'),
 +    AV_CODEC_ID_AYUV       = MKBETAG('A','Y','U','V'),
 +    AV_CODEC_ID_TARGA_Y216 = MKBETAG('T','2','1','6'),
 +    AV_CODEC_ID_V308       = MKBETAG('V','3','0','8'),
 +    AV_CODEC_ID_V408       = MKBETAG('V','4','0','8'),
 +    AV_CODEC_ID_YUV4       = MKBETAG('Y','U','V','4'),
 +    AV_CODEC_ID_SANM       = MKBETAG('S','A','N','M'),
 +    AV_CODEC_ID_PAF_VIDEO  = MKBETAG('P','A','F','V'),
 +    AV_CODEC_ID_AVRN       = MKBETAG('A','V','R','n'),
 +    AV_CODEC_ID_CPIA       = MKBETAG('C','P','I','A'),
 +    AV_CODEC_ID_XFACE      = MKBETAG('X','F','A','C'),
 +    AV_CODEC_ID_SGIRLE     = MKBETAG('S','G','I','R'),
 +    AV_CODEC_ID_MVC1       = MKBETAG('M','V','C','1'),
 +    AV_CODEC_ID_MVC2       = MKBETAG('M','V','C','2'),
 +    AV_CODEC_ID_SNOW       = MKBETAG('S','N','O','W'),
 +    AV_CODEC_ID_WEBP       = MKBETAG('W','E','B','P'),
 +    AV_CODEC_ID_SMVJPEG    = MKBETAG('S','M','V','J'),
 +    AV_CODEC_ID_HEVC       = MKBETAG('H','2','6','5'),
 +#define AV_CODEC_ID_H265 AV_CODEC_ID_HEVC
  
      /* various PCM "codecs" */
      AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
@@@ -1401,6 -1243,13 +1394,13 @@@ static const AVCodecDescriptor codec_de
          .long_name = NULL_IF_CONFIG_SMALL("HNM 4 video"),
          .props     = AV_CODEC_PROP_LOSSY,
      },
 -        .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
+     {
+         .id        = AV_CODEC_ID_HEVC,
+         .type      = AVMEDIA_TYPE_VIDEO,
+         .name      = "hevc",
++        .long_name = NULL_IF_CONFIG_SMALL("H.265 / HEVC (High Efficiency Video Coding)"),
+         .props     = AV_CODEC_PROP_LOSSY,
+     },
  
      /* various PCM "codecs" */
      {
@@@ -2882,7 -3087,7 +2882,7 @@@ static void hevc_decode_flush(AVCodecCo
  #define OFFSET(x) offsetof(HEVCContext, x)
  #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
  static const AVOption options[] = {
-     { "strict-displaywin", "stricly apply default display window size", OFFSET(strict_def_disp_win),
 -    { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
++    { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
          AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
      { NULL },
  };
@@@ -493,10 -491,10 +494,10 @@@ typedef struct HEVCPPS 
      uint8_t deblocking_filter_control_present_flag;
      uint8_t deblocking_filter_override_enabled_flag;
      uint8_t disable_dbf;
-     int beta_offset; ///< beta_offset_div2 * 2
-     int tc_offset; ///< tc_offset_div2 * 2
+     int beta_offset;    ///< beta_offset_div2 * 2
+     int tc_offset;      ///< tc_offset_div2 * 2
  
-     int pps_scaling_list_data_present_flag;
 -    uint8_t scaling_list_data_present_flag;
++    int scaling_list_data_present_flag;
      ScalingList scaling_list;
  
      uint8_t lists_modification_present_flag;
      int num_extra_slice_header_bits;
      uint8_t slice_header_extension_present_flag;
  
 +    uint8_t pps_extension_flag;
 +    uint8_t pps_extension_data_flag;
 +
      // Inferred parameters
-     int *column_width; ///< ColumnWidth
-     int *row_height; ///< RowHeight
-     int *col_bd; ///< ColBd
-     int *row_bd; ///< RowBd
+     int *column_width;  ///< ColumnWidth
+     int *row_height;    ///< RowHeight
+     int *col_bd;        ///< ColBd
+     int *row_bd;        ///< RowBd
      int *col_idxX;
  
      int *ctb_addr_rs_to_ts; ///< CtbAddrRSToTS
@@@ -565,15 -560,11 +566,15 @@@ typedef struct SliceHeader 
      int slice_cb_qp_offset;
      int slice_cr_qp_offset;
  
-     int beta_offset; ///< beta_offset_div2 * 2
-     int tc_offset; ///< tc_offset_div2 * 2
+     int beta_offset;    ///< beta_offset_div2 * 2
+     int tc_offset;      ///< tc_offset_div2 * 2
  
 -    unsigned int max_num_merge_cand; ///< 5 - 5_minus_max_num_merge_cand
 +    int max_num_merge_cand; ///< 5 - 5_minus_max_num_merge_cand
  
 +
 +    int *entry_point_offset;
 +    int * offset;
 +    int * size;
      int num_entry_point_offsets;
  
      int8_t slice_qp;
@@@ -756,20 -826,11 +757,20 @@@ typedef struct HEVCLocalContext 
  
  typedef struct HEVCContext {
      const AVClass *c;  // needed by private avoptions
-     AVCodecContext      *avctx;
+     AVCodecContext *avctx;
  
-     struct HEVCContext  *sList[MAX_NB_THREADS];
 -    HEVCLocalContext HEVClc;
++    struct HEVCContext *sList[MAX_NB_THREADS];
  
-     HEVCLocalContext    *HEVClcList[MAX_NB_THREADS];
-     HEVCLocalContext    *HEVClc;
 -    uint8_t cabac_state[HEVC_CONTEXTS];
++    HEVCLocalContext *HEVClcList[MAX_NB_THREADS];
++    HEVCLocalContext *HEVClc;
 +
 +    uint8_t             threads_type;
 +    uint8_t             threads_number;
 +
 +    int                 width;
 +    int                 height;
 +
 +    uint8_t *cabac_state;
  
      /** 1 if the independent slice segment header was successfully parsed */
      uint8_t slice_initialized;
      AVFrame *sao_frame;
      AVFrame *tmp_frame;
      AVFrame *output_frame;
-     VPS *vps;
 -
 -    const HEVCVPS *vps;
++    HEVCVPS *vps;
      const HEVCSPS *sps;
 -    const HEVCPPS *pps;
 +    HEVCPPS *pps;
-     VPS *vps_list[MAX_VPS_COUNT];
+     HEVCVPS     *vps_list[MAX_VPS_COUNT];
      AVBufferRef *sps_list[MAX_SPS_COUNT];
      AVBufferRef *pps_list[MAX_PPS_COUNT];
  
      uint8_t       md5[3][16];
      uint8_t is_md5;
  
 -    uint8_t context_initialized;
 -    uint8_t is_nalff;       ///< this flag is != 0 if bitstream is encapsulated
 +    int context_initialized;
-     int is_nalff;         ///< this flag is != 0 if bitstream is encapsulated
-                           ///< as a format defined in 14496-15
-     int strict_def_disp_win;
++    int is_nalff;           ///< this flag is != 0 if bitstream is encapsulated
+                             ///< as a format defined in 14496-15
+     int apply_defdispwin;
  
-     int nal_length_size;  ///< Number of bytes used for nal length (1, 2 or 4)
 +    int active_seq_parameter_set_id;
 +
+     int nal_length_size;    ///< Number of bytes used for nal length (1, 2 or 4)
      int nuh_layer_id;
 +
 +    int picture_struct;
  } HEVCContext;
  
  int ff_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps,
@@@ -1051,374 -867,6 +1051,374 @@@ static av_always_inline int coeff_sign_
      int ret = 0;
  
      for (i = 0; i < nb; i++)
 -        ret = (ret << 1) | get_cabac_bypass(&s->HEVClc.cc);
 +        ret = (ret << 1) | get_cabac_bypass(&s->HEVClc->cc);
      return ret;
  }
-             const ScalingList *sl = s->pps->pps_scaling_list_data_present_flag ?
 +
 +void ff_hevc_hls_residual_coding(HEVCContext *s, int x0, int y0,
 +                                int log2_trafo_size, enum ScanType scan_idx,
 +                                int c_idx)
 +{
 +#define GET_COORD(offset, n)                                    \
 +    do {                                                        \
 +        x_c = (scan_x_cg[offset >> 4] << 2) + scan_x_off[n];    \
 +        y_c = (scan_y_cg[offset >> 4] << 2) + scan_y_off[n];    \
 +    } while (0)
 +    HEVCLocalContext *lc = s->HEVClc;
 +    int transform_skip_flag = 0;
 +
 +    int last_significant_coeff_x, last_significant_coeff_y;
 +    int last_scan_pos;
 +    int n_end;
 +    int num_coeff = 0;
 +    int greater1_ctx = 1;
 +
 +    int num_last_subset;
 +    int x_cg_last_sig, y_cg_last_sig;
 +
 +    const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off;
 +
 +    ptrdiff_t stride = s->frame->linesize[c_idx];
 +    int hshift = s->sps->hshift[c_idx];
 +    int vshift = s->sps->vshift[c_idx];
 +    uint8_t *dst = &s->frame->data[c_idx][(y0 >> vshift) * stride +
 +                                          ((x0 >> hshift) << s->sps->pixel_shift)];
 +    DECLARE_ALIGNED(16, int16_t, coeffs[MAX_TB_SIZE * MAX_TB_SIZE]) = {0};
 +    DECLARE_ALIGNED(8, uint8_t, significant_coeff_group_flag[8][8]) = {{0}};
 +
 +    int trafo_size = 1 << log2_trafo_size;
 +    int i;
 +    int qp,shift,add,scale,scale_m;
 +    const uint8_t level_scale[] = { 40, 45, 51, 57, 64, 72 };
 +    const uint8_t *scale_matrix;
 +    uint8_t dc_scale;
 +
 +    // Derive QP for dequant
 +    if (!lc->cu.cu_transquant_bypass_flag) {
 +        static const int qp_c[] = { 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37 };
 +        static const uint8_t rem6[51 + 2 * 6 + 1] = {
 +            0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
 +            3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
 +            0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
 +        };
 +
 +        static const uint8_t div6[51 + 2 * 6 + 1] = {
 +            0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3,  3,  3,
 +            3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6,  6,  6,
 +            7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
 +        };
 +        int qp_y = lc->qp_y;
 +
 +        if (c_idx == 0) {
 +            qp = qp_y + s->sps->qp_bd_offset;
 +        } else {
 +            int qp_i, offset;
 +
 +            if (c_idx == 1)
 +                offset = s->pps->cb_qp_offset + s->sh.slice_cb_qp_offset;
 +            else
 +                offset = s->pps->cr_qp_offset + s->sh.slice_cr_qp_offset;
 +
 +            qp_i = av_clip_c(qp_y + offset, - s->sps->qp_bd_offset, 57);
 +            if (qp_i < 30)
 +                qp = qp_i;
 +            else if (qp_i > 43)
 +                qp = qp_i - 6;
 +            else
 +                qp = qp_c[qp_i - 30];
 +
 +            qp += s->sps->qp_bd_offset;
 +        }
 +
 +        shift    = s->sps->bit_depth + log2_trafo_size - 5;
 +        add      = 1 << (shift-1);
 +        scale    = level_scale[rem6[qp]] << (div6[qp]);
 +        scale_m  = 16; // default when no custom scaling lists.
 +        dc_scale = 16;
 +
 +        if (s->sps->scaling_list_enable_flag) {
++            const ScalingList *sl = s->pps->scaling_list_data_present_flag ?
 +            &s->pps->scaling_list : &s->sps->scaling_list;
 +            int matrix_id = lc->cu.pred_mode != MODE_INTRA;
 +
 +            if (log2_trafo_size != 5)
 +                matrix_id = 3 * matrix_id + c_idx;
 +
 +            scale_matrix = sl->sl[log2_trafo_size - 2][matrix_id];
 +            if (log2_trafo_size >= 4)
 +                dc_scale = sl->sl_dc[log2_trafo_size - 4][matrix_id];
 +        }
 +    }
 +
 +    if (s->pps->transform_skip_enabled_flag && !lc->cu.cu_transquant_bypass_flag &&
 +        log2_trafo_size == 2) {
 +        transform_skip_flag = ff_hevc_transform_skip_flag_decode(s, c_idx);
 +    }
 +
 +    last_significant_coeff_x =
 +        last_significant_coeff_x_prefix_decode(s, c_idx, log2_trafo_size);
 +    last_significant_coeff_y =
 +        last_significant_coeff_y_prefix_decode(s, c_idx, log2_trafo_size);
 +
 +    if (last_significant_coeff_x > 3) {
 +        int suffix = last_significant_coeff_suffix_decode(s, last_significant_coeff_x);
 +        last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
 +        (2 + (last_significant_coeff_x & 1)) +
 +        suffix;
 +    }
 +
 +    if (last_significant_coeff_y > 3) {
 +        int suffix = last_significant_coeff_suffix_decode(s, last_significant_coeff_y);
 +        last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
 +        (2 + (last_significant_coeff_y & 1)) +
 +        suffix;
 +    }
 +
 +    if (scan_idx == SCAN_VERT)
 +        FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y);
 +
 +    x_cg_last_sig = last_significant_coeff_x >> 2;
 +    y_cg_last_sig = last_significant_coeff_y >> 2;
 +
 +    switch (scan_idx) {
 +    case SCAN_DIAG: {
 +        int last_x_c = last_significant_coeff_x & 3;
 +        int last_y_c = last_significant_coeff_y & 3;
 +
 +        scan_x_off = ff_hevc_diag_scan4x4_x;
 +        scan_y_off = ff_hevc_diag_scan4x4_y;
 +        num_coeff = diag_scan4x4_inv[last_y_c][last_x_c];
 +        if (trafo_size == 4) {
 +            scan_x_cg = scan_1x1;
 +            scan_y_cg = scan_1x1;
 +        } else if (trafo_size == 8) {
 +            num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4;
 +            scan_x_cg = diag_scan2x2_x;
 +            scan_y_cg = diag_scan2x2_y;
 +        } else if (trafo_size == 16) {
 +            num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4;
 +            scan_x_cg = ff_hevc_diag_scan4x4_x;
 +            scan_y_cg = ff_hevc_diag_scan4x4_y;
 +        } else { // trafo_size == 32
 +            num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4;
 +            scan_x_cg = ff_hevc_diag_scan8x8_x;
 +            scan_y_cg = ff_hevc_diag_scan8x8_y;
 +        }
 +        break;
 +    }
 +    case SCAN_HORIZ:
 +        scan_x_cg = horiz_scan2x2_x;
 +        scan_y_cg = horiz_scan2x2_y;
 +        scan_x_off = horiz_scan4x4_x;
 +        scan_y_off = horiz_scan4x4_y;
 +        num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
 +        break;
 +    default: //SCAN_VERT
 +        scan_x_cg = horiz_scan2x2_y;
 +        scan_y_cg = horiz_scan2x2_x;
 +        scan_x_off = horiz_scan4x4_y;
 +        scan_y_off = horiz_scan4x4_x;
 +        num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
 +        break;
 +    }
 +    num_coeff++;
 +    num_last_subset = (num_coeff - 1) >> 4;
 +
 +    for (i = num_last_subset; i >= 0; i--) {
 +        int n, m;
 +        int x_cg, y_cg, x_c, y_c, pos;
 +        int implicit_non_zero_coeff = 0;
 +        int64_t trans_coeff_level;
 +        int prev_sig = 0;
 +        int offset = i << 4;
 +
 +        uint8_t significant_coeff_flag_idx[16];
 +        uint8_t nb_significant_coeff_flag = 0;
 +
 +        x_cg = scan_x_cg[i];
 +        y_cg = scan_y_cg[i];
 +
 +        if ((i < num_last_subset) && (i > 0)) {
 +            int ctx_cg = 0;
 +            if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
 +                ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];
 +            if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
 +                ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];
 +
 +            significant_coeff_group_flag[x_cg][y_cg] =
 +                significant_coeff_group_flag_decode(s, c_idx, ctx_cg);
 +            implicit_non_zero_coeff = 1;
 +        } else {
 +            significant_coeff_group_flag[x_cg][y_cg] =
 +            ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
 +             (x_cg == 0 && y_cg == 0));
 +        }
 +
 +        last_scan_pos = num_coeff - offset - 1;
 +
 +        if (i == num_last_subset) {
 +            n_end = last_scan_pos - 1;
 +            significant_coeff_flag_idx[0] = last_scan_pos;
 +            nb_significant_coeff_flag = 1;
 +        } else {
 +            n_end = 15;
 +        }
 +
 +        if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
 +            prev_sig = significant_coeff_group_flag[x_cg + 1][y_cg];
 +        if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
 +            prev_sig += (significant_coeff_group_flag[x_cg][y_cg + 1] << 1);
 +
 +        for (n = n_end; n >= 0; n--) {
 +            GET_COORD(offset, n);
 +
 +            if (significant_coeff_group_flag[x_cg][y_cg] &&
 +                (n > 0 || implicit_non_zero_coeff == 0)) {
 +                if (significant_coeff_flag_decode(s, c_idx, x_c, y_c, log2_trafo_size, scan_idx, prev_sig) == 1) {
 +                    significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
 +                    nb_significant_coeff_flag++;
 +                    implicit_non_zero_coeff = 0;
 +                }
 +            } else {
 +                int last_cg = (x_c == (x_cg << 2) && y_c == (y_cg << 2));
 +                if (last_cg && implicit_non_zero_coeff && significant_coeff_group_flag[x_cg][y_cg]) {
 +                    significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
 +                    nb_significant_coeff_flag++;
 +                }
 +            }
 +        }
 +
 +        n_end = nb_significant_coeff_flag;
 +
 +
 +        if (n_end) {
 +            int first_nz_pos_in_cg = 16;
 +            int last_nz_pos_in_cg = -1;
 +            int c_rice_param = 0;
 +            int first_greater1_coeff_idx = -1;
 +            uint8_t coeff_abs_level_greater1_flag[16] = {0};
 +            uint16_t coeff_sign_flag;
 +            int sum_abs = 0;
 +            int sign_hidden = 0;
 +
 +            // initialize first elem of coeff_bas_level_greater1_flag
 +            int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;
 +
 +            if (!(i == num_last_subset) && greater1_ctx == 0)
 +                ctx_set++;
 +            greater1_ctx = 1;
 +            last_nz_pos_in_cg = significant_coeff_flag_idx[0];
 +
 +            for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {
 +                int n_idx = significant_coeff_flag_idx[m];
 +                int inc = (ctx_set << 2) + greater1_ctx;
 +                coeff_abs_level_greater1_flag[n_idx] =
 +                    coeff_abs_level_greater1_flag_decode(s, c_idx, inc);
 +                if (coeff_abs_level_greater1_flag[n_idx]) {
 +                    greater1_ctx = 0;
 +                } else if (greater1_ctx > 0 && greater1_ctx < 3) {
 +                    greater1_ctx++;
 +                }
 +
 +                if (coeff_abs_level_greater1_flag[n_idx] &&
 +                    first_greater1_coeff_idx == -1)
 +                    first_greater1_coeff_idx = n_idx;
 +            }
 +            first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];
 +            sign_hidden = (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4 &&
 +                           !lc->cu.cu_transquant_bypass_flag);
 +
 +            if (first_greater1_coeff_idx != -1) {
 +                coeff_abs_level_greater1_flag[first_greater1_coeff_idx] += coeff_abs_level_greater2_flag_decode(s, c_idx, ctx_set);
 +            }
 +            if (!s->pps->sign_data_hiding_flag || !sign_hidden ) {
 +                coeff_sign_flag = coeff_sign_flag_decode(s, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
 +            } else {
 +                coeff_sign_flag = coeff_sign_flag_decode(s, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1));
 +            }
 +
 +            for (m = 0; m < n_end; m++) {
 +                n = significant_coeff_flag_idx[m];
 +                GET_COORD(offset, n);
 +                trans_coeff_level = 1 + coeff_abs_level_greater1_flag[n];
 +                if (trans_coeff_level == ((m < 8) ?
 +                                          ((n == first_greater1_coeff_idx) ? 3 : 2) : 1)) {
 +                    int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(s, trans_coeff_level, c_rice_param);
 +
 +                    trans_coeff_level += last_coeff_abs_level_remaining;
 +                    if (trans_coeff_level > (3 << c_rice_param))
 +                        c_rice_param = FFMIN(c_rice_param + 1, 4);
 +
 +                }
 +                if (s->pps->sign_data_hiding_flag && sign_hidden) {
 +                    sum_abs += trans_coeff_level;
 +                    if (n == first_nz_pos_in_cg && (sum_abs&1))
 +                        trans_coeff_level = -trans_coeff_level;
 +                }
 +                if (coeff_sign_flag >> 15)
 +                    trans_coeff_level = -trans_coeff_level;
 +                coeff_sign_flag <<= 1;
 +                if(!lc->cu.cu_transquant_bypass_flag) {
 +                    if(s->sps->scaling_list_enable_flag) {
 +                        if(y_c || x_c || log2_trafo_size < 4) {
 +                            switch(log2_trafo_size) {
 +                                case 3: pos = (y_c << 3) + x_c; break;
 +                                case 4: pos = ((y_c >> 1) << 3) + (x_c >> 1); break;
 +                                case 5: pos = ((y_c >> 2) << 3) + (x_c >> 2); break;
 +                                default: pos = (y_c << 2) + x_c;
 +                            }
 +                            scale_m = scale_matrix[pos];
 +                        } else {
 +                            scale_m = dc_scale;
 +                        }
 +                    }
 +                    trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;
 +                    if(trans_coeff_level < 0) {
 +                        if((~trans_coeff_level) & 0xFffffffffff8000)
 +                            trans_coeff_level = -32768;
 +                    } else {
 +                        if(trans_coeff_level & 0xffffffffffff8000)
 +                            trans_coeff_level = 32767;
 +                    }
 +                }
 +                coeffs[y_c * trafo_size + x_c] = trans_coeff_level;
 +            }
 +        }
 +    }
 +
 +    if (lc->cu.cu_transquant_bypass_flag) {
 +        s->hevcdsp.transquant_bypass[log2_trafo_size-2](dst, coeffs, stride);
 +    } else {
 +        if (transform_skip_flag)
 +            s->hevcdsp.transform_skip(dst, coeffs, stride);
 +        else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 && log2_trafo_size == 2)
 +            s->hevcdsp.transform_4x4_luma_add(dst, coeffs, stride);
 +        else
 +            s->hevcdsp.transform_add[log2_trafo_size-2](dst, coeffs, stride);
 +    }
 +}
 +
 +void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size)
 +{
 +    HEVCLocalContext *lc = s->HEVClc;
 +    int x = abs_mvd_greater0_flag_decode(s);
 +    int y = abs_mvd_greater0_flag_decode(s);
 +
 +    if (x)
 +        x += abs_mvd_greater1_flag_decode(s);
 +    if (y)
 +        y += abs_mvd_greater1_flag_decode(s);
 +
 +    switch (x) {
 +    case 2: lc->pu.mvd.x = mvd_decode(s);           break;
 +    case 1: lc->pu.mvd.x = mvd_sign_flag_decode(s); break;
 +    case 0: lc->pu.mvd.x = 0;                       break;
 +    }
 +
 +    switch (y) {
 +    case 2: lc->pu.mvd.y = mvd_decode(s);           break;
 +    case 1: lc->pu.mvd.y = mvd_sign_flag_decode(s); break;
 +    case 0: lc->pu.mvd.y = 0;                       break;
 +    }
 +}
 +
@@@ -191,28 -192,21 +192,29 @@@ int ff_hevc_decode_short_term_rps(HEVCC
      return 0;
  }
  
- static int decode_profile_tier_level(HEVCContext *s, PTL *ptl, int max_num_sub_layers)
 -static int decode_profile_tier_level(HEVCLocalContext *lc, PTL *ptl,
++static int decode_profile_tier_level(HEVCContext *s, PTL *ptl,
+                                      int max_num_sub_layers)
  {
+     int i, j;
 +    HEVCLocalContext *lc = s->HEVClc;
      GetBitContext *gb = &lc->gb;
-     int i, j;
  
      ptl->general_profile_space = get_bits(gb, 2);
-     ptl->general_tier_flag = get_bits1(gb);
-     ptl->general_profile_idc = get_bits(gb, 5);
+     ptl->general_tier_flag     = get_bits1(gb);
+     ptl->general_profile_idc   = get_bits(gb, 5);
 +    if (ptl->general_profile_idc == 1)
 +        av_log(s->avctx, AV_LOG_DEBUG, "Main profile bitstream\n");
 +    else if (ptl->general_profile_idc == 2)
 +        av_log(s->avctx, AV_LOG_DEBUG, "Main10 profile bitstream\n");
 +    else
 +        av_log(s->avctx, AV_LOG_WARNING, "No profile indication! (%d)\n", ptl->general_profile_idc);
 +
      for (i = 0; i < 32; i++)
          ptl->general_profile_compatibility_flag[i] = get_bits1(gb);
-     skip_bits1(gb);// general_progressive_source_flag
-     skip_bits1(gb);// general_interlaced_source_flag
-     skip_bits1(gb);// general_non_packed_constraint_flag
-     skip_bits1(gb);// general_frame_only_constraint_flag
+     skip_bits1(gb); // general_progressive_source_flag
+     skip_bits1(gb); // general_interlaced_source_flag
+     skip_bits1(gb); // general_non_packed_constraint_flag
+     skip_bits1(gb); // general_frame_only_constraint_flag
      if (get_bits(gb, 16) != 0) // XXX_reserved_zero_44bits[0..15]
          return -1;
      if (get_bits(gb, 16) != 0) // XXX_reserved_zero_44bits[16..31]
      return 0;
  }
  
- static void decode_sublayer_hrd(HEVCContext *s, int nb_cpb, int subpic_params_present)
+ static void decode_sublayer_hrd(HEVCContext *s, int nb_cpb,
+                                 int subpic_params_present)
  {
 -    GetBitContext *gb = &s->HEVClc.gb;
 +    GetBitContext *gb = &s->HEVClc->gb;
      int i;
  
      for (i = 0; i < nb_cpb; i++) {
@@@ -329,9 -325,9 +332,9 @@@ static void decode_hrd(HEVCContext *s, 
  int ff_hevc_decode_nal_vps(HEVCContext *s)
  {
      int i,j;
 -    GetBitContext *gb = &s->HEVClc.gb;
 +    GetBitContext *gb = &s->HEVClc->gb;
      int vps_id = 0;
-     VPS *vps;
+     HEVCVPS *vps;
  
      av_log(s->avctx, AV_LOG_DEBUG, "Decoding VPS\n");
  
@@@ -425,8 -421,8 +428,8 @@@ err
  
  static void decode_vui(HEVCContext *s, HEVCSPS *sps)
  {
-     VUI *vui = &sps->vui;
+     VUI *vui          = &sps->vui;
 -    GetBitContext *gb = &s->HEVClc.gb;
 +    GetBitContext *gb = &s->HEVClc->gb;
      int sar_present;
  
      av_log(s->avctx, AV_LOG_DEBUG, "Decoding VUI\n");
@@@ -548,13 -544,13 +551,13 @@@ static void set_default_scaling_list_da
  
  static int scaling_list_data(HEVCContext *s, ScalingList *sl)
  {
 -    GetBitContext *gb = &s->HEVClc.gb;
 +    GetBitContext *gb = &s->HEVClc->gb;
      uint8_t scaling_list_pred_mode_flag[4][6];
      int32_t scaling_list_dc_coef[2][6];
      int size_id, matrix_id, i, pos, delta;
      for (size_id = 0; size_id < 4; size_id++)
-         for (matrix_id = 0; matrix_id < ((size_id == 3) ? 2 : 6); matrix_id++) {
+         for (matrix_id = 0; matrix_id < (size_id == 3 ? 2 : 6); matrix_id++) {
              scaling_list_pred_mode_flag[size_id][matrix_id] = get_bits1(gb);
              if (!scaling_list_pred_mode_flag[size_id][matrix_id]) {
                  delta = get_ue_golomb_long(gb);
                  int32_t scaling_list_delta_coef;
  
                  next_coef = 8;
-                 coef_num = FFMIN(64, (1  <<  (4 + (size_id  <<  1))));
 -                coef_num  = FFMIN(64, 1 << (4 + (size_id << 1)));
++                coef_num  = FFMIN(64, 1  <<  (4 + (size_id  <<  1)));
                  if (size_id > 1) {
                      scaling_list_dc_coef[size_id - 2][matrix_id] = get_se_golomb(gb) + 8;
                      next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
@@@ -2593,23 -1597,6 +2593,24 @@@ av_cold int avcodec_close(AVCodecContex
      return 0;
  }
  
 +static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
 +{
 +    switch(id){
 +        //This is for future deprecatec codec ids, its empty since
 +        //last major bump but will fill up again over time, please don't remove it
 +//         case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
 +        case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
 +        case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
 +        case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
 +        case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
 +        case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
 +        case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
 +        case AV_CODEC_ID_WEBP_DEPRECATED: return AV_CODEC_ID_WEBP;
++        case AV_CODEC_ID_HEVC_DEPRECATED: return AV_CODEC_ID_HEVC;
 +        default                         : return id;
 +    }
 +}
 +
  static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  {
      AVCodec *p, *experimental = NULL;
   * Libavcodec version macros.
   */
  
 +#include "libavutil/avutil.h"
 +
  #define LIBAVCODEC_VERSION_MAJOR 55
 -#define LIBAVCODEC_VERSION_MINOR 24
 -#define LIBAVCODEC_VERSION_MICRO  0
 +#define LIBAVCODEC_VERSION_MINOR  40
- #define LIBAVCODEC_VERSION_MICRO 100
++#define LIBAVCODEC_VERSION_MICRO 101
  
  #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                 LIBAVCODEC_VERSION_MINOR, \