vp9_loopfilter.c 14.4 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
 */

Attila Nagy's avatar
Attila Nagy committed
11
#include "vpx_config.h"
12 13
#include "vp9/common/vp9_loopfilter.h"
#include "vp9/common/vp9_onyxc_int.h"
14
#include "vp9/common/vp9_reconinter.h"
Attila Nagy's avatar
Attila Nagy committed
15
#include "vpx_mem/vpx_mem.h"
John Koleszar's avatar
John Koleszar committed
16

17
#include "vp9/common/vp9_seg_common.h"
18

19 20 21 22 23 24
struct loop_filter_info {
  const uint8_t *mblim;
  const uint8_t *lim;
  const uint8_t *hev_thr;
};

John Koleszar's avatar
John Koleszar committed
25
static void lf_init_lut(loop_filter_info_n *lfi) {
26 27 28 29 30 31 32 33 34 35 36 37 38 39
  lfi->mode_lf_lut[DC_PRED] = 0;
  lfi->mode_lf_lut[D45_PRED] = 0;
  lfi->mode_lf_lut[D135_PRED] = 0;
  lfi->mode_lf_lut[D117_PRED] = 0;
  lfi->mode_lf_lut[D153_PRED] = 0;
  lfi->mode_lf_lut[D27_PRED] = 0;
  lfi->mode_lf_lut[D63_PRED] = 0;
  lfi->mode_lf_lut[V_PRED] = 0;
  lfi->mode_lf_lut[H_PRED] = 0;
  lfi->mode_lf_lut[TM_PRED] = 0;
  lfi->mode_lf_lut[ZEROMV]  = 0;
  lfi->mode_lf_lut[NEARESTMV] = 1;
  lfi->mode_lf_lut[NEARMV] = 1;
  lfi->mode_lf_lut[NEWMV] = 1;
Attila Nagy's avatar
Attila Nagy committed
40 41
}

42
static void update_sharpness(loop_filter_info_n *const lfi, int sharpness_lvl) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
43
  int lvl;
John Koleszar's avatar
John Koleszar committed
44

Dmitry Kovalev's avatar
Dmitry Kovalev committed
45 46 47 48
  // For each possible value for the loop filter fill out limits
  for (lvl = 0; lvl <= MAX_LOOP_FILTER; lvl++) {
    // Set loop filter paramaeters that control sharpness.
    int block_inside_limit = lvl >> ((sharpness_lvl > 0) + (sharpness_lvl > 4));
John Koleszar's avatar
John Koleszar committed
49

John Koleszar's avatar
John Koleszar committed
50 51 52
    if (sharpness_lvl > 0) {
      if (block_inside_limit > (9 - sharpness_lvl))
        block_inside_limit = (9 - sharpness_lvl);
John Koleszar's avatar
John Koleszar committed
53
    }
John Koleszar's avatar
John Koleszar committed
54 55 56 57

    if (block_inside_limit < 1)
      block_inside_limit = 1;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
58 59
    vpx_memset(lfi->lim[lvl], block_inside_limit, SIMD_WIDTH);
    vpx_memset(lfi->mblim[lvl], (2 * (lvl + 2) + block_inside_limit),
John Koleszar's avatar
John Koleszar committed
60 61
               SIMD_WIDTH);
  }
Attila Nagy's avatar
Attila Nagy committed
62
}
John Koleszar's avatar
John Koleszar committed
63

64
void vp9_loop_filter_init(VP9_COMMON *cm) {
John Koleszar's avatar
John Koleszar committed
65
  loop_filter_info_n *lfi = &cm->lf_info;
66
  struct loopfilter *lf = &cm->lf;
John Koleszar's avatar
John Koleszar committed
67
  int i;
Attila Nagy's avatar
Attila Nagy committed
68

69
  // init limits for given sharpness
70 71
  update_sharpness(lfi, lf->sharpness_level);
  lf->last_sharpness_level = lf->sharpness_level;
Attila Nagy's avatar
Attila Nagy committed
72

73
  // init LUT for lvl  and hev thr picking
John Koleszar's avatar
John Koleszar committed
74
  lf_init_lut(lfi);
Attila Nagy's avatar
Attila Nagy committed
75

76 77
  // init hev threshold const vectors
  for (i = 0; i < 4; i++)
John Koleszar's avatar
John Koleszar committed
78
    vpx_memset(lfi->hev_thr[i], i, SIMD_WIDTH);
John Koleszar's avatar
John Koleszar committed
79 80
}

81 82
void vp9_loop_filter_frame_init(VP9_COMMON *const cm, MACROBLOCKD *const xd,
                                int default_filt_lvl) {
83
  int seg_id;
84 85 86
  // n_shift is the a multiplier for lf_deltas
  // the multiplier is 1 for when filter_lvl is between 0 and 31;
  // 2 when filter_lvl is between 32 and 63
Dmitry Kovalev's avatar
Dmitry Kovalev committed
87 88
  const int n_shift = default_filt_lvl >> 5;
  loop_filter_info_n *const lfi = &cm->lf_info;
89
  struct loopfilter *const lf = &cm->lf;
90
  struct segmentation *const seg = &xd->seg;
John Koleszar's avatar
John Koleszar committed
91

Dmitry Kovalev's avatar
Dmitry Kovalev committed
92
  // update limits if sharpness has changed
93 94 95
  if (lf->last_sharpness_level != lf->sharpness_level) {
    update_sharpness(lfi, lf->sharpness_level);
    lf->last_sharpness_level = lf->sharpness_level;
John Koleszar's avatar
John Koleszar committed
96 97
  }

98
  for (seg_id = 0; seg_id < MAX_SEGMENTS; seg_id++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
99
    int lvl_seg = default_filt_lvl, ref, mode, intra_lvl;
John Koleszar's avatar
John Koleszar committed
100 101

    // Set the baseline filter values for each segment
102 103 104
    if (vp9_segfeature_active(&xd->seg, seg_id, SEG_LVL_ALT_LF)) {
      const int data = vp9_get_segdata(seg, seg_id, SEG_LVL_ALT_LF);
      lvl_seg = seg->abs_delta == SEGMENT_ABSDATA
Dmitry Kovalev's avatar
Dmitry Kovalev committed
105 106
                  ? data
                  : clamp(default_filt_lvl + data, 0, MAX_LOOP_FILTER);
John Koleszar's avatar
John Koleszar committed
107
    }
John Koleszar's avatar
John Koleszar committed
108

109
    if (!lf->mode_ref_delta_enabled) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
110 111
      // we could get rid of this if we assume that deltas are set to
      // zero when not in use; encoder always uses deltas
112
      vpx_memset(lfi->lvl[seg_id][0], lvl_seg, sizeof(lfi->lvl[seg_id][0]));
John Koleszar's avatar
John Koleszar committed
113 114
      continue;
    }
John Koleszar's avatar
John Koleszar committed
115

116
    intra_lvl = lvl_seg + (lf->ref_deltas[INTRA_FRAME] << n_shift);
117
    lfi->lvl[seg_id][INTRA_FRAME][0] = clamp(intra_lvl, 0, MAX_LOOP_FILTER);
John Koleszar's avatar
John Koleszar committed
118

Dmitry Kovalev's avatar
Dmitry Kovalev committed
119 120
    for (ref = LAST_FRAME; ref < MAX_REF_FRAMES; ++ref)
      for (mode = 0; mode < MAX_MODE_LF_DELTAS; ++mode) {
121 122
        const int inter_lvl = lvl_seg + (lf->ref_deltas[ref] << n_shift)
                                      + (lf->mode_deltas[mode] << n_shift);
123
        lfi->lvl[seg_id][ref][mode] = clamp(inter_lvl, 0, MAX_LOOP_FILTER);
John Koleszar's avatar
John Koleszar committed
124 125
      }
  }
John Koleszar's avatar
John Koleszar committed
126 127
}

128 129 130
static int build_lfi(const loop_filter_info_n *const lfi_n,
                     const MB_MODE_INFO *const mbmi,
                     struct loop_filter_info *const lfi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
131 132 133 134 135 136
  const int seg = mbmi->segment_id;
  const int ref = mbmi->ref_frame[0];
  const int mode = lfi_n->mode_lf_lut[mbmi->mode];
  const int filter_level = lfi_n->lvl[seg][ref][mode];

  if (filter_level > 0) {
137 138
    lfi->mblim = lfi_n->mblim[filter_level];
    lfi->lim = lfi_n->lim[filter_level];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
139
    lfi->hev_thr = lfi_n->hev_thr[filter_level >> 4];
140
    return 1;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
141 142
  } else {
    return 0;
143 144 145 146 147 148 149
  }
}

static void filter_selectively_vert(uint8_t *s, int pitch,
                                    unsigned int mask_16x16,
                                    unsigned int mask_8x8,
                                    unsigned int mask_4x4,
150
                                    unsigned int mask_4x4_int,
151 152 153
                                    const struct loop_filter_info *lfi) {
  unsigned int mask;

John Koleszar's avatar
John Koleszar committed
154 155
  for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
       mask; mask >>= 1) {
156 157 158
    if (mask & 1) {
      if (mask_16x16 & 1) {
        vp9_mb_lpf_vertical_edge_w(s, pitch, lfi->mblim, lfi->lim,
159
                                   lfi->hev_thr);
160 161
        assert(!(mask_8x8 & 1));
        assert(!(mask_4x4 & 1));
162
        assert(!(mask_4x4_int & 1));
163 164 165 166 167 168 169 170 171 172 173 174
      } else if (mask_8x8 & 1) {
        vp9_mbloop_filter_vertical_edge(s, pitch, lfi->mblim, lfi->lim,
                                        lfi->hev_thr, 1);
        assert(!(mask_16x16 & 1));
        assert(!(mask_4x4 & 1));
      } else if (mask_4x4 & 1) {
        vp9_loop_filter_vertical_edge(s, pitch, lfi->mblim, lfi->lim,
                                      lfi->hev_thr, 1);
        assert(!(mask_16x16 & 1));
        assert(!(mask_8x8 & 1));
      }
    }
175 176 177
    if (mask_4x4_int & 1)
      vp9_loop_filter_vertical_edge(s + 4, pitch, lfi->mblim, lfi->lim,
                                    lfi->hev_thr, 1);
178 179 180 181 182
    s += 8;
    lfi++;
    mask_16x16 >>= 1;
    mask_8x8 >>= 1;
    mask_4x4 >>= 1;
183
    mask_4x4_int >>= 1;
184 185 186 187 188 189 190
  }
}

static void filter_selectively_horiz(uint8_t *s, int pitch,
                                     unsigned int mask_16x16,
                                     unsigned int mask_8x8,
                                     unsigned int mask_4x4,
191
                                     unsigned int mask_4x4_int,
192 193 194
                                     int only_4x4_1,
                                     const struct loop_filter_info *lfi) {
  unsigned int mask;
195
  int count;
196

John Koleszar's avatar
John Koleszar committed
197
  for (mask = mask_16x16 | mask_8x8 | mask_4x4 | mask_4x4_int;
198
       mask; mask >>= count) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
199
    count = 1;
200 201 202
    if (mask & 1) {
      if (!only_4x4_1) {
        if (mask_16x16 & 1) {
203 204 205 206 207 208 209 210
          if ((mask_16x16 & 3) == 3) {
            vp9_mb_lpf_horizontal_edge_w(s, pitch, lfi->mblim, lfi->lim,
                                         lfi->hev_thr, 2);
            count = 2;
          } else {
            vp9_mb_lpf_horizontal_edge_w(s, pitch, lfi->mblim, lfi->lim,
                                         lfi->hev_thr, 1);
          }
211 212
          assert(!(mask_8x8 & 1));
          assert(!(mask_4x4 & 1));
213
          assert(!(mask_4x4_int & 1));
214 215 216 217 218 219 220 221 222 223 224 225 226
        } else if (mask_8x8 & 1) {
          vp9_mbloop_filter_horizontal_edge(s, pitch, lfi->mblim, lfi->lim,
                                            lfi->hev_thr, 1);
          assert(!(mask_16x16 & 1));
          assert(!(mask_4x4 & 1));
        } else if (mask_4x4 & 1) {
          vp9_loop_filter_horizontal_edge(s, pitch, lfi->mblim, lfi->lim,
                                          lfi->hev_thr, 1);
          assert(!(mask_16x16 & 1));
          assert(!(mask_8x8 & 1));
        }
      }

227
      if (mask_4x4_int & 1)
228 229 230
        vp9_loop_filter_horizontal_edge(s + 4 * pitch, pitch, lfi->mblim,
                                        lfi->lim, lfi->hev_thr, 1);
    }
231 232 233 234 235 236
    s += 8 * count;
    lfi += count;
    mask_16x16 >>= count;
    mask_8x8 >>= count;
    mask_4x4 >>= count;
    mask_4x4_int >>= count;
237 238 239
  }
}

240 241 242 243 244 245
static void filter_block_plane(VP9_COMMON *const cm,
                               struct macroblockd_plane *const plane,
                               const MODE_INFO *mi,
                               int mi_row, int mi_col) {
  const int ss_x = plane->subsampling_x;
  const int ss_y = plane->subsampling_y;
246 247
  const int row_step = 1 << ss_x;
  const int col_step = 1 << ss_y;
248 249
  const int row_step_stride = cm->mode_info_stride * row_step;
  struct buf_2d *const dst = &plane->dst;
250
  uint8_t* const dst0 = dst->buf;
251 252 253 254 255
  unsigned int mask_16x16[MI_BLOCK_SIZE] = {0};
  unsigned int mask_8x8[MI_BLOCK_SIZE] = {0};
  unsigned int mask_4x4[MI_BLOCK_SIZE] = {0};
  unsigned int mask_4x4_int[MI_BLOCK_SIZE] = {0};
  struct loop_filter_info lfi[MI_BLOCK_SIZE][MI_BLOCK_SIZE];
256
  int r, c;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
257

258
  for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
259 260 261 262 263 264
    unsigned int mask_16x16_c = 0;
    unsigned int mask_8x8_c = 0;
    unsigned int mask_4x4_c = 0;
    unsigned int border_mask;

    // Determine the vertical edges that need filtering
265
    for (c = 0; c < MI_BLOCK_SIZE && mi_col + c < cm->mi_cols; c += col_step) {
Paul Wilkins's avatar
Paul Wilkins committed
266
      const int skip_this = mi[c].mbmi.skip_coeff
267
                            && is_inter_block(&mi[c].mbmi);
268
      // left edge of current unit is block/partition edge -> no skip
269 270
      const int block_edge_left = b_width_log2(mi[c].mbmi.sb_type) ?
          !(c & ((1 << (b_width_log2(mi[c].mbmi.sb_type)-1)) - 1)) : 1;
271
      const int skip_this_c = skip_this && !block_edge_left;
272
      // top edge of current unit is block/partition edge -> no skip
273 274
      const int block_edge_above = b_height_log2(mi[c].mbmi.sb_type) ?
          !(r & ((1 << (b_height_log2(mi[c].mbmi.sb_type)-1)) - 1)) : 1;
275
      const int skip_this_r = skip_this && !block_edge_above;
276 277 278
      const TX_SIZE tx_size = (plane->plane_type == PLANE_TYPE_UV)
                            ? get_uv_tx_size(&mi[c].mbmi)
                            : mi[c].mbmi.txfm_size;
279
      const int skip_border_4x4_c = ss_x && mi_col + c == cm->mi_cols - 1;
280
      const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
281 282

      // Filter level can vary per MI
283
      if (!build_lfi(&cm->lf_info, &mi[c].mbmi, lfi[r] + (c >> ss_x)))
284 285 286 287
        continue;

      // Build masks based on the transform size of each block
      if (tx_size == TX_32X32) {
288 289 290 291 292 293 294 295 296 297 298 299
        if (!skip_this_c && ((c >> ss_x) & 3) == 0) {
          if (!skip_border_4x4_c)
            mask_16x16_c |= 1 << (c >> ss_x);
          else
            mask_8x8_c |= 1 << (c >> ss_x);
        }
        if (!skip_this_r && ((r >> ss_y) & 3) == 0) {
          if (!skip_border_4x4_r)
            mask_16x16[r] |= 1 << (c >> ss_x);
          else
            mask_8x8[r] |= 1 << (c >> ss_x);
        }
300
      } else if (tx_size == TX_16X16) {
301 302 303 304 305 306 307 308 309 310 311 312
        if (!skip_this_c && ((c >> ss_x) & 1) == 0) {
          if (!skip_border_4x4_c)
            mask_16x16_c |= 1 << (c >> ss_x);
          else
            mask_8x8_c |= 1 << (c >> ss_x);
        }
        if (!skip_this_r && ((r >> ss_y) & 1) == 0) {
          if (!skip_border_4x4_r)
            mask_16x16[r] |= 1 << (c >> ss_x);
          else
            mask_8x8[r] |= 1 << (c >> ss_x);
        }
313 314 315
      } else {
        // force 8x8 filtering on 32x32 boundaries
        if (!skip_this_c) {
316
          if (tx_size == TX_8X8 || ((c >> ss_x) & 3) == 0)
317 318 319 320 321 322
            mask_8x8_c |= 1 << (c >> ss_x);
          else
            mask_4x4_c |= 1 << (c >> ss_x);
        }

        if (!skip_this_r) {
323
          if (tx_size == TX_8X8 || ((r >> ss_y) & 3) == 0)
324 325 326 327 328
            mask_8x8[r] |= 1 << (c >> ss_x);
          else
            mask_4x4[r] |= 1 << (c >> ss_x);
        }

329 330
        if (!skip_this && tx_size < TX_8X8 && !skip_border_4x4_c)
          mask_4x4_int[r] |= 1 << (c >> ss_x);
331 332 333 334 335 336 337 338 339
      }
    }

    // Disable filtering on the leftmost column
    border_mask = ~(mi_col == 0);
    filter_selectively_vert(dst->buf, dst->stride,
                            mask_16x16_c & border_mask,
                            mask_8x8_c & border_mask,
                            mask_4x4_c & border_mask,
340
                            mask_4x4_int[r], lfi[r]);
341
    dst->buf += 8 * dst->stride;
342
    mi += row_step_stride;
343 344 345 346
  }

  // Now do horizontal pass
  dst->buf = dst0;
347
  for (r = 0; r < MI_BLOCK_SIZE && mi_row + r < cm->mi_rows; r += row_step) {
348 349 350
    const int skip_border_4x4_r = ss_y && mi_row + r == cm->mi_rows - 1;
    const unsigned int mask_4x4_int_r = skip_border_4x4_r ? 0 : mask_4x4_int[r];

351 352 353 354
    filter_selectively_horiz(dst->buf, dst->stride,
                             mask_16x16[r],
                             mask_8x8[r],
                             mask_4x4[r],
355
                             mask_4x4_int_r, mi_row + r == 0, lfi[r]);
356 357 358 359
    dst->buf += 8 * dst->stride;
  }
}

360 361 362
void vp9_loop_filter_rows(const YV12_BUFFER_CONFIG *frame_buffer,
                          VP9_COMMON *cm, MACROBLOCKD *xd,
                          int start, int stop, int y_only) {
363
  const int num_planes = y_only ? 1 : MAX_MB_PLANE;
364 365
  int mi_row, mi_col;

366
  for (mi_row = start; mi_row < stop; mi_row += MI_BLOCK_SIZE) {
367 368
    MODE_INFO* const mi = cm->mi + mi_row * cm->mode_info_stride;

369
    for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE) {
370 371
      int plane;

372
      setup_dst_planes(xd, frame_buffer, mi_row, mi_col);
373 374
      for (plane = 0; plane < num_planes; ++plane) {
        filter_block_plane(cm, &xd->plane[plane], mi + mi_col, mi_row, mi_col);
375 376 377 378
      }
    }
  }
}
379 380

void vp9_loop_filter_frame(VP9_COMMON *cm, MACROBLOCKD *xd,
381 382 383
                           int frame_filter_level,
                           int y_only, int partial) {
  int start_mi_row, end_mi_row, mi_rows_to_filter;
384
  if (!frame_filter_level) return;
385 386 387 388 389 390 391 392 393

  start_mi_row = 0;
  mi_rows_to_filter = cm->mi_rows;
  if (partial && cm->mi_rows > 8) {
    start_mi_row = cm->mi_rows >> 1;
    start_mi_row &= 0xfffffff8;
    mi_rows_to_filter = MAX(cm->mi_rows / 8, 8);
  }
  end_mi_row = start_mi_row + mi_rows_to_filter;
394 395
  vp9_loop_filter_frame_init(cm, xd, frame_filter_level);
  vp9_loop_filter_rows(cm->frame_to_show, cm, xd,
396 397
                       start_mi_row, end_mi_row,
                       y_only);
398
}
399 400 401 402 403 404 405 406

int vp9_loop_filter_worker(void *arg1, void *arg2) {
  LFWorkerData *const lf_data = (LFWorkerData*)arg1;
  (void)arg2;
  vp9_loop_filter_rows(lf_data->frame_buffer, lf_data->cm, &lf_data->xd,
                       lf_data->start, lf_data->stop, lf_data->y_only);
  return 1;
}