vp9_blockd.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2010 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_BLOCKD_H_
  11. #define VP9_COMMON_VP9_BLOCKD_H_
  12. #include "./vpx_config.h"
  13. #include "vpx_dsp/vpx_dsp_common.h"
  14. #include "vpx_ports/mem.h"
  15. #include "vpx_scale/yv12config.h"
  16. #include "vp9/common/vp9_common_data.h"
  17. #include "vp9/common/vp9_entropy.h"
  18. #include "vp9/common/vp9_entropymode.h"
  19. #include "vp9/common/vp9_mv.h"
  20. #include "vp9/common/vp9_scale.h"
  21. #include "vp9/common/vp9_seg_common.h"
  22. #include "vp9/common/vp9_tile_common.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #define MAX_MB_PLANE 3
  27. typedef enum {
  28. KEY_FRAME = 0,
  29. INTER_FRAME = 1,
  30. FRAME_TYPES,
  31. } FRAME_TYPE;
  32. static INLINE int is_inter_mode(PREDICTION_MODE mode) {
  33. return mode >= NEARESTMV && mode <= NEWMV;
  34. }
  35. /* For keyframes, intra block modes are predicted by the (already decoded)
  36. modes for the Y blocks to the left and above us; for interframes, there
  37. is a single probability table. */
  38. typedef struct {
  39. PREDICTION_MODE as_mode;
  40. int_mv as_mv[2]; // first, second inter predictor motion vectors
  41. } b_mode_info;
  42. // Note that the rate-distortion optimization loop, bit-stream writer, and
  43. // decoder implementation modules critically rely on the defined entry values
  44. // specified herein. They should be refactored concurrently.
  45. #define NONE -1
  46. #define INTRA_FRAME 0
  47. #define LAST_FRAME 1
  48. #define GOLDEN_FRAME 2
  49. #define ALTREF_FRAME 3
  50. #define MAX_REF_FRAMES 4
  51. typedef int8_t MV_REFERENCE_FRAME;
  52. // This structure now relates to 8x8 block regions.
  53. typedef struct MODE_INFO {
  54. // Common for both INTER and INTRA blocks
  55. BLOCK_SIZE sb_type;
  56. PREDICTION_MODE mode;
  57. TX_SIZE tx_size;
  58. int8_t skip;
  59. int8_t segment_id;
  60. int8_t seg_id_predicted; // valid only when temporal_update is enabled
  61. // Only for INTRA blocks
  62. PREDICTION_MODE uv_mode;
  63. // Only for INTER blocks
  64. INTERP_FILTER interp_filter;
  65. MV_REFERENCE_FRAME ref_frame[2];
  66. // TODO(slavarnway): Delete and use bmi[3].as_mv[] instead.
  67. int_mv mv[2];
  68. b_mode_info bmi[4];
  69. } MODE_INFO;
  70. static INLINE PREDICTION_MODE get_y_mode(const MODE_INFO *mi, int block) {
  71. return mi->sb_type < BLOCK_8X8 ? mi->bmi[block].as_mode
  72. : mi->mode;
  73. }
  74. static INLINE int is_inter_block(const MODE_INFO *mi) {
  75. return mi->ref_frame[0] > INTRA_FRAME;
  76. }
  77. static INLINE int has_second_ref(const MODE_INFO *mi) {
  78. return mi->ref_frame[1] > INTRA_FRAME;
  79. }
  80. PREDICTION_MODE vp9_left_block_mode(const MODE_INFO *cur_mi,
  81. const MODE_INFO *left_mi, int b);
  82. PREDICTION_MODE vp9_above_block_mode(const MODE_INFO *cur_mi,
  83. const MODE_INFO *above_mi, int b);
  84. enum mv_precision {
  85. MV_PRECISION_Q3,
  86. MV_PRECISION_Q4
  87. };
  88. struct buf_2d {
  89. uint8_t *buf;
  90. int stride;
  91. };
  92. struct macroblockd_plane {
  93. tran_low_t *dqcoeff;
  94. int subsampling_x;
  95. int subsampling_y;
  96. struct buf_2d dst;
  97. struct buf_2d pre[2];
  98. ENTROPY_CONTEXT *above_context;
  99. ENTROPY_CONTEXT *left_context;
  100. int16_t seg_dequant[MAX_SEGMENTS][2];
  101. // number of 4x4s in current block
  102. uint16_t n4_w, n4_h;
  103. // log2 of n4_w, n4_h
  104. uint8_t n4_wl, n4_hl;
  105. // encoder
  106. const int16_t *dequant;
  107. };
  108. #define BLOCK_OFFSET(x, i) ((x) + (i) * 16)
  109. typedef struct RefBuffer {
  110. // TODO(dkovalev): idx is not really required and should be removed, now it
  111. // is used in vp9_onyxd_if.c
  112. int idx;
  113. YV12_BUFFER_CONFIG *buf;
  114. struct scale_factors sf;
  115. } RefBuffer;
  116. typedef struct macroblockd {
  117. struct macroblockd_plane plane[MAX_MB_PLANE];
  118. uint8_t bmode_blocks_wl;
  119. uint8_t bmode_blocks_hl;
  120. FRAME_COUNTS *counts;
  121. TileInfo tile;
  122. int mi_stride;
  123. MODE_INFO **mi;
  124. MODE_INFO *left_mi;
  125. MODE_INFO *above_mi;
  126. unsigned int max_blocks_wide;
  127. unsigned int max_blocks_high;
  128. const vpx_prob (*partition_probs)[PARTITION_TYPES - 1];
  129. /* Distance of MB away from frame edges */
  130. int mb_to_left_edge;
  131. int mb_to_right_edge;
  132. int mb_to_top_edge;
  133. int mb_to_bottom_edge;
  134. FRAME_CONTEXT *fc;
  135. /* pointers to reference frames */
  136. RefBuffer *block_refs[2];
  137. /* pointer to current frame */
  138. const YV12_BUFFER_CONFIG *cur_buf;
  139. ENTROPY_CONTEXT *above_context[MAX_MB_PLANE];
  140. ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16];
  141. PARTITION_CONTEXT *above_seg_context;
  142. PARTITION_CONTEXT left_seg_context[8];
  143. #if CONFIG_VP9_HIGHBITDEPTH
  144. /* Bit depth: 8, 10, 12 */
  145. int bd;
  146. #endif
  147. int lossless;
  148. int corrupted;
  149. struct vpx_internal_error_info *error_info;
  150. } MACROBLOCKD;
  151. static INLINE PLANE_TYPE get_plane_type(int plane) {
  152. return (PLANE_TYPE)(plane > 0);
  153. }
  154. static INLINE BLOCK_SIZE get_subsize(BLOCK_SIZE bsize,
  155. PARTITION_TYPE partition) {
  156. return subsize_lookup[partition][bsize];
  157. }
  158. extern const TX_TYPE intra_mode_to_tx_type_lookup[INTRA_MODES];
  159. static INLINE TX_TYPE get_tx_type(PLANE_TYPE plane_type,
  160. const MACROBLOCKD *xd) {
  161. const MODE_INFO *const mi = xd->mi[0];
  162. if (plane_type != PLANE_TYPE_Y || xd->lossless || is_inter_block(mi))
  163. return DCT_DCT;
  164. return intra_mode_to_tx_type_lookup[mi->mode];
  165. }
  166. static INLINE TX_TYPE get_tx_type_4x4(PLANE_TYPE plane_type,
  167. const MACROBLOCKD *xd, int ib) {
  168. const MODE_INFO *const mi = xd->mi[0];
  169. if (plane_type != PLANE_TYPE_Y || xd->lossless || is_inter_block(mi))
  170. return DCT_DCT;
  171. return intra_mode_to_tx_type_lookup[get_y_mode(mi, ib)];
  172. }
  173. void vp9_setup_block_planes(MACROBLOCKD *xd, int ss_x, int ss_y);
  174. static INLINE TX_SIZE get_uv_tx_size_impl(TX_SIZE y_tx_size, BLOCK_SIZE bsize,
  175. int xss, int yss) {
  176. if (bsize < BLOCK_8X8) {
  177. return TX_4X4;
  178. } else {
  179. const BLOCK_SIZE plane_bsize = ss_size_lookup[bsize][xss][yss];
  180. return VPXMIN(y_tx_size, max_txsize_lookup[plane_bsize]);
  181. }
  182. }
  183. static INLINE TX_SIZE get_uv_tx_size(const MODE_INFO *mi,
  184. const struct macroblockd_plane *pd) {
  185. return get_uv_tx_size_impl(mi->tx_size, mi->sb_type, pd->subsampling_x,
  186. pd->subsampling_y);
  187. }
  188. static INLINE BLOCK_SIZE get_plane_block_size(BLOCK_SIZE bsize,
  189. const struct macroblockd_plane *pd) {
  190. return ss_size_lookup[bsize][pd->subsampling_x][pd->subsampling_y];
  191. }
  192. static INLINE void reset_skip_context(MACROBLOCKD *xd, BLOCK_SIZE bsize) {
  193. int i;
  194. for (i = 0; i < MAX_MB_PLANE; i++) {
  195. struct macroblockd_plane *const pd = &xd->plane[i];
  196. const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
  197. memset(pd->above_context, 0,
  198. sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide_lookup[plane_bsize]);
  199. memset(pd->left_context, 0,
  200. sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high_lookup[plane_bsize]);
  201. }
  202. }
  203. static INLINE const vpx_prob *get_y_mode_probs(const MODE_INFO *mi,
  204. const MODE_INFO *above_mi,
  205. const MODE_INFO *left_mi,
  206. int block) {
  207. const PREDICTION_MODE above = vp9_above_block_mode(mi, above_mi, block);
  208. const PREDICTION_MODE left = vp9_left_block_mode(mi, left_mi, block);
  209. return vp9_kf_y_mode_prob[above][left];
  210. }
  211. typedef void (*foreach_transformed_block_visitor)(int plane, int block,
  212. BLOCK_SIZE plane_bsize,
  213. TX_SIZE tx_size,
  214. void *arg);
  215. void vp9_foreach_transformed_block_in_plane(
  216. const MACROBLOCKD *const xd, BLOCK_SIZE bsize, int plane,
  217. foreach_transformed_block_visitor visit, void *arg);
  218. void vp9_foreach_transformed_block(
  219. const MACROBLOCKD* const xd, BLOCK_SIZE bsize,
  220. foreach_transformed_block_visitor visit, void *arg);
  221. static INLINE void txfrm_block_to_raster_xy(BLOCK_SIZE plane_bsize,
  222. TX_SIZE tx_size, int block,
  223. int *x, int *y) {
  224. const int bwl = b_width_log2_lookup[plane_bsize];
  225. const int tx_cols_log2 = bwl - tx_size;
  226. const int tx_cols = 1 << tx_cols_log2;
  227. const int raster_mb = block >> (tx_size << 1);
  228. *x = (raster_mb & (tx_cols - 1)) << tx_size;
  229. *y = (raster_mb >> tx_cols_log2) << tx_size;
  230. }
  231. void vp9_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
  232. BLOCK_SIZE plane_bsize, TX_SIZE tx_size, int has_eob,
  233. int aoff, int loff);
  234. #ifdef __cplusplus
  235. } // extern "C"
  236. #endif
  237. #endif // VP9_COMMON_VP9_BLOCKD_H_