vp9_blockd.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2014 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. #include "vp9/common/vp9_blockd.h"
  11. PREDICTION_MODE vp9_left_block_mode(const MODE_INFO *cur_mi,
  12. const MODE_INFO *left_mi, int b) {
  13. if (b == 0 || b == 2) {
  14. if (!left_mi || is_inter_block(left_mi)) return DC_PRED;
  15. return get_y_mode(left_mi, b + 1);
  16. } else {
  17. assert(b == 1 || b == 3);
  18. return cur_mi->bmi[b - 1].as_mode;
  19. }
  20. }
  21. PREDICTION_MODE vp9_above_block_mode(const MODE_INFO *cur_mi,
  22. const MODE_INFO *above_mi, int b) {
  23. if (b == 0 || b == 1) {
  24. if (!above_mi || is_inter_block(above_mi)) return DC_PRED;
  25. return get_y_mode(above_mi, b + 2);
  26. } else {
  27. assert(b == 2 || b == 3);
  28. return cur_mi->bmi[b - 2].as_mode;
  29. }
  30. }
  31. void vp9_foreach_transformed_block_in_plane(
  32. const MACROBLOCKD *const xd, BLOCK_SIZE bsize, int plane,
  33. foreach_transformed_block_visitor visit, void *arg) {
  34. const struct macroblockd_plane *const pd = &xd->plane[plane];
  35. const MODE_INFO *mi = xd->mi[0];
  36. // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
  37. // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
  38. // transform size varies per plane, look it up in a common way.
  39. const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
  40. const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
  41. const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
  42. const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
  43. const int step = 1 << (tx_size << 1);
  44. int i = 0, r, c;
  45. // If mb_to_right_edge is < 0 we are in a situation in which
  46. // the current block size extends into the UMV and we won't
  47. // visit the sub blocks that are wholly within the UMV.
  48. const int max_blocks_wide =
  49. num_4x4_w + (xd->mb_to_right_edge >= 0
  50. ? 0
  51. : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
  52. const int max_blocks_high =
  53. num_4x4_h + (xd->mb_to_bottom_edge >= 0
  54. ? 0
  55. : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
  56. const int extra_step = ((num_4x4_w - max_blocks_wide) >> tx_size) * step;
  57. // Keep track of the row and column of the blocks we use so that we know
  58. // if we are in the unrestricted motion border.
  59. for (r = 0; r < max_blocks_high; r += (1 << tx_size)) {
  60. // Skip visiting the sub blocks that are wholly within the UMV.
  61. for (c = 0; c < max_blocks_wide; c += (1 << tx_size)) {
  62. visit(plane, i, r, c, plane_bsize, tx_size, arg);
  63. i += step;
  64. }
  65. i += extra_step;
  66. }
  67. }
  68. void vp9_foreach_transformed_block(const MACROBLOCKD *const xd,
  69. BLOCK_SIZE bsize,
  70. foreach_transformed_block_visitor visit,
  71. void *arg) {
  72. int plane;
  73. for (plane = 0; plane < MAX_MB_PLANE; ++plane)
  74. vp9_foreach_transformed_block_in_plane(xd, bsize, plane, visit, arg);
  75. }
  76. void vp9_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
  77. BLOCK_SIZE plane_bsize, TX_SIZE tx_size, int has_eob,
  78. int aoff, int loff) {
  79. ENTROPY_CONTEXT *const a = pd->above_context + aoff;
  80. ENTROPY_CONTEXT *const l = pd->left_context + loff;
  81. const int tx_size_in_blocks = 1 << tx_size;
  82. // above
  83. if (has_eob && xd->mb_to_right_edge < 0) {
  84. int i;
  85. const int blocks_wide = num_4x4_blocks_wide_lookup[plane_bsize] +
  86. (xd->mb_to_right_edge >> (5 + pd->subsampling_x));
  87. int above_contexts = tx_size_in_blocks;
  88. if (above_contexts + aoff > blocks_wide)
  89. above_contexts = blocks_wide - aoff;
  90. for (i = 0; i < above_contexts; ++i) a[i] = has_eob;
  91. for (i = above_contexts; i < tx_size_in_blocks; ++i) a[i] = 0;
  92. } else {
  93. memset(a, has_eob, sizeof(ENTROPY_CONTEXT) * tx_size_in_blocks);
  94. }
  95. // left
  96. if (has_eob && xd->mb_to_bottom_edge < 0) {
  97. int i;
  98. const int blocks_high = num_4x4_blocks_high_lookup[plane_bsize] +
  99. (xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
  100. int left_contexts = tx_size_in_blocks;
  101. if (left_contexts + loff > blocks_high) left_contexts = blocks_high - loff;
  102. for (i = 0; i < left_contexts; ++i) l[i] = has_eob;
  103. for (i = left_contexts; i < tx_size_in_blocks; ++i) l[i] = 0;
  104. } else {
  105. memset(l, has_eob, sizeof(ENTROPY_CONTEXT) * tx_size_in_blocks);
  106. }
  107. }
  108. void vp9_setup_block_planes(MACROBLOCKD *xd, int ss_x, int ss_y) {
  109. int i;
  110. for (i = 0; i < MAX_MB_PLANE; i++) {
  111. xd->plane[i].subsampling_x = i ? ss_x : 0;
  112. xd->plane[i].subsampling_y = i ? ss_y : 0;
  113. }
  114. }