[FFmpeg-cvslog] hevc: optimize residual coding( cherry picked from commit 70692a44708157b4dfa50e402e446bfa2b27f55e)

Mickaël Raulet git at videolan.org
Thu Oct 17 02:48:02 CEST 2013


ffmpeg | branch: master | Mickaël Raulet <mraulet at insa-rennes.fr> | Fri Oct 11 22:07:00 2013 +0200| [b4948943904a5e496e31c5e7778364fa0b03253c] | committer: Michael Niedermayer

hevc: optimize residual coding(cherry picked from commit 70692a44708157b4dfa50e402e446bfa2b27f55e)

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=b4948943904a5e496e31c5e7778364fa0b03253c
---

 libavcodec/hevc.c        |  245 +++++++++++++++++++++++++---------------------
 libavcodec/hevc.h        |   25 +----
 libavcodec/hevc_cabac.c  |   80 +++------------
 libavcodec/hevc_filter.c |   20 ++--
 libavcodec/hevc_ps.c     |  126 ++++++++++++------------
 5 files changed, 226 insertions(+), 270 deletions(-)

diff --git a/libavcodec/hevc.c b/libavcodec/hevc.c
index d59b4e4..ad82012 100644
--- a/libavcodec/hevc.c
+++ b/libavcodec/hevc.c
@@ -318,7 +318,7 @@ static void pred_weight_table(HEVCContext *s, GetBitContext *gb)
     uint8_t luma_weight_l1_flag[16];
     uint8_t chroma_weight_l1_flag[16];
 
-    s->sh.luma_log2_weight_denom = get_ue_golomb(gb);
+    s->sh.luma_log2_weight_denom = get_ue_golomb_long(gb);
     if (s->sps->chroma_format_idc != 0) {
         int delta = get_se_golomb(gb);
         s->sh.chroma_log2_weight_denom = av_clip_c(s->sh.luma_log2_weight_denom + delta, 0, 7);
@@ -415,8 +415,8 @@ static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
         return 0;
 
     if (sps->num_long_term_ref_pics_sps > 0)
-        nb_sps = get_ue_golomb(gb);
-    nb_sh = get_ue_golomb(gb);
+        nb_sps = get_ue_golomb_long(gb);
+    nb_sh = get_ue_golomb_long(gb);
 
     if (nb_sh + nb_sps > FF_ARRAY_ELEMS(rps->poc))
         return AVERROR_INVALIDDATA;
@@ -441,7 +441,7 @@ static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
 
         delta_poc_msb_present = get_bits1(gb);
         if (delta_poc_msb_present) {
-            int delta = get_ue_golomb(gb);
+            int delta = get_ue_golomb_long(gb);
 
             if (i && i != nb_sps)
                 delta += prev_delta_msb;
@@ -471,7 +471,7 @@ static int hls_slice_header(HEVCContext *s)
     if (s->nal_unit_type >= 16 && s->nal_unit_type <= 23)
         sh->no_output_of_prior_pics_flag = get_bits1(gb);
 
-    sh->pps_id = get_ue_golomb(gb);
+    sh->pps_id = get_ue_golomb_long(gb);
     if (sh->pps_id >= MAX_PPS_COUNT || !s->pps_list[sh->pps_id]) {
         av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
         return AVERROR_INVALIDDATA;
@@ -552,7 +552,7 @@ static int hls_slice_header(HEVCContext *s)
         for (i = 0; i < s->pps->num_extra_slice_header_bits; i++)
             skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
 
-        sh->slice_type = get_ue_golomb(gb);
+        sh->slice_type = get_ue_golomb_long(gb);
         if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
               sh->slice_type == B_SLICE)) {
             av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
@@ -646,9 +646,9 @@ static int hls_slice_header(HEVCContext *s)
                 sh->nb_refs[L1] = s->pps->num_ref_idx_l1_default_active;
 
             if (get_bits1(gb)) { // num_ref_idx_active_override_flag
-                sh->nb_refs[L0] = get_ue_golomb(gb) + 1;
+                sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
                 if (sh->slice_type == B_SLICE)
-                    sh->nb_refs[L1] = get_ue_golomb(gb) + 1;
+                    sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
             }
             if (sh->nb_refs[L0] > MAX_REFS || sh->nb_refs[L1] > MAX_REFS) {
                 av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
@@ -694,7 +694,7 @@ static int hls_slice_header(HEVCContext *s)
                     sh->collocated_list = !get_bits1(gb);
 
                 if (sh->nb_refs[sh->collocated_list] > 1) {
-                    sh->collocated_ref_idx = get_ue_golomb(gb);
+                    sh->collocated_ref_idx = get_ue_golomb_long(gb);
                     if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
                         av_log(s->avctx, AV_LOG_ERROR,
                                "Invalid collocated_ref_idx: %d.\n", sh->collocated_ref_idx);
@@ -708,7 +708,7 @@ static int hls_slice_header(HEVCContext *s)
                 pred_weight_table(s, gb);
             }
 
-            sh->max_num_merge_cand = 5 - get_ue_golomb(gb);
+            sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
         }
 
         sh->slice_qp_delta = get_se_golomb(gb);
@@ -759,9 +759,9 @@ static int hls_slice_header(HEVCContext *s)
 
     sh->num_entry_point_offsets = 0;
     if (s->pps->tiles_enabled_flag || s->pps->entropy_coding_sync_enabled_flag) {
-        sh->num_entry_point_offsets = get_ue_golomb(gb);
+        sh->num_entry_point_offsets = get_ue_golomb_long(gb);
         if (sh->num_entry_point_offsets > 0) {
-            int offset_len = get_ue_golomb(gb) + 1;
+            int offset_len = get_ue_golomb_long(gb) + 1;
 
             for (i = 0; i < sh->num_entry_point_offsets; i++)
                 skip_bits(gb, offset_len);
@@ -769,7 +769,7 @@ static int hls_slice_header(HEVCContext *s)
     }
 
     if (s->pps->slice_header_extension_present_flag) {
-        int length = get_ue_golomb(gb);
+        int length = get_ue_golomb_long(gb);
         for (i = 0; i < length; i++)
             skip_bits(gb, 8); // slice_header_extension_data_byte
     }
@@ -889,6 +889,8 @@ static void hls_residual_coding(HEVCContext *s, int x0, int y0,
     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;
 
@@ -899,7 +901,8 @@ static void hls_residual_coding(HEVCContext *s, int x0, int y0,
     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(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;
@@ -911,6 +914,17 @@ static void hls_residual_coding(HEVCContext *s, int x0, int y0,
     // 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) {
@@ -936,7 +950,7 @@ static void hls_residual_coding(HEVCContext *s, int x0, int y0,
 
         shift    = s->sps->bit_depth + log2_trafo_size - 5;
         add      = 1 << (shift-1);
-        scale    = level_scale[qp%6] << (qp/6);
+        scale    = level_scale[rem6[qp]] << (div6[qp]);
         scale_m  = 16; // default when no custom scaling lists.
         dc_scale = 16;
 
@@ -954,8 +968,6 @@ static void hls_residual_coding(HEVCContext *s, int x0, int y0,
         }
     }
 
-    memset(lc->rc.significant_coeff_group_flag, 0, 8 * 8);
-
     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);
@@ -1028,38 +1040,34 @@ static void hls_residual_coding(HEVCContext *s, int x0, int y0,
         break;
     }
     num_coeff++;
-
     num_last_subset = (num_coeff - 1) >> 4;
 
     for (i = num_last_subset; i >= 0; i--) {
         int n, m;
-        int first_nz_pos_in_cg, last_nz_pos_in_cg, num_sig_coeff, first_greater1_coeff_idx;
-        int sign_hidden;
-        int sum_abs;
         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] = {0};
-        uint8_t coeff_abs_level_greater1_flag[16] = {0};
-        uint8_t coeff_abs_level_greater2_flag[16] = {0};
-        uint16_t coeff_sign_flag;
+        uint8_t significant_coeff_flag_idx[16];
         uint8_t nb_significant_coeff_flag = 0;
 
-        int first_elem;
-
         x_cg = scan_x_cg[i];
         y_cg = scan_y_cg[i];
 
         if ((i < num_last_subset) && (i > 0)) {
-            lc->rc.significant_coeff_group_flag[x_cg][y_cg] =
-            ff_hevc_significant_coeff_group_flag_decode(s, c_idx, x_cg, y_cg,
-                                                        log2_trafo_size);
+            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 {
-            lc->rc.significant_coeff_group_flag[x_cg][y_cg] =
+            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));
         }
@@ -1073,107 +1081,126 @@ static void hls_residual_coding(HEVCContext *s, int x0, int y0,
         } 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 (lc->rc.significant_coeff_group_flag[x_cg][y_cg] &&
+            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) == 1) {
+                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 = nb_significant_coeff_flag + 1;
+                    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 && lc->rc.significant_coeff_group_flag[x_cg][y_cg]) {
+                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 = nb_significant_coeff_flag + 1;
+                    nb_significant_coeff_flag++;
                 }
             }
         }
 
         n_end = nb_significant_coeff_flag;
 
-        first_nz_pos_in_cg = 16;
-        last_nz_pos_in_cg = -1;
-        num_sig_coeff = 0;
-        first_greater1_coeff_idx = -1;
-        for (m = 0; m < n_end; m++) {
-            n = significant_coeff_flag_idx[m];
-            if (num_sig_coeff < 8) {
-                coeff_abs_level_greater1_flag[n] =
-                ff_hevc_coeff_abs_level_greater1_flag_decode(s, c_idx, i, n,
-                                                             (num_sig_coeff == 0),
-                                                             (i == num_last_subset));
-                num_sig_coeff++;
-                if (coeff_abs_level_greater1_flag[n] &&
+
+        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] =
+                    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;
+                    first_greater1_coeff_idx = n_idx;
             }
-            if (last_nz_pos_in_cg == -1)
-                last_nz_pos_in_cg = n;
-            first_nz_pos_in_cg = n;
-        }
-
-        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_greater2_flag[first_greater1_coeff_idx] =
-            ff_hevc_coeff_abs_level_greater2_flag_decode(s, c_idx, i, first_greater1_coeff_idx);
-        }
-        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));
-        }
+            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);
 
-        num_sig_coeff = 0;
-        sum_abs = 0;
-        first_elem = 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] +
-                                coeff_abs_level_greater2_flag[n];
-            if (trans_coeff_level == ((num_sig_coeff < 8) ?
-                                      ((n == first_greater1_coeff_idx) ? 3 : 2) : 1)) {
-                trans_coeff_level += ff_hevc_coeff_abs_level_remaining(s, first_elem, trans_coeff_level);
-                first_elem = 0;
+            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) {
-                sum_abs += trans_coeff_level;
-                if (n == first_nz_pos_in_cg && ((sum_abs&1) == 1))
-                    trans_coeff_level = -trans_coeff_level;
+            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));
             }
-            if (coeff_sign_flag >> 15)
-                trans_coeff_level = -trans_coeff_level;
-            coeff_sign_flag <<= 1;
-            num_sig_coeff++;
-
-            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;
+
+            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);
+
                 }
-                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;
+                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) {
+                            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;
             }
-
-            coeffs[y_c * trafo_size + x_c] = trans_coeff_level;
         }
     }
 
diff --git a/libavcodec/hevc.h b/libavcodec/hevc.h
index 1aaf584..9e91c3b 100644
--- a/libavcodec/hevc.h
+++ b/libavcodec/hevc.h
@@ -653,10 +653,6 @@ typedef struct TransformUnit {
     int cur_intra_pred_mode;
 } TransformUnit;
 
-typedef struct ResidualCoding {
-    uint8_t significant_coeff_group_flag[8][8];
-} ResidualCoding;
-
 typedef struct SAOParams {
     uint8_t type_idx[3]; ///< sao_type_idx
 
@@ -726,16 +722,10 @@ typedef struct HEVCNAL {
 
 typedef struct HEVCLocalContext {
     uint8_t cabac_state[HEVC_CONTEXTS];
-    int ctx_set;
-    int greater1_ctx;
-    int last_coeff_abs_level_greater1_flag;
-    int c_rice_param;
-    int last_coeff_abs_level_remaining;
     GetBitContext gb;
     CABACContext cc;
     TransformTree tt;
     TransformUnit tu;
-    ResidualCoding rc;
     uint8_t first_qp_group;
     int8_t qp_y;
     int8_t curr_qp_y;
@@ -935,17 +925,12 @@ int ff_hevc_last_significant_coeff_y_prefix_decode(HEVCContext *s, int c_idx,
                                                    int log2_size);
 int ff_hevc_last_significant_coeff_suffix_decode(HEVCContext *s,
                                                  int last_significant_coeff_prefix);
-int ff_hevc_significant_coeff_group_flag_decode(HEVCContext *s, int c_idx, int x_cg,
-                                                int y_cg, int log2_trafo_size);
+int ff_hevc_significant_coeff_group_flag_decode(HEVCContext *s, int c_idx, int ctx_cg);
 int ff_hevc_significant_coeff_flag_decode(HEVCContext *s, int c_idx, int x_c, int y_c,
-                                          int log2_trafo_size, int scan_idx);
-int ff_hevc_coeff_abs_level_greater1_flag_decode(HEVCContext *s, int c_idx,
-                                                 int i, int n,
-                                                 int first_greater1_coeff_idx,
-                                                 int first_subset);
-int ff_hevc_coeff_abs_level_greater2_flag_decode(HEVCContext *s, int c_idx,
-                                                 int i, int n);
-int ff_hevc_coeff_abs_level_remaining(HEVCContext *s, int n, int base_level);
+                                          int log2_trafo_size, int scan_idx, int prev_sig);
+int ff_hevc_coeff_abs_level_greater1_flag_decode(HEVCContext *s, int c_idx, int ctx_set);
+int ff_hevc_coeff_abs_level_greater2_flag_decode(HEVCContext *s, int c_idx, int inc);
+int ff_hevc_coeff_abs_level_remaining(HEVCContext *s, int base_level, int rc_rice_param);
 int ff_hevc_coeff_sign_flag(HEVCContext *s, uint8_t nb);
 
 /**
diff --git a/libavcodec/hevc_cabac.c b/libavcodec/hevc_cabac.c
index 8468d56..ef3a94b 100644
--- a/libavcodec/hevc_cabac.c
+++ b/libavcodec/hevc_cabac.c
@@ -764,24 +764,17 @@ int ff_hevc_last_significant_coeff_suffix_decode(HEVCContext *s,
     return value;
 }
 
-int ff_hevc_significant_coeff_group_flag_decode(HEVCContext *s, int c_idx, int x_cg,
-                                                int y_cg, int log2_trafo_size)
+int ff_hevc_significant_coeff_group_flag_decode(HEVCContext *s, int c_idx, int ctx_cg)
 {
-    int ctx_cg = 0;
     int inc;
 
-    if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
-        ctx_cg += s->HEVClc.rc.significant_coeff_group_flag[x_cg + 1][y_cg];
-    if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
-        ctx_cg += s->HEVClc.rc.significant_coeff_group_flag[x_cg][y_cg + 1];
-
     inc = FFMIN(ctx_cg, 1) + (c_idx>0 ? 2 : 0);
 
     return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_GROUP_FLAG] + inc);
 }
 
 int ff_hevc_significant_coeff_flag_decode(HEVCContext *s, int c_idx, int x_c, int y_c,
-                                          int log2_trafo_size, int scan_idx)
+                                          int log2_trafo_size, int scan_idx, int prev_sig)
 {
     static const uint8_t ctx_idx_map[] = {
         0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8
@@ -796,13 +789,6 @@ int ff_hevc_significant_coeff_flag_decode(HEVCContext *s, int c_idx, int x_c, in
     } else if (log2_trafo_size == 2) {
         sig_ctx = ctx_idx_map[(y_c << 2) + x_c];
     } else {
-        int prev_sig = 0;
-
-        if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
-            prev_sig += s->HEVClc.rc.significant_coeff_group_flag[x_cg + 1][y_cg];
-        if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
-            prev_sig += (s->HEVClc.rc.significant_coeff_group_flag[x_cg][y_cg + 1] << 1);
-
         switch (prev_sig) {
         case 0: {
                 int x_off = x_c & 3;
@@ -839,82 +825,46 @@ int ff_hevc_significant_coeff_flag_decode(HEVCContext *s, int c_idx, int x_c, in
     return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_FLAG] + inc);
 }
 
-int ff_hevc_coeff_abs_level_greater1_flag_decode(HEVCContext *s, int c_idx,
-                                                 int i, int n,
-                                                 int first_elem,
-                                                 int first_subset)
+int ff_hevc_coeff_abs_level_greater1_flag_decode(HEVCContext *s, int c_idx, int inc)
 {
 
-    int inc;
-
-    if (first_elem) {
-        s->HEVClc.ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;
-
-        if (!first_subset && s->HEVClc.greater1_ctx == 0)
-            s->HEVClc.ctx_set++;
-        s->HEVClc.greater1_ctx = 1;
-    }
-
-    inc = (s->HEVClc.ctx_set << 2) + s->HEVClc.greater1_ctx;
     if (c_idx > 0)
         inc += 16;
 
-    s->HEVClc.last_coeff_abs_level_greater1_flag =
-        GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER1_FLAG] + inc);
-
-    if (s->HEVClc.last_coeff_abs_level_greater1_flag) {
-        s->HEVClc.greater1_ctx = 0;
-    } else if (s->HEVClc.greater1_ctx > 0 && s->HEVClc.greater1_ctx < 3) {
-        s->HEVClc.greater1_ctx++;
-    }
-
-    return s->HEVClc.last_coeff_abs_level_greater1_flag;
+    return GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER1_FLAG] + inc);
 }
 
-int ff_hevc_coeff_abs_level_greater2_flag_decode(HEVCContext *s, int c_idx,
-                                                 int i, int n)
+int ff_hevc_coeff_abs_level_greater2_flag_decode(HEVCContext *s, int c_idx, int inc)
 {
-    int inc;
-
-    inc = s->HEVClc.ctx_set;
     if (c_idx > 0)
         inc += 4;
 
     return GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER2_FLAG] + inc);
 }
 
-int ff_hevc_coeff_abs_level_remaining(HEVCContext *s, int first_elem, int base_level)
+int ff_hevc_coeff_abs_level_remaining(HEVCContext *s, int base_level, int rc_rice_param)
 {
-    int i;
-    HEVCLocalContext *lc = &s->HEVClc;
     int prefix = 0;
     int suffix = 0;
-
-    if (first_elem) {
-        lc->c_rice_param = 0;
-        lc->last_coeff_abs_level_remaining = 0;
-    }
+    int last_coeff_abs_level_remaining;
+    int i;
 
     while (prefix < CABAC_MAX_BIN && get_cabac_bypass(&s->HEVClc.cc))
         prefix++;
     if (prefix == CABAC_MAX_BIN)
         av_log(s->avctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", prefix);
     if (prefix < 3) {
-        for (i = 0; i < lc->c_rice_param; i++)
+        for (i = 0; i < rc_rice_param; i++)
             suffix = (suffix << 1) | get_cabac_bypass(&s->HEVClc.cc);
-        lc->last_coeff_abs_level_remaining = (prefix << lc->c_rice_param) + suffix;
+        last_coeff_abs_level_remaining = (prefix << rc_rice_param) + suffix;
     } else {
-        for (i = 0; i < prefix - 3 + lc->c_rice_param; i++)
+        int prefix_minus3 = prefix - 3;
+        for (i = 0; i < prefix_minus3 + rc_rice_param; i++)
             suffix = (suffix << 1) | get_cabac_bypass(&s->HEVClc.cc);
-        lc->last_coeff_abs_level_remaining = (((1 << (prefix - 3)) + 3 - 1)
-                                              << lc->c_rice_param) + suffix;
+        last_coeff_abs_level_remaining = (((1 << prefix_minus3) + 3 - 1)
+                                              << rc_rice_param) + suffix;
     }
-
-    lc->c_rice_param = FFMIN(lc->c_rice_param +
-                             ((base_level + lc->last_coeff_abs_level_remaining) >
-                              (3 * (1 << lc->c_rice_param))), 4);
-
-    return lc->last_coeff_abs_level_remaining;
+    return last_coeff_abs_level_remaining;
 }
 
 int ff_hevc_coeff_sign_flag(HEVCContext *s, uint8_t nb)
diff --git a/libavcodec/hevc_filter.c b/libavcodec/hevc_filter.c
index 85189a8..ea83795 100644
--- a/libavcodec/hevc_filter.c
+++ b/libavcodec/hevc_filter.c
@@ -182,7 +182,7 @@ static void copy_CTB(uint8_t *dst, uint8_t *src, int width, int height, int stri
 
 #define CTB(tab, x, y) ((tab)[(y) * s->sps->ctb_width + (x)])
 
-static void sao_filter_CTB(HEVCContext *s, int x, int y, int c_idx_min, int c_idx_max)
+static void sao_filter_CTB(HEVCContext *s, int x, int y)
 {
     //  TODO: This should be easily parallelizable
     //  TODO: skip CBs when (cu_transquant_bypass_flag || (pcm_loop_filter_disable_flag && pcm_flag))
@@ -283,8 +283,8 @@ static void sao_filter_CTB(HEVCContext *s, int x, int y, int c_idx_min, int c_id
                  (edges[2] ? width  + (x_shift >> chroma) : width)  << s->sps->pixel_shift,
                  (edges[3] ? height + (y_shift >> chroma) : height), stride);
 
-        for (class_index = 0; class_index < class && c_idx >= c_idx_min &&
-                              c_idx < c_idx_max; class_index++) {
+        for (class_index = 0; class_index < class; class_index++) {
+
             switch (sao[class_index]->type_idx[c_idx]) {
             case SAO_BAND:
                 s->hevcdsp.sao_band_filter[classes[class_index]](dst, src, stride, sao[class_index], edges, width, height, c_idx);
@@ -334,11 +334,8 @@ static void deblocking_filter_CTB(HEVCContext *s, int x0, int y0)
     int pcmf        = (s->sps->pcm_enabled_flag && s->sps->pcm.loop_filter_disable_flag) ||
                       s->pps->transquant_bypass_enable_flag;
 
-    if (s->deblock[ctb].disable)
-        return;
-
-    if (x0) {
-        left_tc_offset   = s->deblock[ctb-1].tc_offset;
+    if(x0) {
+        left_tc_offset = s->deblock[ctb-1].tc_offset;
         left_beta_offset = s->deblock[ctb-1].beta_offset;
     }
 
@@ -649,7 +646,7 @@ void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0, int l
     }
 
     // bs for TU internal vertical PU boundaries
-    if (log2_trafo_size > s->sps->log2_min_pu_size && !is_intra)
+    if (log2_trafo_size > log2_min_pu_size && !is_intra)
         for (j = 0; j < (1 << log2_trafo_size); j += 4) {
             int y_pu = (y0 + j) >> log2_min_pu_size;
             int y_tu = (y0 + j) >> log2_min_tu_size;
@@ -665,7 +662,6 @@ void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0, int l
                 uint8_t curr_cbf_luma = s->cbf_luma[y_tu * pic_width_in_min_tu + xq_tu];
                 RefPicList* left_refPicList = ff_hevc_get_ref_list(s, s->ref, x0 + i - 1, y0 + j);
 
-
                 bs = boundary_strength(s, curr, curr_cbf_luma, left, left_cbf_luma, left_refPicList, 0);
                 if (s->sh.disable_deblocking_filter_flag == 1)
                     bs = 0;
@@ -680,11 +676,9 @@ void ff_hevc_deblocking_boundary_strengths(HEVCContext *s, int x0, int y0, int l
 
 void ff_hevc_hls_filter(HEVCContext *s, int x, int y)
 {
-    int c_idx_min = s->sh.slice_sample_adaptive_offset_flag[0] != 0 ? 0 : 1;
-    int c_idx_max = s->sh.slice_sample_adaptive_offset_flag[1] != 0 ? 3 : 1;
     deblocking_filter_CTB(s, x, y);
     if (s->sps->sao_enabled)
-        sao_filter_CTB(s, x, y, c_idx_min, c_idx_max);
+        sao_filter_CTB(s, x, y);
 }
 
 void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size)
diff --git a/libavcodec/hevc_ps.c b/libavcodec/hevc_ps.c
index 84d53ab..b9015ae 100644
--- a/libavcodec/hevc_ps.c
+++ b/libavcodec/hevc_ps.c
@@ -92,7 +92,7 @@ int ff_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps,
         uint8_t delta_rps_sign;
 
         if (is_slice_header) {
-            int delta_idx = get_ue_golomb(gb) + 1;
+            int delta_idx = get_ue_golomb_long(gb) + 1;
             if (delta_idx > sps->nb_st_rps) {
                 av_log(s->avctx, AV_LOG_ERROR, "Invalid value of delta_idx "
                        "in slice header RPS: %d > %d.\n", delta_idx,
@@ -104,7 +104,7 @@ int ff_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps,
             rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
 
         delta_rps_sign = get_bits1(gb);
-        abs_delta_rps  = get_ue_golomb(gb) + 1;
+        abs_delta_rps  = get_ue_golomb_long(gb) + 1;
         delta_rps      = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
         for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
             int used = rps->used[k] = get_bits1(gb);
@@ -161,8 +161,8 @@ int ff_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps,
         }
     } else {
         unsigned int prev, nb_positive_pics;
-        rps->num_negative_pics = get_ue_golomb(gb);
-        nb_positive_pics       = get_ue_golomb(gb);
+        rps->num_negative_pics = get_ue_golomb_long(gb);
+        nb_positive_pics       = get_ue_golomb_long(gb);
 
         if (rps->num_negative_pics >= MAX_REFS ||
             nb_positive_pics >= MAX_REFS) {
@@ -174,14 +174,14 @@ int ff_hevc_decode_short_term_rps(HEVCContext *s, ShortTermRPS *rps,
         if (rps->num_delta_pocs) {
             prev = 0;
             for (i = 0; i < rps->num_negative_pics; i++) {
-                delta_poc = get_ue_golomb(gb) + 1;
+                delta_poc = get_ue_golomb_long(gb) + 1;
                 prev -= delta_poc;
                 rps->delta_poc[i] = prev;
                 rps->used[i] = get_bits1(gb);
             }
             prev = 0;
             for (i = 0; i < nb_positive_pics; i++) {
-                delta_poc = get_ue_golomb(gb) + 1;
+                delta_poc = get_ue_golomb_long(gb) + 1;
                 prev += delta_poc;
                 rps->delta_poc[rps->num_negative_pics + i] = prev;
                 rps->used[rps->num_negative_pics + i] = get_bits1(gb);
@@ -297,16 +297,16 @@ int ff_hevc_decode_nal_vps(HEVCContext *s)
 
     i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers - 1;
     for (; i < vps->vps_max_sub_layers; i++) {
-        vps->vps_max_dec_pic_buffering[i] = get_ue_golomb(gb);
-        vps->vps_num_reorder_pics[i]      = get_ue_golomb(gb);
-        vps->vps_max_latency_increase[i]  = get_ue_golomb(gb);
+        vps->vps_max_dec_pic_buffering[i] = get_ue_golomb_long(gb) + 1;
+        vps->vps_num_reorder_pics[i]      = get_ue_golomb_long(gb);
+        vps->vps_max_latency_increase[i]  = get_ue_golomb_long(gb) - 1;
 
-        if (vps->vps_max_dec_pic_buffering[i] >= MAX_DPB_SIZE) {
+        if (vps->vps_max_dec_pic_buffering[i] > MAX_DPB_SIZE) {
             av_log(s->avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
                    vps->vps_max_dec_pic_buffering[i] - 1);
             goto err;
         }
-        if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i]) {
+        if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
             av_log(s->avctx, AV_LOG_ERROR, "vps_max_num_reorder_pics out of range: %d\n",
                    vps->vps_num_reorder_pics[i]);
             goto err;
@@ -314,7 +314,7 @@ int ff_hevc_decode_nal_vps(HEVCContext *s)
     }
 
     vps->vps_max_layer_id   = get_bits(gb, 6);
-    vps->vps_num_layer_sets = get_ue_golomb(gb) + 1;
+    vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
     for (i = 1; i < vps->vps_num_layer_sets; i++)
         for (j = 0; j <= vps->vps_max_layer_id; j++)
             skip_bits(gb, 1); // layer_id_included_flag[i][j]
@@ -325,8 +325,8 @@ int ff_hevc_decode_nal_vps(HEVCContext *s)
         vps->vps_time_scale                      = get_bits_long(gb, 32);
         vps->vps_poc_proportional_to_timing_flag = get_bits1(gb);
         if (vps->vps_poc_proportional_to_timing_flag)
-            vps->vps_num_ticks_poc_diff_one = get_ue_golomb(gb) + 1;
-        vps->vps_num_hrd_parameters = get_ue_golomb(gb);
+            vps->vps_num_ticks_poc_diff_one = get_ue_golomb_long(gb) + 1;
+        vps->vps_num_hrd_parameters = get_ue_golomb_long(gb);
         if (vps->vps_num_hrd_parameters != 0) {
             avpriv_report_missing_feature(s->avctx, "support for vps_num_hrd_parameters != 0");
             av_free(vps);
@@ -383,8 +383,8 @@ static void decode_vui(HEVCContext *s, HEVCSPS *sps)
 
     vui->chroma_loc_info_present_flag = get_bits1(gb);
     if (vui->chroma_loc_info_present_flag) {
-        vui->chroma_sample_loc_type_top_field    = get_ue_golomb(gb);
-        vui->chroma_sample_loc_type_bottom_field = get_ue_golomb(gb);
+        vui->chroma_sample_loc_type_top_field    = get_ue_golomb_long(gb);
+        vui->chroma_sample_loc_type_bottom_field = get_ue_golomb_long(gb);
     }
 
     vui->neutra_chroma_indication_flag = get_bits1(gb);
@@ -394,10 +394,10 @@ static void decode_vui(HEVCContext *s, HEVCSPS *sps)
     vui->default_display_window_flag = get_bits1(gb);
     if (vui->default_display_window_flag) {
         //TODO: * 2 is only valid for 420
-        vui->def_disp_win.left_offset   = get_ue_golomb(gb) * 2;
-        vui->def_disp_win.right_offset  = get_ue_golomb(gb) * 2;
-        vui->def_disp_win.top_offset    = get_ue_golomb(gb) * 2;
-        vui->def_disp_win.bottom_offset = get_ue_golomb(gb) * 2;
+        vui->def_disp_win.left_offset   = get_ue_golomb_long(gb) * 2;
+        vui->def_disp_win.right_offset  = get_ue_golomb_long(gb) * 2;
+        vui->def_disp_win.top_offset    = get_ue_golomb_long(gb) * 2;
+        vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) * 2;
 
         if (s->strict_def_disp_win &&
             s->avctx->flags2 & CODEC_FLAG2_IGNORE_CROP) {
@@ -422,7 +422,7 @@ static void decode_vui(HEVCContext *s, HEVCSPS *sps)
         vui->vui_time_scale                      = get_bits(gb, 32);
         vui->vui_poc_proportional_to_timing_flag = get_bits1(gb);
         if (vui->vui_poc_proportional_to_timing_flag)
-            vui->vui_num_ticks_poc_diff_one_minus1 = get_ue_golomb(gb);
+            vui->vui_num_ticks_poc_diff_one_minus1 = get_ue_golomb_long(gb);
         vui->vui_hrd_parameters_present_flag = get_bits1(gb);
         if (vui->vui_hrd_parameters_present_flag)
             decode_hrd(s);
@@ -433,11 +433,11 @@ static void decode_vui(HEVCContext *s, HEVCSPS *sps)
         vui->tiles_fixed_structure_flag              = get_bits1(gb);
         vui->motion_vectors_over_pic_boundaries_flag = get_bits1(gb);
         vui->restricted_ref_pic_lists_flag           = get_bits1(gb);
-        vui->min_spatial_segmentation_idc            = get_ue_golomb(gb);
-        vui->max_bytes_per_pic_denom                 = get_ue_golomb(gb);
-        vui->max_bits_per_min_cu_denom               = get_ue_golomb(gb);
-        vui->log2_max_mv_length_horizontal           = get_ue_golomb(gb);
-        vui->log2_max_mv_length_vertical             = get_ue_golomb(gb);
+        vui->min_spatial_segmentation_idc            = get_ue_golomb_long(gb);
+        vui->max_bytes_per_pic_denom                 = get_ue_golomb_long(gb);
+        vui->max_bits_per_min_cu_denom               = get_ue_golomb_long(gb);
+        vui->log2_max_mv_length_horizontal           = get_ue_golomb_long(gb);
+        vui->log2_max_mv_length_vertical             = get_ue_golomb_long(gb);
     }
 }
 
@@ -478,7 +478,7 @@ static int scaling_list_data(HEVCContext *s, ScalingList *sl)
         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(gb);
+                delta = get_ue_golomb_long(gb);
                 // Only need to handle non-zero delta. Zero means default, which should already be in the arrays.
                 if (delta) {
                     // Copy from previous array.
@@ -564,14 +564,14 @@ int ff_hevc_decode_nal_sps(HEVCContext *s)
         ret = AVERROR_INVALIDDATA;
         goto err;
     }
-    sps_id = get_ue_golomb(gb);
+    sps_id = get_ue_golomb_long(gb);
     if (sps_id >= MAX_SPS_COUNT) {
         av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", sps_id);
         ret = AVERROR_INVALIDDATA;
         goto err;
     }
 
-    sps->chroma_format_idc = get_ue_golomb(gb);
+    sps->chroma_format_idc = get_ue_golomb_long(gb);
     if (sps->chroma_format_idc != 1) {
         avpriv_report_missing_feature(s->avctx, "chroma_format_idc != 1\n");
         ret = AVERROR_INVALIDDATA;
@@ -581,18 +581,18 @@ int ff_hevc_decode_nal_sps(HEVCContext *s)
     if (sps->chroma_format_idc == 3)
         sps->separate_colour_plane_flag = get_bits1(gb);
 
-    sps->width  = get_ue_golomb(gb);
-    sps->height = get_ue_golomb(gb);
+    sps->width  = get_ue_golomb_long(gb);
+    sps->height = get_ue_golomb_long(gb);
     if ((ret = av_image_check_size(sps->width,
                                    sps->height, 0, s->avctx)) < 0)
         goto err;
 
     if (get_bits1(gb)) { // pic_conformance_flag
         //TODO: * 2 is only valid for 420
-        sps->pic_conf_win.left_offset   = get_ue_golomb(gb) * 2;
-        sps->pic_conf_win.right_offset  = get_ue_golomb(gb) * 2;
-        sps->pic_conf_win.top_offset    = get_ue_golomb(gb) * 2;
-        sps->pic_conf_win.bottom_offset = get_ue_golomb(gb) * 2;
+        sps->pic_conf_win.left_offset   = get_ue_golomb_long(gb) * 2;
+        sps->pic_conf_win.right_offset  = get_ue_golomb_long(gb) * 2;
+        sps->pic_conf_win.top_offset    = get_ue_golomb_long(gb) * 2;
+        sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) * 2;
 
         if (s->avctx->flags2 & CODEC_FLAG2_IGNORE_CROP) {
             av_log(s->avctx, AV_LOG_DEBUG,
@@ -611,8 +611,8 @@ int ff_hevc_decode_nal_sps(HEVCContext *s)
         sps->output_window = sps->pic_conf_win;
     }
 
-    sps->bit_depth   = get_ue_golomb(gb) + 8;
-    bit_depth_chroma = get_ue_golomb(gb) + 8;
+    sps->bit_depth   = get_ue_golomb_long(gb) + 8;
+    bit_depth_chroma = get_ue_golomb_long(gb) + 8;
     if (bit_depth_chroma != sps->bit_depth) {
         av_log(s->avctx, AV_LOG_ERROR,
                "Luma bit depth (%d) is different from chroma bit depth (%d), this is unsupported.\n",
@@ -649,7 +649,7 @@ int ff_hevc_decode_nal_sps(HEVCContext *s)
 
     sps->pixel_shift = sps->bit_depth > 8;
 
-    sps->log2_max_poc_lsb = get_ue_golomb(gb) + 4;
+    sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
     if (sps->log2_max_poc_lsb > 16) {
         av_log(s->avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
                sps->log2_max_poc_lsb - 4);
@@ -660,16 +660,16 @@ int ff_hevc_decode_nal_sps(HEVCContext *s)
     sublayer_ordering_info = get_bits1(gb);
     start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
     for (i = start; i < sps->max_sub_layers; i++) {
-        sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb(gb);
-        sps->temporal_layer[i].num_reorder_pics      = get_ue_golomb(gb);
-        sps->temporal_layer[i].max_latency_increase  = get_ue_golomb(gb);
-        if (sps->temporal_layer[i].max_dec_pic_buffering >= MAX_DPB_SIZE) {
+        sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
+        sps->temporal_layer[i].num_reorder_pics      = get_ue_golomb_long(gb);
+        sps->temporal_layer[i].max_latency_increase  = get_ue_golomb_long(gb) - 1;
+        if (sps->temporal_layer[i].max_dec_pic_buffering > MAX_DPB_SIZE) {
             av_log(s->avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
                    sps->temporal_layer[i].max_dec_pic_buffering - 1);
             ret = AVERROR_INVALIDDATA;
             goto err;
         }
-        if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering) {
+        if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering - 1) {
             av_log(s->avctx, AV_LOG_ERROR, "sps_max_num_reorder_pics out of range: %d\n",
                    sps->temporal_layer[i].num_reorder_pics);
             ret = AVERROR_INVALIDDATA;
@@ -685,10 +685,10 @@ int ff_hevc_decode_nal_sps(HEVCContext *s)
         }
     }
 
-    sps->log2_min_coding_block_size             = get_ue_golomb(gb) + 3;
-    sps->log2_diff_max_min_coding_block_size    = get_ue_golomb(gb);
-    sps->log2_min_transform_block_size          = get_ue_golomb(gb) + 2;
-    log2_diff_max_min_transform_block_size      = get_ue_golomb(gb);
+    sps->log2_min_coding_block_size             = get_ue_golomb_long(gb) + 3;
+    sps->log2_diff_max_min_coding_block_size    = get_ue_golomb_long(gb);
+    sps->log2_min_transform_block_size          = get_ue_golomb_long(gb) + 2;
+    log2_diff_max_min_transform_block_size      = get_ue_golomb_long(gb);
     sps->log2_max_trafo_size                    = log2_diff_max_min_transform_block_size + sps->log2_min_transform_block_size;
 
     if (sps->log2_min_transform_block_size >= sps->log2_min_coding_block_size) {
@@ -696,8 +696,8 @@ int ff_hevc_decode_nal_sps(HEVCContext *s)
         ret = AVERROR_INVALIDDATA;
         goto err;
     }
-    sps->max_transform_hierarchy_depth_inter = get_ue_golomb(gb);
-    sps->max_transform_hierarchy_depth_intra = get_ue_golomb(gb);
+    sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb);
+    sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb);
 
     sps->scaling_list_enable_flag = get_bits1(gb);
     if (sps->scaling_list_enable_flag) {
@@ -727,9 +727,9 @@ int ff_hevc_decode_nal_sps(HEVCContext *s)
             goto err;
         }
 
-        sps->pcm.log2_min_pcm_cb_size = get_ue_golomb(gb) + 3;
+        sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3;
         sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size +
-                                        get_ue_golomb(gb);
+                                        get_ue_golomb_long(gb);
         if (sps->pcm.bit_depth > sps->bit_depth) {
             av_log(s->avctx, AV_LOG_ERROR,
                    "PCM bit depth (%d) is greater than normal bit depth (%d)\n",
@@ -741,7 +741,7 @@ int ff_hevc_decode_nal_sps(HEVCContext *s)
         sps->pcm.loop_filter_disable_flag = get_bits1(gb);
     }
 
-    sps->nb_st_rps = get_ue_golomb(gb);
+    sps->nb_st_rps = get_ue_golomb_long(gb);
     if (sps->nb_st_rps > MAX_SHORT_TERM_RPS_COUNT) {
         av_log(s->avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
                sps->nb_st_rps);
@@ -756,7 +756,7 @@ int ff_hevc_decode_nal_sps(HEVCContext *s)
 
     sps->long_term_ref_pics_present_flag = get_bits1(gb);
     if (sps->long_term_ref_pics_present_flag) {
-        sps->num_long_term_ref_pics_sps = get_ue_golomb(gb);
+        sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
         for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
             sps->lt_ref_pic_poc_lsb_sps[i]       = get_bits(gb, sps->log2_max_poc_lsb);
             sps->used_by_curr_pic_lt_sps_flag[i] = get_bits1(gb);
@@ -929,13 +929,13 @@ int ff_hevc_decode_nal_pps(HEVCContext *s)
     pps->tc_offset                             = 0;
 
     // Coded parameters
-    pps_id = get_ue_golomb(gb);
+    pps_id = get_ue_golomb_long(gb);
     if (pps_id >= MAX_PPS_COUNT) {
         av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
         ret = AVERROR_INVALIDDATA;
         goto err;
     }
-    pps->sps_id = get_ue_golomb(gb);
+    pps->sps_id = get_ue_golomb_long(gb);
     if (pps->sps_id >= MAX_SPS_COUNT) {
         av_log(s->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
         ret = AVERROR_INVALIDDATA;
@@ -956,8 +956,8 @@ int ff_hevc_decode_nal_pps(HEVCContext *s)
 
     pps->cabac_init_present_flag = get_bits1(gb);
 
-    pps->num_ref_idx_l0_default_active = get_ue_golomb(gb) + 1;
-    pps->num_ref_idx_l1_default_active = get_ue_golomb(gb) + 1;
+    pps->num_ref_idx_l0_default_active = get_ue_golomb_long(gb) + 1;
+    pps->num_ref_idx_l1_default_active = get_ue_golomb_long(gb) + 1;
 
     pps->pic_init_qp_minus26 = get_se_golomb(gb);
 
@@ -967,7 +967,7 @@ int ff_hevc_decode_nal_pps(HEVCContext *s)
     pps->cu_qp_delta_enabled_flag = get_bits1(gb);
     pps->diff_cu_qp_delta_depth   = 0;
     if (pps->cu_qp_delta_enabled_flag)
-        pps->diff_cu_qp_delta_depth = get_ue_golomb(gb);
+        pps->diff_cu_qp_delta_depth = get_ue_golomb_long(gb);
 
     pps->cb_qp_offset = get_se_golomb(gb);
     if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
@@ -993,8 +993,8 @@ int ff_hevc_decode_nal_pps(HEVCContext *s)
     pps->entropy_coding_sync_enabled_flag = get_bits1(gb);
 
     if (pps->tiles_enabled_flag) {
-        pps->num_tile_columns     = get_ue_golomb(gb) + 1;
-        pps->num_tile_rows        = get_ue_golomb(gb) + 1;
+        pps->num_tile_columns     = get_ue_golomb_long(gb) + 1;
+        pps->num_tile_rows        = get_ue_golomb_long(gb) + 1;
         if (pps->num_tile_columns == 0 ||
             pps->num_tile_columns >= sps->width) {
             av_log(s->avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
@@ -1021,7 +1021,7 @@ int ff_hevc_decode_nal_pps(HEVCContext *s)
         if (!pps->uniform_spacing_flag) {
             int sum = 0;
             for (i = 0; i < pps->num_tile_columns - 1; i++) {
-                pps->column_width[i] = get_ue_golomb(gb) + 1;
+                pps->column_width[i] = get_ue_golomb_long(gb) + 1;
                 sum += pps->column_width[i];
             }
             if (sum >= sps->ctb_width) {
@@ -1033,7 +1033,7 @@ int ff_hevc_decode_nal_pps(HEVCContext *s)
 
             sum = 0;
             for (i = 0; i < pps->num_tile_rows - 1; i++) {
-                pps->row_height[i] = get_ue_golomb(gb) + 1;
+                pps->row_height[i] = get_ue_golomb_long(gb) + 1;
                 sum += pps->row_height[i];
             }
             if (sum >= sps->ctb_height) {
@@ -1078,7 +1078,7 @@ int ff_hevc_decode_nal_pps(HEVCContext *s)
             goto err;
     }
     pps->lists_modification_present_flag = get_bits1(gb);
-    pps->log2_parallel_merge_level       = get_ue_golomb(gb) + 2;
+    pps->log2_parallel_merge_level       = get_ue_golomb_long(gb) + 2;
     if (pps->log2_parallel_merge_level > sps->log2_ctb_size) {
         av_log(s->avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
                pps->log2_parallel_merge_level - 2);



More information about the ffmpeg-cvslog mailing list