2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
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.
11 #include "vp9/common/vp9_blockd.h"
13 PREDICTION_MODE vp9_left_block_mode(const MODE_INFO *cur_mi,
14 const MODE_INFO *left_mi, int b) {
15 if (b == 0 || b == 2) {
16 if (!left_mi || is_inter_block(&left_mi->mbmi))
19 return get_y_mode(left_mi, b + 1);
21 assert(b == 1 || b == 3);
22 return cur_mi->bmi[b - 1].as_mode;
26 PREDICTION_MODE vp9_above_block_mode(const MODE_INFO *cur_mi,
27 const MODE_INFO *above_mi, int b) {
28 if (b == 0 || b == 1) {
29 if (!above_mi || is_inter_block(&above_mi->mbmi))
32 return get_y_mode(above_mi, b + 2);
34 assert(b == 2 || b == 3);
35 return cur_mi->bmi[b - 2].as_mode;
39 void vp9_foreach_transformed_block_in_plane(
40 const MACROBLOCKD *const xd, BLOCK_SIZE bsize, int plane,
41 foreach_transformed_block_visitor visit, void *arg) {
42 const struct macroblockd_plane *const pd = &xd->plane[plane];
43 const MB_MODE_INFO* mbmi = &xd->mi[0].src_mi->mbmi;
44 // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
45 // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
46 // transform size varies per plane, look it up in a common way.
47 const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd)
49 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
50 const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
51 const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
52 const int step = 1 << (tx_size << 1);
55 // If mb_to_right_edge is < 0 we are in a situation in which
56 // the current block size extends into the UMV and we won't
57 // visit the sub blocks that are wholly within the UMV.
58 if (xd->mb_to_right_edge < 0 || xd->mb_to_bottom_edge < 0) {
61 int max_blocks_wide = num_4x4_w;
62 int max_blocks_high = num_4x4_h;
64 // xd->mb_to_right_edge is in units of pixels * 8. This converts
65 // it to 4x4 block sizes.
66 if (xd->mb_to_right_edge < 0)
67 max_blocks_wide += (xd->mb_to_right_edge >> (5 + pd->subsampling_x));
69 if (xd->mb_to_bottom_edge < 0)
70 max_blocks_high += (xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
73 // Unlike the normal case - in here we have to keep track of the
74 // row and column of the blocks we use so that we know if we are in
75 // the unrestricted motion border.
76 for (r = 0; r < num_4x4_h; r += (1 << tx_size)) {
77 for (c = 0; c < num_4x4_w; c += (1 << tx_size)) {
78 if (r < max_blocks_high && c < max_blocks_wide)
79 visit(plane, i, plane_bsize, tx_size, arg);
84 for (i = 0; i < num_4x4_w * num_4x4_h; i += step)
85 visit(plane, i, plane_bsize, tx_size, arg);
89 void vp9_foreach_transformed_block(const MACROBLOCKD* const xd,
91 foreach_transformed_block_visitor visit,
95 for (plane = 0; plane < MAX_MB_PLANE; plane++)
96 vp9_foreach_transformed_block_in_plane(xd, bsize, plane, visit, arg);
99 void vp9_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
100 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, int has_eob,
101 int aoff, int loff) {
102 ENTROPY_CONTEXT *const a = pd->above_context + aoff;
103 ENTROPY_CONTEXT *const l = pd->left_context + loff;
104 const int tx_size_in_blocks = 1 << tx_size;
107 if (has_eob && xd->mb_to_right_edge < 0) {
109 const int blocks_wide = num_4x4_blocks_wide_lookup[plane_bsize] +
110 (xd->mb_to_right_edge >> (5 + pd->subsampling_x));
111 int above_contexts = tx_size_in_blocks;
112 if (above_contexts + aoff > blocks_wide)
113 above_contexts = blocks_wide - aoff;
115 for (i = 0; i < above_contexts; ++i)
117 for (i = above_contexts; i < tx_size_in_blocks; ++i)
120 vpx_memset(a, has_eob, sizeof(ENTROPY_CONTEXT) * tx_size_in_blocks);
124 if (has_eob && xd->mb_to_bottom_edge < 0) {
126 const int blocks_high = num_4x4_blocks_high_lookup[plane_bsize] +
127 (xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
128 int left_contexts = tx_size_in_blocks;
129 if (left_contexts + loff > blocks_high)
130 left_contexts = blocks_high - loff;
132 for (i = 0; i < left_contexts; ++i)
134 for (i = left_contexts; i < tx_size_in_blocks; ++i)
137 vpx_memset(l, has_eob, sizeof(ENTROPY_CONTEXT) * tx_size_in_blocks);
141 void vp9_setup_block_planes(MACROBLOCKD *xd, int ss_x, int ss_y) {
144 for (i = 0; i < MAX_MB_PLANE; i++) {
145 xd->plane[i].plane_type = i ? PLANE_TYPE_UV : PLANE_TYPE_Y;
146 xd->plane[i].subsampling_x = i ? ss_x : 0;
147 xd->plane[i].subsampling_y = i ? ss_y : 0;