summaryrefslogtreecommitdiffstats
path: root/media/libvpx/libvpx/vp8/common/loopfilter_filters.c
blob: 61a55d3c92f8d50730d4dcbb9acaeced0402bd07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <stdlib.h>
#include "loopfilter.h"
#include "onyxc_int.h"

typedef unsigned char uc;

static signed char vp8_signed_char_clamp(int t) {
  t = (t < -128 ? -128 : t);
  t = (t > 127 ? 127 : t);
  return (signed char)t;
}

/* should we apply any filter at all ( 11111111 yes, 00000000 no) */
static signed char vp8_filter_mask(uc limit, uc blimit, uc p3, uc p2, uc p1,
                                   uc p0, uc q0, uc q1, uc q2, uc q3) {
  signed char mask = 0;
  mask |= (abs(p3 - p2) > limit);
  mask |= (abs(p2 - p1) > limit);
  mask |= (abs(p1 - p0) > limit);
  mask |= (abs(q1 - q0) > limit);
  mask |= (abs(q2 - q1) > limit);
  mask |= (abs(q3 - q2) > limit);
  mask |= (abs(p0 - q0) * 2 + abs(p1 - q1) / 2 > blimit);
  return mask - 1;
}

/* is there high variance internal edge ( 11111111 yes, 00000000 no) */
static signed char vp8_hevmask(uc thresh, uc p1, uc p0, uc q0, uc q1) {
  signed char hev = 0;
  hev |= (abs(p1 - p0) > thresh) * -1;
  hev |= (abs(q1 - q0) > thresh) * -1;
  return hev;
}

static void vp8_filter(signed char mask, uc hev, uc *op1, uc *op0, uc *oq0,
                       uc *oq1) {
  signed char ps0, qs0;
  signed char ps1, qs1;
  signed char filter_value, Filter1, Filter2;
  signed char u;

  ps1 = (signed char)*op1 ^ 0x80;
  ps0 = (signed char)*op0 ^ 0x80;
  qs0 = (signed char)*oq0 ^ 0x80;
  qs1 = (signed char)*oq1 ^ 0x80;

  /* add outer taps if we have high edge variance */
  filter_value = vp8_signed_char_clamp(ps1 - qs1);
  filter_value &= hev;

  /* inner taps */
  filter_value = vp8_signed_char_clamp(filter_value + 3 * (qs0 - ps0));
  filter_value &= mask;

  /* save bottom 3 bits so that we round one side +4 and the other +3
   * if it equals 4 we'll set it to adjust by -1 to account for the fact
   * we'd round it by 3 the other way
   */
  Filter1 = vp8_signed_char_clamp(filter_value + 4);
  Filter2 = vp8_signed_char_clamp(filter_value + 3);
  Filter1 >>= 3;
  Filter2 >>= 3;
  u = vp8_signed_char_clamp(qs0 - Filter1);
  *oq0 = u ^ 0x80;
  u = vp8_signed_char_clamp(ps0 + Filter2);
  *op0 = u ^ 0x80;
  filter_value = Filter1;

  /* outer tap adjustments */
  filter_value += 1;
  filter_value >>= 1;
  filter_value &= ~hev;

  u = vp8_signed_char_clamp(qs1 - filter_value);
  *oq1 = u ^ 0x80;
  u = vp8_signed_char_clamp(ps1 + filter_value);
  *op1 = u ^ 0x80;
}

static void loop_filter_horizontal_edge_c(unsigned char *s, int p, /* pitch */
                                          const unsigned char *blimit,
                                          const unsigned char *limit,
                                          const unsigned char *thresh,
                                          int count) {
  int hev = 0; /* high edge variance */
  signed char mask = 0;
  int i = 0;

  /* loop filter designed to work using chars so that we can make maximum use
   * of 8 bit simd instructions.
   */
  do {
    mask = vp8_filter_mask(limit[0], blimit[0], s[-4 * p], s[-3 * p], s[-2 * p],
                           s[-1 * p], s[0 * p], s[1 * p], s[2 * p], s[3 * p]);

    hev = vp8_hevmask(thresh[0], s[-2 * p], s[-1 * p], s[0 * p], s[1 * p]);

    vp8_filter(mask, hev, s - 2 * p, s - 1 * p, s, s + 1 * p);

    ++s;
  } while (++i < count * 8);
}

static void loop_filter_vertical_edge_c(unsigned char *s, int p,
                                        const unsigned char *blimit,
                                        const unsigned char *limit,
                                        const unsigned char *thresh,
                                        int count) {
  int hev = 0; /* high edge variance */
  signed char mask = 0;
  int i = 0;

  /* loop filter designed to work using chars so that we can make maximum use
   * of 8 bit simd instructions.
   */
  do {
    mask = vp8_filter_mask(limit[0], blimit[0], s[-4], s[-3], s[-2], s[-1],
                           s[0], s[1], s[2], s[3]);

    hev = vp8_hevmask(thresh[0], s[-2], s[-1], s[0], s[1]);

    vp8_filter(mask, hev, s - 2, s - 1, s, s + 1);

    s += p;
  } while (++i < count * 8);
}

static void vp8_mbfilter(signed char mask, uc hev, uc *op2, uc *op1, uc *op0,
                         uc *oq0, uc *oq1, uc *oq2) {
  signed char s, u;
  signed char filter_value, Filter1, Filter2;
  signed char ps2 = (signed char)*op2 ^ 0x80;
  signed char ps1 = (signed char)*op1 ^ 0x80;
  signed char ps0 = (signed char)*op0 ^ 0x80;
  signed char qs0 = (signed char)*oq0 ^ 0x80;
  signed char qs1 = (signed char)*oq1 ^ 0x80;
  signed char qs2 = (signed char)*oq2 ^ 0x80;

  /* add outer taps if we have high edge variance */
  filter_value = vp8_signed_char_clamp(ps1 - qs1);
  filter_value = vp8_signed_char_clamp(filter_value + 3 * (qs0 - ps0));
  filter_value &= mask;

  Filter2 = filter_value;
  Filter2 &= hev;

  /* save bottom 3 bits so that we round one side +4 and the other +3 */
  Filter1 = vp8_signed_char_clamp(Filter2 + 4);
  Filter2 = vp8_signed_char_clamp(Filter2 + 3);
  Filter1 >>= 3;
  Filter2 >>= 3;
  qs0 = vp8_signed_char_clamp(qs0 - Filter1);
  ps0 = vp8_signed_char_clamp(ps0 + Filter2);

  /* only apply wider filter if not high edge variance */
  filter_value &= ~hev;
  Filter2 = filter_value;

  /* roughly 3/7th difference across boundary */
  u = vp8_signed_char_clamp((63 + Filter2 * 27) >> 7);
  s = vp8_signed_char_clamp(qs0 - u);
  *oq0 = s ^ 0x80;
  s = vp8_signed_char_clamp(ps0 + u);
  *op0 = s ^ 0x80;

  /* roughly 2/7th difference across boundary */
  u = vp8_signed_char_clamp((63 + Filter2 * 18) >> 7);
  s = vp8_signed_char_clamp(qs1 - u);
  *oq1 = s ^ 0x80;
  s = vp8_signed_char_clamp(ps1 + u);
  *op1 = s ^ 0x80;

  /* roughly 1/7th difference across boundary */
  u = vp8_signed_char_clamp((63 + Filter2 * 9) >> 7);
  s = vp8_signed_char_clamp(qs2 - u);
  *oq2 = s ^ 0x80;
  s = vp8_signed_char_clamp(ps2 + u);
  *op2 = s ^ 0x80;
}

static void mbloop_filter_horizontal_edge_c(unsigned char *s, int p,
                                            const unsigned char *blimit,
                                            const unsigned char *limit,
                                            const unsigned char *thresh,
                                            int count) {
  signed char hev = 0; /* high edge variance */
  signed char mask = 0;
  int i = 0;

  /* loop filter designed to work using chars so that we can make maximum use
   * of 8 bit simd instructions.
   */
  do {
    mask = vp8_filter_mask(limit[0], blimit[0], s[-4 * p], s[-3 * p], s[-2 * p],
                           s[-1 * p], s[0 * p], s[1 * p], s[2 * p], s[3 * p]);

    hev = vp8_hevmask(thresh[0], s[-2 * p], s[-1 * p], s[0 * p], s[1 * p]);

    vp8_mbfilter(mask, hev, s - 3 * p, s - 2 * p, s - 1 * p, s, s + 1 * p,
                 s + 2 * p);

    ++s;
  } while (++i < count * 8);
}

static void mbloop_filter_vertical_edge_c(unsigned char *s, int p,
                                          const unsigned char *blimit,
                                          const unsigned char *limit,
                                          const unsigned char *thresh,
                                          int count) {
  signed char hev = 0; /* high edge variance */
  signed char mask = 0;
  int i = 0;

  do {
    mask = vp8_filter_mask(limit[0], blimit[0], s[-4], s[-3], s[-2], s[-1],
                           s[0], s[1], s[2], s[3]);

    hev = vp8_hevmask(thresh[0], s[-2], s[-1], s[0], s[1]);

    vp8_mbfilter(mask, hev, s - 3, s - 2, s - 1, s, s + 1, s + 2);

    s += p;
  } while (++i < count * 8);
}

/* should we apply any filter at all ( 11111111 yes, 00000000 no) */
static signed char vp8_simple_filter_mask(uc blimit, uc p1, uc p0, uc q0,
                                          uc q1) {
  /* Why does this cause problems for win32?
   * error C2143: syntax error : missing ';' before 'type'
   *  (void) limit;
   */
  signed char mask = (abs(p0 - q0) * 2 + abs(p1 - q1) / 2 <= blimit) * -1;
  return mask;
}

static void vp8_simple_filter(signed char mask, uc *op1, uc *op0, uc *oq0,
                              uc *oq1) {
  signed char filter_value, Filter1, Filter2;
  signed char p1 = (signed char)*op1 ^ 0x80;
  signed char p0 = (signed char)*op0 ^ 0x80;
  signed char q0 = (signed char)*oq0 ^ 0x80;
  signed char q1 = (signed char)*oq1 ^ 0x80;
  signed char u;

  filter_value = vp8_signed_char_clamp(p1 - q1);
  filter_value = vp8_signed_char_clamp(filter_value + 3 * (q0 - p0));
  filter_value &= mask;

  /* save bottom 3 bits so that we round one side +4 and the other +3 */
  Filter1 = vp8_signed_char_clamp(filter_value + 4);
  Filter1 >>= 3;
  u = vp8_signed_char_clamp(q0 - Filter1);
  *oq0 = u ^ 0x80;

  Filter2 = vp8_signed_char_clamp(filter_value + 3);
  Filter2 >>= 3;
  u = vp8_signed_char_clamp(p0 + Filter2);
  *op0 = u ^ 0x80;
}

void vp8_loop_filter_simple_horizontal_edge_c(unsigned char *y_ptr,
                                              int y_stride,
                                              const unsigned char *blimit) {
  signed char mask = 0;
  int i = 0;

  do {
    mask = vp8_simple_filter_mask(blimit[0], y_ptr[-2 * y_stride],
                                  y_ptr[-1 * y_stride], y_ptr[0 * y_stride],
                                  y_ptr[1 * y_stride]);
    vp8_simple_filter(mask, y_ptr - 2 * y_stride, y_ptr - 1 * y_stride, y_ptr,
                      y_ptr + 1 * y_stride);
    ++y_ptr;
  } while (++i < 16);
}

void vp8_loop_filter_simple_vertical_edge_c(unsigned char *y_ptr, int y_stride,
                                            const unsigned char *blimit) {
  signed char mask = 0;
  int i = 0;

  do {
    mask = vp8_simple_filter_mask(blimit[0], y_ptr[-2], y_ptr[-1], y_ptr[0],
                                  y_ptr[1]);
    vp8_simple_filter(mask, y_ptr - 2, y_ptr - 1, y_ptr, y_ptr + 1);
    y_ptr += y_stride;
  } while (++i < 16);
}

/* Horizontal MB filtering */
void vp8_loop_filter_mbh_c(unsigned char *y_ptr, unsigned char *u_ptr,
                           unsigned char *v_ptr, int y_stride, int uv_stride,
                           loop_filter_info *lfi) {
  mbloop_filter_horizontal_edge_c(y_ptr, y_stride, lfi->mblim, lfi->lim,
                                  lfi->hev_thr, 2);

  if (u_ptr) {
    mbloop_filter_horizontal_edge_c(u_ptr, uv_stride, lfi->mblim, lfi->lim,
                                    lfi->hev_thr, 1);
  }

  if (v_ptr) {
    mbloop_filter_horizontal_edge_c(v_ptr, uv_stride, lfi->mblim, lfi->lim,
                                    lfi->hev_thr, 1);
  }
}

/* Vertical MB Filtering */
void vp8_loop_filter_mbv_c(unsigned char *y_ptr, unsigned char *u_ptr,
                           unsigned char *v_ptr, int y_stride, int uv_stride,
                           loop_filter_info *lfi) {
  mbloop_filter_vertical_edge_c(y_ptr, y_stride, lfi->mblim, lfi->lim,
                                lfi->hev_thr, 2);

  if (u_ptr) {
    mbloop_filter_vertical_edge_c(u_ptr, uv_stride, lfi->mblim, lfi->lim,
                                  lfi->hev_thr, 1);
  }

  if (v_ptr) {
    mbloop_filter_vertical_edge_c(v_ptr, uv_stride, lfi->mblim, lfi->lim,
                                  lfi->hev_thr, 1);
  }
}

/* Horizontal B Filtering */
void vp8_loop_filter_bh_c(unsigned char *y_ptr, unsigned char *u_ptr,
                          unsigned char *v_ptr, int y_stride, int uv_stride,
                          loop_filter_info *lfi) {
  loop_filter_horizontal_edge_c(y_ptr + 4 * y_stride, y_stride, lfi->blim,
                                lfi->lim, lfi->hev_thr, 2);
  loop_filter_horizontal_edge_c(y_ptr + 8 * y_stride, y_stride, lfi->blim,
                                lfi->lim, lfi->hev_thr, 2);
  loop_filter_horizontal_edge_c(y_ptr + 12 * y_stride, y_stride, lfi->blim,
                                lfi->lim, lfi->hev_thr, 2);

  if (u_ptr) {
    loop_filter_horizontal_edge_c(u_ptr + 4 * uv_stride, uv_stride, lfi->blim,
                                  lfi->lim, lfi->hev_thr, 1);
  }

  if (v_ptr) {
    loop_filter_horizontal_edge_c(v_ptr + 4 * uv_stride, uv_stride, lfi->blim,
                                  lfi->lim, lfi->hev_thr, 1);
  }
}

void vp8_loop_filter_bhs_c(unsigned char *y_ptr, int y_stride,
                           const unsigned char *blimit) {
  vp8_loop_filter_simple_horizontal_edge_c(y_ptr + 4 * y_stride, y_stride,
                                           blimit);
  vp8_loop_filter_simple_horizontal_edge_c(y_ptr + 8 * y_stride, y_stride,
                                           blimit);
  vp8_loop_filter_simple_horizontal_edge_c(y_ptr + 12 * y_stride, y_stride,
                                           blimit);
}

/* Vertical B Filtering */
void vp8_loop_filter_bv_c(unsigned char *y_ptr, unsigned char *u_ptr,
                          unsigned char *v_ptr, int y_stride, int uv_stride,
                          loop_filter_info *lfi) {
  loop_filter_vertical_edge_c(y_ptr + 4, y_stride, lfi->blim, lfi->lim,
                              lfi->hev_thr, 2);
  loop_filter_vertical_edge_c(y_ptr + 8, y_stride, lfi->blim, lfi->lim,
                              lfi->hev_thr, 2);
  loop_filter_vertical_edge_c(y_ptr + 12, y_stride, lfi->blim, lfi->lim,
                              lfi->hev_thr, 2);

  if (u_ptr) {
    loop_filter_vertical_edge_c(u_ptr + 4, uv_stride, lfi->blim, lfi->lim,
                                lfi->hev_thr, 1);
  }

  if (v_ptr) {
    loop_filter_vertical_edge_c(v_ptr + 4, uv_stride, lfi->blim, lfi->lim,
                                lfi->hev_thr, 1);
  }
}

void vp8_loop_filter_bvs_c(unsigned char *y_ptr, int y_stride,
                           const unsigned char *blimit) {
  vp8_loop_filter_simple_vertical_edge_c(y_ptr + 4, y_stride, blimit);
  vp8_loop_filter_simple_vertical_edge_c(y_ptr + 8, y_stride, blimit);
  vp8_loop_filter_simple_vertical_edge_c(y_ptr + 12, y_stride, blimit);
}