4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 * Copyright (C) 2012 - 2013 Gildas Cocherel
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/attributes.h"
25 #include "libavutil/common.h"
27 #include "cabac_functions.h"
30 #define CABAC_MAX_BIN 100
33 * number of bin by SyntaxElement.
35 static const int8_t num_bins_in_se[] = {
39 0, // sao_band_position
42 0, // end_of_slice_flag
43 3, // split_coding_unit_flag
44 1, // cu_transquant_bypass_flag
50 1, // prev_intra_luma_pred_mode
52 0, // rem_intra_luma_pred_mode
53 2, // intra_chroma_pred_mode
59 2, // abs_mvd_greater0_flag
60 2, // abs_mvd_greater1_flag
64 1, // no_residual_data_flag
65 3, // split_transform_flag
68 2, // transform_skip_flag[][]
69 18, // last_significant_coeff_x_prefix
70 18, // last_significant_coeff_y_prefix
71 0, // last_significant_coeff_x_suffix
72 0, // last_significant_coeff_y_suffix
73 4, // significant_coeff_group_flag
74 42, // significant_coeff_flag
75 24, // coeff_abs_level_greater1_flag
76 6, // coeff_abs_level_greater2_flag
77 0, // coeff_abs_level_remaining
82 * Offset to ctxIdx 0 in init_values and states, indexed by SyntaxElement.
84 static const int elem_offset[sizeof(num_bins_in_se)] = {
132 * Indexed by init_type
134 static const uint8_t init_values[3][HEVC_CONTEXTS] = {
139 // split_coding_unit_flag
141 // cu_transquant_bypass_flag
151 // prev_intra_luma_pred_mode
153 // intra_chroma_pred_mode
160 CNU, CNU, CNU, CNU, CNU,
165 // abs_mvd_greater1_flag
167 // abs_mvd_greater1_flag
171 // no_residual_data_flag
173 // split_transform_flag
179 // transform_skip_flag
181 // last_significant_coeff_x_prefix
182 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
184 // last_significant_coeff_y_prefix
185 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
187 // significant_coeff_group_flag
189 // significant_coeff_flag
190 111, 111, 125, 110, 110, 94, 124, 108, 124, 107, 125, 141, 179, 153,
191 125, 107, 125, 141, 179, 153, 125, 107, 125, 141, 179, 153, 125, 140,
192 139, 182, 182, 152, 136, 152, 136, 153, 136, 139, 111, 136, 139, 111,
193 // coeff_abs_level_greater1_flag
194 140, 92, 137, 138, 140, 152, 138, 139, 153, 74, 149, 92, 139, 107,
195 122, 152, 140, 179, 166, 182, 140, 227, 122, 197,
196 // coeff_abs_level_greater2_flag
197 138, 153, 136, 167, 152, 152, },
202 // split_coding_unit_flag
204 // cu_transquant_bypass_flag
214 // prev_intra_luma_pred_mode
216 // intra_chroma_pred_mode
228 // abs_mvd_greater1_flag
230 // abs_mvd_greater1_flag
234 // no_residual_data_flag
236 // split_transform_flag
242 // transform_skip_flag
244 // last_significant_coeff_x_prefix
245 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95,
247 // last_significant_coeff_y_prefix
248 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95,
250 // significant_coeff_group_flag
252 // significant_coeff_flag
253 155, 154, 139, 153, 139, 123, 123, 63, 153, 166, 183, 140, 136, 153,
254 154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
255 153, 123, 123, 107, 121, 107, 121, 167, 151, 183, 140, 151, 183, 140,
256 // coeff_abs_level_greater1_flag
257 154, 196, 196, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
258 136, 137, 169, 194, 166, 167, 154, 167, 137, 182,
259 // coeff_abs_level_greater2_flag
260 107, 167, 91, 122, 107, 167, },
265 // split_coding_unit_flag
267 // cu_transquant_bypass_flag
277 // prev_intra_luma_pred_mode
279 // intra_chroma_pred_mode
291 // abs_mvd_greater1_flag
293 // abs_mvd_greater1_flag
297 // no_residual_data_flag
299 // split_transform_flag
305 // transform_skip_flag
307 // last_significant_coeff_x_prefix
308 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111,
310 // last_significant_coeff_y_prefix
311 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111,
313 // significant_coeff_group_flag
315 // significant_coeff_flag
316 170, 154, 139, 153, 139, 123, 123, 63, 124, 166, 183, 140, 136, 153,
317 154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
318 153, 138, 138, 122, 121, 122, 121, 167, 151, 183, 140, 151, 183, 140,
319 // coeff_abs_level_greater1_flag
320 154, 196, 167, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
321 136, 122, 169, 208, 166, 167, 154, 152, 167, 182,
322 // coeff_abs_level_greater2_flag
323 107, 167, 91, 107, 107, 167, },
326 static const uint8_t scan_1x1[1] = {
330 static const uint8_t horiz_scan2x2_x[4] = {
334 static const uint8_t horiz_scan2x2_y[4] = {
338 static const uint8_t horiz_scan4x4_x[16] = {
345 static const uint8_t horiz_scan4x4_y[16] = {
352 static const uint8_t horiz_scan8x8_inv[8][8] = {
353 { 0, 1, 2, 3, 16, 17, 18, 19, },
354 { 4, 5, 6, 7, 20, 21, 22, 23, },
355 { 8, 9, 10, 11, 24, 25, 26, 27, },
356 { 12, 13, 14, 15, 28, 29, 30, 31, },
357 { 32, 33, 34, 35, 48, 49, 50, 51, },
358 { 36, 37, 38, 39, 52, 53, 54, 55, },
359 { 40, 41, 42, 43, 56, 57, 58, 59, },
360 { 44, 45, 46, 47, 60, 61, 62, 63, },
363 static const uint8_t diag_scan4x1_x[4] = {
367 static const uint8_t diag_scan1x4_y[4] = {
371 static const uint8_t diag_scan2x2_x[4] = {
375 static const uint8_t diag_scan2x2_y[4] = {
379 static const uint8_t diag_scan2x2_inv[2][2] = {
384 static const uint8_t diag_scan8x2_x[16] = {
391 static const uint8_t diag_scan8x2_y[16] = {
398 static const uint8_t diag_scan8x2_inv[2][8] = {
399 { 0, 2, 4, 6, 8, 10, 12, 14, },
400 { 1, 3, 5, 7, 9, 11, 13, 15, },
403 static const uint8_t diag_scan2x8_x[16] = {
410 static const uint8_t diag_scan2x8_y[16] = {
417 static const uint8_t diag_scan2x8_inv[8][2] = {
428 const uint8_t ff_hevc_diag_scan4x4_x[16] = {
435 const uint8_t ff_hevc_diag_scan4x4_y[16] = {
442 static const uint8_t diag_scan4x4_inv[4][4] = {
449 const uint8_t ff_hevc_diag_scan8x8_x[64] = {
468 const uint8_t ff_hevc_diag_scan8x8_y[64] = {
487 static const uint8_t diag_scan8x8_inv[8][8] = {
488 { 0, 2, 5, 9, 14, 20, 27, 35, },
489 { 1, 4, 8, 13, 19, 26, 34, 42, },
490 { 3, 7, 12, 18, 25, 33, 41, 48, },
491 { 6, 11, 17, 24, 32, 40, 47, 53, },
492 { 10, 16, 23, 31, 39, 46, 52, 57, },
493 { 15, 22, 30, 38, 45, 51, 56, 60, },
494 { 21, 29, 37, 44, 50, 55, 59, 62, },
495 { 28, 36, 43, 49, 54, 58, 61, 63, },
498 void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts)
500 if (s->pps->entropy_coding_sync_enabled_flag &&
501 (ctb_addr_ts % s->sps->ctb_width == 2 ||
502 (s->sps->ctb_width == 2 &&
503 ctb_addr_ts % s->sps->ctb_width == 0))) {
504 memcpy(s->cabac_state, s->HEVClc->cabac_state, HEVC_CONTEXTS);
508 static void load_states(HEVCContext *s)
510 memcpy(s->HEVClc->cabac_state, s->cabac_state, HEVC_CONTEXTS);
513 static void cabac_reinit(HEVCLocalContext *lc)
515 skip_bytes(&lc->cc, 0);
518 static void cabac_init_decoder(HEVCContext *s)
520 GetBitContext *gb = &s->HEVClc->gb;
523 ff_init_cabac_decoder(&s->HEVClc->cc,
524 gb->buffer + get_bits_count(gb) / 8,
525 (get_bits_left(gb) + 7) / 8);
528 static void cabac_init_state(HEVCContext *s)
530 int init_type = 2 - s->sh.slice_type;
533 if (s->sh.cabac_init_flag && s->sh.slice_type != I_SLICE)
536 for (i = 0; i < HEVC_CONTEXTS; i++) {
537 int init_value = init_values[init_type][i];
538 int m = (init_value >> 4) * 5 - 45;
539 int n = ((init_value & 15) << 3) - 16;
540 int pre = 2 * (((m * av_clip_c(s->sh.slice_qp, 0, 51)) >> 4) + n) - 127;
544 pre = 124 + (pre & 1);
545 s->HEVClc->cabac_state[i] = pre;
549 void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts)
551 if (ctb_addr_ts == s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs]) {
552 cabac_init_decoder(s);
553 if (s->sh.dependent_slice_segment_flag == 0 ||
554 (s->pps->tiles_enabled_flag &&
555 s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1]))
558 if (!s->sh.first_slice_in_pic_flag &&
559 s->pps->entropy_coding_sync_enabled_flag) {
560 if (ctb_addr_ts % s->sps->ctb_width == 0) {
561 if (s->sps->ctb_width == 1)
563 else if (s->sh.dependent_slice_segment_flag == 1)
568 if (s->pps->tiles_enabled_flag &&
569 s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1]) {
570 if (s->threads_number == 1)
571 cabac_reinit(s->HEVClc);
573 cabac_init_decoder(s);
576 if (s->pps->entropy_coding_sync_enabled_flag) {
577 if (ctb_addr_ts % s->sps->ctb_width == 0) {
578 get_cabac_terminate(&s->HEVClc->cc);
579 if (s->threads_number == 1)
580 cabac_reinit(s->HEVClc);
582 cabac_init_decoder(s);
584 if (s->sps->ctb_width == 1)
593 #define GET_CABAC(ctx) get_cabac(&s->HEVClc->cc, &s->HEVClc->cabac_state[ctx])
595 int ff_hevc_sao_merge_flag_decode(HEVCContext *s)
597 return GET_CABAC(elem_offset[SAO_MERGE_FLAG]);
600 int ff_hevc_sao_type_idx_decode(HEVCContext *s)
602 if (!GET_CABAC(elem_offset[SAO_TYPE_IDX]))
605 if (!get_cabac_bypass(&s->HEVClc->cc))
610 int ff_hevc_sao_band_position_decode(HEVCContext *s)
613 int value = get_cabac_bypass(&s->HEVClc->cc);
615 for (i = 0; i < 4; i++)
616 value = (value << 1) | get_cabac_bypass(&s->HEVClc->cc);
620 int ff_hevc_sao_offset_abs_decode(HEVCContext *s)
623 int length = (1 << (FFMIN(s->sps->bit_depth, 10) - 5)) - 1;
625 while (i < length && get_cabac_bypass(&s->HEVClc->cc))
630 int ff_hevc_sao_offset_sign_decode(HEVCContext *s)
632 return get_cabac_bypass(&s->HEVClc->cc);
635 int ff_hevc_sao_eo_class_decode(HEVCContext *s)
637 int ret = get_cabac_bypass(&s->HEVClc->cc) << 1;
638 ret |= get_cabac_bypass(&s->HEVClc->cc);
642 int ff_hevc_end_of_slice_flag_decode(HEVCContext *s)
644 return get_cabac_terminate(&s->HEVClc->cc);
647 int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s)
649 return GET_CABAC(elem_offset[CU_TRANSQUANT_BYPASS_FLAG]);
652 int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0, int x_cb, int y_cb)
654 int min_cb_width = s->sps->min_cb_width;
656 int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
657 int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
659 if (s->HEVClc->ctb_left_flag || x0b)
660 inc = !!SAMPLE_CTB(s->skip_flag, x_cb - 1, y_cb);
661 if (s->HEVClc->ctb_up_flag || y0b)
662 inc += !!SAMPLE_CTB(s->skip_flag, x_cb, y_cb - 1);
664 return GET_CABAC(elem_offset[SKIP_FLAG] + inc);
667 int ff_hevc_cu_qp_delta_abs(HEVCContext *s)
673 while (prefix_val < 5 && GET_CABAC(elem_offset[CU_QP_DELTA] + inc)) {
677 if (prefix_val >= 5) {
679 while (k < CABAC_MAX_BIN && get_cabac_bypass(&s->HEVClc->cc)) {
680 suffix_val += 1 << k;
683 if (k == CABAC_MAX_BIN)
684 av_log(s->avctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k);
687 suffix_val += get_cabac_bypass(&s->HEVClc->cc) << k;
689 return prefix_val + suffix_val;
692 int ff_hevc_cu_qp_delta_sign_flag(HEVCContext *s)
694 return get_cabac_bypass(&s->HEVClc->cc);
697 int ff_hevc_pred_mode_decode(HEVCContext *s)
699 return GET_CABAC(elem_offset[PRED_MODE_FLAG]);
702 int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth, int x0, int y0)
704 int inc = 0, depth_left = 0, depth_top = 0;
705 int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
706 int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
707 int x_cb = x0 >> s->sps->log2_min_cb_size;
708 int y_cb = y0 >> s->sps->log2_min_cb_size;
710 if (s->HEVClc->ctb_left_flag || x0b)
711 depth_left = s->tab_ct_depth[(y_cb) * s->sps->min_cb_width + x_cb - 1];
712 if (s->HEVClc->ctb_up_flag || y0b)
713 depth_top = s->tab_ct_depth[(y_cb - 1) * s->sps->min_cb_width + x_cb];
715 inc += (depth_left > ct_depth);
716 inc += (depth_top > ct_depth);
718 return GET_CABAC(elem_offset[SPLIT_CODING_UNIT_FLAG] + inc);
721 int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size)
723 if (GET_CABAC(elem_offset[PART_MODE])) // 1
725 if (log2_cb_size == s->sps->log2_min_cb_size) {
726 if (s->HEVClc->cu.pred_mode == MODE_INTRA) // 0
728 if (GET_CABAC(elem_offset[PART_MODE] + 1)) // 01
730 if (log2_cb_size == 3) // 00
732 if (GET_CABAC(elem_offset[PART_MODE] + 2)) // 001
734 return PART_NxN; // 000
737 if (!s->sps->amp_enabled_flag) {
738 if (GET_CABAC(elem_offset[PART_MODE] + 1)) // 01
743 if (GET_CABAC(elem_offset[PART_MODE] + 1)) { // 01X, 01XX
744 if (GET_CABAC(elem_offset[PART_MODE] + 3)) // 011
746 if (get_cabac_bypass(&s->HEVClc->cc)) // 0101
748 return PART_2NxnU; // 0100
751 if (GET_CABAC(elem_offset[PART_MODE] + 3)) // 001
753 if (get_cabac_bypass(&s->HEVClc->cc)) // 0001
755 return PART_nLx2N; // 0000
758 int ff_hevc_pcm_flag_decode(HEVCContext *s)
760 return get_cabac_terminate(&s->HEVClc->cc);
763 int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s)
765 return GET_CABAC(elem_offset[PREV_INTRA_LUMA_PRED_FLAG]);
768 int ff_hevc_mpm_idx_decode(HEVCContext *s)
771 while (i < 2 && get_cabac_bypass(&s->HEVClc->cc))
776 int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s)
779 int value = get_cabac_bypass(&s->HEVClc->cc);
781 for (i = 0; i < 4; i++)
782 value = (value << 1) | get_cabac_bypass(&s->HEVClc->cc);
786 int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s)
789 if (!GET_CABAC(elem_offset[INTRA_CHROMA_PRED_MODE]))
792 ret = get_cabac_bypass(&s->HEVClc->cc) << 1;
793 ret |= get_cabac_bypass(&s->HEVClc->cc);
797 int ff_hevc_merge_idx_decode(HEVCContext *s)
799 int i = GET_CABAC(elem_offset[MERGE_IDX]);
802 while (i < s->sh.max_num_merge_cand-1 && get_cabac_bypass(&s->HEVClc->cc))
808 int ff_hevc_merge_flag_decode(HEVCContext *s)
810 return GET_CABAC(elem_offset[MERGE_FLAG]);
813 int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH)
815 if (nPbW + nPbH == 12)
816 return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4);
817 if (GET_CABAC(elem_offset[INTER_PRED_IDC] + s->HEVClc->ct.depth))
820 return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4);
823 int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx)
826 int max = num_ref_idx_lx - 1;
827 int max_ctx = FFMIN(max, 2);
829 while (i < max_ctx && GET_CABAC(elem_offset[REF_IDX_L0] + i))
832 while (i < max && get_cabac_bypass(&s->HEVClc->cc))
839 int ff_hevc_mvp_lx_flag_decode(HEVCContext *s)
841 return GET_CABAC(elem_offset[MVP_LX_FLAG]);
844 int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s)
846 return GET_CABAC(elem_offset[NO_RESIDUAL_DATA_FLAG]);
849 static av_always_inline int abs_mvd_greater0_flag_decode(HEVCContext *s)
851 return GET_CABAC(elem_offset[ABS_MVD_GREATER0_FLAG]);
854 static av_always_inline int abs_mvd_greater1_flag_decode(HEVCContext *s)
856 return GET_CABAC(elem_offset[ABS_MVD_GREATER1_FLAG] + 1);
859 static av_always_inline int mvd_decode(HEVCContext *s)
864 while (k < CABAC_MAX_BIN && get_cabac_bypass(&s->HEVClc->cc)) {
868 if (k == CABAC_MAX_BIN)
869 av_log(s->avctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k);
871 ret += get_cabac_bypass(&s->HEVClc->cc) << k;
872 return get_cabac_bypass_sign(&s->HEVClc->cc, -ret);
875 static av_always_inline int mvd_sign_flag_decode(HEVCContext *s)
877 return get_cabac_bypass_sign(&s->HEVClc->cc, -1);
880 int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size)
882 return GET_CABAC(elem_offset[SPLIT_TRANSFORM_FLAG] + 5 - log2_trafo_size);
885 int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth)
887 return GET_CABAC(elem_offset[CBF_CB_CR] + trafo_depth);
890 int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth)
892 return GET_CABAC(elem_offset[CBF_LUMA] + !trafo_depth);
895 int ff_hevc_transform_skip_flag_decode(HEVCContext *s, int c_idx)
897 return GET_CABAC(elem_offset[TRANSFORM_SKIP_FLAG] + !!c_idx);
900 #define LAST_SIG_COEFF(elem) \
902 int max = (log2_size << 1) - 1; \
903 int ctx_offset, ctx_shift; \
906 ctx_offset = 3 * (log2_size - 2) + ((log2_size - 1) >> 2); \
907 ctx_shift = (log2_size + 1) >> 2; \
910 ctx_shift = log2_size - 2; \
913 GET_CABAC(elem_offset[elem] + (i >> ctx_shift) + ctx_offset)) \
917 static av_always_inline int last_significant_coeff_x_prefix_decode(HEVCContext *s, int c_idx,
920 LAST_SIG_COEFF(LAST_SIGNIFICANT_COEFF_X_PREFIX)
923 static av_always_inline int last_significant_coeff_y_prefix_decode(HEVCContext *s, int c_idx,
926 LAST_SIG_COEFF(LAST_SIGNIFICANT_COEFF_Y_PREFIX)
929 static av_always_inline int last_significant_coeff_suffix_decode(HEVCContext *s,
930 int last_significant_coeff_prefix)
933 int length = (last_significant_coeff_prefix >> 1) - 1;
934 int value = get_cabac_bypass(&s->HEVClc->cc);
936 for (i = 1; i < length; i++)
937 value = (value << 1) | get_cabac_bypass(&s->HEVClc->cc);
941 static av_always_inline int significant_coeff_group_flag_decode(HEVCContext *s, int c_idx, int ctx_cg)
945 inc = FFMIN(ctx_cg, 1) + (c_idx>0 ? 2 : 0);
947 return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_GROUP_FLAG] + inc);
950 static av_always_inline int significant_coeff_flag_decode(HEVCContext *s, int c_idx, int x_c, int y_c,
951 int log2_trafo_size, int scan_idx, int prev_sig)
953 static const uint8_t ctx_idx_map[] = {
954 0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8
961 if (x_c + y_c == 0) {
963 } else if (log2_trafo_size == 2) {
964 sig_ctx = ctx_idx_map[(y_c << 2) + x_c];
970 sig_ctx = ((x_off + y_off) == 0) ? 2 : ((x_off + y_off) <= 2) ? 1 : 0;
974 sig_ctx = 2 - FFMIN(y_c & 3, 2);
977 sig_ctx = 2 - FFMIN(x_c & 3, 2);
983 if (c_idx == 0 && (x_cg > 0 || y_cg > 0))
986 if (log2_trafo_size == 3) {
987 sig_ctx += (scan_idx == SCAN_DIAG) ? 9 : 15;
989 sig_ctx += c_idx ? 12 : 21;
998 return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_FLAG] + inc);
1001 static av_always_inline int coeff_abs_level_greater1_flag_decode(HEVCContext *s, int c_idx, int inc)
1007 return GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER1_FLAG] + inc);
1010 static av_always_inline int coeff_abs_level_greater2_flag_decode(HEVCContext *s, int c_idx, int inc)
1015 return GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER2_FLAG] + inc);
1018 static av_always_inline int coeff_abs_level_remaining_decode(HEVCContext *s, int base_level, int rc_rice_param)
1022 int last_coeff_abs_level_remaining;
1025 while (prefix < CABAC_MAX_BIN && get_cabac_bypass(&s->HEVClc->cc))
1027 if (prefix == CABAC_MAX_BIN)
1028 av_log(s->avctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", prefix);
1030 for (i = 0; i < rc_rice_param; i++)
1031 suffix = (suffix << 1) | get_cabac_bypass(&s->HEVClc->cc);
1032 last_coeff_abs_level_remaining = (prefix << rc_rice_param) + suffix;
1034 int prefix_minus3 = prefix - 3;
1035 for (i = 0; i < prefix_minus3 + rc_rice_param; i++)
1036 suffix = (suffix << 1) | get_cabac_bypass(&s->HEVClc->cc);
1037 last_coeff_abs_level_remaining = (((1 << prefix_minus3) + 3 - 1)
1038 << rc_rice_param) + suffix;
1040 return last_coeff_abs_level_remaining;
1043 static av_always_inline int coeff_sign_flag_decode(HEVCContext *s, uint8_t nb)
1048 for (i = 0; i < nb; i++)
1049 ret = (ret << 1) | get_cabac_bypass(&s->HEVClc->cc);
1053 void ff_hevc_hls_residual_coding(HEVCContext *s, int x0, int y0,
1054 int log2_trafo_size, enum ScanType scan_idx,
1057 #define GET_COORD(offset, n) \
1059 x_c = (scan_x_cg[offset >> 4] << 2) + scan_x_off[n]; \
1060 y_c = (scan_y_cg[offset >> 4] << 2) + scan_y_off[n]; \
1062 HEVCLocalContext *lc = s->HEVClc;
1063 int transform_skip_flag = 0;
1065 int last_significant_coeff_x, last_significant_coeff_y;
1069 int greater1_ctx = 1;
1071 int num_last_subset;
1072 int x_cg_last_sig, y_cg_last_sig;
1074 const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off;
1076 ptrdiff_t stride = s->frame->linesize[c_idx];
1077 int hshift = s->sps->hshift[c_idx];
1078 int vshift = s->sps->vshift[c_idx];
1079 uint8_t *dst = &s->frame->data[c_idx][(y0 >> vshift) * stride +
1080 ((x0 >> hshift) << s->sps->pixel_shift)];
1081 DECLARE_ALIGNED(16, int16_t, coeffs[MAX_TB_SIZE * MAX_TB_SIZE]) = {0};
1082 DECLARE_ALIGNED(8, uint8_t, significant_coeff_group_flag[8][8]) = {{0}};
1084 int trafo_size = 1 << log2_trafo_size;
1086 int qp,shift,add,scale,scale_m;
1087 const uint8_t level_scale[] = { 40, 45, 51, 57, 64, 72 };
1088 const uint8_t *scale_matrix;
1091 // Derive QP for dequant
1092 if (!lc->cu.cu_transquant_bypass_flag) {
1093 static const int qp_c[] = { 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37 };
1094 static const uint8_t rem6[51 + 2 * 6 + 1] = {
1095 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
1096 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
1097 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
1100 static const uint8_t div6[51 + 2 * 6 + 1] = {
1101 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
1102 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
1103 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
1105 int qp_y = lc->qp_y;
1108 qp = qp_y + s->sps->qp_bd_offset;
1113 offset = s->pps->cb_qp_offset + s->sh.slice_cb_qp_offset;
1115 offset = s->pps->cr_qp_offset + s->sh.slice_cr_qp_offset;
1117 qp_i = av_clip_c(qp_y + offset, - s->sps->qp_bd_offset, 57);
1123 qp = qp_c[qp_i - 30];
1125 qp += s->sps->qp_bd_offset;
1128 shift = s->sps->bit_depth + log2_trafo_size - 5;
1129 add = 1 << (shift-1);
1130 scale = level_scale[rem6[qp]] << (div6[qp]);
1131 scale_m = 16; // default when no custom scaling lists.
1134 if (s->sps->scaling_list_enable_flag) {
1135 const ScalingList *sl = s->pps->scaling_list_data_present_flag ?
1136 &s->pps->scaling_list : &s->sps->scaling_list;
1137 int matrix_id = lc->cu.pred_mode != MODE_INTRA;
1139 if (log2_trafo_size != 5)
1140 matrix_id = 3 * matrix_id + c_idx;
1142 scale_matrix = sl->sl[log2_trafo_size - 2][matrix_id];
1143 if (log2_trafo_size >= 4)
1144 dc_scale = sl->sl_dc[log2_trafo_size - 4][matrix_id];
1148 if (s->pps->transform_skip_enabled_flag && !lc->cu.cu_transquant_bypass_flag &&
1149 log2_trafo_size == 2) {
1150 transform_skip_flag = ff_hevc_transform_skip_flag_decode(s, c_idx);
1153 last_significant_coeff_x =
1154 last_significant_coeff_x_prefix_decode(s, c_idx, log2_trafo_size);
1155 last_significant_coeff_y =
1156 last_significant_coeff_y_prefix_decode(s, c_idx, log2_trafo_size);
1158 if (last_significant_coeff_x > 3) {
1159 int suffix = last_significant_coeff_suffix_decode(s, last_significant_coeff_x);
1160 last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
1161 (2 + (last_significant_coeff_x & 1)) +
1165 if (last_significant_coeff_y > 3) {
1166 int suffix = last_significant_coeff_suffix_decode(s, last_significant_coeff_y);
1167 last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
1168 (2 + (last_significant_coeff_y & 1)) +
1172 if (scan_idx == SCAN_VERT)
1173 FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y);
1175 x_cg_last_sig = last_significant_coeff_x >> 2;
1176 y_cg_last_sig = last_significant_coeff_y >> 2;
1180 int last_x_c = last_significant_coeff_x & 3;
1181 int last_y_c = last_significant_coeff_y & 3;
1183 scan_x_off = ff_hevc_diag_scan4x4_x;
1184 scan_y_off = ff_hevc_diag_scan4x4_y;
1185 num_coeff = diag_scan4x4_inv[last_y_c][last_x_c];
1186 if (trafo_size == 4) {
1187 scan_x_cg = scan_1x1;
1188 scan_y_cg = scan_1x1;
1189 } else if (trafo_size == 8) {
1190 num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1191 scan_x_cg = diag_scan2x2_x;
1192 scan_y_cg = diag_scan2x2_y;
1193 } else if (trafo_size == 16) {
1194 num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1195 scan_x_cg = ff_hevc_diag_scan4x4_x;
1196 scan_y_cg = ff_hevc_diag_scan4x4_y;
1197 } else { // trafo_size == 32
1198 num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1199 scan_x_cg = ff_hevc_diag_scan8x8_x;
1200 scan_y_cg = ff_hevc_diag_scan8x8_y;
1205 scan_x_cg = horiz_scan2x2_x;
1206 scan_y_cg = horiz_scan2x2_y;
1207 scan_x_off = horiz_scan4x4_x;
1208 scan_y_off = horiz_scan4x4_y;
1209 num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
1211 default: //SCAN_VERT
1212 scan_x_cg = horiz_scan2x2_y;
1213 scan_y_cg = horiz_scan2x2_x;
1214 scan_x_off = horiz_scan4x4_y;
1215 scan_y_off = horiz_scan4x4_x;
1216 num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
1220 num_last_subset = (num_coeff - 1) >> 4;
1222 for (i = num_last_subset; i >= 0; i--) {
1224 int x_cg, y_cg, x_c, y_c, pos;
1225 int implicit_non_zero_coeff = 0;
1226 int64_t trans_coeff_level;
1228 int offset = i << 4;
1230 uint8_t significant_coeff_flag_idx[16];
1231 uint8_t nb_significant_coeff_flag = 0;
1233 x_cg = scan_x_cg[i];
1234 y_cg = scan_y_cg[i];
1236 if ((i < num_last_subset) && (i > 0)) {
1238 if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
1239 ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];
1240 if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
1241 ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];
1243 significant_coeff_group_flag[x_cg][y_cg] =
1244 significant_coeff_group_flag_decode(s, c_idx, ctx_cg);
1245 implicit_non_zero_coeff = 1;
1247 significant_coeff_group_flag[x_cg][y_cg] =
1248 ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
1249 (x_cg == 0 && y_cg == 0));
1252 last_scan_pos = num_coeff - offset - 1;
1254 if (i == num_last_subset) {
1255 n_end = last_scan_pos - 1;
1256 significant_coeff_flag_idx[0] = last_scan_pos;
1257 nb_significant_coeff_flag = 1;
1262 if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
1263 prev_sig = significant_coeff_group_flag[x_cg + 1][y_cg];
1264 if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
1265 prev_sig += (significant_coeff_group_flag[x_cg][y_cg + 1] << 1);
1267 for (n = n_end; n >= 0; n--) {
1268 GET_COORD(offset, n);
1270 if (significant_coeff_group_flag[x_cg][y_cg] &&
1271 (n > 0 || implicit_non_zero_coeff == 0)) {
1272 if (significant_coeff_flag_decode(s, c_idx, x_c, y_c, log2_trafo_size, scan_idx, prev_sig) == 1) {
1273 significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1274 nb_significant_coeff_flag++;
1275 implicit_non_zero_coeff = 0;
1278 int last_cg = (x_c == (x_cg << 2) && y_c == (y_cg << 2));
1279 if (last_cg && implicit_non_zero_coeff && significant_coeff_group_flag[x_cg][y_cg]) {
1280 significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1281 nb_significant_coeff_flag++;
1286 n_end = nb_significant_coeff_flag;
1290 int first_nz_pos_in_cg = 16;
1291 int last_nz_pos_in_cg = -1;
1292 int c_rice_param = 0;
1293 int first_greater1_coeff_idx = -1;
1294 uint8_t coeff_abs_level_greater1_flag[16] = {0};
1295 uint16_t coeff_sign_flag;
1297 int sign_hidden = 0;
1299 // initialize first elem of coeff_bas_level_greater1_flag
1300 int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;
1302 if (!(i == num_last_subset) && greater1_ctx == 0)
1305 last_nz_pos_in_cg = significant_coeff_flag_idx[0];
1307 for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {
1308 int n_idx = significant_coeff_flag_idx[m];
1309 int inc = (ctx_set << 2) + greater1_ctx;
1310 coeff_abs_level_greater1_flag[n_idx] =
1311 coeff_abs_level_greater1_flag_decode(s, c_idx, inc);
1312 if (coeff_abs_level_greater1_flag[n_idx]) {
1314 } else if (greater1_ctx > 0 && greater1_ctx < 3) {
1318 if (coeff_abs_level_greater1_flag[n_idx] &&
1319 first_greater1_coeff_idx == -1)
1320 first_greater1_coeff_idx = n_idx;
1322 first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];
1323 sign_hidden = (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4 &&
1324 !lc->cu.cu_transquant_bypass_flag);
1326 if (first_greater1_coeff_idx != -1) {
1327 coeff_abs_level_greater1_flag[first_greater1_coeff_idx] += coeff_abs_level_greater2_flag_decode(s, c_idx, ctx_set);
1329 if (!s->pps->sign_data_hiding_flag || !sign_hidden ) {
1330 coeff_sign_flag = coeff_sign_flag_decode(s, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
1332 coeff_sign_flag = coeff_sign_flag_decode(s, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1));
1335 for (m = 0; m < n_end; m++) {
1336 n = significant_coeff_flag_idx[m];
1337 GET_COORD(offset, n);
1338 trans_coeff_level = 1 + coeff_abs_level_greater1_flag[n];
1339 if (trans_coeff_level == ((m < 8) ?
1340 ((n == first_greater1_coeff_idx) ? 3 : 2) : 1)) {
1341 int last_coeff_abs_level_remaining = coeff_abs_level_remaining_decode(s, trans_coeff_level, c_rice_param);
1343 trans_coeff_level += last_coeff_abs_level_remaining;
1344 if (trans_coeff_level > (3 << c_rice_param))
1345 c_rice_param = FFMIN(c_rice_param + 1, 4);
1348 if (s->pps->sign_data_hiding_flag && sign_hidden) {
1349 sum_abs += trans_coeff_level;
1350 if (n == first_nz_pos_in_cg && (sum_abs&1))
1351 trans_coeff_level = -trans_coeff_level;
1353 if (coeff_sign_flag >> 15)
1354 trans_coeff_level = -trans_coeff_level;
1355 coeff_sign_flag <<= 1;
1356 if(!lc->cu.cu_transquant_bypass_flag) {
1357 if(s->sps->scaling_list_enable_flag) {
1358 if(y_c || x_c || log2_trafo_size < 4) {
1359 switch(log2_trafo_size) {
1360 case 3: pos = (y_c << 3) + x_c; break;
1361 case 4: pos = ((y_c >> 1) << 3) + (x_c >> 1); break;
1362 case 5: pos = ((y_c >> 2) << 3) + (x_c >> 2); break;
1363 default: pos = (y_c << 2) + x_c;
1365 scale_m = scale_matrix[pos];
1370 trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;
1371 if(trans_coeff_level < 0) {
1372 if((~trans_coeff_level) & 0xFffffffffff8000)
1373 trans_coeff_level = -32768;
1375 if(trans_coeff_level & 0xffffffffffff8000)
1376 trans_coeff_level = 32767;
1379 coeffs[y_c * trafo_size + x_c] = trans_coeff_level;
1384 if (lc->cu.cu_transquant_bypass_flag) {
1385 s->hevcdsp.transquant_bypass[log2_trafo_size-2](dst, coeffs, stride);
1387 if (transform_skip_flag)
1388 s->hevcdsp.transform_skip(dst, coeffs, stride);
1389 else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 && log2_trafo_size == 2)
1390 s->hevcdsp.transform_4x4_luma_add(dst, coeffs, stride);
1392 s->hevcdsp.transform_add[log2_trafo_size-2](dst, coeffs, stride);
1396 void ff_hevc_hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size)
1398 HEVCLocalContext *lc = s->HEVClc;
1399 int x = abs_mvd_greater0_flag_decode(s);
1400 int y = abs_mvd_greater0_flag_decode(s);
1403 x += abs_mvd_greater1_flag_decode(s);
1405 y += abs_mvd_greater1_flag_decode(s);
1408 case 2: lc->pu.mvd.x = mvd_decode(s); break;
1409 case 1: lc->pu.mvd.x = mvd_sign_flag_decode(s); break;
1410 case 0: lc->pu.mvd.x = 0; break;
1414 case 2: lc->pu.mvd.y = mvd_decode(s); break;
1415 case 1: lc->pu.mvd.y = mvd_sign_flag_decode(s); break;
1416 case 0: lc->pu.mvd.y = 0; break;