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
11#include "vp9/common/vp9_blockd.h"
12
13MB_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))
17      return DC_PRED;
18
19    return get_y_mode(left_mi, b + 1);
20  } else {
21    assert(b == 1 || b == 3);
22    return cur_mi->bmi[b - 1].as_mode;
23  }
24}
25
26MB_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))
30      return DC_PRED;
31
32    return get_y_mode(above_mi, b + 2);
33  } else {
34    assert(b == 2 || b == 3);
35    return cur_mi->bmi[b - 2].as_mode;
36  }
37}
38
39void 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]->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)
48                                : mbmi->tx_size;
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);
53  int i;
54
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) {
59    int r, c;
60
61    int max_blocks_wide = num_4x4_w;
62    int max_blocks_high = num_4x4_h;
63
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));
68
69    if (xd->mb_to_bottom_edge < 0)
70      max_blocks_high += (xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
71
72    i = 0;
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);
80        i += step;
81      }
82    }
83  } else {
84    for (i = 0; i < num_4x4_w * num_4x4_h; i += step)
85      visit(plane, i, plane_bsize, tx_size, arg);
86  }
87}
88
89void vp9_foreach_transformed_block(const MACROBLOCKD* const xd,
90                                   BLOCK_SIZE bsize,
91                                   foreach_transformed_block_visitor visit,
92                                   void *arg) {
93  int plane;
94
95  for (plane = 0; plane < MAX_MB_PLANE; plane++)
96    vp9_foreach_transformed_block_in_plane(xd, bsize, plane, visit, arg);
97}
98
99void 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;
105
106  // above
107  if (has_eob && xd->mb_to_right_edge < 0) {
108    int i;
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;
114
115    for (i = 0; i < above_contexts; ++i)
116      a[i] = has_eob;
117    for (i = above_contexts; i < tx_size_in_blocks; ++i)
118      a[i] = 0;
119  } else {
120    vpx_memset(a, has_eob, sizeof(ENTROPY_CONTEXT) * tx_size_in_blocks);
121  }
122
123  // left
124  if (has_eob && xd->mb_to_bottom_edge < 0) {
125    int i;
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;
131
132    for (i = 0; i < left_contexts; ++i)
133      l[i] = has_eob;
134    for (i = left_contexts; i < tx_size_in_blocks; ++i)
135      l[i] = 0;
136  } else {
137    vpx_memset(l, has_eob, sizeof(ENTROPY_CONTEXT) * tx_size_in_blocks);
138  }
139}
140
141void vp9_setup_block_planes(MACROBLOCKD *xd, int ss_x, int ss_y) {
142  int i;
143
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;
148  }
149#if CONFIG_ALPHA
150  // TODO(jkoleszar): Using the Y w/h for now
151  xd->plane[3].plane_type = PLANE_TYPE_Y;
152  xd->plane[3].subsampling_x = 0;
153  xd->plane[3].subsampling_y = 0;
154#endif
155}
156