]> git.sesse.net Git - x264/blob - common/deblock.c
MBAFF: First edge deblocking
[x264] / common / deblock.c
1 /*****************************************************************************
2  * deblock.c: deblocking
3  *****************************************************************************
4  * Copyright (C) 2003-2011 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *          Fiona Glaser <fiona@x264.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include "common.h"
29
30 /* Deblocking filter */
31 static const uint8_t i_alpha_table[52+12*3] =
32 {
33      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
34      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
35      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
36      0,  0,  0,  0,  0,  0,  4,  4,  5,  6,
37      7,  8,  9, 10, 12, 13, 15, 17, 20, 22,
38     25, 28, 32, 36, 40, 45, 50, 56, 63, 71,
39     80, 90,101,113,127,144,162,182,203,226,
40    255,255,
41    255,255,255,255,255,255,255,255,255,255,255,255,
42 };
43 static const uint8_t i_beta_table[52+12*3] =
44 {
45      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,
47      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
48      0,  0,  0,  0,  0,  0,  2,  2,  2,  3,
49      3,  3,  3,  4,  4,  4,  6,  6,  7,  7,
50      8,  8,  9,  9, 10, 10, 11, 11, 12, 12,
51     13, 13, 14, 14, 15, 15, 16, 16, 17, 17,
52     18, 18,
53     18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
54 };
55 static const int8_t i_tc0_table[52+12*3][4] =
56 {
57     {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
58     {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
59     {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
60     {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
61     {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
62     {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
63     {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 1 },
64     {-1, 0, 0, 1 }, {-1, 0, 0, 1 }, {-1, 0, 0, 1 }, {-1, 0, 1, 1 }, {-1, 0, 1, 1 }, {-1, 1, 1, 1 },
65     {-1, 1, 1, 1 }, {-1, 1, 1, 1 }, {-1, 1, 1, 1 }, {-1, 1, 1, 2 }, {-1, 1, 1, 2 }, {-1, 1, 1, 2 },
66     {-1, 1, 1, 2 }, {-1, 1, 2, 3 }, {-1, 1, 2, 3 }, {-1, 2, 2, 3 }, {-1, 2, 2, 4 }, {-1, 2, 3, 4 },
67     {-1, 2, 3, 4 }, {-1, 3, 3, 5 }, {-1, 3, 4, 6 }, {-1, 3, 4, 6 }, {-1, 4, 5, 7 }, {-1, 4, 5, 8 },
68     {-1, 4, 6, 9 }, {-1, 5, 7,10 }, {-1, 6, 8,11 }, {-1, 6, 8,13 }, {-1, 7,10,14 }, {-1, 8,11,16 },
69     {-1, 9,12,18 }, {-1,10,13,20 }, {-1,11,15,23 }, {-1,13,17,25 },
70     {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
71     {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
72 };
73 #define alpha_table(x) i_alpha_table[(x)+24]
74 #define beta_table(x)  i_beta_table[(x)+24]
75 #define tc0_table(x)   i_tc0_table[(x)+24]
76
77 /* From ffmpeg */
78 static ALWAYS_INLINE void deblock_edge_luma_c( pixel *pix, int xstride, int alpha, int beta, int8_t tc0 )
79 {
80     int p2 = pix[-3*xstride];
81     int p1 = pix[-2*xstride];
82     int p0 = pix[-1*xstride];
83     int q0 = pix[ 0*xstride];
84     int q1 = pix[ 1*xstride];
85     int q2 = pix[ 2*xstride];
86
87     if( abs( p0 - q0 ) < alpha && abs( p1 - p0 ) < beta && abs( q1 - q0 ) < beta )
88     {
89         int tc = tc0;
90         int delta;
91         if( abs( p2 - p0 ) < beta )
92         {
93             if( tc0 )
94                 pix[-2*xstride] = p1 + x264_clip3( (( p2 + ((p0 + q0 + 1) >> 1)) >> 1) - p1, -tc0, tc0 );
95             tc++;
96         }
97         if( abs( q2 - q0 ) < beta )
98         {
99             if( tc0 )
100                 pix[ 1*xstride] = q1 + x264_clip3( (( q2 + ((p0 + q0 + 1) >> 1)) >> 1) - q1, -tc0, tc0 );
101             tc++;
102         }
103
104         delta = x264_clip3( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
105         pix[-1*xstride] = x264_clip_pixel( p0 + delta );    /* p0' */
106         pix[ 0*xstride] = x264_clip_pixel( q0 - delta );    /* q0' */
107     }
108 }
109 static inline void deblock_luma_c( pixel *pix, int xstride, int ystride, int alpha, int beta, int8_t *tc0 )
110 {
111     for( int i = 0; i < 4; i++ )
112     {
113         if( tc0[i] < 0 )
114         {
115             pix += 4*ystride;
116             continue;
117         }
118         for( int d = 0; d < 4; d++, pix += ystride )
119             deblock_edge_luma_c( pix, xstride, alpha, beta, tc0[i] );
120     }
121 }
122 static inline void deblock_v_luma_mbaff_c( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 )
123 {
124     for( int d = 0; d < 8; d++, pix += stride )
125         deblock_edge_luma_c( pix, 1, alpha, beta, tc0[d>>1] );
126 }
127 static void deblock_v_luma_c( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 )
128 {
129     deblock_luma_c( pix, stride, 1, alpha, beta, tc0 );
130 }
131 static void deblock_h_luma_c( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 )
132 {
133     deblock_luma_c( pix, 1, stride, alpha, beta, tc0 );
134 }
135
136 static ALWAYS_INLINE void deblock_edge_chroma_c( pixel *pix, int xstride, int alpha, int beta, int8_t tc )
137 {
138     int p1 = pix[-2*xstride];
139     int p0 = pix[-1*xstride];
140     int q0 = pix[ 0*xstride];
141     int q1 = pix[ 1*xstride];
142
143     if( abs( p0 - q0 ) < alpha && abs( p1 - p0 ) < beta && abs( q1 - q0 ) < beta )
144     {
145         int delta = x264_clip3( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
146         pix[-1*xstride] = x264_clip_pixel( p0 + delta );    /* p0' */
147         pix[ 0*xstride] = x264_clip_pixel( q0 - delta );    /* q0' */
148     }
149 }
150 static inline void deblock_chroma_c( pixel *pix, int xstride, int ystride, int alpha, int beta, int8_t *tc0 )
151 {
152     for( int i = 0; i < 4; i++ )
153     {
154         int tc = tc0[i];
155         if( tc <= 0 )
156         {
157             pix += 2*ystride;
158             continue;
159         }
160         for( int d = 0; d < 2; d++, pix += ystride-2 )
161         for( int e = 0; e < 2; e++, pix++ )
162             deblock_edge_chroma_c( pix, xstride, alpha, beta, tc0[i] );
163     }
164 }
165 static inline void deblock_v_chroma_mbaff_c( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 )
166 {
167     for( int i = 0; i < 4; i++, pix += stride )
168         deblock_edge_chroma_c( pix, 2, alpha, beta, tc0[i] );
169 }
170 static void deblock_v_chroma_c( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 )
171 {
172     deblock_chroma_c( pix, stride, 2, alpha, beta, tc0 );
173 }
174 static void deblock_h_chroma_c( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 )
175 {
176     deblock_chroma_c( pix, 2, stride, alpha, beta, tc0 );
177 }
178
179 static ALWAYS_INLINE void deblock_edge_luma_intra_c( pixel *pix, int xstride, int alpha, int beta )
180 {
181     int p2 = pix[-3*xstride];
182     int p1 = pix[-2*xstride];
183     int p0 = pix[-1*xstride];
184     int q0 = pix[ 0*xstride];
185     int q1 = pix[ 1*xstride];
186     int q2 = pix[ 2*xstride];
187
188     if( abs( p0 - q0 ) < alpha && abs( p1 - p0 ) < beta && abs( q1 - q0 ) < beta )
189     {
190         if( abs( p0 - q0 ) < ((alpha >> 2) + 2) )
191         {
192             if( abs( p2 - p0 ) < beta ) /* p0', p1', p2' */
193             {
194                 const int p3 = pix[-4*xstride];
195                 pix[-1*xstride] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
196                 pix[-2*xstride] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
197                 pix[-3*xstride] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
198             }
199             else /* p0' */
200                 pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
201             if( abs( q2 - q0 ) < beta ) /* q0', q1', q2' */
202             {
203                 const int q3 = pix[3*xstride];
204                 pix[0*xstride] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
205                 pix[1*xstride] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
206                 pix[2*xstride] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
207             }
208             else /* q0' */
209                 pix[0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
210         }
211         else /* p0', q0' */
212         {
213             pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
214             pix[ 0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
215         }
216     }
217 }
218 static inline void deblock_luma_intra_c( pixel *pix, int xstride, int ystride, int alpha, int beta )
219 {
220     for( int d = 0; d < 16; d++, pix += ystride )
221         deblock_edge_luma_intra_c( pix, xstride, alpha, beta );
222 }
223 static inline void deblock_v_luma_intra_mbaff_c( pixel *pix, int ystride, int alpha, int beta )
224 {
225     for( int d = 0; d < 8; d++, pix += ystride )
226         deblock_edge_luma_intra_c( pix, 1, alpha, beta );
227 }
228 static void deblock_v_luma_intra_c( pixel *pix, int stride, int alpha, int beta )
229 {
230     deblock_luma_intra_c( pix, stride, 1, alpha, beta );
231 }
232 static void deblock_h_luma_intra_c( pixel *pix, int stride, int alpha, int beta )
233 {
234     deblock_luma_intra_c( pix, 1, stride, alpha, beta );
235 }
236
237 static ALWAYS_INLINE void deblock_edge_chroma_intra_c( pixel *pix, int xstride, int alpha, int beta )
238 {
239     int p1 = pix[-2*xstride];
240     int p0 = pix[-1*xstride];
241     int q0 = pix[ 0*xstride];
242     int q1 = pix[ 1*xstride];
243
244     if( abs( p0 - q0 ) < alpha && abs( p1 - p0 ) < beta && abs( q1 - q0 ) < beta )
245     {
246         pix[-1*xstride] = (2*p1 + p0 + q1 + 2) >> 2;   /* p0' */
247         pix[ 0*xstride] = (2*q1 + q0 + p1 + 2) >> 2;   /* q0' */
248     }
249 }
250 static inline void deblock_chroma_intra_c( pixel *pix, int xstride, int ystride, int alpha, int beta, int dir )
251 {
252     for( int d = 0; d < (dir?16:8); d++, pix += ystride-2 )
253     for( int e = 0; e < (dir?1:2); e++, pix++ )
254         deblock_edge_chroma_intra_c( pix, xstride, alpha, beta );
255 }
256 static inline void deblock_v_chroma_intra_mbaff_c( pixel *pix, int stride, int alpha, int beta )
257 {
258     for( int i = 0; i < 4; i++, pix += stride )
259         deblock_edge_chroma_intra_c( pix, 2, alpha, beta );
260 }
261 static void deblock_v_chroma_intra_c( pixel *pix, int stride, int alpha, int beta )
262 {
263     deblock_chroma_intra_c( pix, stride, 2, alpha, beta, 1 );
264 }
265 static void deblock_h_chroma_intra_c( pixel *pix, int stride, int alpha, int beta )
266 {
267     deblock_chroma_intra_c( pix, 2, stride, alpha, beta, 0 );
268 }
269
270 static void deblock_strength_c( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
271                                 int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4], int mvy_limit,
272                                 int bframe, x264_t *h )
273 {
274     for( int dir = 0; dir < 2; dir++ )
275     {
276         int s1 = dir ? 1 : 8;
277         int s2 = dir ? 8 : 1;
278         for( int edge = 0; edge < 4; edge++ )
279             for( int i = 0, loc = X264_SCAN8_0+edge*s2; i < 4; i++, loc += s1 )
280             {
281                 int locn = loc - s2;
282                 if( nnz[loc] || nnz[locn] )
283                     bs[dir][edge][i] = 2;
284                 else if( ref[0][loc] != ref[0][locn] ||
285                          abs( mv[0][loc][0] - mv[0][locn][0] ) >= 4 ||
286                          abs( mv[0][loc][1] - mv[0][locn][1] ) >= mvy_limit ||
287                         (bframe && (ref[1][loc] != ref[1][locn] ||
288                          abs( mv[1][loc][0] - mv[1][locn][0] ) >= 4 ||
289                          abs( mv[1][loc][1] - mv[1][locn][1] ) >= mvy_limit )))
290                 {
291                     bs[dir][edge][i] = 1;
292                 }
293                 else
294                     bs[dir][edge][i] = 0;
295             }
296     }
297 }
298
299 void deblock_strength_mbaff_c( uint8_t nnz_cache[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
300                                int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
301                                int mvy_limit, int bframe, x264_t *h )
302 {
303     int neighbour_field[2];
304     neighbour_field[0] = h->mb.i_mb_left_xy[0] >= 0 && h->mb.field[h->mb.i_mb_left_xy[0]];
305     neighbour_field[1] = h->mb.i_mb_top_xy >= 0 && h->mb.field[h->mb.i_mb_top_xy];
306     int intra_cur = IS_INTRA( h->mb.i_type );
307
308     if( !intra_cur )
309     {
310         for( int dir = 0; dir < 2; dir++ )
311         {
312             int edge_stride = dir ? 8 : 1;
313             int part_stride = dir ? 1 : 8;
314             for( int edge = 0; edge < 4; edge++ )
315             {
316                 for( int i = 0, q = X264_SCAN8_0+edge*edge_stride; i < 4; i++, q += part_stride )
317                 {
318                     int p = q - edge_stride;
319                     if( nnz_cache[q] || nnz_cache[p] )
320                     {
321                         bs[dir][edge][i] = 2;
322                     }
323                     else if( (edge == 0 && h->mb.b_interlaced != neighbour_field[dir]) ||
324                              ref[0][q] != ref[0][p] ||
325                              abs( mv[0][q][0] - mv[0][p][0] ) >= 4 ||
326                              abs( mv[0][q][1] - mv[0][p][1] ) >= mvy_limit ||
327                             (bframe && (ref[1][q] != ref[1][p] ||
328                              abs( mv[1][q][0] - mv[1][p][0] ) >= 4 ||
329                              abs( mv[1][q][1] - mv[1][p][1] ) >= mvy_limit )) )
330                     {
331                         bs[dir][edge][i] = 1;
332                     }
333                     else
334                         bs[dir][edge][i] = 0;
335                 }
336             }
337         }
338     }
339
340     if( h->mb.i_neighbour & MB_LEFT )
341     {
342         if( h->mb.field[h->mb.i_mb_left_xy[0]] != h->mb.b_interlaced )
343         {
344             static const uint8_t offset[2][2][8] = {
345                 {   { 0, 0, 0, 0, 1, 1, 1, 1 },
346                     { 2, 2, 2, 2, 3, 3, 3, 3 }, },
347                 {   { 0, 1, 2, 3, 0, 1, 2, 3 },
348                     { 0, 1, 2, 3, 0, 1, 2, 3 }, }
349             };
350             uint8_t bS[8];
351
352             if( intra_cur )
353                 memset( bS, 4, 8 );
354             else
355             {
356                 const uint8_t *off = offset[h->mb.b_interlaced][h->mb.i_mb_y&1];
357                 uint8_t (*nnz)[24] = h->mb.non_zero_count;
358
359                 for( int i = 0; i < 8; i++ )
360                 {
361                     int left = h->mb.i_mb_left_xy[h->mb.b_interlaced ? i>>2 : i&1];
362                     int nnz_this = h->mb.cache.non_zero_count[x264_scan8[0]+8*(i>>1)];
363                     int nnz_left = nnz[left][3 + 4*off[i]];
364                     if( !h->param.b_cabac && h->pps->b_transform_8x8_mode )
365                     {
366                         int j = off[i]&~1;
367                         if( h->mb.mb_transform_size[left] )
368                             nnz_left = !!(M16( &nnz[left][2+4*j] ) | M16( &nnz[left][2+4*(1+j)] ));
369                     }
370                     if( IS_INTRA( h->mb.type[left] ) )
371                         bS[i] = 4;
372                     else if( nnz_left || nnz_this )
373                         bS[i] = 2;
374                     else // As left is different interlaced.
375                         bS[i] = 1;
376                 }
377             }
378
379             if( h->mb.b_interlaced )
380             {
381                 for( int i = 0; i < 4; i++ ) bs[0][0][i] = bS[i];
382                 for( int i = 0; i < 4; i++ ) bs[0][4][i] = bS[4+i];
383             }
384             else
385             {
386                 for( int i = 0; i < 4; i++ ) bs[0][0][i] = bS[2*i];
387                 for( int i = 0; i < 4; i++ ) bs[0][4][i] = bS[1+2*i];
388             }
389         }
390     }
391
392     if( h->mb.i_neighbour & MB_TOP )
393     {
394         if( !(h->mb.i_mb_y&1) && !h->mb.b_interlaced && h->mb.field[h->mb.i_mb_top_xy] )
395         {
396             /* Need to filter both fields (even for frame macroblocks).
397              * Filter top two rows using the top macroblock of the above
398              * pair and then the bottom one. */
399             int mbn_xy = h->mb.i_mb_xy - 2 * h->mb.i_mb_stride;
400             uint32_t nnz_cur[4];
401             nnz_cur[0] = h->mb.cache.non_zero_count[x264_scan8[0]+0];
402             nnz_cur[1] = h->mb.cache.non_zero_count[x264_scan8[0]+1];
403             nnz_cur[2] = h->mb.cache.non_zero_count[x264_scan8[0]+2];
404             nnz_cur[3] = h->mb.cache.non_zero_count[x264_scan8[0]+3];
405             /* Munge NNZ for cavlc + 8x8dct */
406             if( !h->param.b_cabac && h->pps->b_transform_8x8_mode &&
407                 h->mb.mb_transform_size[h->mb.i_mb_xy] )
408             {
409                 int nnz0 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 0]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 2]] );
410                 int nnz1 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 4]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 6]] );
411                 nnz_cur[0] = nnz_cur[1] = !!nnz0;
412                 nnz_cur[2] = nnz_cur[3] = !!nnz1;
413             }
414
415             for( int j = 0; j < 2; j++, mbn_xy += h->mb.i_mb_stride )
416             {
417                 int mbn_intra = IS_INTRA( h->mb.type[mbn_xy] );
418                 uint8_t (*nnz)[24] = h->mb.non_zero_count;
419
420                 uint32_t nnz_top[4];
421                 nnz_top[0] = nnz[mbn_xy][3*4+0];
422                 nnz_top[1] = nnz[mbn_xy][3*4+1];
423                 nnz_top[2] = nnz[mbn_xy][3*4+2];
424                 nnz_top[3] = nnz[mbn_xy][3*4+3];
425
426                 if( !h->param.b_cabac && h->pps->b_transform_8x8_mode &&
427                     (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[mbn_xy] )
428                 {
429                     int nnz_top0 = M16( &nnz[mbn_xy][8] ) | M16( &nnz[mbn_xy][12] );
430                     int nnz_top1 = M16( &nnz[mbn_xy][10] ) | M16( &nnz[mbn_xy][14] );
431                     nnz_top[0] = nnz_top[1] = nnz_top0 ? 0x0101 : 0;
432                     nnz_top[2] = nnz_top[3] = nnz_top1 ? 0x0101 : 0;
433                 }
434
435                 uint8_t bS[4];
436                 if( intra_cur || mbn_intra )
437                     M32( bS ) = 0x03030303;
438                 else
439                 {
440                     for( int i = 0; i < 4; i++ )
441                     {
442                         if( nnz_cur[i] || nnz_top[i] )
443                             bS[i] = 2;
444                         else
445                             bS[i] = 1;
446                     }
447                 }
448                 for( int i = 0; i < 4; i++ )
449                     bs[1][4*j][i] = bS[i];
450             }
451         }
452     }
453 }
454
455 static inline void deblock_edge( x264_t *h, pixel *pix, int i_stride, uint8_t bS[4], int i_qp, int b_chroma, x264_deblock_inter_t pf_inter )
456 {
457     int index_a = i_qp-QP_BD_OFFSET + h->sh.i_alpha_c0_offset;
458     int index_b = i_qp-QP_BD_OFFSET + h->sh.i_beta_offset;
459     int alpha = alpha_table(index_a) << (BIT_DEPTH-8);
460     int beta  = beta_table(index_b) << (BIT_DEPTH-8);
461     int8_t tc[4];
462
463     if( !M32(bS) || !alpha || !beta )
464         return;
465
466     tc[0] = (tc0_table(index_a)[bS[0]] << (BIT_DEPTH-8)) + b_chroma;
467     tc[1] = (tc0_table(index_a)[bS[1]] << (BIT_DEPTH-8)) + b_chroma;
468     tc[2] = (tc0_table(index_a)[bS[2]] << (BIT_DEPTH-8)) + b_chroma;
469     tc[3] = (tc0_table(index_a)[bS[3]] << (BIT_DEPTH-8)) + b_chroma;
470
471     pf_inter( pix, i_stride, alpha, beta, tc );
472 }
473
474 static inline void deblock_edge_intra( x264_t *h, pixel *pix, int i_stride, uint8_t bS[4], int i_qp, int b_chroma, x264_deblock_intra_t pf_intra )
475 {
476     int index_a = i_qp-QP_BD_OFFSET + h->sh.i_alpha_c0_offset;
477     int index_b = i_qp-QP_BD_OFFSET + h->sh.i_beta_offset;
478     int alpha = alpha_table(index_a) << (BIT_DEPTH-8);
479     int beta  = beta_table(index_b) << (BIT_DEPTH-8);
480
481     if( !alpha || !beta )
482         return;
483
484     pf_intra( pix, i_stride, alpha, beta );
485 }
486
487 void x264_frame_deblock_row( x264_t *h, int mb_y )
488 {
489     int b_interlaced = h->sh.b_mbaff;
490     int qp_thresh = 15 - X264_MIN( h->sh.i_alpha_c0_offset, h->sh.i_beta_offset ) - X264_MAX( 0, h->pps->i_chroma_qp_index_offset );
491     int stridey   = h->fdec->i_stride[0];
492     int strideuv  = h->fdec->i_stride[1];
493
494     for( int mb_x = 0; mb_x < h->mb.i_mb_width; mb_x += (~b_interlaced | mb_y)&1, mb_y ^= b_interlaced )
495     {
496         x264_prefetch_fenc( h, h->fdec, mb_x, mb_y );
497         x264_macroblock_cache_load_neighbours_deblock( h, mb_x, mb_y );
498
499         int mb_xy = h->mb.i_mb_xy;
500         int transform_8x8 = h->mb.mb_transform_size[h->mb.i_mb_xy];
501         int intra_cur = IS_INTRA( h->mb.type[mb_xy] );
502         uint8_t (*bs)[8][4] = h->deblock_strength[mb_y&1][mb_x];
503
504         pixel *pixy = h->fdec->plane[0] + 16*mb_y*stridey  + 16*mb_x;
505         pixel *pixuv = h->fdec->plane[1] + 8*mb_y*strideuv + 16*mb_x;
506         if( mb_y & h->mb.b_interlaced )
507         {
508             pixy -= 15*stridey;
509             pixuv -= 7*strideuv;
510         }
511
512         int stride2y  = stridey << h->mb.b_interlaced;
513         int stride2uv = strideuv << h->mb.b_interlaced;
514         int qp = h->mb.qp[mb_xy];
515         int qpc = h->chroma_qp_table[qp];
516         int first_edge_only = h->mb.type[mb_xy] == P_SKIP || qp <= qp_thresh;
517
518         #define FILTER( intra, dir, edge, qp, chroma_qp )\
519         do\
520         {\
521             deblock_edge##intra( h, pixy + 4*edge*(dir?stride2y:1),\
522                                  stride2y, bs[dir][edge], qp, 0,\
523                                  h->loopf.deblock_luma##intra[dir] );\
524             if( !(edge & 1) )\
525                 deblock_edge##intra( h, pixuv + 2*edge*(dir?stride2uv:2),\
526                                      stride2uv, bs[dir][edge], chroma_qp, 1,\
527                                      h->loopf.deblock_chroma##intra[dir] );\
528         } while(0)
529
530         if( h->mb.i_neighbour & MB_LEFT )
531         {
532             if( b_interlaced && h->mb.field[h->mb.i_mb_left_xy[0]] != h->mb.b_interlaced )
533             {
534                 int luma_qp[2];
535                 int chroma_qp[2];
536                 int left_qp[2];
537                 int current_qp = h->mb.qp[mb_xy];
538                 left_qp[0] = h->mb.qp[h->mb.i_mb_left_xy[0]];
539                 luma_qp[0] = (current_qp + left_qp[0] + 1) >> 1;
540                 chroma_qp[0] = (h->chroma_qp_table[current_qp] + h->chroma_qp_table[left_qp[0]] + 1) >> 1;
541                 if( bs[0][0][0] == 4)
542                 {
543                     deblock_edge_intra( h, pixy,      2*stridey,  bs[0][0], luma_qp[0],   0, deblock_v_luma_intra_mbaff_c );
544                     deblock_edge_intra( h, pixuv,     2*strideuv, bs[0][0], chroma_qp[0], 1, deblock_v_chroma_intra_mbaff_c );
545                     deblock_edge_intra( h, pixuv + 1, 2*strideuv, bs[0][0], chroma_qp[0], 1, deblock_v_chroma_intra_mbaff_c );
546                 }
547                 else
548                 {
549                     deblock_edge( h, pixy,      2*stridey,  bs[0][0], luma_qp[0],   0, deblock_v_luma_mbaff_c );
550                     deblock_edge( h, pixuv,     2*strideuv, bs[0][0], chroma_qp[0], 1, deblock_v_chroma_mbaff_c );
551                     deblock_edge( h, pixuv + 1, 2*strideuv, bs[0][0], chroma_qp[0], 1, deblock_v_chroma_mbaff_c );
552                 }
553
554                 int offy = h->mb.b_interlaced ? 4 : 0;
555                 int offuv = h->mb.b_interlaced ? 3 : 0;
556                 left_qp[1] = h->mb.qp[h->mb.i_mb_left_xy[1]];
557                 luma_qp[1] = (current_qp + left_qp[1] + 1) >> 1;
558                 chroma_qp[1] = (h->chroma_qp_table[current_qp] + h->chroma_qp_table[left_qp[1]] + 1) >> 1;
559                 if( bs[0][4][0] == 4)
560                 {
561                     deblock_edge_intra( h, pixy      + (stridey<<offy),   2*stridey,  bs[0][4], luma_qp[1],   0, deblock_v_luma_intra_mbaff_c );
562                     deblock_edge_intra( h, pixuv     + (strideuv<<offuv), 2*strideuv, bs[0][4], chroma_qp[1], 1, deblock_v_chroma_intra_mbaff_c );
563                     deblock_edge_intra( h, pixuv + 1 + (strideuv<<offuv), 2*strideuv, bs[0][4], chroma_qp[1], 1, deblock_v_chroma_intra_mbaff_c );
564                 }
565                 else
566                 {
567                     deblock_edge( h, pixy      + (stridey<<offy),   2*stridey,  bs[0][4], luma_qp[1],   0, deblock_v_luma_mbaff_c );
568                     deblock_edge( h, pixuv     + (strideuv<<offuv), 2*strideuv, bs[0][4], chroma_qp[1], 1, deblock_v_chroma_mbaff_c );
569                     deblock_edge( h, pixuv + 1 + (strideuv<<offuv), 2*strideuv, bs[0][4], chroma_qp[1], 1, deblock_v_chroma_mbaff_c );
570                 }
571             }
572             else
573             {
574                 int qpl = h->mb.qp[h->mb.i_mb_xy-1];
575                 int qp_left = (qp + qpl + 1) >> 1;
576                 int qpc_left = (h->chroma_qp_table[qp] + h->chroma_qp_table[qpl] + 1) >> 1;
577                 int intra_left = IS_INTRA( h->mb.type[h->mb.i_mb_xy-1] );
578
579                 if( intra_cur || intra_left )
580                     FILTER( _intra, 0, 0, qp_left, qpc_left );
581                 else
582                     FILTER(       , 0, 0, qp_left, qpc_left );
583             }
584         }
585         if( !first_edge_only )
586         {
587             if( !transform_8x8 ) FILTER( , 0, 1, qp, qpc );
588                                  FILTER( , 0, 2, qp, qpc );
589             if( !transform_8x8 ) FILTER( , 0, 3, qp, qpc );
590         }
591
592         if( h->mb.i_neighbour & MB_TOP )
593         {
594             if( b_interlaced && !(mb_y&1) && !h->mb.b_interlaced && h->mb.field[h->mb.i_mb_top_xy] )
595             {
596                 int mbn_xy = mb_xy - 2 * h->mb.i_mb_stride;
597
598                 for(int j=0; j<2; j++, mbn_xy += h->mb.i_mb_stride)
599                 {
600                     int qpt = h->mb.qp[mbn_xy];
601                     int qp_top = (qp + qpt + 1) >> 1;
602                     int qpc_top = (h->chroma_qp_table[qp] + h->chroma_qp_table[qpt] + 1) >> 1;
603
604                     // deblock the first horizontal edge of the even rows, then the first horizontal edge of the odd rows
605                     deblock_edge( h, pixy      + j*stridey,  2* stridey, bs[1][4*j], qp_top,  0, deblock_v_luma_c );
606                     deblock_edge( h, pixuv     + j*strideuv, 2*strideuv, bs[1][4*j], qpc_top, 1, deblock_v_chroma_c );
607                 }
608             }
609             else
610             {
611                 int qpt = h->mb.qp[h->mb.i_mb_top_xy];
612                 int qp_top = (qp + qpt + 1) >> 1;
613                 int qpc_top = (h->chroma_qp_table[qp] + h->chroma_qp_table[qpt] + 1) >> 1;
614                 int intra_top = IS_INTRA( h->mb.type[h->mb.i_mb_top_xy] );
615
616                 if( (!b_interlaced || (!h->mb.b_interlaced && !h->mb.field[h->mb.i_mb_top_xy]))
617                     && (intra_cur || intra_top) )
618                 {
619                     FILTER( _intra, 1, 0, qp_top, qpc_top );
620                 }
621                 else
622                 {
623                     if( intra_top )
624                         M32( bs[1][0] ) = 0x03030303;
625                     FILTER(       , 1, 0, qp_top, qpc_top );
626                 }
627             }
628         }
629
630         if( !first_edge_only )
631         {
632             if( !transform_8x8 ) FILTER( , 1, 1, qp, qpc );
633                                  FILTER( , 1, 2, qp, qpc );
634             if( !transform_8x8 ) FILTER( , 1, 3, qp, qpc );
635         }
636
637         #undef FILTER
638     }
639 }
640
641 /* For deblock-aware RD.
642  * TODO:
643  *  deblock macroblock edges
644  *  support analysis partitions smaller than 16x16
645  *  deblock chroma
646  *  handle duplicate refs correctly
647  *  handle cavlc+8x8dct correctly
648  */
649 void x264_macroblock_deblock( x264_t *h )
650 {
651     int qp_thresh = 15 - X264_MIN( h->sh.i_alpha_c0_offset, h->sh.i_beta_offset ) - X264_MAX( 0, h->pps->i_chroma_qp_index_offset );
652     int qp = h->mb.i_qp;
653     if( qp <= qp_thresh || h->mb.i_type == P_SKIP )
654         return;
655
656     uint8_t (*bs)[8][4] = h->deblock_strength[h->mb.i_mb_y&1][h->mb.i_mb_x];
657     if( IS_INTRA( h->mb.i_type ) )
658         memset( bs, 3, 2*8*4*sizeof(uint8_t) );
659     else
660         h->loopf.deblock_strength( h->mb.cache.non_zero_count, h->mb.cache.ref, h->mb.cache.mv,
661                                    bs, 4 >> h->sh.b_mbaff, h->sh.i_type == SLICE_TYPE_B, h );
662
663     int transform_8x8 = h->mb.b_transform_8x8;
664     pixel *fdec = h->mb.pic.p_fdec[0];
665
666     #define FILTER( dir, edge )\
667     do\
668     {\
669         deblock_edge( h, fdec + 4*edge*(dir?FDEC_STRIDE:1),\
670                       FDEC_STRIDE, bs[dir][edge], qp, 0,\
671                       h->loopf.deblock_luma[dir] );\
672     } while(0)
673
674     if( !transform_8x8 ) FILTER( 0, 1 );
675                          FILTER( 0, 2 );
676     if( !transform_8x8 ) FILTER( 0, 3 );
677
678     if( !transform_8x8 ) FILTER( 1, 1 );
679                          FILTER( 1, 2 );
680     if( !transform_8x8 ) FILTER( 1, 3 );
681
682     #undef FILTER
683 }
684
685 #if HAVE_MMX
686 void x264_deblock_v_luma_sse2( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
687 void x264_deblock_v_luma_avx ( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
688 void x264_deblock_h_luma_sse2( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
689 void x264_deblock_h_luma_avx ( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
690 void x264_deblock_v_chroma_sse2( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
691 void x264_deblock_v_chroma_avx ( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
692 void x264_deblock_h_chroma_sse2( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
693 void x264_deblock_h_chroma_avx ( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
694 void x264_deblock_v_luma_intra_sse2( pixel *pix, int stride, int alpha, int beta );
695 void x264_deblock_v_luma_intra_avx ( pixel *pix, int stride, int alpha, int beta );
696 void x264_deblock_h_luma_intra_sse2( pixel *pix, int stride, int alpha, int beta );
697 void x264_deblock_h_luma_intra_avx ( pixel *pix, int stride, int alpha, int beta );
698 void x264_deblock_v_chroma_intra_sse2( pixel *pix, int stride, int alpha, int beta );
699 void x264_deblock_v_chroma_intra_avx ( pixel *pix, int stride, int alpha, int beta );
700 void x264_deblock_h_chroma_intra_sse2( pixel *pix, int stride, int alpha, int beta );
701 void x264_deblock_h_chroma_intra_avx ( pixel *pix, int stride, int alpha, int beta );
702 void x264_deblock_strength_mmxext( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
703                                    int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
704                                    int mvy_limit, int bframe, x264_t *h );
705 void x264_deblock_strength_sse2  ( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
706                                    int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
707                                    int mvy_limit, int bframe, x264_t *h );
708 void x264_deblock_strength_ssse3 ( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
709                                    int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
710                                    int mvy_limit, int bframe, x264_t *h );
711 void x264_deblock_strength_avx   ( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
712                                    int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
713                                    int mvy_limit, int bframe, x264_t *h );
714 #if ARCH_X86
715 void x264_deblock_h_luma_mmxext( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
716 void x264_deblock_v8_luma_mmxext( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 );
717 void x264_deblock_v_chroma_mmxext( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
718 void x264_deblock_h_chroma_mmxext( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
719 void x264_deblock_h_luma_intra_mmxext( pixel *pix, int stride, int alpha, int beta );
720 void x264_deblock_v8_luma_intra_mmxext( uint8_t *pix, int stride, int alpha, int beta );
721 void x264_deblock_v_chroma_intra_mmxext( pixel *pix, int stride, int alpha, int beta );
722 void x264_deblock_h_chroma_intra_mmxext( pixel *pix, int stride, int alpha, int beta );
723
724 #if HIGH_BIT_DEPTH
725 void x264_deblock_v_luma_mmxext( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
726 void x264_deblock_v_luma_intra_mmxext( pixel *pix, int stride, int alpha, int beta );
727 #else
728 // FIXME this wrapper has a significant cpu cost
729 static void x264_deblock_v_luma_mmxext( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 )
730 {
731     x264_deblock_v8_luma_mmxext( pix,   stride, alpha, beta, tc0   );
732     x264_deblock_v8_luma_mmxext( pix+8, stride, alpha, beta, tc0+2 );
733 }
734 static void x264_deblock_v_luma_intra_mmxext( uint8_t *pix, int stride, int alpha, int beta )
735 {
736     x264_deblock_v8_luma_intra_mmxext( pix,   stride, alpha, beta );
737     x264_deblock_v8_luma_intra_mmxext( pix+8, stride, alpha, beta );
738 }
739 #endif // HIGH_BIT_DEPTH
740 #endif
741 #endif
742
743 #if ARCH_PPC
744 void x264_deblock_v_luma_altivec( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 );
745 void x264_deblock_h_luma_altivec( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 );
746 #endif // ARCH_PPC
747
748 #if HAVE_ARMV6
749 void x264_deblock_v_luma_neon( uint8_t *, int, int, int, int8_t * );
750 void x264_deblock_h_luma_neon( uint8_t *, int, int, int, int8_t * );
751 void x264_deblock_v_chroma_neon( uint8_t *, int, int, int, int8_t * );
752 void x264_deblock_h_chroma_neon( uint8_t *, int, int, int, int8_t * );
753 #endif
754
755 void x264_deblock_init( int cpu, x264_deblock_function_t *pf, int b_mbaff )
756 {
757     pf->deblock_luma[1] = deblock_v_luma_c;
758     pf->deblock_luma[0] = deblock_h_luma_c;
759     pf->deblock_chroma[1] = deblock_v_chroma_c;
760     pf->deblock_chroma[0] = deblock_h_chroma_c;
761     pf->deblock_luma_intra[1] = deblock_v_luma_intra_c;
762     pf->deblock_luma_intra[0] = deblock_h_luma_intra_c;
763     pf->deblock_chroma_intra[1] = deblock_v_chroma_intra_c;
764     pf->deblock_chroma_intra[0] = deblock_h_chroma_intra_c;
765     pf->deblock_strength = deblock_strength_c;
766
767 #if HAVE_MMX
768     if( cpu&X264_CPU_MMXEXT )
769     {
770 #if ARCH_X86
771         pf->deblock_luma[1] = x264_deblock_v_luma_mmxext;
772         pf->deblock_luma[0] = x264_deblock_h_luma_mmxext;
773         pf->deblock_chroma[1] = x264_deblock_v_chroma_mmxext;
774         pf->deblock_chroma[0] = x264_deblock_h_chroma_mmxext;
775         pf->deblock_luma_intra[1] = x264_deblock_v_luma_intra_mmxext;
776         pf->deblock_luma_intra[0] = x264_deblock_h_luma_intra_mmxext;
777         pf->deblock_chroma_intra[1] = x264_deblock_v_chroma_intra_mmxext;
778         pf->deblock_chroma_intra[0] = x264_deblock_h_chroma_intra_mmxext;
779 #endif
780         pf->deblock_strength = x264_deblock_strength_mmxext;
781         if( cpu&X264_CPU_SSE2 )
782         {
783             pf->deblock_strength = x264_deblock_strength_sse2;
784             if( !(cpu&X264_CPU_STACK_MOD4) )
785             {
786                 pf->deblock_luma[1] = x264_deblock_v_luma_sse2;
787                 pf->deblock_luma[0] = x264_deblock_h_luma_sse2;
788                 pf->deblock_chroma[1] = x264_deblock_v_chroma_sse2;
789                 pf->deblock_chroma[0] = x264_deblock_h_chroma_sse2;
790                 pf->deblock_luma_intra[1] = x264_deblock_v_luma_intra_sse2;
791                 pf->deblock_luma_intra[0] = x264_deblock_h_luma_intra_sse2;
792                 pf->deblock_chroma_intra[1] = x264_deblock_v_chroma_intra_sse2;
793                 pf->deblock_chroma_intra[0] = x264_deblock_h_chroma_intra_sse2;
794             }
795         }
796         if( cpu&X264_CPU_SSSE3 )
797             pf->deblock_strength = x264_deblock_strength_ssse3;
798         if( cpu&X264_CPU_AVX )
799         {
800             pf->deblock_strength = x264_deblock_strength_avx;
801             if( !(cpu&X264_CPU_STACK_MOD4) )
802             {
803                 pf->deblock_luma[1] = x264_deblock_v_luma_avx;
804                 pf->deblock_luma[0] = x264_deblock_h_luma_avx;
805                 pf->deblock_chroma[1] = x264_deblock_v_chroma_avx;
806                 pf->deblock_chroma[0] = x264_deblock_h_chroma_avx;
807                 pf->deblock_luma_intra[1] = x264_deblock_v_luma_intra_avx;
808                 pf->deblock_luma_intra[0] = x264_deblock_h_luma_intra_avx;
809                 pf->deblock_chroma_intra[1] = x264_deblock_v_chroma_intra_avx;
810                 pf->deblock_chroma_intra[0] = x264_deblock_h_chroma_intra_avx;
811             }
812         }
813     }
814 #endif
815
816 #if !HIGH_BIT_DEPTH
817 #if HAVE_ALTIVEC
818     if( cpu&X264_CPU_ALTIVEC )
819     {
820         pf->deblock_luma[1] = x264_deblock_v_luma_altivec;
821         pf->deblock_luma[0] = x264_deblock_h_luma_altivec;
822    }
823 #endif // HAVE_ALTIVEC
824
825 #if HAVE_ARMV6
826    if( cpu&X264_CPU_NEON )
827    {
828         pf->deblock_luma[1] = x264_deblock_v_luma_neon;
829         pf->deblock_luma[0] = x264_deblock_h_luma_neon;
830 //      pf->deblock_chroma[1] = x264_deblock_v_chroma_neon;
831 //      pf->deblock_chroma[0] = x264_deblock_h_chroma_neon;
832    }
833 #endif
834 #endif // !HIGH_BIT_DEPTH
835
836     if( b_mbaff ) pf->deblock_strength = deblock_strength_mbaff_c;
837 }