vp9_reconintra.c 11.9 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 "./vpx_config.h"
12
13
14
15

#include "vpx_mem/vpx_mem.h"
#include "vpx_ports/vpx_once.h"

16
#include "vp9_rtcd.h"
17

18
#include "vp9/common/vp9_reconintra.h"
19
#include "vp9/common/vp9_onyxc_int.h"
John Koleszar's avatar
John Koleszar committed
20

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const TX_TYPE mode2txfm_map[MB_MODE_COUNT] = {
    DCT_DCT,    // DC
    ADST_DCT,   // V
    DCT_ADST,   // H
    DCT_DCT,    // D45
    ADST_ADST,  // D135
    ADST_DCT,   // D117
    DCT_ADST,   // D153
    DCT_ADST,   // D27
    ADST_DCT,   // D63
    ADST_ADST,  // TM
    DCT_DCT,    // NEARESTMV
    DCT_DCT,    // NEARMV
    DCT_DCT,    // ZEROMV
    DCT_DCT     // NEWMV
};

38
#define intra_pred_sized(type, size) \
39
40
41
42
43
44
45
  void vp9_##type##_predictor_##size##x##size##_c(uint8_t *dst, \
                                                  ptrdiff_t stride, \
                                                  const uint8_t *above, \
                                                  const uint8_t *left) { \
    type##_predictor(dst, stride, size, above, left); \
  }

46
47
48
49
50
51
#define intra_pred_allsizes(type) \
  intra_pred_sized(type, 4) \
  intra_pred_sized(type, 8) \
  intra_pred_sized(type, 16) \
  intra_pred_sized(type, 32)

52
53
static INLINE void d27_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                 const uint8_t *above, const uint8_t *left) {
54
  int r, c;
55

56
  // first column
Dmitry Kovalev's avatar
Dmitry Kovalev committed
57
  for (r = 0; r < bs - 1; ++r)
58
59
60
    dst[r * stride] = ROUND_POWER_OF_TWO(left[r] + left[r + 1], 1);
  dst[(bs - 1) * stride] = left[bs - 1];
  dst++;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
61

62
  // second column
Dmitry Kovalev's avatar
Dmitry Kovalev committed
63
  for (r = 0; r < bs - 2; ++r)
64
65
66
67
68
69
    dst[r * stride] = ROUND_POWER_OF_TWO(left[r] + left[r + 1] * 2 +
                                         left[r + 2], 2);
  dst[(bs - 2) * stride] = ROUND_POWER_OF_TWO(left[bs - 2] +
                                              left[bs - 1] * 3, 2);
  dst[(bs - 1) * stride] = left[bs - 1];
  dst++;
70

71
  // rest of last row
Dmitry Kovalev's avatar
Dmitry Kovalev committed
72
  for (c = 0; c < bs - 2; ++c)
73
    dst[(bs - 1) * stride + c] = left[bs - 1];
74

Dmitry Kovalev's avatar
Dmitry Kovalev committed
75
76
  for (r = bs - 2; r >= 0; --r)
    for (c = 0; c < bs - 2; ++c)
77
      dst[r * stride + c] = dst[(r + 1) * stride + c - 2];
78
}
79
intra_pred_allsizes(d27)
80

81
82
static INLINE void d63_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                 const uint8_t *above, const uint8_t *left) {
83
  int r, c;
84
  for (r = 0; r < bs; ++r) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
85
    for (c = 0; c < bs; ++c)
86
87
88
89
90
91
      dst[c] = r & 1 ? ROUND_POWER_OF_TWO(above[r/2 + c] +
                                          above[r/2 + c + 1] * 2 +
                                          above[r/2 + c + 2], 2)
                     : ROUND_POWER_OF_TWO(above[r/2 + c] +
                                          above[r/2 + c + 1], 1);
    dst += stride;
John Koleszar's avatar
John Koleszar committed
92
  }
93
}
94
intra_pred_allsizes(d63)
95

96
97
static INLINE void d45_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                 const uint8_t *above, const uint8_t *left) {
John Koleszar's avatar
John Koleszar committed
98
  int r, c;
99
  for (r = 0; r < bs; ++r) {
100
101
102
103
104
105
    for (c = 0; c < bs; ++c)
      dst[c] = r + c + 2 < bs * 2 ?  ROUND_POWER_OF_TWO(above[r + c] +
                                                        above[r + c + 1] * 2 +
                                                        above[r + c + 2], 2)
                                  : above[bs * 2 - 1];
    dst += stride;
John Koleszar's avatar
John Koleszar committed
106
  }
107
}
108
intra_pred_allsizes(d45)
109

110
111
static INLINE void d117_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                  const uint8_t *above, const uint8_t *left) {
John Koleszar's avatar
John Koleszar committed
112
  int r, c;
113

114
  // first row
115
  for (c = 0; c < bs; c++)
116
117
    dst[c] = ROUND_POWER_OF_TWO(above[c - 1] + above[c], 1);
  dst += stride;
118
119

  // second row
120
  dst[0] = ROUND_POWER_OF_TWO(left[0] + above[-1] * 2 + above[0], 2);
121
  for (c = 1; c < bs; c++)
122
123
    dst[c] = ROUND_POWER_OF_TWO(above[c - 2] + above[c - 1] * 2 + above[c], 2);
  dst += stride;
124
125

  // the rest of first col
126
  dst[0] = ROUND_POWER_OF_TWO(above[-1] + left[0] * 2 + left[1], 2);
127
  for (r = 3; r < bs; ++r)
128
129
130
    dst[(r - 2) * stride] = ROUND_POWER_OF_TWO(left[r - 3] + left[r - 2] * 2 +
                                               left[r - 1], 2);

131
  // the rest of the block
132
133
  for (r = 2; r < bs; ++r) {
    for (c = 1; c < bs; c++)
134
135
      dst[c] = dst[-2 * stride + c - 1];
    dst += stride;
John Koleszar's avatar
John Koleszar committed
136
  }
137
}
138
intra_pred_allsizes(d117)
139

140
141
static INLINE void d135_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                  const uint8_t *above, const uint8_t *left) {
John Koleszar's avatar
John Koleszar committed
142
  int r, c;
143
  dst[0] = ROUND_POWER_OF_TWO(left[0] + above[-1] * 2 + above[0], 2);
144
  for (c = 1; c < bs; c++)
145
    dst[c] = ROUND_POWER_OF_TWO(above[c - 2] + above[c - 1] * 2 + above[c], 2);
146

147
  dst[stride] = ROUND_POWER_OF_TWO(above[-1] + left[0] * 2 + left[1], 2);
148
  for (r = 2; r < bs; ++r)
149
150
    dst[r * stride] = ROUND_POWER_OF_TWO(left[r - 2] + left[r - 1] * 2 +
                                         left[r], 2);
John Koleszar's avatar
John Koleszar committed
151

152
  dst += stride;
153
154
  for (r = 1; r < bs; ++r) {
    for (c = 1; c < bs; c++)
155
156
      dst[c] = dst[-stride + c - 1];
    dst += stride;
John Koleszar's avatar
John Koleszar committed
157
  }
158
}
159
intra_pred_allsizes(d135)
160

161
162
static INLINE void d153_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                  const uint8_t *above, const uint8_t *left) {
John Koleszar's avatar
John Koleszar committed
163
  int r, c;
164
  dst[0] = ROUND_POWER_OF_TWO(above[-1] + left[0], 1);
165
  for (r = 1; r < bs; r++)
166
167
168
169
170
    dst[r * stride] = ROUND_POWER_OF_TWO(left[r - 1] + left[r], 1);
  dst++;

  dst[0] = ROUND_POWER_OF_TWO(left[0] + above[-1] * 2 + above[0], 2);
  dst[stride] = ROUND_POWER_OF_TWO(above[-1] + left[0] * 2 + left[1], 2);
171
  for (r = 2; r < bs; r++)
172
173
174
    dst[r * stride] = ROUND_POWER_OF_TWO(left[r - 2] + left[r - 1] * 2 +
                                         left[r], 2);
  dst++;
John Koleszar's avatar
John Koleszar committed
175

176
  for (c = 0; c < bs - 2; c++)
177
178
179
    dst[c] = ROUND_POWER_OF_TWO(above[c - 1] + above[c] * 2 + above[c + 1], 2);
  dst += stride;

180
181
  for (r = 1; r < bs; ++r) {
    for (c = 0; c < bs - 2; c++)
182
183
      dst[c] = dst[-stride + c - 2];
    dst += stride;
John Koleszar's avatar
John Koleszar committed
184
  }
185
}
186
intra_pred_allsizes(d153)
187

188
189
static INLINE void v_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                               const uint8_t *above, const uint8_t *left) {
190
191
192
  int r;

  for (r = 0; r < bs; r++) {
193
194
    vpx_memcpy(dst, above, bs);
    dst += stride;
195
196
  }
}
197
intra_pred_allsizes(v)
198

199
200
static INLINE void h_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                               const uint8_t *above, const uint8_t *left) {
201
202
203
  int r;

  for (r = 0; r < bs; r++) {
204
205
    vpx_memset(dst, left[r], bs);
    dst += stride;
206
207
  }
}
208
intra_pred_allsizes(h)
209

210
211
static INLINE void tm_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                const uint8_t *above, const uint8_t *left) {
212
  int r, c;
213
  int ytop_left = above[-1];
214
215
216

  for (r = 0; r < bs; r++) {
    for (c = 0; c < bs; c++)
217
218
      dst[c] = clip_pixel(left[r] + above[c] - ytop_left);
    dst += stride;
219
220
  }
}
221
intra_pred_allsizes(tm)
222

223
224
static INLINE void dc_128_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                    const uint8_t *above, const uint8_t *left) {
225
226
227
  int r;

  for (r = 0; r < bs; r++) {
228
229
    vpx_memset(dst, 128, bs);
    dst += stride;
230
231
  }
}
232
intra_pred_allsizes(dc_128)
233

234
235
236
237
static INLINE void dc_left_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                     const uint8_t *above,
                                     const uint8_t *left) {
  int i, r, expected_dc, sum = 0;
238
239

  for (i = 0; i < bs; i++)
240
241
    sum += left[i];
  expected_dc = (sum + (bs >> 1)) / bs;
242
243

  for (r = 0; r < bs; r++) {
244
245
    vpx_memset(dst, expected_dc, bs);
    dst += stride;
246
247
  }
}
248
intra_pred_allsizes(dc_left)
249

250
251
252
static INLINE void dc_top_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                    const uint8_t *above, const uint8_t *left) {
  int i, r, expected_dc, sum = 0;
253
254

  for (i = 0; i < bs; i++)
255
256
    sum += above[i];
  expected_dc = (sum + (bs >> 1)) / bs;
257
258

  for (r = 0; r < bs; r++) {
259
260
    vpx_memset(dst, expected_dc, bs);
    dst += stride;
261
262
  }
}
263
intra_pred_allsizes(dc_top)
264

265
266
267
static INLINE void dc_predictor(uint8_t *dst, ptrdiff_t stride, int bs,
                                const uint8_t *above, const uint8_t *left) {
  int i, r, expected_dc, sum = 0;
268
269
  const int count = 2 * bs;

270
271
272
273
274
275
  for (i = 0; i < bs; i++) {
    sum += above[i];
    sum += left[i];
  }

  expected_dc = (sum + (count >> 1)) / count;
276
277

  for (r = 0; r < bs; r++) {
278
279
    vpx_memset(dst, expected_dc, bs);
    dst += stride;
280
281
  }
}
282
283
284
intra_pred_allsizes(dc)
#undef intra_pred_allsizes

285
286
typedef void (*intra_pred_fn)(uint8_t *dst, ptrdiff_t stride,
                              const uint8_t *above, const uint8_t *left);
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

static intra_pred_fn pred[VP9_INTRA_MODES][4];
static intra_pred_fn dc_pred[2][2][4];

static void init_intra_pred_fn_ptrs(void) {
#define intra_pred_allsizes(l, type) \
  l[0] = vp9_##type##_predictor_4x4; \
  l[1] = vp9_##type##_predictor_8x8; \
  l[2] = vp9_##type##_predictor_16x16; \
  l[3] = vp9_##type##_predictor_32x32

  intra_pred_allsizes(pred[V_PRED], v);
  intra_pred_allsizes(pred[H_PRED], h);
  intra_pred_allsizes(pred[D27_PRED], d27);
  intra_pred_allsizes(pred[D45_PRED], d45);
  intra_pred_allsizes(pred[D63_PRED], d63);
  intra_pred_allsizes(pred[D117_PRED], d117);
  intra_pred_allsizes(pred[D135_PRED], d135);
  intra_pred_allsizes(pred[D153_PRED], d153);
  intra_pred_allsizes(pred[TM_PRED], tm);

  intra_pred_allsizes(dc_pred[0][0], dc_128);
  intra_pred_allsizes(dc_pred[0][1], dc_top);
  intra_pred_allsizes(dc_pred[1][0], dc_left);
  intra_pred_allsizes(dc_pred[1][1], dc);

#undef intra_pred_allsizes
}
315

316
317
318
static void build_intra_predictors(const uint8_t *ref, int ref_stride,
                                   uint8_t *dst, int dst_stride,
                                   MB_PREDICTION_MODE mode, TX_SIZE tx_size,
319
320
321
                                   int up_available, int left_available,
                                   int right_available) {
  int i;
322
  DECLARE_ALIGNED_ARRAY(16, uint8_t, left_col, 64);
323
324
325
326
  DECLARE_ALIGNED_ARRAY(16, uint8_t, above_data, 128 + 16);
  uint8_t *above_row = above_data + 16;
  const uint8_t *const_above_row = above_row;
  const int bs = 4 << tx_size;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
327
328
329
330
331
332
333

  // 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
334

335
  once(init_intra_pred_fn_ptrs);
336
337

  // left
338
  if (left_available) {
339
    for (i = 0; i < bs; i++)
340
      left_col[i] = ref[i * ref_stride - 1];
341
  } else {
342
    vpx_memset(left_col, 129, bs);
343
344
  }

345
  // above
346
  if (up_available) {
347
    const uint8_t *above_ref = ref - ref_stride;
348
    if (bs == 4 && right_available && left_available) {
349
      const_above_row = above_ref;
350
    } else {
351
      vpx_memcpy(above_row, above_ref, bs);
352
      if (bs == 4 && right_available)
353
        vpx_memcpy(above_row + bs, above_ref + bs, bs);
354
      else
355
        vpx_memset(above_row + bs, above_row[bs - 1], bs);
356
      above_row[-1] = left_available ? above_ref[-1] : 129;
357
    }
358
  } else {
359
360
    vpx_memset(above_row, 127, bs * 2);
    above_row[-1] = 127;
John Koleszar's avatar
John Koleszar committed
361
  }
362

363
  // predict
364
  if (mode == DC_PRED) {
365
366
    dc_pred[left_available][up_available][tx_size](dst, dst_stride,
                                                   const_above_row, left_col);
367
  } else {
368
    pred[mode][tx_size](dst, dst_stride, const_above_row, left_col);
John Koleszar's avatar
John Koleszar committed
369
  }
John Koleszar's avatar
John Koleszar committed
370
371
}

372
373
374
375
void vp9_predict_intra_block(MACROBLOCKD *xd, int block_idx, int bwl_in,
                            TX_SIZE tx_size, int mode,
                            const uint8_t *ref, int ref_stride,
                            uint8_t *dst, int dst_stride) {
376
  const int bwl = bwl_in - tx_size;
377
  const int wmask = (1 << bwl) - 1;
378
379
380
  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);
Yaowu Xu's avatar
Yaowu Xu committed
381

382
  assert(bwl >= 0);
383
384
  build_intra_predictors(ref, ref_stride, dst, dst_stride, mode, tx_size,
                         have_top, have_left, have_right);
385
}