Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h264_loopfilter.c @ a8f49215

History | View | Annotate | Download (32.6 KB)

1
/*
2
 * H.26L/H.264/AVC/JVT/14496-10/... loop filter
3
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
/**
23
 * @file libavcodec/h264_loopfilter.c
24
 * H.264 / AVC / MPEG4 part10 loop filter.
25
 * @author Michael Niedermayer <michaelni@gmx.at>
26
 */
27

    
28
#include "internal.h"
29
#include "dsputil.h"
30
#include "avcodec.h"
31
#include "mpegvideo.h"
32
#include "h264.h"
33
#include "mathops.h"
34
#include "rectangle.h"
35

    
36
#if ARCH_X86
37
#include "x86/h264_i386.h"
38
#endif
39

    
40
//#undef NDEBUG
41
#include <assert.h>
42

    
43
/* Deblocking filter (p153) */
44
static const uint8_t alpha_table[52*3] = {
45
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
46
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
47
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
48
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
49
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
50
     0,  0,  0,  0,  0,  0,  4,  4,  5,  6,
51
     7,  8,  9, 10, 12, 13, 15, 17, 20, 22,
52
    25, 28, 32, 36, 40, 45, 50, 56, 63, 71,
53
    80, 90,101,113,127,144,162,182,203,226,
54
   255,255,
55
   255,255,255,255,255,255,255,255,255,255,255,255,255,
56
   255,255,255,255,255,255,255,255,255,255,255,255,255,
57
   255,255,255,255,255,255,255,255,255,255,255,255,255,
58
   255,255,255,255,255,255,255,255,255,255,255,255,255,
59
};
60
static const uint8_t beta_table[52*3] = {
61
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
62
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
63
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
64
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
65
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
66
     0,  0,  0,  0,  0,  0,  2,  2,  2,  3,
67
     3,  3,  3,  4,  4,  4,  6,  6,  7,  7,
68
     8,  8,  9,  9, 10, 10, 11, 11, 12, 12,
69
    13, 13, 14, 14, 15, 15, 16, 16, 17, 17,
70
    18, 18,
71
    18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
72
    18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
73
    18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
74
    18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
75
};
76
static const uint8_t tc0_table[52*3][4] = {
77
    {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
78
    {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
79
    {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
80
    {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
81
    {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
82
    {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
83
    {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
84
    {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
85
    {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
86
    {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
87
    {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
88
    {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 1 },
89
    {-1, 0, 0, 1 }, {-1, 0, 0, 1 }, {-1, 0, 0, 1 }, {-1, 0, 1, 1 }, {-1, 0, 1, 1 }, {-1, 1, 1, 1 },
90
    {-1, 1, 1, 1 }, {-1, 1, 1, 1 }, {-1, 1, 1, 1 }, {-1, 1, 1, 2 }, {-1, 1, 1, 2 }, {-1, 1, 1, 2 },
91
    {-1, 1, 1, 2 }, {-1, 1, 2, 3 }, {-1, 1, 2, 3 }, {-1, 2, 2, 3 }, {-1, 2, 2, 4 }, {-1, 2, 3, 4 },
92
    {-1, 2, 3, 4 }, {-1, 3, 3, 5 }, {-1, 3, 4, 6 }, {-1, 3, 4, 6 }, {-1, 4, 5, 7 }, {-1, 4, 5, 8 },
93
    {-1, 4, 6, 9 }, {-1, 5, 7,10 }, {-1, 6, 8,11 }, {-1, 6, 8,13 }, {-1, 7,10,14 }, {-1, 8,11,16 },
94
    {-1, 9,12,18 }, {-1,10,13,20 }, {-1,11,15,23 }, {-1,13,17,25 },
95
    {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
96
    {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
97
    {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
98
    {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
99
    {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
100
    {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
101
    {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
102
    {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
103
    {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
104
};
105

    
106
static void av_noinline filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
107
    const int index_a = qp + h->slice_alpha_c0_offset;
108
    const int alpha = (alpha_table+52)[index_a];
109
    const int beta  = (beta_table+52)[qp + h->slice_beta_offset];
110
    if (alpha ==0 || beta == 0) return;
111

    
112
    if( bS[0] < 4 ) {
113
        int8_t tc[4];
114
        tc[0] = (tc0_table+52)[index_a][bS[0]];
115
        tc[1] = (tc0_table+52)[index_a][bS[1]];
116
        tc[2] = (tc0_table+52)[index_a][bS[2]];
117
        tc[3] = (tc0_table+52)[index_a][bS[3]];
118
        h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
119
    } else {
120
        h->s.dsp.h264_h_loop_filter_luma_intra(pix, stride, alpha, beta);
121
    }
122
}
123
static void av_noinline filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
124
    const int index_a = qp + h->slice_alpha_c0_offset;
125
    const int alpha = (alpha_table+52)[index_a];
126
    const int beta  = (beta_table+52)[qp + h->slice_beta_offset];
127
    if (alpha ==0 || beta == 0) return;
128

    
129
    if( bS[0] < 4 ) {
130
        int8_t tc[4];
131
        tc[0] = (tc0_table+52)[index_a][bS[0]]+1;
132
        tc[1] = (tc0_table+52)[index_a][bS[1]]+1;
133
        tc[2] = (tc0_table+52)[index_a][bS[2]]+1;
134
        tc[3] = (tc0_table+52)[index_a][bS[3]]+1;
135
        h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
136
    } else {
137
        h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
138
    }
139
}
140

    
141
static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
142
    int i;
143
    for( i = 0; i < 16; i++, pix += stride) {
144
        int index_a;
145
        int alpha;
146
        int beta;
147

    
148
        int qp_index;
149
        int bS_index = (i >> 1);
150
        if (!MB_FIELD) {
151
            bS_index &= ~1;
152
            bS_index |= (i & 1);
153
        }
154

    
155
        if( bS[bS_index] == 0 ) {
156
            continue;
157
        }
158

    
159
        qp_index = MB_FIELD ? (i >> 3) : (i & 1);
160
        index_a = qp[qp_index] + h->slice_alpha_c0_offset;
161
        alpha = (alpha_table+52)[index_a];
162
        beta  = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
163

    
164
        if( bS[bS_index] < 4 ) {
165
            const int tc0 = (tc0_table+52)[index_a][bS[bS_index]];
166
            const int p0 = pix[-1];
167
            const int p1 = pix[-2];
168
            const int p2 = pix[-3];
169
            const int q0 = pix[0];
170
            const int q1 = pix[1];
171
            const int q2 = pix[2];
172

    
173
            if( FFABS( p0 - q0 ) < alpha &&
174
                FFABS( p1 - p0 ) < beta &&
175
                FFABS( q1 - q0 ) < beta ) {
176
                int tc = tc0;
177
                int i_delta;
178

    
179
                if( FFABS( p2 - p0 ) < beta ) {
180
                    pix[-2] = p1 + av_clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
181
                    tc++;
182
                }
183
                if( FFABS( q2 - q0 ) < beta ) {
184
                    pix[1] = q1 + av_clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
185
                    tc++;
186
                }
187

    
188
                i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
189
                pix[-1] = av_clip_uint8( p0 + i_delta );    /* p0' */
190
                pix[0]  = av_clip_uint8( q0 - i_delta );    /* q0' */
191
                tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
192
            }
193
        }else{
194
            const int p0 = pix[-1];
195
            const int p1 = pix[-2];
196
            const int p2 = pix[-3];
197

    
198
            const int q0 = pix[0];
199
            const int q1 = pix[1];
200
            const int q2 = pix[2];
201

    
202
            if( FFABS( p0 - q0 ) < alpha &&
203
                FFABS( p1 - p0 ) < beta &&
204
                FFABS( q1 - q0 ) < beta ) {
205

    
206
                if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
207
                    if( FFABS( p2 - p0 ) < beta)
208
                    {
209
                        const int p3 = pix[-4];
210
                        /* p0', p1', p2' */
211
                        pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
212
                        pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
213
                        pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
214
                    } else {
215
                        /* p0' */
216
                        pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
217
                    }
218
                    if( FFABS( q2 - q0 ) < beta)
219
                    {
220
                        const int q3 = pix[3];
221
                        /* q0', q1', q2' */
222
                        pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
223
                        pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
224
                        pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
225
                    } else {
226
                        /* q0' */
227
                        pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
228
                    }
229
                }else{
230
                    /* p0', q0' */
231
                    pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
232
                    pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
233
                }
234
                tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, p2, p1, p0, q0, q1, q2, pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
235
            }
236
        }
237
    }
238
}
239
static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
240
    int i;
241
    for( i = 0; i < 8; i++, pix += stride) {
242
        int index_a;
243
        int alpha;
244
        int beta;
245

    
246
        int qp_index;
247
        int bS_index = i;
248

    
249
        if( bS[bS_index] == 0 ) {
250
            continue;
251
        }
252

    
253
        qp_index = MB_FIELD ? (i >> 2) : (i & 1);
254
        index_a = qp[qp_index] + h->slice_alpha_c0_offset;
255
        alpha = (alpha_table+52)[index_a];
256
        beta  = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
257

    
258
        if( bS[bS_index] < 4 ) {
259
            const int tc = (tc0_table+52)[index_a][bS[bS_index]] + 1;
260
            const int p0 = pix[-1];
261
            const int p1 = pix[-2];
262
            const int q0 = pix[0];
263
            const int q1 = pix[1];
264

    
265
            if( FFABS( p0 - q0 ) < alpha &&
266
                FFABS( p1 - p0 ) < beta &&
267
                FFABS( q1 - q0 ) < beta ) {
268
                const int i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
269

    
270
                pix[-1] = av_clip_uint8( p0 + i_delta );    /* p0' */
271
                pix[0]  = av_clip_uint8( q0 - i_delta );    /* q0' */
272
                tprintf(h->s.avctx, "filter_mb_mbaff_edgecv i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
273
            }
274
        }else{
275
            const int p0 = pix[-1];
276
            const int p1 = pix[-2];
277
            const int q0 = pix[0];
278
            const int q1 = pix[1];
279

    
280
            if( FFABS( p0 - q0 ) < alpha &&
281
                FFABS( p1 - p0 ) < beta &&
282
                FFABS( q1 - q0 ) < beta ) {
283

    
284
                pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;   /* p0' */
285
                pix[0]  = ( 2*q1 + q0 + p1 + 2 ) >> 2;   /* q0' */
286
                tprintf(h->s.avctx, "filter_mb_mbaff_edgecv i:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, pix[-3], p1, p0, q0, q1, pix[2], pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
287
            }
288
        }
289
    }
290
}
291

    
292
static void av_noinline filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
293
    const int index_a = qp + h->slice_alpha_c0_offset;
294
    const int alpha = (alpha_table+52)[index_a];
295
    const int beta  = (beta_table+52)[qp + h->slice_beta_offset];
296
    if (alpha ==0 || beta == 0) return;
297

    
298
    if( bS[0] < 4 ) {
299
        int8_t tc[4];
300
        tc[0] = (tc0_table+52)[index_a][bS[0]];
301
        tc[1] = (tc0_table+52)[index_a][bS[1]];
302
        tc[2] = (tc0_table+52)[index_a][bS[2]];
303
        tc[3] = (tc0_table+52)[index_a][bS[3]];
304
        h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
305
    } else {
306
        h->s.dsp.h264_v_loop_filter_luma_intra(pix, stride, alpha, beta);
307
    }
308
}
309

    
310
static void av_noinline filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
311
    const int index_a = qp + h->slice_alpha_c0_offset;
312
    const int alpha = (alpha_table+52)[index_a];
313
    const int beta  = (beta_table+52)[qp + h->slice_beta_offset];
314
    if (alpha ==0 || beta == 0) return;
315

    
316
    if( bS[0] < 4 ) {
317
        int8_t tc[4];
318
        tc[0] = (tc0_table+52)[index_a][bS[0]]+1;
319
        tc[1] = (tc0_table+52)[index_a][bS[1]]+1;
320
        tc[2] = (tc0_table+52)[index_a][bS[2]]+1;
321
        tc[3] = (tc0_table+52)[index_a][bS[3]]+1;
322
        h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
323
    } else {
324
        h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
325
    }
326
}
327

    
328
void ff_h264_filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
329
    MpegEncContext * const s = &h->s;
330
    int mb_y_firstrow = s->picture_structure == PICT_BOTTOM_FIELD;
331
    int mb_xy, mb_type;
332
    int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
333

    
334
    mb_xy = h->mb_xy;
335

    
336
    if(mb_x==0 || mb_y==mb_y_firstrow || !s->dsp.h264_loop_filter_strength || h->pps.chroma_qp_diff ||
337
       !(h->slice_type_nos == FF_I_TYPE ||
338
         (CABAC && h->slice_type_nos == FF_P_TYPE && !FIELD_PICTURE) ||
339
         (s->flags2 & CODEC_FLAG2_FAST)) ||
340
       (h->deblocking_filter == 2 && (h->slice_table[mb_xy] != h->slice_table[h->top_mb_xy] || //use slice_num
341
                                      h->slice_table[mb_xy] != h->slice_table[mb_xy - 1]))) {
342
        ff_h264_filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
343
        return;
344
    }
345
    assert(!FRAME_MBAFF);
346

    
347
    mb_type = s->current_picture.mb_type[mb_xy];
348
    qp = s->current_picture.qscale_table[mb_xy];
349
    qp0 = s->current_picture.qscale_table[mb_xy-1];
350
    qp1 = s->current_picture.qscale_table[h->top_mb_xy];
351
    qpc = get_chroma_qp( h, 0, qp );
352
    qpc0 = get_chroma_qp( h, 0, qp0 );
353
    qpc1 = get_chroma_qp( h, 0, qp1 );
354
    qp0 = (qp + qp0 + 1) >> 1;
355
    qp1 = (qp + qp1 + 1) >> 1;
356
    qpc0 = (qpc + qpc0 + 1) >> 1;
357
    qpc1 = (qpc + qpc1 + 1) >> 1;
358
    qp_thresh = 15 - h->slice_alpha_c0_offset;
359
    if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
360
       qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
361
        return;
362

    
363
    if( IS_INTRA(mb_type) ) {
364
        int16_t bS4[4] = {4,4,4,4};
365
        int16_t bS3[4] = {3,3,3,3};
366
        int16_t *bSH = FIELD_PICTURE ? bS3 : bS4;
367
        if( IS_8x8DCT(mb_type) ) {
368
            filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
369
            filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
370
            filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bSH, qp1 );
371
            filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
372
        } else {
373
            filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
374
            filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
375
            filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
376
            filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
377
            filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bSH, qp1 );
378
            filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
379
            filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
380
            filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
381
        }
382
        filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
383
        filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
384
        filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
385
        filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
386
        filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bSH, qpc1 );
387
        filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
388
        filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bSH, qpc1 );
389
        filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
390
        return;
391
    } else {
392
        DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
393
        uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
394
        int edges;
395
        if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
396
            edges = 4;
397
            bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
398
        } else {
399
            int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
400
                             (mb_type & MB_TYPE_16x8) ? 1 : 0;
401
            int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
402
                             && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
403
                             ? 3 : 0;
404
            int step = IS_8x8DCT(mb_type) ? 2 : 1;
405
            edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
406
            s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
407
                                              (h->slice_type_nos == FF_B_TYPE), edges, step, mask_edge0, mask_edge1, FIELD_PICTURE);
408
        }
409
        if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
410
            bSv[0][0] = 0x0004000400040004ULL;
411
        if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
412
            bSv[1][0] = FIELD_PICTURE ? 0x0003000300030003ULL : 0x0004000400040004ULL;
413

    
414
#define FILTER(hv,dir,edge)\
415
        if(bSv[dir][edge]) {\
416
            filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
417
            if(!(edge&1)) {\
418
                filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
419
                filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
420
            }\
421
        }
422
        if( edges == 1 ) {
423
            FILTER(v,0,0);
424
            FILTER(h,1,0);
425
        } else if( IS_8x8DCT(mb_type) ) {
426
            FILTER(v,0,0);
427
            FILTER(v,0,2);
428
            FILTER(h,1,0);
429
            FILTER(h,1,2);
430
        } else {
431
            FILTER(v,0,0);
432
            FILTER(v,0,1);
433
            FILTER(v,0,2);
434
            FILTER(v,0,3);
435
            FILTER(h,1,0);
436
            FILTER(h,1,1);
437
            FILTER(h,1,2);
438
            FILTER(h,1,3);
439
        }
440
#undef FILTER
441
    }
442
}
443

    
444

    
445
static av_always_inline void filter_mb_dir(H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize, int mb_xy, int mb_type, int mvy_limit, int first_vertical_edge_done, int dir) {
446
    MpegEncContext * const s = &h->s;
447
    int edge;
448
    const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
449
    const int mbm_type = s->current_picture.mb_type[mbm_xy];
450
    int start = h->slice_table[mbm_xy] == 0xFFFF ? 1 : 0;
451

    
452
    const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
453
                              == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
454
    // how often to recheck mv-based bS when iterating between edges
455
    const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
456
                          (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
457
    // how often to recheck mv-based bS when iterating along each edge
458
    const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
459

    
460
    if (first_vertical_edge_done) {
461
        start = 1;
462
    }
463

    
464
    if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
465
        start = 1;
466

    
467
    if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
468
        && !IS_INTERLACED(mb_type)
469
        && IS_INTERLACED(mbm_type)
470
        ) {
471
        // This is a special case in the norm where the filtering must
472
        // be done twice (one each of the field) even if we are in a
473
        // frame macroblock.
474
        //
475
        unsigned int tmp_linesize   = 2 *   linesize;
476
        unsigned int tmp_uvlinesize = 2 * uvlinesize;
477
        int mbn_xy = mb_xy - 2 * s->mb_stride;
478
        int qp;
479
        int i, j;
480
        int16_t bS[4];
481

    
482
        for(j=0; j<2; j++, mbn_xy += s->mb_stride){
483
            if( IS_INTRA(mb_type) ||
484
                IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
485
                bS[0] = bS[1] = bS[2] = bS[3] = 3;
486
            } else {
487
                const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
488
                for( i = 0; i < 4; i++ ) {
489
                    if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
490
                        mbn_nnz[i+4+3*8] != 0 )
491
                        bS[i] = 2;
492
                    else
493
                        bS[i] = 1;
494
                }
495
            }
496
            // Do not use s->qscale as luma quantizer because it has not the same
497
            // value in IPCM macroblocks.
498
            qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
499
            tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize);
500
            { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
501
            filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
502
            filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS,
503
                              ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
504
            filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS,
505
                              ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
506
        }
507

    
508
        start = 1;
509
    }
510

    
511
    /* Calculate bS */
512
    for( edge = start; edge < edges; edge++ ) {
513
        /* mbn_xy: neighbor macroblock */
514
        const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
515
        const int mbn_type = s->current_picture.mb_type[mbn_xy];
516
        int16_t bS[4];
517
        int qp;
518

    
519
        if( (edge&1) && IS_8x8DCT(mb_type) )
520
            continue;
521

    
522
        if( IS_INTRA(mb_type) ||
523
            IS_INTRA(mbn_type) ) {
524
            int value;
525
            if (edge == 0) {
526
                if (   (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
527
                    || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
528
                ) {
529
                    value = 4;
530
                } else {
531
                    value = 3;
532
                }
533
            } else {
534
                value = 3;
535
            }
536
            bS[0] = bS[1] = bS[2] = bS[3] = value;
537
        } else {
538
            int i, l;
539
            int mv_done;
540

    
541
            if( edge & mask_edge ) {
542
                bS[0] = bS[1] = bS[2] = bS[3] = 0;
543
                mv_done = 1;
544
            }
545
            else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
546
                bS[0] = bS[1] = bS[2] = bS[3] = 1;
547
                mv_done = 1;
548
            }
549
            else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
550
                int b_idx= 8 + 4 + edge * (dir ? 8:1);
551
                int bn_idx= b_idx - (dir ? 8:1);
552
                int v = 0;
553

    
554
                for( l = 0; !v && l < 1 + (h->slice_type_nos == FF_B_TYPE); l++ ) {
555
                    v |= h->ref_cache[l][b_idx] != h->ref_cache[l][bn_idx] |
556
                         h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] + 3 >= 7U |
557
                         FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
558
                }
559

    
560
                if(h->slice_type_nos == FF_B_TYPE && v){
561
                    v=0;
562
                    for( l = 0; !v && l < 2; l++ ) {
563
                        int ln= 1-l;
564
                        v |= h->ref_cache[l][b_idx] != h->ref_cache[ln][bn_idx] |
565
                            h->mv_cache[l][b_idx][0] - h->mv_cache[ln][bn_idx][0] + 3 >= 7U |
566
                            FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[ln][bn_idx][1] ) >= mvy_limit;
567
                    }
568
                }
569

    
570
                bS[0] = bS[1] = bS[2] = bS[3] = v;
571
                mv_done = 1;
572
            }
573
            else
574
                mv_done = 0;
575

    
576
            for( i = 0; i < 4; i++ ) {
577
                int x = dir == 0 ? edge : i;
578
                int y = dir == 0 ? i    : edge;
579
                int b_idx= 8 + 4 + x + 8*y;
580
                int bn_idx= b_idx - (dir ? 8:1);
581

    
582
                if( h->non_zero_count_cache[b_idx] |
583
                    h->non_zero_count_cache[bn_idx] ) {
584
                    bS[i] = 2;
585
                }
586
                else if(!mv_done)
587
                {
588
                    bS[i] = 0;
589
                    for( l = 0; l < 1 + (h->slice_type_nos == FF_B_TYPE); l++ ) {
590
                        if( h->ref_cache[l][b_idx] != h->ref_cache[l][bn_idx] |
591
                            h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] + 3 >= 7U |
592
                            FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
593
                            bS[i] = 1;
594
                            break;
595
                        }
596
                    }
597

    
598
                    if(h->slice_type_nos == FF_B_TYPE && bS[i]){
599
                        bS[i] = 0;
600
                        for( l = 0; l < 2; l++ ) {
601
                            int ln= 1-l;
602
                            if( h->ref_cache[l][b_idx] != h->ref_cache[ln][bn_idx] |
603
                                h->mv_cache[l][b_idx][0] - h->mv_cache[ln][bn_idx][0] + 3 >= 7U |
604
                                FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[ln][bn_idx][1] ) >= mvy_limit ) {
605
                                bS[i] = 1;
606
                                break;
607
                            }
608
                        }
609
                    }
610
                }
611
            }
612

    
613
            if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
614
                continue;
615
        }
616

    
617
        /* Filter edge */
618
        // Do not use s->qscale as luma quantizer because it has not the same
619
        // value in IPCM macroblocks.
620
        qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
621
        //tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp[0], s->current_picture.qscale_table[mbn_xy]);
622
        tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
623
        //{ int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
624
        if( dir == 0 ) {
625
            filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
626
            if( (edge&1) == 0 ) {
627
                filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS,
628
                                  ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
629
                filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS,
630
                                  ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
631
            }
632
        } else {
633
            filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
634
            if( (edge&1) == 0 ) {
635
                filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS,
636
                                  ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
637
                filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS,
638
                                  ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1);
639
            }
640
        }
641
    }
642
}
643

    
644
void ff_h264_filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
645
    MpegEncContext * const s = &h->s;
646
    const int mb_xy= mb_x + mb_y*s->mb_stride;
647
    const int mb_type = s->current_picture.mb_type[mb_xy];
648
    const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
649
    int first_vertical_edge_done = 0;
650
    av_unused int dir;
651
    int list;
652

    
653
    if (FRAME_MBAFF
654
            // left mb is in picture
655
            && h->slice_table[mb_xy-1] != 0xFFFF
656
            // and current and left pair do not have the same interlaced type
657
            && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
658
            // and left mb is in the same slice if deblocking_filter == 2
659
            && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
660
        /* First vertical edge is different in MBAFF frames
661
         * There are 8 different bS to compute and 2 different Qp
662
         */
663
        const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
664
        const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
665
        int16_t bS[8];
666
        int qp[2];
667
        int bqp[2];
668
        int rqp[2];
669
        int mb_qp, mbn0_qp, mbn1_qp;
670
        int i;
671
        first_vertical_edge_done = 1;
672

    
673
        if( IS_INTRA(mb_type) )
674
            bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
675
        else {
676
            for( i = 0; i < 8; i++ ) {
677
                int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
678

    
679
                if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
680
                    bS[i] = 4;
681
                else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
682
                         ((!h->pps.cabac && IS_8x8DCT(s->current_picture.mb_type[mbn_xy])) ?
683
                            (h->cbp_table[mbn_xy] & ((MB_FIELD ? (i&2) : (mb_y&1)) ? 8 : 2))
684
                                                                       :
685
                            h->non_zero_count[mbn_xy][7+(MB_FIELD ? (i&3) : (i>>2)+(mb_y&1)*2)*8]))
686
                    bS[i] = 2;
687
                else
688
                    bS[i] = 1;
689
            }
690
        }
691

    
692
        mb_qp = s->current_picture.qscale_table[mb_xy];
693
        mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
694
        mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
695
        qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
696
        bqp[0] = ( get_chroma_qp( h, 0, mb_qp ) +
697
                   get_chroma_qp( h, 0, mbn0_qp ) + 1 ) >> 1;
698
        rqp[0] = ( get_chroma_qp( h, 1, mb_qp ) +
699
                   get_chroma_qp( h, 1, mbn0_qp ) + 1 ) >> 1;
700
        qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
701
        bqp[1] = ( get_chroma_qp( h, 0, mb_qp ) +
702
                   get_chroma_qp( h, 0, mbn1_qp ) + 1 ) >> 1;
703
        rqp[1] = ( get_chroma_qp( h, 1, mb_qp ) +
704
                   get_chroma_qp( h, 1, mbn1_qp ) + 1 ) >> 1;
705

    
706
        /* Filter edge */
707
        tprintf(s->avctx, "filter mb:%d/%d MBAFF, QPy:%d/%d, QPb:%d/%d QPr:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], bqp[0], bqp[1], rqp[0], rqp[1], linesize, uvlinesize);
708
        { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
709
        filter_mb_mbaff_edgev ( h, &img_y [0], linesize,   bS, qp );
710
        filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, bqp );
711
        filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, rqp );
712
    }
713

    
714
#if CONFIG_SMALL
715
    for( dir = 0; dir < 2; dir++ )
716
        filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, dir ? 0 : first_vertical_edge_done, dir);
717
#else
718
    filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, first_vertical_edge_done, 0);
719
    filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, 0, 1);
720
#endif
721
}