vp9_loopfilter.c 17.5 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"
Attila Nagy's avatar
Attila Nagy committed
14
#include "vpx_mem/vpx_mem.h"
John Koleszar's avatar
John Koleszar committed
15

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

John Koleszar's avatar
John Koleszar committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
static void lf_init_lut(loop_filter_info_n *lfi) {
  int filt_lvl;

  for (filt_lvl = 0; filt_lvl <= MAX_LOOP_FILTER; filt_lvl++) {
    if (filt_lvl >= 40) {
      lfi->hev_thr_lut[KEY_FRAME][filt_lvl] = 2;
      lfi->hev_thr_lut[INTER_FRAME][filt_lvl] = 3;
    } else if (filt_lvl >= 20) {
      lfi->hev_thr_lut[KEY_FRAME][filt_lvl] = 1;
      lfi->hev_thr_lut[INTER_FRAME][filt_lvl] = 2;
    } else if (filt_lvl >= 15) {
      lfi->hev_thr_lut[KEY_FRAME][filt_lvl] = 1;
      lfi->hev_thr_lut[INTER_FRAME][filt_lvl] = 1;
    } else {
      lfi->hev_thr_lut[KEY_FRAME][filt_lvl] = 0;
      lfi->hev_thr_lut[INTER_FRAME][filt_lvl] = 0;
Attila Nagy's avatar
Attila Nagy committed
34
    }
John Koleszar's avatar
John Koleszar committed
35
  }
Attila Nagy's avatar
Attila Nagy committed
36

John Koleszar's avatar
John Koleszar committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  lfi->mode_lf_lut[DC_PRED] = 1;
  lfi->mode_lf_lut[D45_PRED] = 1;
  lfi->mode_lf_lut[D135_PRED] = 1;
  lfi->mode_lf_lut[D117_PRED] = 1;
  lfi->mode_lf_lut[D153_PRED] = 1;
  lfi->mode_lf_lut[D27_PRED] = 1;
  lfi->mode_lf_lut[D63_PRED] = 1;
  lfi->mode_lf_lut[V_PRED] = 1;
  lfi->mode_lf_lut[H_PRED] = 1;
  lfi->mode_lf_lut[TM_PRED] = 1;
  lfi->mode_lf_lut[B_PRED]  = 0;
  lfi->mode_lf_lut[I8X8_PRED] = 0;
  lfi->mode_lf_lut[ZEROMV]  = 1;
  lfi->mode_lf_lut[NEARESTMV] = 2;
  lfi->mode_lf_lut[NEARMV] = 2;
  lfi->mode_lf_lut[NEWMV] = 2;
  lfi->mode_lf_lut[SPLITMV] = 3;
Attila Nagy's avatar
Attila Nagy committed
54 55
}

56
void vp9_loop_filter_update_sharpness(loop_filter_info_n *lfi,
John Koleszar's avatar
John Koleszar committed
57 58 59 60 61 62 63
                                      int sharpness_lvl) {
  int i;

  /* For each possible value for the loop filter fill out limits */
  for (i = 0; i <= MAX_LOOP_FILTER; i++) {
    int filt_lvl = i;
    int block_inside_limit = 0;
John Koleszar's avatar
John Koleszar committed
64

John Koleszar's avatar
John Koleszar committed
65 66 67
    /* Set loop filter paramaeters that control sharpness. */
    block_inside_limit = filt_lvl >> (sharpness_lvl > 0);
    block_inside_limit = block_inside_limit >> (sharpness_lvl > 4);
John Koleszar's avatar
John Koleszar committed
68

John Koleszar's avatar
John Koleszar committed
69 70 71
    if (sharpness_lvl > 0) {
      if (block_inside_limit > (9 - sharpness_lvl))
        block_inside_limit = (9 - sharpness_lvl);
John Koleszar's avatar
John Koleszar committed
72
    }
John Koleszar's avatar
John Koleszar committed
73 74 75 76 77 78 79 80 81 82

    if (block_inside_limit < 1)
      block_inside_limit = 1;

    vpx_memset(lfi->lim[i], block_inside_limit, SIMD_WIDTH);
    vpx_memset(lfi->blim[i], (2 * filt_lvl + block_inside_limit),
               SIMD_WIDTH);
    vpx_memset(lfi->mblim[i], (2 * (filt_lvl + 2) + block_inside_limit),
               SIMD_WIDTH);
  }
Attila Nagy's avatar
Attila Nagy committed
83
}
John Koleszar's avatar
John Koleszar committed
84

85
void vp9_loop_filter_init(VP9_COMMON *cm) {
John Koleszar's avatar
John Koleszar committed
86 87
  loop_filter_info_n *lfi = &cm->lf_info;
  int i;
Attila Nagy's avatar
Attila Nagy committed
88

John Koleszar's avatar
John Koleszar committed
89
  /* init limits for given sharpness*/
90
  vp9_loop_filter_update_sharpness(lfi, cm->sharpness_level);
John Koleszar's avatar
John Koleszar committed
91
  cm->last_sharpness_level = cm->sharpness_level;
Attila Nagy's avatar
Attila Nagy committed
92

John Koleszar's avatar
John Koleszar committed
93 94
  /* init LUT for lvl  and hev thr picking */
  lf_init_lut(lfi);
Attila Nagy's avatar
Attila Nagy committed
95

John Koleszar's avatar
John Koleszar committed
96 97 98 99
  /* init hev threshold const vectors */
  for (i = 0; i < 4; i++) {
    vpx_memset(lfi->hev_thr[i], i, SIMD_WIDTH);
  }
John Koleszar's avatar
John Koleszar committed
100 101
}

102
void vp9_loop_filter_frame_init(VP9_COMMON *cm,
103
                                MACROBLOCKD *xd,
John Koleszar's avatar
John Koleszar committed
104 105 106 107
                                int default_filt_lvl) {
  int seg,  /* segment number */
      ref,  /* index in ref_lf_deltas */
      mode; /* index in mode_lf_deltas */
John Koleszar's avatar
John Koleszar committed
108

John Koleszar's avatar
John Koleszar committed
109
  loop_filter_info_n *lfi = &cm->lf_info;
John Koleszar's avatar
John Koleszar committed
110

John Koleszar's avatar
John Koleszar committed
111 112
  /* update limits if sharpness has changed */
  if (cm->last_sharpness_level != cm->sharpness_level) {
113
    vp9_loop_filter_update_sharpness(lfi, cm->sharpness_level);
John Koleszar's avatar
John Koleszar committed
114 115 116 117 118 119 120 121 122
    cm->last_sharpness_level = cm->sharpness_level;
  }

  for (seg = 0; seg < MAX_MB_SEGMENTS; seg++) {
    int lvl_seg = default_filt_lvl;
    int lvl_ref, lvl_mode;


    // Set the baseline filter values for each segment
123
    if (vp9_segfeature_active(xd, seg, SEG_LVL_ALT_LF)) {
John Koleszar's avatar
John Koleszar committed
124 125
      /* Abs value */
      if (xd->mb_segment_abs_delta == SEGMENT_ABSDATA) {
126
        lvl_seg = vp9_get_segdata(xd, seg, SEG_LVL_ALT_LF);
John Koleszar's avatar
John Koleszar committed
127
      } else { /* Delta Value */
128
        lvl_seg += vp9_get_segdata(xd, seg, SEG_LVL_ALT_LF);
John Koleszar's avatar
John Koleszar committed
129 130 131
        lvl_seg = (lvl_seg > 0) ? ((lvl_seg > 63) ? 63 : lvl_seg) : 0;
      }
    }
John Koleszar's avatar
John Koleszar committed
132

John Koleszar's avatar
John Koleszar committed
133 134 135 136 137 138 139
    if (!xd->mode_ref_lf_delta_enabled) {
      /* we could get rid of this if we assume that deltas are set to
       * zero when not in use; encoder always uses deltas
       */
      vpx_memset(lfi->lvl[seg][0], lvl_seg, 4 * 4);
      continue;
    }
John Koleszar's avatar
John Koleszar committed
140

John Koleszar's avatar
John Koleszar committed
141
    lvl_ref = lvl_seg;
John Koleszar's avatar
John Koleszar committed
142

John Koleszar's avatar
John Koleszar committed
143 144
    /* INTRA_FRAME */
    ref = INTRA_FRAME;
John Koleszar's avatar
John Koleszar committed
145

John Koleszar's avatar
John Koleszar committed
146 147
    /* Apply delta for reference frame */
    lvl_ref += xd->ref_lf_deltas[ref];
Attila Nagy's avatar
Attila Nagy committed
148

John Koleszar's avatar
John Koleszar committed
149 150 151 152 153
    /* Apply delta for Intra modes */
    mode = 0; /* B_PRED */
    /* Only the split mode BPRED has a further special case */
    lvl_mode = lvl_ref +  xd->mode_lf_deltas[mode];
    lvl_mode = (lvl_mode > 0) ? (lvl_mode > 63 ? 63 : lvl_mode) : 0; /* clamp */
Attila Nagy's avatar
Attila Nagy committed
154

John Koleszar's avatar
John Koleszar committed
155
    lfi->lvl[seg][ref][mode] = lvl_mode;
Attila Nagy's avatar
Attila Nagy committed
156

John Koleszar's avatar
John Koleszar committed
157 158 159
    mode = 1; /* all the rest of Intra modes */
    lvl_mode = (lvl_ref > 0) ? (lvl_ref > 63 ? 63 : lvl_ref)  : 0; /* clamp */
    lfi->lvl[seg][ref][mode] = lvl_mode;
John Koleszar's avatar
John Koleszar committed
160

John Koleszar's avatar
John Koleszar committed
161 162 163
    /* LAST, GOLDEN, ALT */
    for (ref = 1; ref < MAX_REF_FRAMES; ref++) {
      int lvl_ref = lvl_seg;
John Koleszar's avatar
John Koleszar committed
164

John Koleszar's avatar
John Koleszar committed
165 166
      /* Apply delta for reference frame */
      lvl_ref += xd->ref_lf_deltas[ref];
John Koleszar's avatar
John Koleszar committed
167

John Koleszar's avatar
John Koleszar committed
168 169 170 171 172 173 174
      /* Apply delta for Inter modes */
      for (mode = 1; mode < 4; mode++) {
        lvl_mode = lvl_ref + xd->mode_lf_deltas[mode];
        lvl_mode = (lvl_mode > 0) ? (lvl_mode > 63 ? 63 : lvl_mode) : 0; /* clamp */

        lfi->lvl[seg][ref][mode] = lvl_mode;
      }
John Koleszar's avatar
John Koleszar committed
175
    }
John Koleszar's avatar
John Koleszar committed
176
  }
John Koleszar's avatar
John Koleszar committed
177 178
}

179
void vp9_loop_filter_frame(VP9_COMMON *cm, MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
180 181
  YV12_BUFFER_CONFIG *post = cm->frame_to_show;
  loop_filter_info_n *lfi_n = &cm->lf_info;
182
  struct loop_filter_info lfi;
John Koleszar's avatar
John Koleszar committed
183 184 185 186 187 188 189 190 191 192 193 194

  FRAME_TYPE frame_type = cm->frame_type;

  int mb_row;
  int mb_col;

  int filter_level;

  unsigned char *y_ptr, *u_ptr, *v_ptr;

  /* Point at base of Mb MODE_INFO list */
  const MODE_INFO *mode_info_context = cm->mi;
195 196 197
#if CONFIG_SUPERBLOCKS
  const int mis = cm->mode_info_stride;
#endif
John Koleszar's avatar
John Koleszar committed
198 199

  /* Initialize the loop filter for this frame. */
200
  vp9_loop_filter_frame_init(cm, xd, cm->filter_level);
John Koleszar's avatar
John Koleszar committed
201 202 203 204 205 206

  /* Set up the buffer pointers */
  y_ptr = post->y_buffer;
  u_ptr = post->u_buffer;
  v_ptr = post->v_buffer;

207
  /* vp9_filter each macro block */
John Koleszar's avatar
John Koleszar committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
    for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
      int skip_lf = (mode_info_context->mbmi.mode != B_PRED &&
                     mode_info_context->mbmi.mode != I8X8_PRED &&
                     mode_info_context->mbmi.mode != SPLITMV &&
                     mode_info_context->mbmi.mb_skip_coeff);

      const int mode_index = lfi_n->mode_lf_lut[mode_info_context->mbmi.mode];
      const int seg = mode_info_context->mbmi.segment_id;
      const int ref_frame = mode_info_context->mbmi.ref_frame;
      int tx_type = mode_info_context->mbmi.txfm_size;
      filter_level = lfi_n->lvl[seg][ref_frame][mode_index];

      if (filter_level) {
        if (cm->filter_type == NORMAL_LOOPFILTER) {
          const int hev_index = lfi_n->hev_thr_lut[frame_type][filter_level];
          lfi.mblim = lfi_n->mblim[filter_level];
          lfi.blim = lfi_n->blim[filter_level];
          lfi.lim = lfi_n->lim[filter_level];
          lfi.hev_thr = lfi_n->hev_thr[hev_index];

Ronald S. Bultje's avatar
Ronald S. Bultje committed
229 230 231
          if (mb_col > 0
#if CONFIG_SUPERBLOCKS
              && !((mb_col & 1) && mode_info_context->mbmi.encoded_as_sb &&
232 233 234 235 236 237
                   ((mode_info_context[0].mbmi.mb_skip_coeff &&
                     mode_info_context[-1].mbmi.mb_skip_coeff)
#if CONFIG_TX32X32
                    || mode_info_context[-1].mbmi.txfm_size == TX_32X32
#endif
                    ))
Ronald S. Bultje's avatar
Ronald S. Bultje committed
238 239
#endif
              )
240
            vp9_loop_filter_mbv(y_ptr, u_ptr, v_ptr, post->y_stride,
241
                                post->uv_stride, &lfi);
John Koleszar's avatar
John Koleszar committed
242

243
          if (!skip_lf && tx_type < TX_16X16) {
John Koleszar's avatar
John Koleszar committed
244
            if (tx_type == TX_8X8)
245
              vp9_loop_filter_bv8x8(y_ptr, u_ptr, v_ptr, post->y_stride,
246
                                    post->uv_stride, &lfi);
John Koleszar's avatar
John Koleszar committed
247
            else
248
              vp9_loop_filter_bv(y_ptr, u_ptr, v_ptr, post->y_stride,
249
                                 post->uv_stride, &lfi);
John Koleszar's avatar
John Koleszar committed
250 251 252 253

          }

          /* don't apply across umv border */
Ronald S. Bultje's avatar
Ronald S. Bultje committed
254 255 256
          if (mb_row > 0
#if CONFIG_SUPERBLOCKS
              && !((mb_row & 1) && mode_info_context->mbmi.encoded_as_sb &&
257 258 259 260 261 262
                   ((mode_info_context[0].mbmi.mb_skip_coeff &&
                     mode_info_context[-mis].mbmi.mb_skip_coeff)
#if CONFIG_TX32X32
                    || mode_info_context[-mis].mbmi.txfm_size == TX_32X32
#endif
                    ))
Ronald S. Bultje's avatar
Ronald S. Bultje committed
263 264
#endif
              )
265
            vp9_loop_filter_mbh(y_ptr, u_ptr, v_ptr, post->y_stride,
266
                                post->uv_stride, &lfi);
John Koleszar's avatar
John Koleszar committed
267

268
          if (!skip_lf && tx_type < TX_16X16) {
John Koleszar's avatar
John Koleszar committed
269
            if (tx_type == TX_8X8)
270
              vp9_loop_filter_bh8x8(y_ptr, u_ptr, v_ptr, post->y_stride,
271
                                    post->uv_stride, &lfi);
John Koleszar's avatar
John Koleszar committed
272
            else
273
              vp9_loop_filter_bh(y_ptr, u_ptr, v_ptr, post->y_stride,
274
                                 post->uv_stride, &lfi);
John Koleszar's avatar
John Koleszar committed
275 276
          }
        } else {
Daniel Kang's avatar
Daniel Kang committed
277
          // FIXME: Not 8x8 aware
Ronald S. Bultje's avatar
Ronald S. Bultje committed
278 279 280 281 282 283 284
          if (mb_col > 0
#if CONFIG_SUPERBLOCKS
              && !((mb_col & 1) && mode_info_context->mbmi.encoded_as_sb &&
                   mode_info_context[0].mbmi.mb_skip_coeff &&
                   mode_info_context[-1].mbmi.mb_skip_coeff)
#endif
              )
285
            vp9_loop_filter_simple_mbv(y_ptr, post->y_stride,
286
                                       lfi_n->mblim[filter_level]);
John Koleszar's avatar
John Koleszar committed
287 288

          if (!skip_lf)
289
            vp9_loop_filter_simple_bv(y_ptr, post->y_stride,
290
                                      lfi_n->blim[filter_level]);
John Koleszar's avatar
John Koleszar committed
291 292

          /* don't apply across umv border */
Ronald S. Bultje's avatar
Ronald S. Bultje committed
293 294 295 296 297 298 299
          if (mb_row > 0
#if CONFIG_SUPERBLOCKS
              && !((mb_row & 1) && mode_info_context->mbmi.encoded_as_sb &&
                   mode_info_context[0].mbmi.mb_skip_coeff &&
                   mode_info_context[-cm->mode_info_stride].mbmi.mb_skip_coeff)
#endif
              )
300
            vp9_loop_filter_simple_mbh(y_ptr, post->y_stride,
301
                                       lfi_n->mblim[filter_level]);
John Koleszar's avatar
John Koleszar committed
302 303

          if (!skip_lf)
304
            vp9_loop_filter_simple_bh(y_ptr, post->y_stride,
305
                                      lfi_n->blim[filter_level]);
John Koleszar's avatar
John Koleszar committed
306
        }
John Koleszar's avatar
John Koleszar committed
307
      }
John Koleszar's avatar
John Koleszar committed
308

John Koleszar's avatar
John Koleszar committed
309 310 311
      y_ptr += 16;
      u_ptr += 8;
      v_ptr += 8;
John Koleszar's avatar
John Koleszar committed
312

John Koleszar's avatar
John Koleszar committed
313
      mode_info_context++;     /* step to next MB */
John Koleszar's avatar
John Koleszar committed
314
    }
John Koleszar's avatar
John Koleszar committed
315 316 317 318 319 320 321

    y_ptr += post->y_stride  * 16 - post->y_width;
    u_ptr += post->uv_stride *  8 - post->uv_width;
    v_ptr += post->uv_stride *  8 - post->uv_width;

    mode_info_context++;         /* Skip border mb */
  }
John Koleszar's avatar
John Koleszar committed
322 323
}

324 325
void vp9_loop_filter_frame_yonly(VP9_COMMON *cm, MACROBLOCKD *xd,
                                 int default_filt_lvl) {
John Koleszar's avatar
John Koleszar committed
326
  YV12_BUFFER_CONFIG *post = cm->frame_to_show;
John Koleszar's avatar
John Koleszar committed
327

John Koleszar's avatar
John Koleszar committed
328 329 330
  unsigned char *y_ptr;
  int mb_row;
  int mb_col;
John Koleszar's avatar
John Koleszar committed
331

John Koleszar's avatar
John Koleszar committed
332
  loop_filter_info_n *lfi_n = &cm->lf_info;
333
  struct loop_filter_info lfi;
Attila Nagy's avatar
Attila Nagy committed
334

John Koleszar's avatar
John Koleszar committed
335 336
  int filter_level;
  FRAME_TYPE frame_type = cm->frame_type;
John Koleszar's avatar
John Koleszar committed
337

John Koleszar's avatar
John Koleszar committed
338 339
  /* Point at base of Mb MODE_INFO list */
  const MODE_INFO *mode_info_context = cm->mi;
John Koleszar's avatar
John Koleszar committed
340

Attila Nagy's avatar
Attila Nagy committed
341
#if 0
John Koleszar's avatar
John Koleszar committed
342 343
  if (default_filt_lvl == 0) /* no filter applied */
    return;
Attila Nagy's avatar
Attila Nagy committed
344
#endif
John Koleszar's avatar
John Koleszar committed
345

John Koleszar's avatar
John Koleszar committed
346
  /* Initialize the loop filter for this frame. */
347
  vp9_loop_filter_frame_init(cm, xd, default_filt_lvl);
John Koleszar's avatar
John Koleszar committed
348 349 350 351

  /* Set up the buffer pointers */
  y_ptr = post->y_buffer;

352
  /* vp9_filter each macro block */
John Koleszar's avatar
John Koleszar committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
  for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
    for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
      int skip_lf = (mode_info_context->mbmi.mode != B_PRED &&
                     mode_info_context->mbmi.mode != I8X8_PRED &&
                     mode_info_context->mbmi.mode != SPLITMV &&
                     mode_info_context->mbmi.mb_skip_coeff);

      const int mode_index = lfi_n->mode_lf_lut[mode_info_context->mbmi.mode];
      const int seg = mode_info_context->mbmi.segment_id;
      const int ref_frame = mode_info_context->mbmi.ref_frame;
      int tx_type = mode_info_context->mbmi.txfm_size;
      filter_level = lfi_n->lvl[seg][ref_frame][mode_index];

      if (filter_level) {
        if (cm->filter_type == NORMAL_LOOPFILTER) {
          const int hev_index = lfi_n->hev_thr_lut[frame_type][filter_level];
          lfi.mblim = lfi_n->mblim[filter_level];
          lfi.blim = lfi_n->blim[filter_level];
          lfi.lim = lfi_n->lim[filter_level];
          lfi.hev_thr = lfi_n->hev_thr[hev_index];

          if (mb_col > 0)
375
            vp9_loop_filter_mbv(y_ptr, 0, 0, post->y_stride, 0, &lfi);
John Koleszar's avatar
John Koleszar committed
376

377
          if (!skip_lf && tx_type != TX_16X16) {
John Koleszar's avatar
John Koleszar committed
378
            if (tx_type == TX_8X8)
379
              vp9_loop_filter_bv8x8(y_ptr, 0, 0, post->y_stride, 0, &lfi);
John Koleszar's avatar
John Koleszar committed
380
            else
381
              vp9_loop_filter_bv(y_ptr, 0, 0, post->y_stride, 0, &lfi);
John Koleszar's avatar
John Koleszar committed
382 383 384 385
          }

          /* don't apply across umv border */
          if (mb_row > 0)
386
            vp9_loop_filter_mbh(y_ptr, 0, 0, post->y_stride, 0, &lfi);
John Koleszar's avatar
John Koleszar committed
387

388
          if (!skip_lf && tx_type != TX_16X16) {
John Koleszar's avatar
John Koleszar committed
389
            if (tx_type == TX_8X8)
390
              vp9_loop_filter_bh8x8(y_ptr, 0, 0, post->y_stride, 0, &lfi);
John Koleszar's avatar
John Koleszar committed
391
            else
392
              vp9_loop_filter_bh(y_ptr, 0, 0, post->y_stride, 0, &lfi);
John Koleszar's avatar
John Koleszar committed
393 394
          }
        } else {
Daniel Kang's avatar
Daniel Kang committed
395
          // FIXME: Not 8x8 aware
John Koleszar's avatar
John Koleszar committed
396
          if (mb_col > 0)
397
            vp9_loop_filter_simple_mbv(y_ptr, post->y_stride,
398
                                       lfi_n->mblim[filter_level]);
John Koleszar's avatar
John Koleszar committed
399 400

          if (!skip_lf)
401
            vp9_loop_filter_simple_bv(y_ptr, post->y_stride,
402
                                      lfi_n->blim[filter_level]);
John Koleszar's avatar
John Koleszar committed
403 404 405

          /* don't apply across umv border */
          if (mb_row > 0)
406
            vp9_loop_filter_simple_mbh(y_ptr, post->y_stride,
407
                                       lfi_n->mblim[filter_level]);
John Koleszar's avatar
John Koleszar committed
408 409

          if (!skip_lf)
410
            vp9_loop_filter_simple_bh(y_ptr, post->y_stride,
411
                                      lfi_n->blim[filter_level]);
John Koleszar's avatar
John Koleszar committed
412
        }
John Koleszar's avatar
John Koleszar committed
413 414 415 416
      }

      y_ptr += 16;
      mode_info_context++;        /* step to next MB */
John Koleszar's avatar
John Koleszar committed
417 418
    }

John Koleszar's avatar
John Koleszar committed
419 420 421
    y_ptr += post->y_stride  * 16 - post->y_width;
    mode_info_context++;            /* Skip border mb */
  }
John Koleszar's avatar
John Koleszar committed
422
}
423

424 425
void vp9_loop_filter_partial_frame(VP9_COMMON *cm, MACROBLOCKD *xd,
                                   int default_filt_lvl) {
John Koleszar's avatar
John Koleszar committed
426 427 428 429 430 431 432 433 434 435
  YV12_BUFFER_CONFIG *post = cm->frame_to_show;

  unsigned char *y_ptr;
  int mb_row;
  int mb_col;
  int mb_cols = post->y_width  >> 4;

  int linestocopy, i;

  loop_filter_info_n *lfi_n = &cm->lf_info;
436
  struct loop_filter_info lfi;
John Koleszar's avatar
John Koleszar committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

  int filter_level;
  int alt_flt_enabled = xd->segmentation_enabled;
  FRAME_TYPE frame_type = cm->frame_type;

  const MODE_INFO *mode_info_context;

  int lvl_seg[MAX_MB_SEGMENTS];

  mode_info_context = cm->mi + (post->y_height >> 5) * (mb_cols + 1);

  /* 3 is a magic number. 4 is probably magic too */
  linestocopy = (post->y_height >> (4 + 3));

  if (linestocopy < 1)
    linestocopy = 1;

  linestocopy <<= 4;

  /* Note the baseline filter values for each segment */
457
  /* See vp9_loop_filter_frame_init. Rather than call that for each change
John Koleszar's avatar
John Koleszar committed
458 459 460 461 462 463
   * to default_filt_lvl, copy the relevant calculation here.
   */
  if (alt_flt_enabled) {
    for (i = 0; i < MAX_MB_SEGMENTS; i++) {
      /* Abs value */
      if (xd->mb_segment_abs_delta == SEGMENT_ABSDATA) {
464
        lvl_seg[i] = vp9_get_segdata(xd, i, SEG_LVL_ALT_LF);
John Koleszar's avatar
John Koleszar committed
465 466 467 468
      }
      /* Delta Value */
      else {
        lvl_seg[i] = default_filt_lvl +
469
                     vp9_get_segdata(xd, i, SEG_LVL_ALT_LF);
John Koleszar's avatar
John Koleszar committed
470 471 472
        lvl_seg[i] = (lvl_seg[i] > 0) ?
                     ((lvl_seg[i] > 63) ? 63 : lvl_seg[i]) : 0;
      }
John Koleszar's avatar
John Koleszar committed
473
    }
John Koleszar's avatar
John Koleszar committed
474 475 476 477 478
  }

  /* Set up the buffer pointers */
  y_ptr = post->y_buffer + (post->y_height >> 5) * 16 * post->y_stride;

479
  /* vp9_filter each macro block */
John Koleszar's avatar
John Koleszar committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
  for (mb_row = 0; mb_row < (linestocopy >> 4); mb_row++) {
    for (mb_col = 0; mb_col < mb_cols; mb_col++) {
      int skip_lf = (mode_info_context->mbmi.mode != B_PRED &&
                     mode_info_context->mbmi.mode != I8X8_PRED &&
                     mode_info_context->mbmi.mode != SPLITMV &&
                     mode_info_context->mbmi.mb_skip_coeff);

      if (alt_flt_enabled)
        filter_level = lvl_seg[mode_info_context->mbmi.segment_id];
      else
        filter_level = default_filt_lvl;

      if (filter_level) {
        if (cm->filter_type == NORMAL_LOOPFILTER) {
          const int hev_index = lfi_n->hev_thr_lut[frame_type][filter_level];
          lfi.mblim = lfi_n->mblim[filter_level];
          lfi.blim = lfi_n->blim[filter_level];
          lfi.lim = lfi_n->lim[filter_level];
          lfi.hev_thr = lfi_n->hev_thr[hev_index];

          if (mb_col > 0)
501
            vp9_loop_filter_mbv(y_ptr, 0, 0, post->y_stride, 0, &lfi);
John Koleszar's avatar
John Koleszar committed
502 503

          if (!skip_lf)
504
            vp9_loop_filter_bv(y_ptr, 0, 0, post->y_stride, 0, &lfi);
John Koleszar's avatar
John Koleszar committed
505

506
          vp9_loop_filter_mbh(y_ptr, 0, 0, post->y_stride, 0, &lfi);
John Koleszar's avatar
John Koleszar committed
507 508

          if (!skip_lf)
509
            vp9_loop_filter_bh(y_ptr, 0, 0, post->y_stride, 0, &lfi);
John Koleszar's avatar
John Koleszar committed
510 511
        } else {
          if (mb_col > 0)
512
            vp9_loop_filter_simple_mbv (y_ptr, post->y_stride,
513
                                        lfi_n->mblim[filter_level]);
John Koleszar's avatar
John Koleszar committed
514 515

          if (!skip_lf)
516
            vp9_loop_filter_simple_bv(y_ptr, post->y_stride,
517
                                      lfi_n->blim[filter_level]);
John Koleszar's avatar
John Koleszar committed
518

519
          vp9_loop_filter_simple_mbh(y_ptr, post->y_stride,
520
                                     lfi_n->mblim[filter_level]);
John Koleszar's avatar
John Koleszar committed
521 522

          if (!skip_lf)
523
            vp9_loop_filter_simple_bh(y_ptr, post->y_stride,
524
                                      lfi_n->blim[filter_level]);
John Koleszar's avatar
John Koleszar committed
525
        }
John Koleszar's avatar
John Koleszar committed
526
      }
John Koleszar's avatar
John Koleszar committed
527

John Koleszar's avatar
John Koleszar committed
528 529
      y_ptr += 16;
      mode_info_context += 1;      /* step to next MB */
John Koleszar's avatar
John Koleszar committed
530
    }
John Koleszar's avatar
John Koleszar committed
531 532 533 534

    y_ptr += post->y_stride  * 16 - post->y_width;
    mode_info_context += 1;          /* Skip border mb */
  }
John Koleszar's avatar
John Koleszar committed
535
}