summaryrefslogtreecommitdiffstats
path: root/libvpx/vp8/common/loopfilter.c
blob: 7a07e76fc41cd319bc57fa6902deafc7dc3fa3c4 (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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*
 *  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 "vpx_config.h"
#include "vp8_rtcd.h"
#include "loopfilter.h"
#include "onyxc_int.h"
#include "vpx_mem/vpx_mem.h"


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;
        }
    }

    lfi->mode_lf_lut[DC_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[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;

}

void vp8_loop_filter_update_sharpness(loop_filter_info_n *lfi,
                                      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;

        /* Set loop filter paramaeters that control sharpness. */
        block_inside_limit = filt_lvl >> (sharpness_lvl > 0);
        block_inside_limit = block_inside_limit >> (sharpness_lvl > 4);

        if (sharpness_lvl > 0)
        {
            if (block_inside_limit > (9 - sharpness_lvl))
                block_inside_limit = (9 - sharpness_lvl);
        }

        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);
    }
}

void vp8_loop_filter_init(VP8_COMMON *cm)
{
    loop_filter_info_n *lfi = &cm->lf_info;
    int i;

    /* init limits for given sharpness*/
    vp8_loop_filter_update_sharpness(lfi, cm->sharpness_level);
    cm->last_sharpness_level = cm->sharpness_level;

    /* init LUT for lvl  and hev thr picking */
    lf_init_lut(lfi);

    /* init hev threshold const vectors */
    for(i = 0; i < 4 ; i++)
    {
        vpx_memset(lfi->hev_thr[i], i, SIMD_WIDTH);
    }
}

void vp8_loop_filter_frame_init(VP8_COMMON *cm,
                                MACROBLOCKD *mbd,
                                int default_filt_lvl)
{
    int seg,  /* segment number */
        ref,  /* index in ref_lf_deltas */
        mode; /* index in mode_lf_deltas */

    loop_filter_info_n *lfi = &cm->lf_info;

    /* update limits if sharpness has changed */
    if(cm->last_sharpness_level != cm->sharpness_level)
    {
        vp8_loop_filter_update_sharpness(lfi, cm->sharpness_level);
        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;

        /* Note the baseline filter values for each segment */
        if (mbd->segmentation_enabled)
        {
            /* Abs value */
            if (mbd->mb_segement_abs_delta == SEGMENT_ABSDATA)
            {
                lvl_seg = mbd->segment_feature_data[MB_LVL_ALT_LF][seg];
            }
            else  /* Delta Value */
            {
                lvl_seg += mbd->segment_feature_data[MB_LVL_ALT_LF][seg];
                lvl_seg = (lvl_seg > 0) ? ((lvl_seg > 63) ? 63: lvl_seg) : 0;
            }
        }

        if (!mbd->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;
        }

        /* INTRA_FRAME */
        ref = INTRA_FRAME;

        /* Apply delta for reference frame */
        lvl_ref = lvl_seg + mbd->ref_lf_deltas[ref];

        /* Apply delta for Intra modes */
        mode = 0; /* B_PRED */
        /* Only the split mode BPRED has a further special case */
        lvl_mode = lvl_ref + mbd->mode_lf_deltas[mode];
        /* clamp */
        lvl_mode = (lvl_mode > 0) ? (lvl_mode > 63 ? 63 : lvl_mode) : 0;

        lfi->lvl[seg][ref][mode] = lvl_mode;

        mode = 1; /* all the rest of Intra modes */
        /* clamp */
        lvl_mode = (lvl_ref > 0) ? (lvl_ref > 63 ? 63 : lvl_ref) : 0;
        lfi->lvl[seg][ref][mode] = lvl_mode;

        /* LAST, GOLDEN, ALT */
        for(ref = 1; ref < MAX_REF_FRAMES; ref++)
        {
            /* Apply delta for reference frame */
            lvl_ref = lvl_seg + mbd->ref_lf_deltas[ref];

            /* Apply delta for Inter modes */
            for (mode = 1; mode < 4; mode++)
            {
                lvl_mode = lvl_ref + mbd->mode_lf_deltas[mode];
                /* clamp */
                lvl_mode = (lvl_mode > 0) ? (lvl_mode > 63 ? 63 : lvl_mode) : 0;

                lfi->lvl[seg][ref][mode] = lvl_mode;
            }
        }
    }
}


void vp8_loop_filter_row_normal(VP8_COMMON *cm, MODE_INFO *mode_info_context,
                         int mb_row, int post_ystride, int post_uvstride,
                         unsigned char *y_ptr, unsigned char *u_ptr,
                         unsigned char *v_ptr)
{
    int mb_col;
    int filter_level;
    loop_filter_info_n *lfi_n = &cm->lf_info;
    loop_filter_info lfi;
    FRAME_TYPE frame_type = cm->frame_type;

    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 != 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;

        filter_level = lfi_n->lvl[seg][ref_frame][mode_index];

        if (filter_level)
        {
            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)
                vp8_loop_filter_mbv
                (y_ptr, u_ptr, v_ptr, post_ystride, post_uvstride, &lfi);

            if (!skip_lf)
                vp8_loop_filter_bv
                (y_ptr, u_ptr, v_ptr, post_ystride, post_uvstride, &lfi);

            /* don't apply across umv border */
            if (mb_row > 0)
                vp8_loop_filter_mbh
                (y_ptr, u_ptr, v_ptr, post_ystride, post_uvstride, &lfi);

            if (!skip_lf)
                vp8_loop_filter_bh
                (y_ptr, u_ptr, v_ptr, post_ystride, post_uvstride, &lfi);
        }

        y_ptr += 16;
        u_ptr += 8;
        v_ptr += 8;

        mode_info_context++;     /* step to next MB */
    }

}

void vp8_loop_filter_row_simple(VP8_COMMON *cm, MODE_INFO *mode_info_context,
                         int mb_row, int post_ystride, int post_uvstride,
                         unsigned char *y_ptr, unsigned char *u_ptr,
                         unsigned char *v_ptr)
{
    int mb_col;
    int filter_level;
    loop_filter_info_n *lfi_n = &cm->lf_info;

    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 != 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;

        filter_level = lfi_n->lvl[seg][ref_frame][mode_index];

        if (filter_level)
        {
            if (mb_col > 0)
                vp8_loop_filter_simple_mbv
                (y_ptr, post_ystride, lfi_n->mblim[filter_level]);

            if (!skip_lf)
                vp8_loop_filter_simple_bv
                (y_ptr, post_ystride, lfi_n->blim[filter_level]);

            /* don't apply across umv border */
            if (mb_row > 0)
                vp8_loop_filter_simple_mbh
                (y_ptr, post_ystride, lfi_n->mblim[filter_level]);

            if (!skip_lf)
                vp8_loop_filter_simple_bh
                (y_ptr, post_ystride, lfi_n->blim[filter_level]);
        }

        y_ptr += 16;
        u_ptr += 8;
        v_ptr += 8;

        mode_info_context++;     /* step to next MB */
    }

}
void vp8_loop_filter_frame(VP8_COMMON *cm,
                           MACROBLOCKD *mbd,
                           int frame_type)
{
    YV12_BUFFER_CONFIG *post = cm->frame_to_show;
    loop_filter_info_n *lfi_n = &cm->lf_info;
    loop_filter_info lfi;

    int mb_row;
    int mb_col;
    int mb_rows = cm->mb_rows;
    int mb_cols = cm->mb_cols;

    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;
    int post_y_stride = post->y_stride;
    int post_uv_stride = post->uv_stride;

    /* Initialize the loop filter for this frame. */
    vp8_loop_filter_frame_init(cm, mbd, cm->filter_level);

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

    /* vp8_filter each macro block */
    if (cm->filter_type == NORMAL_LOOPFILTER)
    {
        for (mb_row = 0; mb_row < mb_rows; 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 != 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;

                filter_level = lfi_n->lvl[seg][ref_frame][mode_index];

                if (filter_level)
                {
                    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)
                        vp8_loop_filter_mbv
                        (y_ptr, u_ptr, v_ptr, post_y_stride, post_uv_stride, &lfi);

                    if (!skip_lf)
                        vp8_loop_filter_bv
                        (y_ptr, u_ptr, v_ptr, post_y_stride, post_uv_stride, &lfi);

                    /* don't apply across umv border */
                    if (mb_row > 0)
                        vp8_loop_filter_mbh
                        (y_ptr, u_ptr, v_ptr, post_y_stride, post_uv_stride, &lfi);

                    if (!skip_lf)
                        vp8_loop_filter_bh
                        (y_ptr, u_ptr, v_ptr, post_y_stride, post_uv_stride, &lfi);
                }

                y_ptr += 16;
                u_ptr += 8;
                v_ptr += 8;

                mode_info_context++;     /* step to next MB */
            }
            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 */

        }
    }
    else /* SIMPLE_LOOPFILTER */
    {
        for (mb_row = 0; mb_row < mb_rows; 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 != 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;

                filter_level = lfi_n->lvl[seg][ref_frame][mode_index];
                if (filter_level)
                {
                    const unsigned char * mblim = lfi_n->mblim[filter_level];
                    const unsigned char * blim = lfi_n->blim[filter_level];

                    if (mb_col > 0)
                        vp8_loop_filter_simple_mbv
                        (y_ptr, post_y_stride, mblim);

                    if (!skip_lf)
                        vp8_loop_filter_simple_bv
                        (y_ptr, post_y_stride, blim);

                    /* don't apply across umv border */
                    if (mb_row > 0)
                        vp8_loop_filter_simple_mbh
                        (y_ptr, post_y_stride, mblim);

                    if (!skip_lf)
                        vp8_loop_filter_simple_bh
                        (y_ptr, post_y_stride, blim);
                }

                y_ptr += 16;
                u_ptr += 8;
                v_ptr += 8;

                mode_info_context++;     /* step to next MB */
            }
            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 */

        }
    }
}

void vp8_loop_filter_frame_yonly
(
    VP8_COMMON *cm,
    MACROBLOCKD *mbd,
    int default_filt_lvl
)
{
    YV12_BUFFER_CONFIG *post = cm->frame_to_show;

    unsigned char *y_ptr;
    int mb_row;
    int mb_col;

    loop_filter_info_n *lfi_n = &cm->lf_info;
    loop_filter_info lfi;

    int filter_level;
    FRAME_TYPE frame_type = cm->frame_type;

    /* Point at base of Mb MODE_INFO list */
    const MODE_INFO *mode_info_context = cm->mi;

#if 0
    if(default_filt_lvl == 0) /* no filter applied */
        return;
#endif

    /* Initialize the loop filter for this frame. */
    vp8_loop_filter_frame_init( cm, mbd, default_filt_lvl);

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

    /* vp8_filter each macro block */
    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 != 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;

            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)
                        vp8_loop_filter_mbv
                        (y_ptr, 0, 0, post->y_stride, 0, &lfi);

                    if (!skip_lf)
                        vp8_loop_filter_bv
                        (y_ptr, 0, 0, post->y_stride, 0, &lfi);

                    /* don't apply across umv border */
                    if (mb_row > 0)
                        vp8_loop_filter_mbh
                        (y_ptr, 0, 0, post->y_stride, 0, &lfi);

                    if (!skip_lf)
                        vp8_loop_filter_bh
                        (y_ptr, 0, 0, post->y_stride, 0, &lfi);
                }
                else
                {
                    if (mb_col > 0)
                        vp8_loop_filter_simple_mbv
                        (y_ptr, post->y_stride, lfi_n->mblim[filter_level]);

                    if (!skip_lf)
                        vp8_loop_filter_simple_bv
                        (y_ptr, post->y_stride, lfi_n->blim[filter_level]);

                    /* don't apply across umv border */
                    if (mb_row > 0)
                        vp8_loop_filter_simple_mbh
                        (y_ptr, post->y_stride, lfi_n->mblim[filter_level]);

                    if (!skip_lf)
                        vp8_loop_filter_simple_bh
                        (y_ptr, post->y_stride, lfi_n->blim[filter_level]);
                }
            }

            y_ptr += 16;
            mode_info_context ++;        /* step to next MB */

        }

        y_ptr += post->y_stride  * 16 - post->y_width;
        mode_info_context ++;            /* Skip border mb */
    }

}

void vp8_loop_filter_partial_frame
(
    VP8_COMMON *cm,
    MACROBLOCKD *mbd,
    int default_filt_lvl
)
{
    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 mb_rows = post->y_height >> 4;

    int linestocopy;

    loop_filter_info_n *lfi_n = &cm->lf_info;
    loop_filter_info lfi;

    int filter_level;
    FRAME_TYPE frame_type = cm->frame_type;

    const MODE_INFO *mode_info_context;

#if 0
    if(default_filt_lvl == 0) /* no filter applied */
        return;
#endif

    /* Initialize the loop filter for this frame. */
    vp8_loop_filter_frame_init( cm, mbd, default_filt_lvl);

    /* number of MB rows to use in partial filtering */
    linestocopy = mb_rows / PARTIAL_FRAME_FRACTION;
    linestocopy = linestocopy ? linestocopy << 4 : 16;     /* 16 lines per MB */

    /* Set up the buffer pointers; partial image starts at ~middle of frame */
    y_ptr = post->y_buffer + ((post->y_height >> 5) * 16) * post->y_stride;
    mode_info_context = cm->mi + (post->y_height >> 5) * (mb_cols + 1);

    /* vp8_filter each macro block */
    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 != 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;

            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)
                        vp8_loop_filter_mbv
                        (y_ptr, 0, 0, post->y_stride, 0, &lfi);

                    if (!skip_lf)
                        vp8_loop_filter_bv
                        (y_ptr, 0, 0, post->y_stride, 0, &lfi);

                    vp8_loop_filter_mbh
                        (y_ptr, 0, 0, post->y_stride, 0, &lfi);

                    if (!skip_lf)
                        vp8_loop_filter_bh
                        (y_ptr, 0, 0, post->y_stride, 0, &lfi);
                }
                else
                {
                    if (mb_col > 0)
                        vp8_loop_filter_simple_mbv
                        (y_ptr, post->y_stride, lfi_n->mblim[filter_level]);

                    if (!skip_lf)
                        vp8_loop_filter_simple_bv
                        (y_ptr, post->y_stride, lfi_n->blim[filter_level]);

                    vp8_loop_filter_simple_mbh
                        (y_ptr, post->y_stride, lfi_n->mblim[filter_level]);

                    if (!skip_lf)
                        vp8_loop_filter_simple_bh
                        (y_ptr, post->y_stride, lfi_n->blim[filter_level]);
                }
            }

            y_ptr += 16;
            mode_info_context += 1;      /* step to next MB */
        }

        y_ptr += post->y_stride  * 16 - post->y_width;
        mode_info_context += 1;          /* Skip border mb */
    }
}