libavcodec/hevc.c:2920: error: Buffer Overrun L3
  Offset: [-2, +oo] Size: 4 by call to `decode_nal_units`.

libavcodec/hevc.c:2920:14: Call
2918. 
2919.     s->ref = NULL;
2920.     ret    = decode_nal_units(s, avpkt->data, avpkt->size);
                   ^
2921.     if (ret < 0)
2922.         return ret;

libavcodec/hevc.c:2739:1: Parameter `s->sps->log2_ctb_size`
  2737. }
  2738. 
  2739. static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
        ^
  2740. {
  2741.     int i, consumed, ret = 0;

libavcodec/hevc.c:2816:19: Call
  2814.     /* parse the NAL units */
  2815.     for (i = 0; i < s->nb_nals; i++) {
  2816.         int ret = decode_nal_unit(s, s->nals[i].data, s->nals[i].size);
                          ^
  2817.         if (ret < 0) {
  2818.             av_log(s->avctx, AV_LOG_WARNING,

libavcodec/hevc.c:2504:1: Parameter `s->sps->log2_ctb_size`
    2502. }
    2503. 
    2504. static int decode_nal_unit(HEVCContext *s, const uint8_t *nal, int length)
          ^
    2505. {
    2506.     HEVCLocalContext *lc = &s->HEVClc;

libavcodec/hevc.c:2608:23: Call
    2606.         }
    2607. 
    2608.         ctb_addr_ts = hls_slice_data(s);
                                ^
    2609.         if (ctb_addr_ts >= (s->sps->ctb_width * s->sps->ctb_height)) {
    2610.             s->is_decoded = 1;

libavcodec/hevc.c:2311:1: Parameter `s->sps->log2_ctb_size`
      2309. }
      2310. 
      2311. static int hls_slice_data(HEVCContext *s)
            ^
      2312. {
      2313.     int ctb_size    = 1 << s->sps->log2_ctb_size;

libavcodec/hevc.c:2335:15: Call
      2333.         s->filter_slice_edges[ctb_addr_rs]  = s->sh.slice_loop_filter_across_slices_enabled_flag;
      2334. 
      2335.         ret = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
                          ^
      2336.         if (ret < 0)
      2337.             return ret;

libavcodec/hevc.c:2207:1: Parameter `log2_cb_size`
        2205. }
        2206. 
        2207. static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
              ^
        2208.                                int log2_cb_size, int cb_depth)
        2209. {

libavcodec/hevc.c:2250:19: Call
        2248.         SUBDIVIDE(x1, y1);
        2249.     } else {
        2250.         int ret = hls_coding_unit(s, x0, y0, log2_cb_size);
                                ^
        2251.         if (ret < 0)
        2252.             return ret;

libavcodec/hevc.c:2055:1: Parameter `log2_cb_size`
          2053. }
          2054. 
          2055. static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
                ^
          2056. {
          2057.     int cb_size          = 1 << log2_cb_size;

libavcodec/hevc.c:2179:23: Call
          2177.                                          s->sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
          2178.                                          s->sps->max_transform_hierarchy_depth_inter;
          2179.                 ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
                                      ^
          2180.                                          log2_cb_size,
          2181.                                          log2_cb_size, 0, 0, 0, 0);

libavcodec/hevc.c:1335:1: Parameter `log2_trafo_size`
            1333. }
            1334. 
            1335. static int hls_transform_tree(HEVCContext *s, int x0, int y0,
                  ^
            1336.                               int xBase, int yBase, int cb_xBase, int cb_yBase,
            1337.                               int log2_cb_size, int log2_trafo_size,

libavcodec/hevc.c:1407:15: Call
            1405.             cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
            1406. 
            1407.         ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
                                ^
            1408.                                  log2_cb_size, log2_trafo_size, trafo_depth,
            1409.                                  blk_idx, cbf_luma, cbf_cb, cbf_cr);

libavcodec/hevc.c:1233:1: Parameter `log2_trafo_size`
              1231. }
              1232. 
              1233. static int hls_transform_unit(HEVCContext *s, int x0, int y0,
                    ^
              1234.                               int xBase, int yBase, int cb_xBase, int cb_yBase,
              1235.                               int log2_cb_size, int log2_trafo_size,

libavcodec/hevc.c:1304:13: Call
              1302. 
              1303.         if (cbf_luma)
              1304.             hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
                                ^
              1305.         if (log2_trafo_size > 2) {
              1306.             if (cbf_cb)

libavcodec/hevc.c:884:1: <Offset trace>
882. #undef CTB
883. 
884. static void hls_residual_coding(HEVCContext *s, int x0, int y0,
     ^
885.                                 int log2_trafo_size, enum ScanType scan_idx,
886.                                 int c_idx)

libavcodec/hevc.c:884:1: Parameter `log2_trafo_size`
                882. #undef CTB
                883. 
                884. static void hls_residual_coding(HEVCContext *s, int x0, int y0,
                     ^
                885.                                 int log2_trafo_size, enum ScanType scan_idx,
                886.                                 int c_idx)

libavcodec/hevc.c:884:1: <Length trace>
882. #undef CTB
883. 
884. static void hls_residual_coding(HEVCContext *s, int x0, int y0,
     ^
885.                                 int log2_trafo_size, enum ScanType scan_idx,
886.                                 int c_idx)

libavcodec/hevc.c:884:1: Parameter `s->hevcdsp.transquant_bypass[*]`
                882. #undef CTB
                883. 
                884. static void hls_residual_coding(HEVCContext *s, int x0, int y0,
                     ^
                885.                                 int log2_trafo_size, enum ScanType scan_idx,
                886.                                 int c_idx)

libavcodec/hevc.c:1221:9: Array access: Offset: [-2, +oo] Size: 4 by call to `decode_nal_units` 
                1219. 
                1220.     if (lc->cu.cu_transquant_bypass_flag) {
                1221.         s->hevcdsp.transquant_bypass[log2_trafo_size - 2](dst, coeffs, stride);
                              ^
                1222.     } else {
                1223.         if (transform_skip_flag)

static void 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, 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;
    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;
        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 =
        ff_hevc_last_significant_coeff_x_prefix_decode(s, c_idx, log2_trafo_size);
    last_significant_coeff_y =
        ff_hevc_last_significant_coeff_y_prefix_decode(s, c_idx, log2_trafo_size);
    if (last_significant_coeff_x > 3) {
        int suffix = ff_hevc_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 = ff_hevc_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 {
            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_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;
        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] =
                ff_hevc_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 (ff_hevc_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;
            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] =
                    ff_hevc_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] += ff_hevc_coeff_abs_level_greater2_flag_decode(s, c_idx, ctx_set);
            }
            if (!s->pps->sign_data_hiding_flag || !sign_hidden) {
                coeff_sign_flag = ff_hevc_coeff_sign_flag(s, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
            } else {
                coeff_sign_flag = ff_hevc_coeff_sign_flag(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 = ff_hevc_coeff_abs_level_remaining(s, trans_coeff_level, c_rice_param);
                    trans_coeff_level += last_coeff_abs_level_remaining;
                    if ((trans_coeff_level) > (3 * (1 << 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) == 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) {
                            int pos;
                            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);
    }
}
['static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,\n                             AVPacket *avpkt)\n{\n    int ret;\n    HEVCContext *s = avctx->priv_data;\n    if (!avpkt->size) {\n        ret = ff_hevc_output_frame(s, data, 1);\n        if (ret < 0)\n            return ret;\n        *got_output = ret;\n        return 0;\n    }\n    s->ref = NULL;\n    ret    = decode_nal_units(s, avpkt->data, avpkt->size);\n    if (ret < 0)\n        return ret;\n    if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&\n        s->is_md5) {\n        ret = verify_md5(s, s->ref->frame);\n        if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {\n            ff_hevc_unref_frame(s, s->ref, ~0);\n            return ret;\n        }\n    }\n    s->is_md5 = 0;\n    if (s->is_decoded) {\n        av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\\n", s->poc);\n        s->is_decoded = 0;\n    }\n    if (s->output_frame->buf[0]) {\n        av_frame_move_ref(data, s->output_frame);\n        *got_output = 1;\n    }\n    return avpkt->size;\n}', 'static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)\n{\n    int i, consumed, ret = 0;\n    s->ref = NULL;\n    s->eos = 0;\n    s->nb_nals = 0;\n    while (length >= 4) {\n        HEVCNAL *nal;\n        int extract_length = 0;\n        if (s->is_nalff) {\n            int i;\n            for (i = 0; i < s->nal_length_size; i++)\n                extract_length = (extract_length << 8) | buf[i];\n            buf    += s->nal_length_size;\n            length -= s->nal_length_size;\n            if (extract_length > length) {\n                av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\\n");\n                ret = AVERROR_INVALIDDATA;\n                goto fail;\n            }\n        } else {\n            if (buf[2] == 0) {\n                length--;\n                buf++;\n                continue;\n            }\n            if (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {\n                ret = AVERROR_INVALIDDATA;\n                goto fail;\n            }\n            buf           += 3;\n            length        -= 3;\n            extract_length = length;\n        }\n        if (s->nals_allocated < s->nb_nals + 1) {\n            int new_size = s->nals_allocated + 1;\n            HEVCNAL *tmp = av_realloc_array(s->nals, new_size, sizeof(*tmp));\n            if (!tmp) {\n                ret = AVERROR(ENOMEM);\n                goto fail;\n            }\n            s->nals = tmp;\n            memset(s->nals + s->nals_allocated, 0,\n                   (new_size - s->nals_allocated) * sizeof(*tmp));\n            s->nals_allocated = new_size;\n        }\n        nal = &s->nals[s->nb_nals++];\n        consumed = extract_rbsp(buf, extract_length, nal);\n        if (consumed < 0) {\n            ret = consumed;\n            goto fail;\n        }\n        ret = init_get_bits8(&s->HEVClc.gb, nal->data, nal->size);\n        if (ret < 0)\n            goto fail;\n        hls_nal_unit(s);\n        if (s->nal_unit_type == NAL_EOB_NUT ||\n            s->nal_unit_type == NAL_EOS_NUT)\n            s->eos = 1;\n        buf    += consumed;\n        length -= consumed;\n    }\n    for (i = 0; i < s->nb_nals; i++) {\n        int ret = decode_nal_unit(s, s->nals[i].data, s->nals[i].size);\n        if (ret < 0) {\n            av_log(s->avctx, AV_LOG_WARNING,\n                   "Error parsing NAL unit #%d.\\n", i);\n            goto fail;\n        }\n    }\nfail:\n    if (s->ref)\n        ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);\n    return ret;\n}', 'static int decode_nal_unit(HEVCContext *s, const uint8_t *nal, int length)\n{\n    HEVCLocalContext *lc = &s->HEVClc;\n    GetBitContext *gb    = &lc->gb;\n    int ctb_addr_ts, ret;\n    ret = init_get_bits8(gb, nal, length);\n    if (ret < 0)\n        return ret;\n    ret = hls_nal_unit(s);\n    if (ret < 0) {\n        av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\\n",\n               s->nal_unit_type);\n        goto fail;\n    } else if (!ret)\n        return 0;\n    switch (s->nal_unit_type) {\n    case NAL_VPS:\n        ret = ff_hevc_decode_nal_vps(s);\n        if (ret < 0)\n            goto fail;\n        break;\n    case NAL_SPS:\n        ret = ff_hevc_decode_nal_sps(s);\n        if (ret < 0)\n            goto fail;\n        break;\n    case NAL_PPS:\n        ret = ff_hevc_decode_nal_pps(s);\n        if (ret < 0)\n            goto fail;\n        break;\n    case NAL_SEI_PREFIX:\n    case NAL_SEI_SUFFIX:\n        ret = ff_hevc_decode_nal_sei(s);\n        if (ret < 0)\n            goto fail;\n        break;\n    case NAL_TRAIL_R:\n    case NAL_TRAIL_N:\n    case NAL_TSA_N:\n    case NAL_TSA_R:\n    case NAL_STSA_N:\n    case NAL_STSA_R:\n    case NAL_BLA_W_LP:\n    case NAL_BLA_W_RADL:\n    case NAL_BLA_N_LP:\n    case NAL_IDR_W_RADL:\n    case NAL_IDR_N_LP:\n    case NAL_CRA_NUT:\n    case NAL_RADL_N:\n    case NAL_RADL_R:\n    case NAL_RASL_N:\n    case NAL_RASL_R:\n        ret = hls_slice_header(s);\n        if (ret < 0)\n            return ret;\n        if (s->max_ra == INT_MAX) {\n            if (s->nal_unit_type == NAL_CRA_NUT || IS_BLA(s)) {\n                s->max_ra = s->poc;\n            } else {\n                if (IS_IDR(s))\n                    s->max_ra = INT_MIN;\n            }\n        }\n        if ((s->nal_unit_type == NAL_RASL_R || s->nal_unit_type == NAL_RASL_N) &&\n            s->poc <= s->max_ra) {\n            s->is_decoded = 0;\n            break;\n        } else {\n            if (s->nal_unit_type == NAL_RASL_R && s->poc > s->max_ra)\n                s->max_ra = INT_MIN;\n        }\n        if (s->sh.first_slice_in_pic_flag) {\n            ret = hevc_frame_start(s);\n            if (ret < 0)\n                return ret;\n        } else if (!s->ref) {\n            av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\\n");\n            goto fail;\n        }\n        if (s->nal_unit_type != s->first_nal_type) {\n            av_log(s->avctx, AV_LOG_ERROR,\n                   "Non-matching NAL types of the VCL NALUs: %d %d\\n",\n                   s->first_nal_type, s->nal_unit_type);\n            return AVERROR_INVALIDDATA;\n        }\n        if (!s->sh.dependent_slice_segment_flag &&\n            s->sh.slice_type != I_SLICE) {\n            ret = ff_hevc_slice_rpl(s);\n            if (ret < 0) {\n                av_log(s->avctx, AV_LOG_WARNING,\n                       "Error constructing the reference lists for the current slice.\\n");\n                goto fail;\n            }\n        }\n        ctb_addr_ts = hls_slice_data(s);\n        if (ctb_addr_ts >= (s->sps->ctb_width * s->sps->ctb_height)) {\n            s->is_decoded = 1;\n            if ((s->pps->transquant_bypass_enable_flag ||\n                 (s->sps->pcm.loop_filter_disable_flag && s->sps->pcm_enabled_flag)) &&\n                s->sps->sao_enabled)\n                restore_tqb_pixels(s);\n        }\n        if (ctb_addr_ts < 0) {\n            ret = ctb_addr_ts;\n            goto fail;\n        }\n        break;\n    case NAL_EOS_NUT:\n    case NAL_EOB_NUT:\n        s->seq_decode = (s->seq_decode + 1) & 0xff;\n        s->max_ra     = INT_MAX;\n        break;\n    case NAL_AUD:\n    case NAL_FD_NUT:\n        break;\n    default:\n        av_log(s->avctx, AV_LOG_INFO,\n               "Skipping NAL unit %d\\n", s->nal_unit_type);\n    }\n    return 0;\nfail:\n    if (s->avctx->err_recognition & AV_EF_EXPLODE)\n        return ret;\n    return 0;\n}', 'static int hls_slice_data(HEVCContext *s)\n{\n    int ctb_size    = 1 << s->sps->log2_ctb_size;\n    int more_data   = 1;\n    int x_ctb       = 0;\n    int y_ctb       = 0;\n    int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];\n    int ret;\n    while (more_data && ctb_addr_ts < s->sps->ctb_size) {\n        int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];\n        x_ctb = (ctb_addr_rs % ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;\n        y_ctb = (ctb_addr_rs / ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;\n        hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);\n        ff_hevc_cabac_init(s, ctb_addr_ts);\n        hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);\n        s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;\n        s->deblock[ctb_addr_rs].tc_offset   = s->sh.tc_offset;\n        s->filter_slice_edges[ctb_addr_rs]  = s->sh.slice_loop_filter_across_slices_enabled_flag;\n        ret = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);\n        if (ret < 0)\n            return ret;\n        more_data = !ff_hevc_end_of_slice_flag_decode(s);\n        ctb_addr_ts++;\n        ff_hevc_save_states(s, ctb_addr_ts);\n        ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);\n    }\n    if (x_ctb + ctb_size >= s->sps->width &&\n        y_ctb + ctb_size >= s->sps->height)\n        ff_hevc_hls_filter(s, x_ctb, y_ctb);\n    return ctb_addr_ts;\n}', 'static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,\n                               int log2_cb_size, int cb_depth)\n{\n    HEVCLocalContext *lc = &s->HEVClc;\n    const int cb_size    = 1 << log2_cb_size;\n    int split_cu;\n    lc->ct.depth = cb_depth;\n    if (x0 + cb_size <= s->sps->width  &&\n        y0 + cb_size <= s->sps->height &&\n        log2_cb_size > s->sps->log2_min_cb_size) {\n        split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);\n    } else {\n        split_cu = (log2_cb_size > s->sps->log2_min_cb_size);\n    }\n    if (s->pps->cu_qp_delta_enabled_flag &&\n        log2_cb_size >= s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth) {\n        lc->tu.is_cu_qp_delta_coded = 0;\n        lc->tu.cu_qp_delta          = 0;\n    }\n    if (split_cu) {\n        const int cb_size_split = cb_size >> 1;\n        const int x1 = x0 + cb_size_split;\n        const int y1 = y0 + cb_size_split;\n        log2_cb_size--;\n        cb_depth++;\n#define SUBDIVIDE(x, y)                                                \\\ndo {                                                                   \\\n    if (x < s->sps->width && y < s->sps->height) {                     \\\n        int ret = hls_coding_quadtree(s, x, y, log2_cb_size, cb_depth);\\\n        if (ret < 0)                                                   \\\n            return ret;                                                \\\n    }                                                                  \\\n} while (0)\n        SUBDIVIDE(x0, y0);\n        SUBDIVIDE(x1, y0);\n        SUBDIVIDE(x0, y1);\n        SUBDIVIDE(x1, y1);\n    } else {\n        int ret = hls_coding_unit(s, x0, y0, log2_cb_size);\n        if (ret < 0)\n            return ret;\n    }\n    return 0;\n}', 'static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)\n{\n    int cb_size          = 1 << log2_cb_size;\n    HEVCLocalContext *lc = &s->HEVClc;\n    int log2_min_cb_size = s->sps->log2_min_cb_size;\n    int length           = cb_size >> log2_min_cb_size;\n    int min_cb_width     = s->sps->min_cb_width;\n    int x_cb             = x0 >> log2_min_cb_size;\n    int y_cb             = y0 >> log2_min_cb_size;\n    int x, y, ret;\n    lc->cu.x                = x0;\n    lc->cu.y                = y0;\n    lc->cu.rqt_root_cbf     = 1;\n    lc->cu.pred_mode        = MODE_INTRA;\n    lc->cu.part_mode        = PART_2Nx2N;\n    lc->cu.intra_split_flag = 0;\n    lc->cu.pcm_flag         = 0;\n    SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;\n    for (x = 0; x < 4; x++)\n        lc->pu.intra_pred_mode[x] = 1;\n    if (s->pps->transquant_bypass_enable_flag) {\n        lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);\n        if (lc->cu.cu_transquant_bypass_flag)\n            set_deblocking_bypass(s, x0, y0, log2_cb_size);\n    } else\n        lc->cu.cu_transquant_bypass_flag = 0;\n    if (s->sh.slice_type != I_SLICE) {\n        uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);\n        lc->cu.pred_mode = MODE_SKIP;\n        x = y_cb * min_cb_width + x_cb;\n        for (y = 0; y < length; y++) {\n            memset(&s->skip_flag[x], skip_flag, length);\n            x += min_cb_width;\n        }\n        lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;\n    }\n    if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {\n        hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);\n        intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);\n        if (!s->sh.disable_deblocking_filter_flag)\n            ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,\n                                                  lc->slice_or_tiles_up_boundary,\n                                                  lc->slice_or_tiles_left_boundary);\n    } else {\n        if (s->sh.slice_type != I_SLICE)\n            lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);\n        if (lc->cu.pred_mode != MODE_INTRA ||\n            log2_cb_size == s->sps->log2_min_cb_size) {\n            lc->cu.part_mode        = ff_hevc_part_mode_decode(s, log2_cb_size);\n            lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&\n                                      lc->cu.pred_mode == MODE_INTRA;\n        }\n        if (lc->cu.pred_mode == MODE_INTRA) {\n            if (lc->cu.part_mode == PART_2Nx2N && s->sps->pcm_enabled_flag &&\n                log2_cb_size >= s->sps->pcm.log2_min_pcm_cb_size &&\n                log2_cb_size <= s->sps->pcm.log2_max_pcm_cb_size) {\n                lc->cu.pcm_flag = ff_hevc_pcm_flag_decode(s);\n            }\n            if (lc->cu.pcm_flag) {\n                intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);\n                ret = hls_pcm_sample(s, x0, y0, log2_cb_size);\n                if (s->sps->pcm.loop_filter_disable_flag)\n                    set_deblocking_bypass(s, x0, y0, log2_cb_size);\n                if (ret < 0)\n                    return ret;\n            } else {\n                intra_prediction_unit(s, x0, y0, log2_cb_size);\n            }\n        } else {\n            intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);\n            switch (lc->cu.part_mode) {\n            case PART_2Nx2N:\n                hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);\n                break;\n            case PART_2NxN:\n                hls_prediction_unit(s, x0, y0,               cb_size, cb_size / 2, log2_cb_size, 0);\n                hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1);\n                break;\n            case PART_Nx2N:\n                hls_prediction_unit(s, x0,               y0, cb_size / 2, cb_size, log2_cb_size, 0);\n                hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1);\n                break;\n            case PART_2NxnU:\n                hls_prediction_unit(s, x0, y0,               cb_size, cb_size     / 4, log2_cb_size, 0);\n                hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1);\n                break;\n            case PART_2NxnD:\n                hls_prediction_unit(s, x0, y0,                   cb_size, cb_size * 3 / 4, log2_cb_size, 0);\n                hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size     / 4, log2_cb_size, 1);\n                break;\n            case PART_nLx2N:\n                hls_prediction_unit(s, x0,               y0, cb_size     / 4, cb_size, log2_cb_size, 0);\n                hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1);\n                break;\n            case PART_nRx2N:\n                hls_prediction_unit(s, x0,                   y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0);\n                hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size     / 4, cb_size, log2_cb_size, 1);\n                break;\n            case PART_NxN:\n                hls_prediction_unit(s, x0,               y0,               cb_size / 2, cb_size / 2, log2_cb_size, 0);\n                hls_prediction_unit(s, x0 + cb_size / 2, y0,               cb_size / 2, cb_size / 2, log2_cb_size, 1);\n                hls_prediction_unit(s, x0,               y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2);\n                hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3);\n                break;\n            }\n        }\n        if (!lc->cu.pcm_flag) {\n            if (lc->cu.pred_mode != MODE_INTRA &&\n                !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {\n                lc->cu.rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);\n            }\n            if (lc->cu.rqt_root_cbf) {\n                lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?\n                                         s->sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :\n                                         s->sps->max_transform_hierarchy_depth_inter;\n                ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,\n                                         log2_cb_size,\n                                         log2_cb_size, 0, 0, 0, 0);\n                if (ret < 0)\n                    return ret;\n            } else {\n                if (!s->sh.disable_deblocking_filter_flag)\n                    ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,\n                                                          lc->slice_or_tiles_up_boundary,\n                                                          lc->slice_or_tiles_left_boundary);\n            }\n        }\n    }\n    if (s->pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)\n        ff_hevc_set_qPy(s, x0, y0, x0, y0, log2_cb_size);\n    x = y_cb * min_cb_width + x_cb;\n    for (y = 0; y < length; y++) {\n        memset(&s->qp_y_tab[x], lc->qp_y, length);\n        x += min_cb_width;\n    }\n    set_ct_depth(s, x0, y0, log2_cb_size, lc->ct.depth);\n    return 0;\n}', 'static int hls_transform_tree(HEVCContext *s, int x0, int y0,\n                              int xBase, int yBase, int cb_xBase, int cb_yBase,\n                              int log2_cb_size, int log2_trafo_size,\n                              int trafo_depth, int blk_idx,\n                              int cbf_cb, int cbf_cr)\n{\n    HEVCLocalContext *lc = &s->HEVClc;\n    uint8_t split_transform_flag;\n    int ret;\n    if (lc->cu.intra_split_flag) {\n        if (trafo_depth == 1)\n            lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];\n    } else {\n        lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[0];\n    }\n    if (log2_trafo_size <= s->sps->log2_max_trafo_size &&\n        log2_trafo_size >  s->sps->log2_min_tb_size    &&\n        trafo_depth     < lc->cu.max_trafo_depth       &&\n        !(lc->cu.intra_split_flag && trafo_depth == 0)) {\n        split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);\n    } else {\n        int inter_split = s->sps->max_transform_hierarchy_depth_inter == 0 &&\n                          lc->cu.pred_mode == MODE_INTER &&\n                          lc->cu.part_mode != PART_2Nx2N &&\n                          trafo_depth == 0;\n        split_transform_flag = log2_trafo_size > s->sps->log2_max_trafo_size ||\n                               (lc->cu.intra_split_flag && trafo_depth == 0) ||\n                               inter_split;\n    }\n    if (log2_trafo_size > 2 && (trafo_depth == 0 || cbf_cb))\n        cbf_cb = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);\n    else if (log2_trafo_size > 2 || trafo_depth == 0)\n        cbf_cb = 0;\n    if (log2_trafo_size > 2 && (trafo_depth == 0 || cbf_cr))\n        cbf_cr = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);\n    else if (log2_trafo_size > 2 || trafo_depth == 0)\n        cbf_cr = 0;\n    if (split_transform_flag) {\n        const int trafo_size_split = 1 << (log2_trafo_size - 1);\n        const int x1 = x0 + trafo_size_split;\n        const int y1 = y0 + trafo_size_split;\n#define SUBDIVIDE(x, y, idx)                                                    \\\ndo {                                                                            \\\n    ret = hls_transform_tree(s, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \\\n                             log2_trafo_size - 1, trafo_depth + 1, idx,         \\\n                             cbf_cb, cbf_cr);                                   \\\n    if (ret < 0)                                                                \\\n        return ret;                                                             \\\n} while (0)\n        SUBDIVIDE(x0, y0, 0);\n        SUBDIVIDE(x1, y0, 1);\n        SUBDIVIDE(x0, y1, 2);\n        SUBDIVIDE(x1, y1, 3);\n#undef SUBDIVIDE\n    } else {\n        int min_tu_size      = 1 << s->sps->log2_min_tb_size;\n        int log2_min_tu_size = s->sps->log2_min_tb_size;\n        int min_tu_width     = s->sps->min_tb_width;\n        int cbf_luma         = 1;\n        if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||\n            cbf_cb || cbf_cr)\n            cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);\n        ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,\n                                 log2_cb_size, log2_trafo_size, trafo_depth,\n                                 blk_idx, cbf_luma, cbf_cb, cbf_cr);\n        if (ret < 0)\n            return ret;\n        if (cbf_luma) {\n            int i, j;\n            for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)\n                for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {\n                    int x_tu = (x0 + j) >> log2_min_tu_size;\n                    int y_tu = (y0 + i) >> log2_min_tu_size;\n                    s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;\n                }\n        }\n        if (!s->sh.disable_deblocking_filter_flag) {\n            ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size,\n                                                  lc->slice_or_tiles_up_boundary,\n                                                  lc->slice_or_tiles_left_boundary);\n            if (s->pps->transquant_bypass_enable_flag &&\n                lc->cu.cu_transquant_bypass_flag)\n                set_deblocking_bypass(s, x0, y0, log2_trafo_size);\n        }\n    }\n    return 0;\n}', 'static int hls_transform_unit(HEVCContext *s, int x0, int y0,\n                              int xBase, int yBase, int cb_xBase, int cb_yBase,\n                              int log2_cb_size, int log2_trafo_size,\n                              int trafo_depth, int blk_idx,\n                              int cbf_luma, int cbf_cb, int cbf_cr)\n{\n    HEVCLocalContext *lc = &s->HEVClc;\n    if (lc->cu.pred_mode == MODE_INTRA) {\n        int trafo_size = 1 << log2_trafo_size;\n        ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);\n        s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);\n        if (log2_trafo_size > 2) {\n            trafo_size = trafo_size << (s->sps->hshift[1] - 1);\n            ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);\n            s->hpc.intra_pred[log2_trafo_size - 3](s, x0, y0, 1);\n            s->hpc.intra_pred[log2_trafo_size - 3](s, x0, y0, 2);\n        } else if (blk_idx == 3) {\n            trafo_size = trafo_size << s->sps->hshift[1];\n            ff_hevc_set_neighbour_available(s, xBase, yBase,\n                                            trafo_size, trafo_size);\n            s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);\n            s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);\n        }\n    }\n    if (cbf_luma || cbf_cb || cbf_cr) {\n        int scan_idx   = SCAN_DIAG;\n        int scan_idx_c = SCAN_DIAG;\n        if (s->pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {\n            lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);\n            if (lc->tu.cu_qp_delta != 0)\n                if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)\n                    lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;\n            lc->tu.is_cu_qp_delta_coded = 1;\n            if (lc->tu.cu_qp_delta < -(26 + s->sps->qp_bd_offset / 2) ||\n                lc->tu.cu_qp_delta >  (25 + s->sps->qp_bd_offset / 2)) {\n                av_log(s->avctx, AV_LOG_ERROR,\n                       "The cu_qp_delta %d is outside the valid range "\n                       "[%d, %d].\\n",\n                       lc->tu.cu_qp_delta,\n                       -(26 + s->sps->qp_bd_offset / 2),\n                        (25 + s->sps->qp_bd_offset / 2));\n                return AVERROR_INVALIDDATA;\n            }\n            ff_hevc_set_qPy(s, x0, y0, cb_xBase, cb_yBase, log2_cb_size);\n        }\n        if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {\n            if (lc->tu.cur_intra_pred_mode >= 6 &&\n                lc->tu.cur_intra_pred_mode <= 14) {\n                scan_idx = SCAN_VERT;\n            } else if (lc->tu.cur_intra_pred_mode >= 22 &&\n                       lc->tu.cur_intra_pred_mode <= 30) {\n                scan_idx = SCAN_HORIZ;\n            }\n            if (lc->pu.intra_pred_mode_c >=  6 &&\n                lc->pu.intra_pred_mode_c <= 14) {\n                scan_idx_c = SCAN_VERT;\n            } else if (lc->pu.intra_pred_mode_c >= 22 &&\n                       lc->pu.intra_pred_mode_c <= 30) {\n                scan_idx_c = SCAN_HORIZ;\n            }\n        }\n        if (cbf_luma)\n            hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);\n        if (log2_trafo_size > 2) {\n            if (cbf_cb)\n                hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 1);\n            if (cbf_cr)\n                hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 2);\n        } else if (blk_idx == 3) {\n            if (cbf_cb)\n                hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 1);\n            if (cbf_cr)\n                hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 2);\n        }\n    }\n    return 0;\n}', 'static void hls_residual_coding(HEVCContext *s, int x0, int y0,\n                                int log2_trafo_size, enum ScanType scan_idx,\n                                int c_idx)\n{\n#define GET_COORD(offset, n)                                    \\\n    do {                                                        \\\n        x_c = (scan_x_cg[offset >> 4] << 2) + scan_x_off[n];    \\\n        y_c = (scan_y_cg[offset >> 4] << 2) + scan_y_off[n];    \\\n    } while (0)\n    HEVCLocalContext *lc    = &s->HEVClc;\n    int transform_skip_flag = 0;\n    int last_significant_coeff_x, last_significant_coeff_y;\n    int last_scan_pos;\n    int n_end;\n    int num_coeff    = 0;\n    int greater1_ctx = 1;\n    int num_last_subset;\n    int x_cg_last_sig, y_cg_last_sig;\n    const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off;\n    ptrdiff_t stride = s->frame->linesize[c_idx];\n    int hshift       = s->sps->hshift[c_idx];\n    int vshift       = s->sps->vshift[c_idx];\n    uint8_t *dst     = &s->frame->data[c_idx][(y0 >> vshift) * stride +\n                                              ((x0 >> hshift) << s->sps->pixel_shift)];\n    DECLARE_ALIGNED(16, int16_t, coeffs[MAX_TB_SIZE * MAX_TB_SIZE]) = { 0 };\n    DECLARE_ALIGNED(8, uint8_t, significant_coeff_group_flag[8][8]) = { { 0 } };\n    int trafo_size = 1 << log2_trafo_size;\n    int i, qp, shift, add, scale, scale_m;\n    const uint8_t level_scale[] = { 40, 45, 51, 57, 64, 72 };\n    const uint8_t *scale_matrix;\n    uint8_t dc_scale;\n    if (!lc->cu.cu_transquant_bypass_flag) {\n        static const int qp_c[] = {\n            29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37\n        };\n        static const uint8_t rem6[51 + 2 * 6 + 1] = {\n            0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,\n            3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,\n            0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,\n        };\n        static const uint8_t div6[51 + 2 * 6 + 1] = {\n            0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,  3,  3,  3,\n            3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6,  6,  6,  6,\n            7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,\n        };\n        int qp_y = lc->qp_y;\n        if (c_idx == 0) {\n            qp = qp_y + s->sps->qp_bd_offset;\n        } else {\n            int qp_i, offset;\n            if (c_idx == 1)\n                offset = s->pps->cb_qp_offset + s->sh.slice_cb_qp_offset;\n            else\n                offset = s->pps->cr_qp_offset + s->sh.slice_cr_qp_offset;\n            qp_i = av_clip_c(qp_y + offset, -s->sps->qp_bd_offset, 57);\n            if (qp_i < 30)\n                qp = qp_i;\n            else if (qp_i > 43)\n                qp = qp_i - 6;\n            else\n                qp = qp_c[qp_i - 30];\n            qp += s->sps->qp_bd_offset;\n        }\n        shift    = s->sps->bit_depth + log2_trafo_size - 5;\n        add      = 1 << (shift - 1);\n        scale    = level_scale[rem6[qp]] << (div6[qp]);\n        scale_m  = 16;\n        dc_scale = 16;\n        if (s->sps->scaling_list_enable_flag) {\n            const ScalingList *sl = s->pps->scaling_list_data_present_flag ?\n                                    &s->pps->scaling_list : &s->sps->scaling_list;\n            int matrix_id = lc->cu.pred_mode != MODE_INTRA;\n            if (log2_trafo_size != 5)\n                matrix_id = 3 * matrix_id + c_idx;\n            scale_matrix = sl->sl[log2_trafo_size - 2][matrix_id];\n            if (log2_trafo_size >= 4)\n                dc_scale = sl->sl_dc[log2_trafo_size - 4][matrix_id];\n        }\n    }\n    if (s->pps->transform_skip_enabled_flag &&\n        !lc->cu.cu_transquant_bypass_flag   &&\n        log2_trafo_size == 2) {\n        transform_skip_flag = ff_hevc_transform_skip_flag_decode(s, c_idx);\n    }\n    last_significant_coeff_x =\n        ff_hevc_last_significant_coeff_x_prefix_decode(s, c_idx, log2_trafo_size);\n    last_significant_coeff_y =\n        ff_hevc_last_significant_coeff_y_prefix_decode(s, c_idx, log2_trafo_size);\n    if (last_significant_coeff_x > 3) {\n        int suffix = ff_hevc_last_significant_coeff_suffix_decode(s, last_significant_coeff_x);\n        last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *\n                                   (2 + (last_significant_coeff_x & 1)) +\n                                   suffix;\n    }\n    if (last_significant_coeff_y > 3) {\n        int suffix = ff_hevc_last_significant_coeff_suffix_decode(s, last_significant_coeff_y);\n        last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *\n                                   (2 + (last_significant_coeff_y & 1)) +\n                                   suffix;\n    }\n    if (scan_idx == SCAN_VERT)\n        FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y);\n    x_cg_last_sig = last_significant_coeff_x >> 2;\n    y_cg_last_sig = last_significant_coeff_y >> 2;\n    switch (scan_idx) {\n    case SCAN_DIAG: {\n        int last_x_c = last_significant_coeff_x & 3;\n        int last_y_c = last_significant_coeff_y & 3;\n        scan_x_off = ff_hevc_diag_scan4x4_x;\n        scan_y_off = ff_hevc_diag_scan4x4_y;\n        num_coeff  = diag_scan4x4_inv[last_y_c][last_x_c];\n        if (trafo_size == 4) {\n            scan_x_cg = scan_1x1;\n            scan_y_cg = scan_1x1;\n        } else if (trafo_size == 8) {\n            num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4;\n            scan_x_cg  = diag_scan2x2_x;\n            scan_y_cg  = diag_scan2x2_y;\n        } else if (trafo_size == 16) {\n            num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4;\n            scan_x_cg  = ff_hevc_diag_scan4x4_x;\n            scan_y_cg  = ff_hevc_diag_scan4x4_y;\n        } else {\n            num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4;\n            scan_x_cg  = ff_hevc_diag_scan8x8_x;\n            scan_y_cg  = ff_hevc_diag_scan8x8_y;\n        }\n        break;\n    }\n    case SCAN_HORIZ:\n        scan_x_cg  = horiz_scan2x2_x;\n        scan_y_cg  = horiz_scan2x2_y;\n        scan_x_off = horiz_scan4x4_x;\n        scan_y_off = horiz_scan4x4_y;\n        num_coeff  = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];\n        break;\n    default:\n        scan_x_cg  = horiz_scan2x2_y;\n        scan_y_cg  = horiz_scan2x2_x;\n        scan_x_off = horiz_scan4x4_y;\n        scan_y_off = horiz_scan4x4_x;\n        num_coeff  = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];\n        break;\n    }\n    num_coeff++;\n    num_last_subset = (num_coeff - 1) >> 4;\n    for (i = num_last_subset; i >= 0; i--) {\n        int n, m;\n        int x_cg, y_cg, x_c, y_c;\n        int implicit_non_zero_coeff = 0;\n        int64_t trans_coeff_level;\n        int prev_sig = 0;\n        int offset   = i << 4;\n        uint8_t significant_coeff_flag_idx[16];\n        uint8_t nb_significant_coeff_flag = 0;\n        x_cg = scan_x_cg[i];\n        y_cg = scan_y_cg[i];\n        if (i < num_last_subset && i > 0) {\n            int ctx_cg = 0;\n            if (x_cg < (1 << (log2_trafo_size - 2)) - 1)\n                ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];\n            if (y_cg < (1 << (log2_trafo_size - 2)) - 1)\n                ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];\n            significant_coeff_group_flag[x_cg][y_cg] =\n                ff_hevc_significant_coeff_group_flag_decode(s, c_idx, ctx_cg);\n            implicit_non_zero_coeff = 1;\n        } else {\n            significant_coeff_group_flag[x_cg][y_cg] =\n                ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||\n                 (x_cg == 0 && y_cg == 0));\n        }\n        last_scan_pos = num_coeff - offset - 1;\n        if (i == num_last_subset) {\n            n_end                         = last_scan_pos - 1;\n            significant_coeff_flag_idx[0] = last_scan_pos;\n            nb_significant_coeff_flag     = 1;\n        } else {\n            n_end = 15;\n        }\n        if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)\n            prev_sig = significant_coeff_group_flag[x_cg + 1][y_cg];\n        if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)\n            prev_sig += significant_coeff_group_flag[x_cg][y_cg + 1] << 1;\n        for (n = n_end; n >= 0; n--) {\n            GET_COORD(offset, n);\n            if (significant_coeff_group_flag[x_cg][y_cg] &&\n                (n > 0 || implicit_non_zero_coeff == 0)) {\n                if (ff_hevc_significant_coeff_flag_decode(s, c_idx, x_c, y_c,\n                                                          log2_trafo_size,\n                                                          scan_idx,\n                                                          prev_sig) == 1) {\n                    significant_coeff_flag_idx[nb_significant_coeff_flag] = n;\n                    nb_significant_coeff_flag++;\n                    implicit_non_zero_coeff = 0;\n                }\n            } else {\n                int last_cg = (x_c == (x_cg << 2) && y_c == (y_cg << 2));\n                if (last_cg && implicit_non_zero_coeff && significant_coeff_group_flag[x_cg][y_cg]) {\n                    significant_coeff_flag_idx[nb_significant_coeff_flag] = n;\n                    nb_significant_coeff_flag++;\n                }\n            }\n        }\n        n_end = nb_significant_coeff_flag;\n        if (n_end) {\n            int first_nz_pos_in_cg = 16;\n            int last_nz_pos_in_cg = -1;\n            int c_rice_param = 0;\n            int first_greater1_coeff_idx = -1;\n            uint8_t coeff_abs_level_greater1_flag[16] = { 0 };\n            uint16_t coeff_sign_flag;\n            int sum_abs = 0;\n            int sign_hidden = 0;\n            int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;\n            if (!(i == num_last_subset) && greater1_ctx == 0)\n                ctx_set++;\n            greater1_ctx      = 1;\n            last_nz_pos_in_cg = significant_coeff_flag_idx[0];\n            for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {\n                int n_idx = significant_coeff_flag_idx[m];\n                int inc   = (ctx_set << 2) + greater1_ctx;\n                coeff_abs_level_greater1_flag[n_idx] =\n                    ff_hevc_coeff_abs_level_greater1_flag_decode(s, c_idx, inc);\n                if (coeff_abs_level_greater1_flag[n_idx]) {\n                    greater1_ctx = 0;\n                } else if (greater1_ctx > 0 && greater1_ctx < 3) {\n                    greater1_ctx++;\n                }\n                if (coeff_abs_level_greater1_flag[n_idx] &&\n                    first_greater1_coeff_idx == -1)\n                    first_greater1_coeff_idx = n_idx;\n            }\n            first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];\n            sign_hidden        = last_nz_pos_in_cg - first_nz_pos_in_cg >= 4 &&\n                                 !lc->cu.cu_transquant_bypass_flag;\n            if (first_greater1_coeff_idx != -1) {\n                coeff_abs_level_greater1_flag[first_greater1_coeff_idx] += ff_hevc_coeff_abs_level_greater2_flag_decode(s, c_idx, ctx_set);\n            }\n            if (!s->pps->sign_data_hiding_flag || !sign_hidden) {\n                coeff_sign_flag = ff_hevc_coeff_sign_flag(s, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);\n            } else {\n                coeff_sign_flag = ff_hevc_coeff_sign_flag(s, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1));\n            }\n            for (m = 0; m < n_end; m++) {\n                n = significant_coeff_flag_idx[m];\n                GET_COORD(offset, n);\n                trans_coeff_level = 1 + coeff_abs_level_greater1_flag[n];\n                if (trans_coeff_level == ((m < 8) ?\n                                          ((n == first_greater1_coeff_idx) ? 3 : 2) : 1)) {\n                    int last_coeff_abs_level_remaining = ff_hevc_coeff_abs_level_remaining(s, trans_coeff_level, c_rice_param);\n                    trans_coeff_level += last_coeff_abs_level_remaining;\n                    if ((trans_coeff_level) > (3 * (1 << c_rice_param)))\n                        c_rice_param = FFMIN(c_rice_param + 1, 4);\n                }\n                if (s->pps->sign_data_hiding_flag && sign_hidden) {\n                    sum_abs += trans_coeff_level;\n                    if (n == first_nz_pos_in_cg && ((sum_abs & 1) == 1))\n                        trans_coeff_level = -trans_coeff_level;\n                }\n                if (coeff_sign_flag >> 15)\n                    trans_coeff_level = -trans_coeff_level;\n                coeff_sign_flag <<= 1;\n                if (!lc->cu.cu_transquant_bypass_flag) {\n                    if (s->sps->scaling_list_enable_flag) {\n                        if (y_c || x_c || log2_trafo_size < 4) {\n                            int pos;\n                            switch (log2_trafo_size) {\n                            case 3:  pos = (y_c        << 3) +  x_c;       break;\n                            case 4:  pos = ((y_c >> 1) << 3) + (x_c >> 1); break;\n                            case 5:  pos = ((y_c >> 2) << 3) + (x_c >> 2); break;\n                            default: pos = (y_c        << 2) +  x_c;\n                            }\n                            scale_m = scale_matrix[pos];\n                        } else {\n                            scale_m = dc_scale;\n                        }\n                    }\n                    trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;\n                    if(trans_coeff_level < 0) {\n                        if((~trans_coeff_level) & 0xFffffffffff8000)\n                            trans_coeff_level = -32768;\n                    } else {\n                        if (trans_coeff_level & 0xffffffffffff8000)\n                            trans_coeff_level = 32767;\n                    }\n                }\n                coeffs[y_c * trafo_size + x_c] = trans_coeff_level;\n            }\n        }\n    }\n    if (lc->cu.cu_transquant_bypass_flag) {\n        s->hevcdsp.transquant_bypass[log2_trafo_size - 2](dst, coeffs, stride);\n    } else {\n        if (transform_skip_flag)\n            s->hevcdsp.transform_skip(dst, coeffs, stride);\n        else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 &&\n                 log2_trafo_size == 2)\n            s->hevcdsp.transform_4x4_luma_add(dst, coeffs, stride);\n        else\n            s->hevcdsp.transform_add[log2_trafo_size - 2](dst, coeffs, stride);\n    }\n}']