int ff_hevc_inter_pred_idc_decode(HEVCContext *s, int nPbW, int nPbH) { if (nPbW + nPbH == 12) return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4); if (GET_CABAC(elem_offset[INTER_PRED_IDC] + s->HEVClc.ct.depth)) return PRED_BI; return GET_CABAC(elem_offset[INTER_PRED_IDC] + 4); }
int ff_hevc_coeff_abs_level_greater1_flag_decode(HEVCContext *s, int c_idx, int i, int n, int first_elem, int first_subset) { static int greater1_ctx = 0; static int last_coeff_abs_level_greater1_flag = 0; int inc; if (first_elem) { ctx_set = (i > 0 && c_idx == 0) ? 2 : 0; if (!first_subset && greater1_ctx == 0) ctx_set++; greater1_ctx = 1; } inc = (ctx_set * 4) + greater1_ctx; if (c_idx > 0) inc += 16; last_coeff_abs_level_greater1_flag = GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER1_FLAG] + inc); if (last_coeff_abs_level_greater1_flag) { greater1_ctx = 0; } else if (greater1_ctx > 0 && greater1_ctx < 3) { greater1_ctx++; } return last_coeff_abs_level_greater1_flag; }
int ff_hevc_coeff_abs_level_greater2_flag_decode(HEVCContext *s, int c_idx, int inc) { if (c_idx > 0) inc += 4; return GET_CABAC(elem_offset[COEFF_ABS_LEVEL_GREATER2_FLAG] + inc); }
int ff_hevc_significant_coeff_group_flag_decode(HEVCContext *s, int c_idx, int ctx_cg) { int inc; inc = FFMIN(ctx_cg, 1) + (c_idx>0 ? 2 : 0); return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_GROUP_FLAG] + inc); }
int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s) { if (!GET_CABAC(elem_offset[INTRA_CHROMA_PRED_MODE])) return 4; return (get_cabac_bypass(&s->cc) << 1) | get_cabac_bypass(&s->cc); }
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) { static const uint8_t ctx_idx_map[] = { 0, 1, 4, 5, 2, 3, 4, 5, 6, 6, 8, 8, 7, 7, 8, 8 }; int x_cg = x_c >> 2; int y_cg = y_c >> 2; int sig_ctx; int inc; if (x_c + y_c == 0) { sig_ctx = 0; } else if (log2_trafo_size == 2) { sig_ctx = ctx_idx_map[(y_c << 2) + x_c]; } else { int prev_sig = 0; int x_off = x_c - (x_cg << 2); int y_off = y_c - (y_cg << 2); if (x_cg < ((1 << log2_trafo_size) - 1) >> 2) prev_sig += s->rc.significant_coeff_group_flag[x_cg + 1][y_cg]; if (y_cg < ((1 << log2_trafo_size) - 1) >> 2) prev_sig += (s->rc.significant_coeff_group_flag[x_cg][y_cg + 1] << 1); av_dlog(s->avctx, "prev_sig: %d\n", prev_sig); switch (prev_sig) { case 0: sig_ctx = ((x_off + y_off) == 0) ? 2 : ((x_off + y_off) <= 2) ? 1 : 0; break; case 1: sig_ctx = 2 - FFMIN(y_off, 2); break; case 2: sig_ctx = 2 - FFMIN(x_off, 2); break; default: sig_ctx = 2; } if (c_idx == 0 && (x_cg > 0 || y_cg > 0)) sig_ctx += 3; if (log2_trafo_size == 3) { sig_ctx += (scan_idx == SCAN_DIAG) ? 9 : 15; } else { sig_ctx += c_idx ? 12 : 21; } } if (c_idx == 0) { inc = sig_ctx; } else { inc = sig_ctx + 27; } return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_FLAG] + inc); }
int ff_hevc_sao_type_idx_decode(HEVCContext *s) { if (!GET_CABAC(elem_offset[SAO_TYPE_IDX])) return 0; if (!get_cabac_bypass(&s->HEVClc.cc)) return SAO_BAND; return SAO_EDGE; }
int ff_hevc_merge_idx_decode(HEVCContext *s) { int i = GET_CABAC(elem_offset[MERGE_IDX]); if (i != 0) { while (i < s->sh.max_num_merge_cand-1 && get_cabac_bypass(&s->HEVClc.cc)) i++; } return i; }
int ff_hevc_intra_chroma_pred_mode_decode(HEVCContext *s) { int ret; if (!GET_CABAC(elem_offset[INTRA_CHROMA_PRED_MODE])) return 4; ret = get_cabac_bypass(&s->HEVClc.cc) << 1; ret |= get_cabac_bypass(&s->HEVClc.cc); return ret; }
int ff_hevc_skip_flag_decode(HEVCContext *s, int x_cb, int y_cb) { int inc = 0; if (x_cb > 0) inc = SAMPLE(s->cu.skip_flag, x_cb-1, y_cb); if (y_cb > 0) inc += SAMPLE(s->cu.skip_flag, x_cb, y_cb-1); return GET_CABAC(elem_offset[SKIP_FLAG] + inc); }
int ff_hevc_skip_flag_decode(HEVCContext *s, int x0, int y0, int x_cb, int y_cb) { int min_cb_width = s->ps.sps->min_cb_width; int inc = 0; int x0b = x0 & ((1 << s->ps.sps->log2_ctb_size) - 1); int y0b = y0 & ((1 << s->ps.sps->log2_ctb_size) - 1); if (s->HEVClc.ctb_left_flag || x0b) inc = !!SAMPLE_CTB(s->skip_flag, x_cb - 1, y_cb); if (s->HEVClc.ctb_up_flag || y0b) inc += !!SAMPLE_CTB(s->skip_flag, x_cb, y_cb - 1); return GET_CABAC(elem_offset[SKIP_FLAG] + inc); }
int ff_hevc_ref_idx_lx_decode(HEVCContext *s, int num_ref_idx_lx) { int i = 0; int max = num_ref_idx_lx - 1; int max_ctx = FFMIN(max, 2); while (i < max_ctx && GET_CABAC(elem_offset[REF_IDX_L0] + i)) i++; if (i==2) { while (i < max && get_cabac_bypass(&s->cc)) i++; } return i; }
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 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 }; int x_cg = x_c >> 2; int y_cg = y_c >> 2; int sig_ctx, inc; if (x_c + y_c == 0) { sig_ctx = 0; } else if (log2_trafo_size == 2) { sig_ctx = ctx_idx_map[(y_c << 2) + x_c]; } else { switch (prev_sig) { case 0: { int x_off = x_c & 3; int y_off = y_c & 3; sig_ctx = ((x_off + y_off) == 0) ? 2 : ((x_off + y_off) <= 2) ? 1 : 0; } break; case 1: sig_ctx = 2 - FFMIN(y_c & 3, 2); break; case 2: sig_ctx = 2 - FFMIN(x_c & 3, 2); break; default: sig_ctx = 2; } if (c_idx == 0 && (x_cg > 0 || y_cg > 0)) sig_ctx += 3; if (log2_trafo_size == 3) { sig_ctx += (scan_idx == SCAN_DIAG) ? 9 : 15; } else { sig_ctx += c_idx ? 12 : 21; } } if (c_idx == 0) inc = sig_ctx; else inc = sig_ctx + 27; return GET_CABAC(elem_offset[SIGNIFICANT_COEFF_FLAG] + inc); }
int ff_hevc_significant_coeff_group_flag_decode(HEVCContext *s, int c_idx, int x_cg, int y_cg, int log2_trafo_size) { int ctx_cg = 0; int inc; if (x_cg < (1 << (log2_trafo_size - 2)) - 1) ctx_cg += s->rc.significant_coeff_group_flag[x_cg + 1][y_cg]; if (y_cg < (1 << (log2_trafo_size - 2)) - 1) ctx_cg += s->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_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth, int x0, int y0) { int inc = 0, depth_left = 0, depth_top = 0; if (x0 > 0) depth_left = s->cu.left_ct_depth[y0 >> s->sps->log2_min_coding_block_size]; if (y0 > 0) depth_top = s->cu.top_ct_depth[x0 >> s->sps->log2_min_coding_block_size]; av_dlog(s->avctx, "depth cur: %d, left: %d, top: %d\n", ct_depth, depth_left, depth_top); inc += (depth_left > ct_depth); inc += (depth_top > ct_depth); return GET_CABAC(elem_offset[SPLIT_CODING_UNIT_FLAG] + inc); }
int ff_hevc_split_coding_unit_flag_decode(HEVCContext *s, int ct_depth, int x0, int y0) { int inc = 0, depth_left = 0, depth_top = 0; int x0b = x0 & ((1 << s->ps.sps->log2_ctb_size) - 1); int y0b = y0 & ((1 << s->ps.sps->log2_ctb_size) - 1); int x_cb = x0 >> s->ps.sps->log2_min_cb_size; int y_cb = y0 >> s->ps.sps->log2_min_cb_size; if (s->HEVClc.ctb_left_flag || x0b) depth_left = s->tab_ct_depth[(y_cb) * s->ps.sps->min_cb_width + x_cb - 1]; if (s->HEVClc.ctb_up_flag || y0b) depth_top = s->tab_ct_depth[(y_cb - 1) * s->ps.sps->min_cb_width + x_cb]; inc += (depth_left > ct_depth); inc += (depth_top > ct_depth); return GET_CABAC(elem_offset[SPLIT_CODING_UNIT_FLAG] + inc); }
int ff_hevc_cu_qp_delta_abs(HEVCContext *s) { int prefix_val = 0; int suffix_val = 0; int inc = 0; while (prefix_val < 5 && GET_CABAC(elem_offset[CU_QP_DELTA] + inc)) { prefix_val++; inc = 1; } if (prefix_val >= 5) { int k = 0; while (k < CABAC_MAX_BIN && get_cabac_bypass(&s->HEVClc.cc)) { suffix_val += 1 << k; k++; } if (k == CABAC_MAX_BIN) av_log(s->avctx, AV_LOG_ERROR, "CABAC_MAX_BIN : %d\n", k); while (k--) suffix_val += get_cabac_bypass(&s->HEVClc.cc) << k; } return prefix_val + suffix_val; }
int ff_hevc_part_mode_decode(HEVCContext *s, int log2_cb_size) { if (GET_CABAC(elem_offset[PART_MODE])) // 1 return PART_2Nx2N; if (log2_cb_size == s->ps.sps->log2_min_cb_size) { if (s->HEVClc.cu.pred_mode == MODE_INTRA) // 0 return PART_NxN; if (GET_CABAC(elem_offset[PART_MODE] + 1)) // 01 return PART_2NxN; if (log2_cb_size == 3) // 00 return PART_Nx2N; if (GET_CABAC(elem_offset[PART_MODE] + 2)) // 001 return PART_Nx2N; return PART_NxN; // 000 } if (!s->ps.sps->amp_enabled_flag) { if (GET_CABAC(elem_offset[PART_MODE] + 1)) // 01 return PART_2NxN; return PART_Nx2N; } if (GET_CABAC(elem_offset[PART_MODE] + 1)) { // 01X, 01XX if (GET_CABAC(elem_offset[PART_MODE] + 3)) // 011 return PART_2NxN; if (get_cabac_bypass(&s->HEVClc.cc)) // 0101 return PART_2NxnD; return PART_2NxnU; // 0100 } if (GET_CABAC(elem_offset[PART_MODE] + 3)) // 001 return PART_Nx2N; if (get_cabac_bypass(&s->HEVClc.cc)) // 0001 return PART_nRx2N; return PART_nLx2N; // 0000 }
int ff_hevc_transform_skip_flag_decode(HEVCContext *s, int c_idx) { return GET_CABAC(elem_offset[TRANSFORM_SKIP_FLAG] + !!c_idx); }
int ff_hevc_cbf_luma_decode(HEVCContext *s, int trafo_depth) { return GET_CABAC(elem_offset[CBF_LUMA] + !trafo_depth); }
int ff_hevc_cbf_cb_cr_decode(HEVCContext *s, int trafo_depth) { return GET_CABAC(elem_offset[CBF_CB_CR] + trafo_depth); }
int ff_hevc_split_transform_flag_decode(HEVCContext *s, int log2_trafo_size) { return GET_CABAC(elem_offset[SPLIT_TRANSFORM_FLAG] + 5 - log2_trafo_size); }
int ff_hevc_abs_mvd_greater1_flag_decode(HEVCContext *s) { return GET_CABAC(elem_offset[ABS_MVD_GREATER1_FLAG] + 1); }
int ff_hevc_no_residual_syntax_flag_decode(HEVCContext *s) { return GET_CABAC(elem_offset[NO_RESIDUAL_DATA_FLAG]); }
int ff_hevc_pred_mode_decode(HEVCContext *s) { return GET_CABAC(elem_offset[PRED_MODE_FLAG]); }
int ff_hevc_merge_flag_decode(HEVCContext *s) { return GET_CABAC(elem_offset[MERGE_FLAG]); }
int ff_hevc_cu_transquant_bypass_flag_decode(HEVCContext *s) { return GET_CABAC(elem_offset[CU_TRANSQUANT_BYPASS_FLAG]); }
int ff_hevc_prev_intra_luma_pred_flag_decode(HEVCContext *s) { return GET_CABAC(elem_offset[PREV_INTRA_LUMA_PRED_FLAG]); }
int ff_hevc_mvp_lx_flag_decode(HEVCContext *s) { return GET_CABAC(elem_offset[MVP_LX_FLAG]); }