vp9_reconintra.c 10.7 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9
10
 */

11
#include <stdio.h>
12

13
#include "./vpx_config.h"
14
#include "vp9_rtcd.h"
15
#include "vp9/common/vp9_reconintra.h"
16
#include "vp9/common/vp9_onyxc_int.h"
John Koleszar's avatar
John Koleszar committed
17
18
#include "vpx_mem/vpx_mem.h"

19
20
static void d27_predictor(uint8_t *ypred_ptr, int y_stride,
                          int bw, int bh,
21
                          uint8_t *yabove_row, uint8_t *yleft_col) {
22
  int r, c;
23
24
25
26
  // first column
  for (r = 0; r < bh - 1; ++r) {
      ypred_ptr[r * y_stride] = ROUND_POWER_OF_TWO(yleft_col[r] +
                                                   yleft_col[r + 1], 1);
John Koleszar's avatar
John Koleszar committed
27
  }
28
29
30
31
32
33
34
  ypred_ptr[(bh - 1) * y_stride] = yleft_col[bh-1];
  ypred_ptr++;
  // second column
  for (r = 0; r < bh - 2; ++r) {
      ypred_ptr[r * y_stride] = ROUND_POWER_OF_TWO(yleft_col[r] +
                                                   yleft_col[r + 1] * 2 +
                                                   yleft_col[r + 2], 2);
John Koleszar's avatar
John Koleszar committed
35
  }
36
37
38
39
40
  ypred_ptr[(bh - 2) * y_stride] = ROUND_POWER_OF_TWO(yleft_col[bh - 2] +
                                                      yleft_col[bh - 1] * 3,
                                                      2);
  ypred_ptr[(bh - 1) * y_stride] = yleft_col[bh-1];
  ypred_ptr++;
41

42
43
44
  // rest of last row
  for (c = 0; c < bw - 2; ++c) {
    ypred_ptr[(bh - 1) * y_stride + c] = yleft_col[bh-1];
John Koleszar's avatar
John Koleszar committed
45
  }
46

47
48
49
  for (r = bh - 2; r >= 0; --r) {
    for (c = 0; c < bw - 2; ++c) {
      ypred_ptr[r * y_stride + c] = ypred_ptr[(r + 1) * y_stride + c - 2];
John Koleszar's avatar
John Koleszar committed
50
51
    }
  }
52
53
}

54
55
static void d63_predictor(uint8_t *ypred_ptr, int y_stride,
                          int bw, int bh,
56
                          uint8_t *yabove_row, uint8_t *yleft_col) {
57
  int r, c;
58
59
60
61
62
63
64
65
66
67
  for (r = 0; r < bh; ++r) {
    for (c = 0; c < bw; ++c) {
      if (r & 1) {
        ypred_ptr[c] = ROUND_POWER_OF_TWO(yabove_row[r/2 + c] +
                                          yabove_row[r/2 + c + 1] * 2 +
                                          yabove_row[r/2 + c + 2], 2);
      } else {
        ypred_ptr[c] =ROUND_POWER_OF_TWO(yabove_row[r/2 + c] +
                                         yabove_row[r/2+ c + 1], 1);
      }
John Koleszar's avatar
John Koleszar committed
68
    }
69
    ypred_ptr += y_stride;
John Koleszar's avatar
John Koleszar committed
70
  }
71
72
}

73
74
static void d45_predictor(uint8_t *ypred_ptr, int y_stride,
                          int bw, int bh,
75
                          uint8_t *yabove_row, uint8_t *yleft_col) {
John Koleszar's avatar
John Koleszar committed
76
  int r, c;
77
78
79
80
81
82
83
  for (r = 0; r < bh; ++r) {
    for (c = 0; c < bw; ++c) {
      if (r + c + 2 < bw * 2)
        ypred_ptr[c] = ROUND_POWER_OF_TWO(yabove_row[r + c] +
                                          yabove_row[r + c + 1] * 2 +
                                          yabove_row[r + c + 2], 2);
      else
84
        ypred_ptr[c] = yabove_row[bw * 2 - 1];
85
    }
86
    ypred_ptr += y_stride;
John Koleszar's avatar
John Koleszar committed
87
  }
88
89
}

90
91
static void d117_predictor(uint8_t *ypred_ptr, int y_stride,
                           int bw, int bh,
92
                           uint8_t *yabove_row, uint8_t *yleft_col) {
John Koleszar's avatar
John Koleszar committed
93
  int r, c;
94
  // first row
95
  for (c = 0; c < bw; c++)
96
    ypred_ptr[c] = ROUND_POWER_OF_TWO(yabove_row[c - 1] + yabove_row[c], 1);
John Koleszar's avatar
John Koleszar committed
97
  ypred_ptr += y_stride;
98
99
100
101
102
103
104
105
106

  // second row
  ypred_ptr[0] = ROUND_POWER_OF_TWO(yleft_col[0] +
                                    yabove_row[-1] * 2 +
                                    yabove_row[0], 2);
  for (c = 1; c < bw; c++)
    ypred_ptr[c] = ROUND_POWER_OF_TWO(yabove_row[c - 2] +
                                      yabove_row[c - 1] * 2 +
                                      yabove_row[c], 2);
John Koleszar's avatar
John Koleszar committed
107
  ypred_ptr += y_stride;
108
109
110
111
112
113
114
115
116
117

  // the rest of first col
  ypred_ptr[0] = ROUND_POWER_OF_TWO(yabove_row[-1] +
                                    yleft_col[0] * 2 +
                                    yleft_col[1], 2);
  for (r = 3; r < bh; ++r)
    ypred_ptr[(r-2) * y_stride] = ROUND_POWER_OF_TWO(yleft_col[r - 3] +
                                                     yleft_col[r - 2] * 2 +
                                                     yleft_col[r - 1], 2);
  // the rest of the block
118
119
  for (r = 2; r < bh; ++r) {
    for (c = 1; c < bw; c++)
John Koleszar's avatar
John Koleszar committed
120
      ypred_ptr[c] = ypred_ptr[-2 * y_stride + c - 1];
121
    ypred_ptr += y_stride;
John Koleszar's avatar
John Koleszar committed
122
  }
123
124
}

125

126
127
static void d135_predictor(uint8_t *ypred_ptr, int y_stride,
                           int bw, int bh,
128
                           uint8_t *yabove_row, uint8_t *yleft_col) {
John Koleszar's avatar
John Koleszar committed
129
  int r, c;
130
131
132
  ypred_ptr[0] = ROUND_POWER_OF_TWO(yleft_col[0] +
                                    yabove_row[-1] * 2 +
                                    yabove_row[0], 2);
133
  for (c = 1; c < bw; c++)
134
135
136
137
138
139
140
    ypred_ptr[c] = ROUND_POWER_OF_TWO(yabove_row[c - 2] +
                                      yabove_row[c - 1] * 2 +
                                      yabove_row[c], 2);

  ypred_ptr[y_stride] = ROUND_POWER_OF_TWO(yabove_row[-1] +
                                           yleft_col[0] * 2 +
                                           yleft_col[1], 2);
141
  for (r = 2; r < bh; ++r)
142
143
    ypred_ptr[r * y_stride] = ROUND_POWER_OF_TWO(yleft_col[r - 2] +
                                                 yleft_col[r - 1] * 2 +
144
                                                 yleft_col[r], 2);
John Koleszar's avatar
John Koleszar committed
145
146

  ypred_ptr += y_stride;
147
  for (r = 1; r < bh; ++r) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
148
    for (c = 1; c < bw; c++)
John Koleszar's avatar
John Koleszar committed
149
150
151
      ypred_ptr[c] = ypred_ptr[-y_stride + c - 1];
    ypred_ptr += y_stride;
  }
152
153
}

154
155
static void d153_predictor(uint8_t *ypred_ptr,
                           int y_stride,
156
                           int bw, int bh,
157
158
                           uint8_t *yabove_row,
                           uint8_t *yleft_col) {
John Koleszar's avatar
John Koleszar committed
159
  int r, c;
160
  ypred_ptr[0] = ROUND_POWER_OF_TWO(yabove_row[-1] + yleft_col[0], 1);
161
  for (r = 1; r < bh; r++)
162
163
    ypred_ptr[r * y_stride] =
        ROUND_POWER_OF_TWO(yleft_col[r - 1] + yleft_col[r], 1);
John Koleszar's avatar
John Koleszar committed
164
  ypred_ptr++;
165
166
167
168
169
170
171
172
173
174
175

  ypred_ptr[0] = ROUND_POWER_OF_TWO(yleft_col[0] +
                                    yabove_row[-1] * 2 +
                                    yabove_row[0], 2);
  ypred_ptr[y_stride] = ROUND_POWER_OF_TWO(yabove_row[-1] +
                                           yleft_col[0] * 2 +
                                           yleft_col[1], 2);
  for (r = 2; r < bh; r++)
    ypred_ptr[r * y_stride] = ROUND_POWER_OF_TWO(yleft_col[r - 2] +
                                                 yleft_col[r - 1] * 2 +
                                                 yleft_col[r], 2);
John Koleszar's avatar
John Koleszar committed
176
177
  ypred_ptr++;

178
  for (c = 0; c < bw - 2; c++)
179
180
181
    ypred_ptr[c] = ROUND_POWER_OF_TWO(yabove_row[c - 1] +
                                      yabove_row[c] * 2 +
                                      yabove_row[c + 1], 2);
John Koleszar's avatar
John Koleszar committed
182
  ypred_ptr += y_stride;
183
184
  for (r = 1; r < bh; ++r) {
    for (c = 0; c < bw - 2; c++)
John Koleszar's avatar
John Koleszar committed
185
      ypred_ptr[c] = ypred_ptr[-y_stride + c - 2];
186
    ypred_ptr += y_stride;
John Koleszar's avatar
John Koleszar committed
187
  }
188
189
}

190
191
192
193
194
195
void vp9_build_intra_predictors(uint8_t *src, int src_stride,
                                uint8_t *ypred_ptr,
                                int y_stride, int mode,
                                int bw, int bh,
                                int up_available, int left_available,
                                int right_available) {
John Koleszar's avatar
John Koleszar committed
196
  int r, c, i;
197
  uint8_t yleft_col[64], yabove_data[129], ytop_left;
198
  uint8_t *yabove_row = yabove_data + 1;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
199
200
201
202
203
204
205

  // 127 127 127 .. 127 127 127 127 127 127
  // 129  A   B  ..  Y   Z
  // 129  C   D  ..  W   X
  // 129  E   F  ..  U   V
  // 129  G   H  ..  S   T   T   T   T   T
  // ..
John Koleszar's avatar
John Koleszar committed
206

207
208
  assert(bw == bh);

209
  if (left_available) {
210
    for (i = 0; i < bh; i++)
211
212
      yleft_col[i] = src[i * src_stride - 1];
  } else {
213
    vpx_memset(yleft_col, 129, bh);
214
215
216
217
  }

  if (up_available) {
    uint8_t *yabove_ptr = src - src_stride;
218
    vpx_memcpy(yabove_row, yabove_ptr, bw);
219
220
221
222
    if (bw == 4 && right_available)
      vpx_memcpy(yabove_row + bw, yabove_ptr + bw, bw);
    else
      vpx_memset(yabove_row + bw, yabove_row[bw -1], bw);
223
    ytop_left = left_available ? yabove_ptr[-1] : 129;
224
  } else {
225
    vpx_memset(yabove_row, 127, bw * 2);
226
    ytop_left = 127;
John Koleszar's avatar
John Koleszar committed
227
  }
228
  yabove_row[-1] = ytop_left;
John Koleszar's avatar
John Koleszar committed
229
230
231
232

  switch (mode) {
    case DC_PRED: {
      int i;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
233
      int expected_dc = 128;
John Koleszar's avatar
John Koleszar committed
234
      int average = 0;
235
      int count = 0;
John Koleszar's avatar
John Koleszar committed
236

237
238
      if (up_available || left_available) {
        if (up_available) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
239
          for (i = 0; i < bw; i++)
John Koleszar's avatar
John Koleszar committed
240
            average += yabove_row[i];
241
          count += bw;
John Koleszar's avatar
John Koleszar committed
242
        }
243
        if (left_available) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
244
          for (i = 0; i < bh; i++)
John Koleszar's avatar
John Koleszar committed
245
            average += yleft_col[i];
246
          count += bh;
John Koleszar's avatar
John Koleszar committed
247
        }
248
        expected_dc = (average + (count >> 1)) / count;
John Koleszar's avatar
John Koleszar committed
249
      }
250
251
      for (r = 0; r < bh; r++) {
        vpx_memset(ypred_ptr, expected_dc, bw);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
252
        ypred_ptr += y_stride;
John Koleszar's avatar
John Koleszar committed
253
      }
John Koleszar's avatar
John Koleszar committed
254
255
    }
    break;
256
    case V_PRED:
257
      for (r = 0; r < bh; r++) {
258
        vpx_memcpy(ypred_ptr, yabove_row, bw);
John Koleszar's avatar
John Koleszar committed
259
260
        ypred_ptr += y_stride;
      }
261
262
      break;
    case H_PRED:
263
264
      for (r = 0; r < bh; r++) {
        vpx_memset(ypred_ptr, yleft_col[r], bw);
John Koleszar's avatar
John Koleszar committed
265
266
        ypred_ptr += y_stride;
      }
267
268
      break;
    case TM_PRED:
269
      for (r = 0; r < bh; r++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
270
        for (c = 0; c < bw; c++)
271
          ypred_ptr[c] = clip_pixel(yleft_col[r] + yabove_row[c] - ytop_left);
John Koleszar's avatar
John Koleszar committed
272
273
        ypred_ptr += y_stride;
      }
274
275
      break;
    case D45_PRED:
276
277
      d45_predictor(ypred_ptr, y_stride, bw, bh, yabove_row, yleft_col);
      break;
278
    case D135_PRED:
279
280
      d135_predictor(ypred_ptr, y_stride, bw, bh, yabove_row, yleft_col);
      break;
281
    case D117_PRED:
282
283
      d117_predictor(ypred_ptr, y_stride, bw, bh, yabove_row, yleft_col);
      break;
284
    case D153_PRED:
285
286
      d153_predictor(ypred_ptr, y_stride, bw, bh, yabove_row, yleft_col);
      break;
287
    case D27_PRED:
288
289
      d27_predictor(ypred_ptr, y_stride, bw, bh, yabove_row, yleft_col);
      break;
290
    case D63_PRED:
291
      d63_predictor(ypred_ptr, y_stride, bw, bh, yabove_row, yleft_col);
292
      break;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
293
    default:
John Koleszar's avatar
John Koleszar committed
294
295
      break;
  }
John Koleszar's avatar
John Koleszar committed
296
297
}

298
299
void vp9_predict_intra_block(MACROBLOCKD *xd,
                            int block_idx,
300
                            int bwl_in,
301
302
303
                            TX_SIZE tx_size,
                            int mode,
                            uint8_t *predictor, int pre_stride) {
304
  const int bwl = bwl_in - tx_size;
305
  const int wmask = (1 << bwl) - 1;
306
307
308
  const int have_top = (block_idx >> bwl) || xd->up_available;
  const int have_left = (block_idx & wmask) || xd->left_available;
  const int have_right = ((block_idx & wmask) != wmask);
309
  const int txfm_block_size = 4 << tx_size;
Yaowu Xu's avatar
Yaowu Xu committed
310

311
  assert(bwl >= 0);
312
313
  vp9_build_intra_predictors(predictor, pre_stride,
                             predictor, pre_stride,
314
315
316
317
                             mode,
                             txfm_block_size,
                             txfm_block_size,
                             have_top, have_left,
318
319
                             have_right);
}