vp9_blockd.c 4.9 KB

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