michael@0: michael@0: /* michael@0: * Copyright (c) 2012 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: #include michael@0: michael@0: #include "vp9/common/vp9_common.h" michael@0: #include "vp9/common/vp9_pred_common.h" michael@0: #include "vp9/common/vp9_seg_common.h" michael@0: #include "vp9/common/vp9_treecoder.h" michael@0: michael@0: static INLINE const MB_MODE_INFO *get_above_mbmi(const MODE_INFO *const above) { michael@0: return (above != NULL) ? &above->mbmi : NULL; michael@0: } michael@0: michael@0: static INLINE const MB_MODE_INFO *get_left_mbmi(const MODE_INFO *const left) { michael@0: return (left != NULL) ? &left->mbmi : NULL; michael@0: } michael@0: michael@0: // Returns a context number for the given MB prediction signal michael@0: unsigned char vp9_get_pred_context_switchable_interp(const MACROBLOCKD *xd) { michael@0: const MODE_INFO *const above_mi = get_above_mi(xd); michael@0: const MODE_INFO *const left_mi = get_left_mi(xd); michael@0: const int above_in_image = above_mi != NULL; michael@0: const int left_in_image = left_mi != NULL; michael@0: // Note: michael@0: // The mode info data structure has a one element border above and to the michael@0: // left of the entries correpsonding to real macroblocks. michael@0: // The prediction flags in these dummy entries are initialised to 0. michael@0: // left michael@0: const int left_mv_pred = left_in_image ? is_inter_block(&left_mi->mbmi) michael@0: : 0; michael@0: const int left_interp = left_in_image && left_mv_pred michael@0: ? left_mi->mbmi.interp_filter michael@0: : SWITCHABLE_FILTERS; michael@0: michael@0: // above michael@0: const int above_mv_pred = above_in_image ? is_inter_block(&above_mi->mbmi) michael@0: : 0; michael@0: const int above_interp = above_in_image && above_mv_pred michael@0: ? above_mi->mbmi.interp_filter michael@0: : SWITCHABLE_FILTERS; michael@0: michael@0: if (left_interp == above_interp) michael@0: return left_interp; michael@0: else if (left_interp == SWITCHABLE_FILTERS && michael@0: above_interp != SWITCHABLE_FILTERS) michael@0: return above_interp; michael@0: else if (left_interp != SWITCHABLE_FILTERS && michael@0: above_interp == SWITCHABLE_FILTERS) michael@0: return left_interp; michael@0: else michael@0: return SWITCHABLE_FILTERS; michael@0: } michael@0: // Returns a context number for the given MB prediction signal michael@0: unsigned char vp9_get_pred_context_intra_inter(const MACROBLOCKD *xd) { michael@0: const MODE_INFO *const above_mi = get_above_mi(xd); michael@0: const MODE_INFO *const left_mi = get_left_mi(xd); michael@0: const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi); michael@0: const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi); michael@0: const int above_in_image = above_mi != NULL; michael@0: const int left_in_image = left_mi != NULL; michael@0: const int above_intra = above_in_image ? !is_inter_block(above_mbmi) : 1; michael@0: const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1; michael@0: michael@0: // The mode info data structure has a one element border above and to the michael@0: // left of the entries corresponding to real macroblocks. michael@0: // The prediction flags in these dummy entries are initialized to 0. michael@0: // 0 - inter/inter, inter/--, --/inter, --/-- michael@0: // 1 - intra/inter, inter/intra michael@0: // 2 - intra/--, --/intra michael@0: // 3 - intra/intra michael@0: if (above_in_image && left_in_image) // both edges available michael@0: return left_intra && above_intra ? 3 michael@0: : left_intra || above_intra; michael@0: else if (above_in_image || left_in_image) // one edge available michael@0: return 2 * (above_in_image ? above_intra : left_intra); michael@0: else michael@0: return 0; michael@0: } michael@0: // Returns a context number for the given MB prediction signal michael@0: unsigned char vp9_get_pred_context_comp_inter_inter(const VP9_COMMON *cm, michael@0: const MACROBLOCKD *xd) { michael@0: int pred_context; michael@0: const MODE_INFO *const above_mi = get_above_mi(xd); michael@0: const MODE_INFO *const left_mi = get_left_mi(xd); michael@0: const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi); michael@0: const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi); michael@0: const int above_in_image = above_mi != NULL; michael@0: const int left_in_image = left_mi != NULL; michael@0: // Note: michael@0: // The mode info data structure has a one element border above and to the michael@0: // left of the entries correpsonding to real macroblocks. michael@0: // The prediction flags in these dummy entries are initialised to 0. michael@0: if (above_in_image && left_in_image) { // both edges available michael@0: if (!has_second_ref(above_mbmi) && !has_second_ref(left_mbmi)) michael@0: // neither edge uses comp pred (0/1) michael@0: pred_context = (above_mbmi->ref_frame[0] == cm->comp_fixed_ref) ^ michael@0: (left_mbmi->ref_frame[0] == cm->comp_fixed_ref); michael@0: else if (!has_second_ref(above_mbmi)) michael@0: // one of two edges uses comp pred (2/3) michael@0: pred_context = 2 + (above_mbmi->ref_frame[0] == cm->comp_fixed_ref || michael@0: !is_inter_block(above_mbmi)); michael@0: else if (!has_second_ref(left_mbmi)) michael@0: // one of two edges uses comp pred (2/3) michael@0: pred_context = 2 + (left_mbmi->ref_frame[0] == cm->comp_fixed_ref || michael@0: !is_inter_block(left_mbmi)); michael@0: else // both edges use comp pred (4) michael@0: pred_context = 4; michael@0: } else if (above_in_image || left_in_image) { // one edge available michael@0: const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi; michael@0: michael@0: if (!has_second_ref(edge_mbmi)) michael@0: // edge does not use comp pred (0/1) michael@0: pred_context = edge_mbmi->ref_frame[0] == cm->comp_fixed_ref; michael@0: else michael@0: // edge uses comp pred (3) michael@0: pred_context = 3; michael@0: } else { // no edges available (1) michael@0: pred_context = 1; michael@0: } michael@0: assert(pred_context >= 0 && pred_context < COMP_INTER_CONTEXTS); michael@0: return pred_context; michael@0: } michael@0: michael@0: // Returns a context number for the given MB prediction signal michael@0: unsigned char vp9_get_pred_context_comp_ref_p(const VP9_COMMON *cm, michael@0: const MACROBLOCKD *xd) { michael@0: int pred_context; michael@0: const MODE_INFO *const above_mi = get_above_mi(xd); michael@0: const MODE_INFO *const left_mi = get_left_mi(xd); michael@0: const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi); michael@0: const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi); michael@0: const int above_in_image = above_mi != NULL; michael@0: const int left_in_image = left_mi != NULL; michael@0: const int above_intra = above_in_image ? !is_inter_block(above_mbmi) : 1; michael@0: const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1; michael@0: // Note: michael@0: // The mode info data structure has a one element border above and to the michael@0: // left of the entries correpsonding to real macroblocks. michael@0: // The prediction flags in these dummy entries are initialised to 0. michael@0: const int fix_ref_idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref]; michael@0: const int var_ref_idx = !fix_ref_idx; michael@0: michael@0: if (above_in_image && left_in_image) { // both edges available michael@0: if (above_intra && left_intra) { // intra/intra (2) michael@0: pred_context = 2; michael@0: } else if (above_intra || left_intra) { // intra/inter michael@0: const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi; michael@0: michael@0: if (!has_second_ref(edge_mbmi)) // single pred (1/3) michael@0: pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] != cm->comp_var_ref[1]); michael@0: else // comp pred (1/3) michael@0: pred_context = 1 + 2 * (edge_mbmi->ref_frame[var_ref_idx] michael@0: != cm->comp_var_ref[1]); michael@0: } else { // inter/inter michael@0: const int l_sg = !has_second_ref(left_mbmi); michael@0: const int a_sg = !has_second_ref(above_mbmi); michael@0: MV_REFERENCE_FRAME vrfa = a_sg ? above_mbmi->ref_frame[0] michael@0: : above_mbmi->ref_frame[var_ref_idx]; michael@0: MV_REFERENCE_FRAME vrfl = l_sg ? left_mbmi->ref_frame[0] michael@0: : left_mbmi->ref_frame[var_ref_idx]; michael@0: michael@0: if (vrfa == vrfl && cm->comp_var_ref[1] == vrfa) { michael@0: pred_context = 0; michael@0: } else if (l_sg && a_sg) { // single/single michael@0: if ((vrfa == cm->comp_fixed_ref && vrfl == cm->comp_var_ref[0]) || michael@0: (vrfl == cm->comp_fixed_ref && vrfa == cm->comp_var_ref[0])) michael@0: pred_context = 4; michael@0: else if (vrfa == vrfl) michael@0: pred_context = 3; michael@0: else michael@0: pred_context = 1; michael@0: } else if (l_sg || a_sg) { // single/comp michael@0: MV_REFERENCE_FRAME vrfc = l_sg ? vrfa : vrfl; michael@0: MV_REFERENCE_FRAME rfs = a_sg ? vrfa : vrfl; michael@0: if (vrfc == cm->comp_var_ref[1] && rfs != cm->comp_var_ref[1]) michael@0: pred_context = 1; michael@0: else if (rfs == cm->comp_var_ref[1] && vrfc != cm->comp_var_ref[1]) michael@0: pred_context = 2; michael@0: else michael@0: pred_context = 4; michael@0: } else if (vrfa == vrfl) { // comp/comp michael@0: pred_context = 4; michael@0: } else { michael@0: pred_context = 2; michael@0: } michael@0: } michael@0: } else if (above_in_image || left_in_image) { // one edge available michael@0: const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi; michael@0: michael@0: if (!is_inter_block(edge_mbmi)) { michael@0: pred_context = 2; michael@0: } else { michael@0: if (has_second_ref(edge_mbmi)) michael@0: pred_context = 4 * (edge_mbmi->ref_frame[var_ref_idx] michael@0: != cm->comp_var_ref[1]); michael@0: else michael@0: pred_context = 3 * (edge_mbmi->ref_frame[0] != cm->comp_var_ref[1]); michael@0: } michael@0: } else { // no edges available (2) michael@0: pred_context = 2; michael@0: } michael@0: assert(pred_context >= 0 && pred_context < REF_CONTEXTS); michael@0: michael@0: return pred_context; michael@0: } michael@0: unsigned char vp9_get_pred_context_single_ref_p1(const MACROBLOCKD *xd) { michael@0: int pred_context; michael@0: const MODE_INFO *const above_mi = get_above_mi(xd); michael@0: const MODE_INFO *const left_mi = get_left_mi(xd); michael@0: const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi); michael@0: const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi); michael@0: const int above_in_image = above_mi != NULL; michael@0: const int left_in_image = left_mi != NULL; michael@0: const int above_intra = above_in_image ? !is_inter_block(above_mbmi) : 1; michael@0: const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1; michael@0: // Note: michael@0: // The mode info data structure has a one element border above and to the michael@0: // left of the entries correpsonding to real macroblocks. michael@0: // The prediction flags in these dummy entries are initialised to 0. michael@0: if (above_in_image && left_in_image) { // both edges available michael@0: if (above_intra && left_intra) { // intra/intra michael@0: pred_context = 2; michael@0: } else if (above_intra || left_intra) { // intra/inter or inter/intra michael@0: const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi; michael@0: if (!has_second_ref(edge_mbmi)) michael@0: pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST_FRAME); michael@0: else michael@0: pred_context = 1 + (edge_mbmi->ref_frame[0] == LAST_FRAME || michael@0: edge_mbmi->ref_frame[1] == LAST_FRAME); michael@0: } else { // inter/inter michael@0: if (!has_second_ref(above_mbmi) && !has_second_ref(left_mbmi)) { michael@0: pred_context = 2 * (above_mbmi->ref_frame[0] == LAST_FRAME) + michael@0: 2 * (left_mbmi->ref_frame[0] == LAST_FRAME); michael@0: } else if (has_second_ref(above_mbmi) && has_second_ref(left_mbmi)) { michael@0: pred_context = 1 + (above_mbmi->ref_frame[0] == LAST_FRAME || michael@0: above_mbmi->ref_frame[1] == LAST_FRAME || michael@0: left_mbmi->ref_frame[0] == LAST_FRAME || michael@0: left_mbmi->ref_frame[1] == LAST_FRAME); michael@0: } else { michael@0: const MV_REFERENCE_FRAME rfs = !has_second_ref(above_mbmi) ? michael@0: above_mbmi->ref_frame[0] : left_mbmi->ref_frame[0]; michael@0: const MV_REFERENCE_FRAME crf1 = has_second_ref(above_mbmi) ? michael@0: above_mbmi->ref_frame[0] : left_mbmi->ref_frame[0]; michael@0: const MV_REFERENCE_FRAME crf2 = has_second_ref(above_mbmi) ? michael@0: above_mbmi->ref_frame[1] : left_mbmi->ref_frame[1]; michael@0: michael@0: if (rfs == LAST_FRAME) michael@0: pred_context = 3 + (crf1 == LAST_FRAME || crf2 == LAST_FRAME); michael@0: else michael@0: pred_context = crf1 == LAST_FRAME || crf2 == LAST_FRAME; michael@0: } michael@0: } michael@0: } else if (above_in_image || left_in_image) { // one edge available michael@0: const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi; michael@0: if (!is_inter_block(edge_mbmi)) { // intra michael@0: pred_context = 2; michael@0: } else { // inter michael@0: if (!has_second_ref(edge_mbmi)) michael@0: pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST_FRAME); michael@0: else michael@0: pred_context = 1 + (edge_mbmi->ref_frame[0] == LAST_FRAME || michael@0: edge_mbmi->ref_frame[1] == LAST_FRAME); michael@0: } michael@0: } else { // no edges available michael@0: pred_context = 2; michael@0: } michael@0: michael@0: assert(pred_context >= 0 && pred_context < REF_CONTEXTS); michael@0: return pred_context; michael@0: } michael@0: michael@0: unsigned char vp9_get_pred_context_single_ref_p2(const MACROBLOCKD *xd) { michael@0: int pred_context; michael@0: const MODE_INFO *const above_mi = get_above_mi(xd); michael@0: const MODE_INFO *const left_mi = get_left_mi(xd); michael@0: const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi); michael@0: const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi); michael@0: const int above_in_image = above_mi != NULL; michael@0: const int left_in_image = left_mi != NULL; michael@0: const int above_intra = above_in_image ? !is_inter_block(above_mbmi) : 1; michael@0: const int left_intra = left_in_image ? !is_inter_block(left_mbmi) : 1; michael@0: michael@0: // Note: michael@0: // The mode info data structure has a one element border above and to the michael@0: // left of the entries correpsonding to real macroblocks. michael@0: // The prediction flags in these dummy entries are initialised to 0. michael@0: if (above_in_image && left_in_image) { // both edges available michael@0: if (above_intra && left_intra) { // intra/intra michael@0: pred_context = 2; michael@0: } else if (above_intra || left_intra) { // intra/inter or inter/intra michael@0: const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi; michael@0: if (!has_second_ref(edge_mbmi)) { michael@0: if (edge_mbmi->ref_frame[0] == LAST_FRAME) michael@0: pred_context = 3; michael@0: else michael@0: pred_context = 4 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME); michael@0: } else { michael@0: pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME || michael@0: edge_mbmi->ref_frame[1] == GOLDEN_FRAME); michael@0: } michael@0: } else { // inter/inter michael@0: if (!has_second_ref(above_mbmi) && !has_second_ref(left_mbmi)) { michael@0: if (above_mbmi->ref_frame[0] == LAST_FRAME && michael@0: left_mbmi->ref_frame[0] == LAST_FRAME) { michael@0: pred_context = 3; michael@0: } else if (above_mbmi->ref_frame[0] == LAST_FRAME || michael@0: left_mbmi->ref_frame[0] == LAST_FRAME) { michael@0: const MB_MODE_INFO *edge_mbmi = michael@0: above_mbmi->ref_frame[0] == LAST_FRAME ? left_mbmi : above_mbmi; michael@0: michael@0: pred_context = 4 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME); michael@0: } else { michael@0: pred_context = 2 * (above_mbmi->ref_frame[0] == GOLDEN_FRAME) + michael@0: 2 * (left_mbmi->ref_frame[0] == GOLDEN_FRAME); michael@0: } michael@0: } else if (has_second_ref(above_mbmi) && has_second_ref(left_mbmi)) { michael@0: if (above_mbmi->ref_frame[0] == left_mbmi->ref_frame[0] && michael@0: above_mbmi->ref_frame[1] == left_mbmi->ref_frame[1]) michael@0: pred_context = 3 * (above_mbmi->ref_frame[0] == GOLDEN_FRAME || michael@0: above_mbmi->ref_frame[1] == GOLDEN_FRAME || michael@0: left_mbmi->ref_frame[0] == GOLDEN_FRAME || michael@0: left_mbmi->ref_frame[1] == GOLDEN_FRAME); michael@0: else michael@0: pred_context = 2; michael@0: } else { michael@0: const MV_REFERENCE_FRAME rfs = !has_second_ref(above_mbmi) ? michael@0: above_mbmi->ref_frame[0] : left_mbmi->ref_frame[0]; michael@0: const MV_REFERENCE_FRAME crf1 = has_second_ref(above_mbmi) ? michael@0: above_mbmi->ref_frame[0] : left_mbmi->ref_frame[0]; michael@0: const MV_REFERENCE_FRAME crf2 = has_second_ref(above_mbmi) ? michael@0: above_mbmi->ref_frame[1] : left_mbmi->ref_frame[1]; michael@0: michael@0: if (rfs == GOLDEN_FRAME) michael@0: pred_context = 3 + (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME); michael@0: else if (rfs == ALTREF_FRAME) michael@0: pred_context = crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME; michael@0: else michael@0: pred_context = 1 + 2 * (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME); michael@0: } michael@0: } michael@0: } else if (above_in_image || left_in_image) { // one edge available michael@0: const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi; michael@0: michael@0: if (!is_inter_block(edge_mbmi) || michael@0: (edge_mbmi->ref_frame[0] == LAST_FRAME && !has_second_ref(edge_mbmi))) michael@0: pred_context = 2; michael@0: else if (!has_second_ref(edge_mbmi)) michael@0: pred_context = 4 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME); michael@0: else michael@0: pred_context = 3 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME || michael@0: edge_mbmi->ref_frame[1] == GOLDEN_FRAME); michael@0: } else { // no edges available (2) michael@0: pred_context = 2; michael@0: } michael@0: assert(pred_context >= 0 && pred_context < REF_CONTEXTS); michael@0: return pred_context; michael@0: } michael@0: // Returns a context number for the given MB prediction signal michael@0: // The mode info data structure has a one element border above and to the michael@0: // left of the entries corresponding to real blocks. michael@0: // The prediction flags in these dummy entries are initialized to 0. michael@0: unsigned char vp9_get_pred_context_tx_size(const MACROBLOCKD *xd) { michael@0: const MODE_INFO *const above_mi = get_above_mi(xd); michael@0: const MODE_INFO *const left_mi = get_left_mi(xd); michael@0: const MB_MODE_INFO *const above_mbmi = get_above_mbmi(above_mi); michael@0: const MB_MODE_INFO *const left_mbmi = get_left_mbmi(left_mi); michael@0: const int above_in_image = above_mi != NULL; michael@0: const int left_in_image = left_mi != NULL; michael@0: const int max_tx_size = max_txsize_lookup[xd->mi_8x8[0]->mbmi.sb_type]; michael@0: int above_context = max_tx_size; michael@0: int left_context = max_tx_size; michael@0: michael@0: if (above_in_image) michael@0: above_context = above_mbmi->skip_coeff ? max_tx_size michael@0: : above_mbmi->tx_size; michael@0: michael@0: if (left_in_image) michael@0: left_context = left_mbmi->skip_coeff ? max_tx_size michael@0: : left_mbmi->tx_size; michael@0: michael@0: if (!left_in_image) michael@0: left_context = above_context; michael@0: michael@0: if (!above_in_image) michael@0: above_context = left_context; michael@0: michael@0: return above_context + left_context > max_tx_size; michael@0: } michael@0: michael@0: void vp9_set_pred_flag_seg_id(MACROBLOCKD *xd, uint8_t pred_flag) { michael@0: xd->mi_8x8[0]->mbmi.seg_id_predicted = pred_flag; michael@0: } michael@0: michael@0: int vp9_get_segment_id(VP9_COMMON *cm, const uint8_t *segment_ids, michael@0: BLOCK_SIZE bsize, int mi_row, int mi_col) { michael@0: const int mi_offset = mi_row * cm->mi_cols + mi_col; michael@0: const int bw = num_8x8_blocks_wide_lookup[bsize]; michael@0: const int bh = num_8x8_blocks_high_lookup[bsize]; michael@0: const int xmis = MIN(cm->mi_cols - mi_col, bw); michael@0: const int ymis = MIN(cm->mi_rows - mi_row, bh); michael@0: int x, y, segment_id = INT_MAX; michael@0: michael@0: for (y = 0; y < ymis; y++) michael@0: for (x = 0; x < xmis; x++) michael@0: segment_id = MIN(segment_id, michael@0: segment_ids[mi_offset + y * cm->mi_cols + x]); michael@0: michael@0: assert(segment_id >= 0 && segment_id < MAX_SEGMENTS); michael@0: return segment_id; michael@0: }