vp9_pred_common.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef VP9_COMMON_VP9_PRED_COMMON_H_
  11. #define VP9_COMMON_VP9_PRED_COMMON_H_
  12. #include "vp9/common/vp9_blockd.h"
  13. #include "vp9/common/vp9_onyxc_int.h"
  14. #include "vpx_dsp/vpx_dsp_common.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. static INLINE int get_segment_id(const VP9_COMMON *cm,
  19. const uint8_t *segment_ids, BLOCK_SIZE bsize,
  20. int mi_row, int mi_col) {
  21. const int mi_offset = mi_row * cm->mi_cols + mi_col;
  22. const int bw = num_8x8_blocks_wide_lookup[bsize];
  23. const int bh = num_8x8_blocks_high_lookup[bsize];
  24. const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
  25. const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
  26. int x, y, segment_id = MAX_SEGMENTS;
  27. for (y = 0; y < ymis; ++y)
  28. for (x = 0; x < xmis; ++x)
  29. segment_id =
  30. VPXMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
  31. assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
  32. return segment_id;
  33. }
  34. static INLINE int vp9_get_pred_context_seg_id(const MACROBLOCKD *xd) {
  35. const MODE_INFO *const above_mi = xd->above_mi;
  36. const MODE_INFO *const left_mi = xd->left_mi;
  37. const int above_sip = (above_mi != NULL) ? above_mi->seg_id_predicted : 0;
  38. const int left_sip = (left_mi != NULL) ? left_mi->seg_id_predicted : 0;
  39. return above_sip + left_sip;
  40. }
  41. static INLINE vpx_prob vp9_get_pred_prob_seg_id(const struct segmentation *seg,
  42. const MACROBLOCKD *xd) {
  43. return seg->pred_probs[vp9_get_pred_context_seg_id(xd)];
  44. }
  45. static INLINE int vp9_get_skip_context(const MACROBLOCKD *xd) {
  46. const MODE_INFO *const above_mi = xd->above_mi;
  47. const MODE_INFO *const left_mi = xd->left_mi;
  48. const int above_skip = (above_mi != NULL) ? above_mi->skip : 0;
  49. const int left_skip = (left_mi != NULL) ? left_mi->skip : 0;
  50. return above_skip + left_skip;
  51. }
  52. static INLINE vpx_prob vp9_get_skip_prob(const VP9_COMMON *cm,
  53. const MACROBLOCKD *xd) {
  54. return cm->fc->skip_probs[vp9_get_skip_context(xd)];
  55. }
  56. // Returns a context number for the given MB prediction signal
  57. static INLINE int get_pred_context_switchable_interp(const MACROBLOCKD *xd) {
  58. // Note:
  59. // The mode info data structure has a one element border above and to the
  60. // left of the entries corresponding to real macroblocks.
  61. // The prediction flags in these dummy entries are initialized to 0.
  62. const MODE_INFO *const left_mi = xd->left_mi;
  63. const int left_type = left_mi ? left_mi->interp_filter : SWITCHABLE_FILTERS;
  64. const MODE_INFO *const above_mi = xd->above_mi;
  65. const int above_type =
  66. above_mi ? above_mi->interp_filter : SWITCHABLE_FILTERS;
  67. if (left_type == above_type)
  68. return left_type;
  69. else if (left_type == SWITCHABLE_FILTERS)
  70. return above_type;
  71. else if (above_type == SWITCHABLE_FILTERS)
  72. return left_type;
  73. else
  74. return SWITCHABLE_FILTERS;
  75. }
  76. // The mode info data structure has a one element border above and to the
  77. // left of the entries corresponding to real macroblocks.
  78. // The prediction flags in these dummy entries are initialized to 0.
  79. // 0 - inter/inter, inter/--, --/inter, --/--
  80. // 1 - intra/inter, inter/intra
  81. // 2 - intra/--, --/intra
  82. // 3 - intra/intra
  83. static INLINE int get_intra_inter_context(const MACROBLOCKD *xd) {
  84. const MODE_INFO *const above_mi = xd->above_mi;
  85. const MODE_INFO *const left_mi = xd->left_mi;
  86. const int has_above = !!above_mi;
  87. const int has_left = !!left_mi;
  88. if (has_above && has_left) { // both edges available
  89. const int above_intra = !is_inter_block(above_mi);
  90. const int left_intra = !is_inter_block(left_mi);
  91. return left_intra && above_intra ? 3 : left_intra || above_intra;
  92. } else if (has_above || has_left) { // one edge available
  93. return 2 * !is_inter_block(has_above ? above_mi : left_mi);
  94. }
  95. return 0;
  96. }
  97. static INLINE vpx_prob vp9_get_intra_inter_prob(const VP9_COMMON *cm,
  98. const MACROBLOCKD *xd) {
  99. return cm->fc->intra_inter_prob[get_intra_inter_context(xd)];
  100. }
  101. int vp9_get_reference_mode_context(const VP9_COMMON *cm, const MACROBLOCKD *xd);
  102. static INLINE vpx_prob vp9_get_reference_mode_prob(const VP9_COMMON *cm,
  103. const MACROBLOCKD *xd) {
  104. return cm->fc->comp_inter_prob[vp9_get_reference_mode_context(cm, xd)];
  105. }
  106. int vp9_get_pred_context_comp_ref_p(const VP9_COMMON *cm,
  107. const MACROBLOCKD *xd);
  108. static INLINE vpx_prob vp9_get_pred_prob_comp_ref_p(const VP9_COMMON *cm,
  109. const MACROBLOCKD *xd) {
  110. const int pred_context = vp9_get_pred_context_comp_ref_p(cm, xd);
  111. return cm->fc->comp_ref_prob[pred_context];
  112. }
  113. int vp9_get_pred_context_single_ref_p1(const MACROBLOCKD *xd);
  114. static INLINE vpx_prob vp9_get_pred_prob_single_ref_p1(const VP9_COMMON *cm,
  115. const MACROBLOCKD *xd) {
  116. return cm->fc->single_ref_prob[vp9_get_pred_context_single_ref_p1(xd)][0];
  117. }
  118. int vp9_get_pred_context_single_ref_p2(const MACROBLOCKD *xd);
  119. static INLINE vpx_prob vp9_get_pred_prob_single_ref_p2(const VP9_COMMON *cm,
  120. const MACROBLOCKD *xd) {
  121. return cm->fc->single_ref_prob[vp9_get_pred_context_single_ref_p2(xd)][1];
  122. }
  123. // Returns a context number for the given MB prediction signal
  124. // The mode info data structure has a one element border above and to the
  125. // left of the entries corresponding to real blocks.
  126. // The prediction flags in these dummy entries are initialized to 0.
  127. static INLINE int get_tx_size_context(const MACROBLOCKD *xd) {
  128. const int max_tx_size = max_txsize_lookup[xd->mi[0]->sb_type];
  129. const MODE_INFO *const above_mi = xd->above_mi;
  130. const MODE_INFO *const left_mi = xd->left_mi;
  131. const int has_above = !!above_mi;
  132. const int has_left = !!left_mi;
  133. int above_ctx =
  134. (has_above && !above_mi->skip) ? (int)above_mi->tx_size : max_tx_size;
  135. int left_ctx =
  136. (has_left && !left_mi->skip) ? (int)left_mi->tx_size : max_tx_size;
  137. if (!has_left) left_ctx = above_ctx;
  138. if (!has_above) above_ctx = left_ctx;
  139. return (above_ctx + left_ctx) > max_tx_size;
  140. }
  141. static INLINE const vpx_prob *get_tx_probs(TX_SIZE max_tx_size, int ctx,
  142. const struct tx_probs *tx_probs) {
  143. switch (max_tx_size) {
  144. case TX_8X8: return tx_probs->p8x8[ctx];
  145. case TX_16X16: return tx_probs->p16x16[ctx];
  146. case TX_32X32: return tx_probs->p32x32[ctx];
  147. default: assert(0 && "Invalid max_tx_size."); return NULL;
  148. }
  149. }
  150. static INLINE const vpx_prob *get_tx_probs2(TX_SIZE max_tx_size,
  151. const MACROBLOCKD *xd,
  152. const struct tx_probs *tx_probs) {
  153. return get_tx_probs(max_tx_size, get_tx_size_context(xd), tx_probs);
  154. }
  155. static INLINE unsigned int *get_tx_counts(TX_SIZE max_tx_size, int ctx,
  156. struct tx_counts *tx_counts) {
  157. switch (max_tx_size) {
  158. case TX_8X8: return tx_counts->p8x8[ctx];
  159. case TX_16X16: return tx_counts->p16x16[ctx];
  160. case TX_32X32: return tx_counts->p32x32[ctx];
  161. default: assert(0 && "Invalid max_tx_size."); return NULL;
  162. }
  163. }
  164. #ifdef __cplusplus
  165. } // extern "C"
  166. #endif
  167. #endif // VP9_COMMON_VP9_PRED_COMMON_H_