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] = {
140 // split_coding_unit_flag
142 // cu_transquant_bypass_flag
152 // prev_intra_luma_pred_mode
154 // intra_chroma_pred_mode
161 CNU, CNU, CNU, CNU, CNU,
166 // abs_mvd_greater1_flag
168 // abs_mvd_greater1_flag
172 // no_residual_data_flag
174 // split_transform_flag
180 // transform_skip_flag
182 // last_significant_coeff_x_prefix
183 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
185 // last_significant_coeff_y_prefix
186 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
188 // significant_coeff_group_flag
190 // significant_coeff_flag
191 111, 111, 125, 110, 110, 94, 124, 108, 124, 107, 125, 141, 179, 153,
192 125, 107, 125, 141, 179, 153, 125, 107, 125, 141, 179, 153, 125, 140,
193 139, 182, 182, 152, 136, 152, 136, 153, 136, 139, 111, 136, 139, 111,
194 // coeff_abs_level_greater1_flag
195 140, 92, 137, 138, 140, 152, 138, 139, 153, 74, 149, 92, 139, 107,
196 122, 152, 140, 179, 166, 182, 140, 227, 122, 197,
197 // coeff_abs_level_greater2_flag
198 138, 153, 136, 167, 152, 152,
205 // split_coding_unit_flag
207 // cu_transquant_bypass_flag
217 // prev_intra_luma_pred_mode
219 // intra_chroma_pred_mode
231 // abs_mvd_greater1_flag
233 // abs_mvd_greater1_flag
237 // no_residual_data_flag
239 // split_transform_flag
245 // transform_skip_flag
247 // last_significant_coeff_x_prefix
248 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95,
250 // last_significant_coeff_y_prefix
251 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95,
253 // significant_coeff_group_flag
255 // significant_coeff_flag
256 155, 154, 139, 153, 139, 123, 123, 63, 153, 166, 183, 140, 136, 153,
257 154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
258 153, 123, 123, 107, 121, 107, 121, 167, 151, 183, 140, 151, 183, 140,
259 // coeff_abs_level_greater1_flag
260 154, 196, 196, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
261 136, 137, 169, 194, 166, 167, 154, 167, 137, 182,
262 // coeff_abs_level_greater2_flag
263 107, 167, 91, 122, 107, 167,
270 // split_coding_unit_flag
272 // cu_transquant_bypass_flag
282 // prev_intra_luma_pred_mode
284 // intra_chroma_pred_mode
296 // abs_mvd_greater1_flag
298 // abs_mvd_greater1_flag
302 // no_residual_data_flag
304 // split_transform_flag
310 // transform_skip_flag
312 // last_significant_coeff_x_prefix
313 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111,
315 // last_significant_coeff_y_prefix
316 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111,
318 // significant_coeff_group_flag
320 // significant_coeff_flag
321 170, 154, 139, 153, 139, 123, 123, 63, 124, 166, 183, 140, 136, 153,
322 154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170,
323 153, 138, 138, 122, 121, 122, 121, 167, 151, 183, 140, 151, 183, 140,
324 // coeff_abs_level_greater1_flag
325 154, 196, 167, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121,
326 136, 122, 169, 208, 166, 167, 154, 152, 167, 182,
327 // coeff_abs_level_greater2_flag
328 107, 167, 91, 107, 107, 167,
332 void ff_hevc_save_states(HEVCContext *s, int ctb_addr_ts)
334 if (s->pps->entropy_coding_sync_enabled_flag &&
335 ((ctb_addr_ts % s->sps->ctb_width) == 2 ||
336 (s->sps->ctb_width == 2 &&
337 (ctb_addr_ts % s->sps->ctb_width) == 0))) {
338 memcpy(s->cabac_state, s->HEVClc.cabac_state, HEVC_CONTEXTS);
342 static void load_states(HEVCContext *s)
344 memcpy(s->HEVClc.cabac_state, s->cabac_state, HEVC_CONTEXTS);
347 static void cabac_reinit(HEVCLocalContext *lc)
349 skip_bytes(&lc->cc, 0);
352 static void cabac_init_decoder(HEVCContext *s)
354 GetBitContext *gb = &s->HEVClc.gb;
357 ff_init_cabac_decoder(&s->HEVClc.cc,
358 gb->buffer + get_bits_count(gb) / 8,
359 (get_bits_left(gb) + 7) / 8);
362 static void cabac_init_state(HEVCContext *s)
364 int init_type = 2 - s->sh.slice_type;
367 if (s->sh.cabac_init_flag && s->sh.slice_type != I_SLICE)
370 for (i = 0; i < HEVC_CONTEXTS; i++) {
371 int init_value = init_values[init_type][i];
372 int m = (init_value >> 4) * 5 - 45;
373 int n = ((init_value & 15) << 3) - 16;
374 int pre = 2 * (((m * av_clip_c(s->sh.slice_qp, 0, 51)) >> 4) + n) - 127;
378 pre = 124 + (pre & 1);
379 s->HEVClc.cabac_state[i] = pre;
383 void ff_hevc_cabac_init(HEVCContext *s, int ctb_addr_ts)
385 if (ctb_addr_ts == s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs]) {
386 cabac_init_decoder(s);
387 if ((s->sh.dependent_slice_segment_flag == 0) ||
388 (s->pps->tiles_enabled_flag &&
389 (s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1])))
392 if (!s->sh.first_slice_in_pic_flag && s->pps->entropy_coding_sync_enabled_flag) {
393 if ((ctb_addr_ts % s->sps->ctb_width) == 0) {
394 if (s->sps->ctb_width == 1)
396 else if (s->sh.dependent_slice_segment_flag == 1)
401 if (s->pps->tiles_enabled_flag &&
402 (s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1])) {
403 cabac_reinit(&s->HEVClc);
406 if (s->pps->entropy_coding_sync_enabled_flag) {
407 if ((ctb_addr_ts % s->sps->ctb_width) == 0) {
408 get_cabac_terminate(&s->HEVClc.cc);
409 cabac_reinit(&s->HEVClc);
411 if (s->sps->ctb_width == 1)
420 #define GET_CABAC(ctx) get_cabac(&s->HEVClc.cc, &s->HEVClc.cabac_state[ctx])
422 int ff_hevc_sao_merge_flag_decode(HEVCContext *s)
424 return GET_CABAC(elem_offset[SAO_MERGE_FLAG]);
427 int ff_hevc_sao_type_idx_decode(HEVCContext *s)
429 if (!GET_CABAC(elem_offset[SAO_TYPE_IDX]))
432 if (!get_cabac_bypass(&s->HEVClc.cc))
437 int ff_hevc_sao_band_position_decode(HEVCContext *s)
440 int value = get_cabac_bypass(&s->HEVClc.cc);
442 for (i = 0; i < 4; i++)
443 value = (value << 1) | get_cabac_bypass(&s->HEVClc.cc);
447 int ff_hevc_sao_offset_abs_decode(HEVCContext *s)
450 int length = (1 << (FFMIN(s->sps->bit_depth, 10) - 5)) - 1;
452 while (i < length && get_cabac_bypass(&s->HEVClc.cc))
457 int ff_hevc_sao_offset_sign_decode(HEVCContext *s)
459 return get_cabac_bypass(&s->HEVClc.cc);
462 int ff_hevc_sao_eo_class_decode(HEVCContext *s)
464 int ret = (get_cabac_bypass(&s->HEVClc.cc) << 1);
465 ret |= get_cabac_bypass(&s->HEVClc.cc);
469 int ff_hevc_end_of_slice_flag_decode(HEVCContext *s)
471 return get_cabac_terminate(&s->HEVClc.cc);
474 int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s)
476 return GET_CABAC(elem_offset[CU_TRANSQUANT_BYPASS_FLAG]);
479 int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0, int x_cb, int y_cb)
481 int pic_width_in_ctb = s->sps->width >> s->sps->log2_min_coding_block_size;
483 int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
484 int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
486 if (s->HEVClc.ctb_left_flag || x0b)
487 inc = SAMPLE_CTB(s->skip_flag, x_cb-1, y_cb);
488 if (s->HEVClc.ctb_up_flag || y0b)
489 inc += SAMPLE_CTB(s->skip_flag, x_cb, y_cb-1);
491 return GET_CABAC(elem_offset[SKIP_FLAG] + inc);
494 int ff_hevc_cu_qp_delta_abs(HEVCContext *s)
500 while (prefix_val < 5 && GET_CABAC(elem_offset[CU_QP_DELTA] + inc)) {
504 if (prefix_val >= 5) {
506 while (k < CABAC_MAX_BIN && get_cabac_bypass(&s->HEVClc.cc)) {
507 suffix_val += 1 << k;
510 if (k == CABAC_MAX_BIN)
511 av_log(s->avctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k);
514 suffix_val += get_cabac_bypass(&s->HEVClc.cc) << k;
516 return prefix_val + suffix_val;
519 int ff_hevc_cu_qp_delta_sign_flag(HEVCContext *s)
521 return get_cabac_bypass(&s->HEVClc.cc);
524 int ff_hevc_pred_mode_decode(HEVCContext *s)
526 return GET_CABAC(elem_offset[PRED_MODE_FLAG]);
529 int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth, int x0, int y0)
531 int inc = 0, depth_left = 0, depth_top = 0;
532 int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
533 int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
534 int x_cb = x0 >> s->sps->log2_min_coding_block_size;
535 int y_cb = y0 >> s->sps->log2_min_coding_block_size;
537 if (s->HEVClc.ctb_left_flag || x0b)
538 depth_left = s->tab_ct_depth[(y_cb)*s->sps->min_cb_width + x_cb-1];
539 if (s->HEVClc.ctb_up_flag || y0b)
540 depth_top = s->tab_ct_depth[(y_cb-1)*s->sps->min_cb_width + x_cb];
542 inc += (depth_left > ct_depth);
543 inc += (depth_top > ct_depth);
544 return GET_CABAC(elem_offset[SPLIT_CODING_UNIT_FLAG] + inc);
547 int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size)
549 if (GET_CABAC(elem_offset[PART_MODE])) // 1
551 if (log2_cb_size == s->sps->log2_min_coding_block_size) {
552 if (s->HEVClc.cu.pred_mode == MODE_INTRA) // 0
554 if (GET_CABAC(elem_offset[PART_MODE] + 1)) // 01
556 if (log2_cb_size == 3) // 00
558 if (GET_CABAC(elem_offset[PART_MODE] + 2)) // 001
560 return PART_NxN; // 000
563 if (!s->sps->amp_enabled_flag) {
564 if (GET_CABAC(elem_offset[PART_MODE] + 1)) // 01
569 if (GET_CABAC(elem_offset[PART_MODE] + 1)) { // 01X, 01XX
570 if (GET_CABAC(elem_offset[PART_MODE] + 3)) // 011
572 if (get_cabac_bypass(&s->HEVClc.cc)) // 0101
574 return PART_2NxnU; // 0100
577 if (GET_CABAC(elem_offset[PART_MODE] + 3)) // 001
579 if (get_cabac_bypass(&s->HEVClc.cc)) // 0001
581 return PART_nLx2N; // 0000
584 int ff_hevc_pcm_flag_decode(HEVCContext *s)
586 return get_cabac_terminate(&s->HEVClc.cc);
589 int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s)
591 return GET_CABAC(elem_offset[PREV_INTRA_LUMA_PRED_FLAG]);
594 int ff_hevc_mpm_idx_decode(HEVCContext *s)
597 while (i < 2 && get_cabac_bypass(&s->HEVClc.cc))
602 int ff_hevc_rem_intra_luma_pred_mode_decode(HEVCContext *s)
605 int value = get_cabac_bypass(&s->HEVClc.cc);
607 for (i = 0; i < 4; i++)
608 value = (value << 1) | get_cabac_bypass(&s->HEVClc.cc);
612 int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s)
615 if (!GET_CABAC(elem_offset[INTRA_CHROMA_PRED_MODE]))
618 ret = (get_cabac_bypass(&s->HEVClc.cc) << 1);
619 ret |= get_cabac_bypass(&s->HEVClc.cc);
623 int ff_hevc_merge_idx_decode(HEVCContext *s)
625 int i = GET_CABAC(elem_offset[MERGE_IDX]);
628 while (i < s->sh.max_num_merge_cand-1 && get_cabac_bypass(&s->HEVClc.cc))
634 int ff_hevc_merge_flag_decode(HEVCContext *s)
636 return GET_CABAC(elem_offset[MERGE_FLAG]);
639 int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH)
641 if (nPbW + nPbH == 12)
642 return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4);
643 if (GET_CABAC(elem_offset[INTER_PRED_IDC] + s->HEVClc.ct.depth))
646 return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4);
649 int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx)
652 int max = num_ref_idx_lx - 1;
653 int max_ctx = FFMIN(max, 2);
655 while (i < max_ctx && GET_CABAC(elem_offset[REF_IDX_L0] + i))
658 while (i < max && get_cabac_bypass(&s->HEVClc.cc))
665 int ff_hevc_mvp_lx_flag_decode(HEVCContext *s)
667 return GET_CABAC(elem_offset[MVP_LX_FLAG]);
670 int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s)
672 return GET_CABAC(elem_offset[NO_RESIDUAL_DATA_FLAG]);
675 int ff_hevc_abs_mvd_greater0_flag_decode(HEVCContext *s)
677 return GET_CABAC(elem_offset[ABS_MVD_GREATER0_FLAG]);
680 int ff_hevc_abs_mvd_greater1_flag_decode(HEVCContext *s)
682 return GET_CABAC(elem_offset[ABS_MVD_GREATER1_FLAG] + 1);
685 int ff_hevc_mvd_decode(HEVCContext *s)
690 while (k < CABAC_MAX_BIN && get_cabac_bypass(&s->HEVClc.cc)) {
694 if (k == CABAC_MAX_BIN)
695 av_log(s->avctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k);
697 ret += get_cabac_bypass(&s->HEVClc.cc) << k;
698 return get_cabac_bypass_sign(&s->HEVClc.cc, -ret);
701 int ff_hevc_mvd_sign_flag_decode(HEVCContext *s)
703 return get_cabac_bypass_sign(&s->HEVClc.cc, -1);
706 int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size)
708 return GET_CABAC(elem_offset[SPLIT_TRANSFORM_FLAG] + 5 - log2_trafo_size);
711 int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth)
713 return GET_CABAC(elem_offset[CBF_CB_CR] + trafo_depth);
716 int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth)
718 return GET_CABAC(elem_offset[CBF_LUMA] + !trafo_depth);
721 int ff_hevc_transform_skip_flag_decode(HEVCContext *s, int c_idx)
723 return GET_CABAC(elem_offset[TRANSFORM_SKIP_FLAG] + !!c_idx);
726 #define LAST_SIG_COEFF(elem) \
728 int max = (log2_size << 1) - 1; \
729 int ctx_offset, ctx_shift; \
732 ctx_offset = 3 * (log2_size - 2) + ((log2_size - 1) >> 2); \
733 ctx_shift = (log2_size + 1) >> 2; \
736 ctx_shift = log2_size - 2; \
739 GET_CABAC(elem_offset[elem] + (i >> ctx_shift) + ctx_offset)) \
743 int ff_hevc_last_significant_coeff_x_prefix_decode(HEVCContext *s, int c_idx,
746 LAST_SIG_COEFF(LAST_SIGNIFICANT_COEFF_X_PREFIX)
749 int ff_hevc_last_significant_coeff_y_prefix_decode(HEVCContext *s, int c_idx,
752 LAST_SIG_COEFF(LAST_SIGNIFICANT_COEFF_Y_PREFIX)
755 int ff_hevc_last_significant_coeff_suffix_decode(HEVCContext *s,
756 int last_significant_coeff_prefix)
759 int length = (last_significant_coeff_prefix >> 1) - 1;
760 int value = get_cabac_bypass(&s->HEVClc.cc);
762 for (i = 1; i < length; i++)
763 value = (value << 1) | get_cabac_bypass(&s->HEVClc.cc);
767 int ff_hevc_significant_coeff_group_flag_decode(HEVCContext *s, int c_idx, int ctx_cg)
771 inc = FFMIN(ctx_cg, 1) + (c_idx>0 ? 2 : 0);
773 return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_GROUP_FLAG] + inc);
776 int ff_hevc_significant_coeff_flag_decode(HEVCContext *s, int c_idx, int x_c, int y_c,
777 int log2_trafo_size, int scan_idx, int prev_sig)
779 static const uint8_t ctx_idx_map[] = {
780 0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8
787 if (x_c + y_c == 0) {
789 } else if (log2_trafo_size == 2) {
790 sig_ctx = ctx_idx_map[(y_c << 2) + x_c];
796 sig_ctx = ((x_off + y_off) == 0) ? 2 : ((x_off + y_off) <= 2) ? 1 : 0;
800 sig_ctx = 2 - FFMIN(y_c & 3, 2);
803 sig_ctx = 2 - FFMIN(x_c & 3, 2);
809 if (c_idx == 0 && (x_cg > 0 || y_cg > 0))
812 if (log2_trafo_size == 3) {
813 sig_ctx += (scan_idx == SCAN_DIAG) ? 9 : 15;
815 sig_ctx += c_idx ? 12 : 21;
825 return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_FLAG] + inc);
828 int ff_hevc_coeff_abs_level_greater1_flag_decode(HEVCContext *s, int c_idx, int inc)
834 return GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER1_FLAG] + inc);
837 int ff_hevc_coeff_abs_level_greater2_flag_decode(HEVCContext *s, int c_idx, int inc)
842 return GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER2_FLAG] + inc);
845 int ff_hevc_coeff_abs_level_remaining(HEVCContext *s, int base_level, int rc_rice_param)
849 int last_coeff_abs_level_remaining;
852 while (prefix < CABAC_MAX_BIN && get_cabac_bypass(&s->HEVClc.cc))
854 if (prefix == CABAC_MAX_BIN)
855 av_log(s->avctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", prefix);
857 for (i = 0; i < rc_rice_param; i++)
858 suffix = (suffix << 1) | get_cabac_bypass(&s->HEVClc.cc);
859 last_coeff_abs_level_remaining = (prefix << rc_rice_param) + suffix;
861 int prefix_minus3 = prefix - 3;
862 for (i = 0; i < prefix_minus3 + rc_rice_param; i++)
863 suffix = (suffix << 1) | get_cabac_bypass(&s->HEVClc.cc);
864 last_coeff_abs_level_remaining = (((1 << prefix_minus3) + 3 - 1)
865 << rc_rice_param) + suffix;
867 return last_coeff_abs_level_remaining;
870 int ff_hevc_coeff_sign_flag(HEVCContext *s, uint8_t nb)
875 for (i = 0; i < nb; i++)
876 ret = (ret << 1) | get_cabac_bypass(&s->HEVClc.cc);