]> git.sesse.net Git - x264/blob - common/deblock.c
Cosmetics: s/mmxext/mmx2/
[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 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 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 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 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 )
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 static ALWAYS_INLINE void deblock_edge( x264_t *h, pixel *pix, int i_stride, uint8_t bS[4], int i_qp, int a, int b, int b_chroma, x264_deblock_inter_t pf_inter )
300 {
301     int index_a = i_qp + a;
302     int index_b = i_qp + b;
303     int alpha = alpha_table(index_a) << (BIT_DEPTH-8);
304     int beta  = beta_table(index_b) << (BIT_DEPTH-8);
305     int8_t tc[4];
306
307     if( !M32(bS) || !alpha || !beta )
308         return;
309
310     tc[0] = (tc0_table(index_a)[bS[0]] << (BIT_DEPTH-8)) + b_chroma;
311     tc[1] = (tc0_table(index_a)[bS[1]] << (BIT_DEPTH-8)) + b_chroma;
312     tc[2] = (tc0_table(index_a)[bS[2]] << (BIT_DEPTH-8)) + b_chroma;
313     tc[3] = (tc0_table(index_a)[bS[3]] << (BIT_DEPTH-8)) + b_chroma;
314
315     pf_inter( pix, i_stride, alpha, beta, tc );
316 }
317
318 static ALWAYS_INLINE void deblock_edge_intra( x264_t *h, pixel *pix, int i_stride, uint8_t bS[4], int i_qp, int a, int b, int b_chroma, x264_deblock_intra_t pf_intra )
319 {
320     int index_a = i_qp + a;
321     int index_b = i_qp + b;
322     int alpha = alpha_table(index_a) << (BIT_DEPTH-8);
323     int beta  = beta_table(index_b) << (BIT_DEPTH-8);
324
325     if( !alpha || !beta )
326         return;
327
328     pf_intra( pix, i_stride, alpha, beta );
329 }
330
331 static ALWAYS_INLINE void x264_macroblock_cache_load_neighbours_deblock( x264_t *h, int mb_x, int mb_y )
332 {
333     int deblock_on_slice_edges = h->sh.i_disable_deblocking_filter_idc != 2;
334
335     h->mb.i_neighbour = 0;
336     h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
337     h->mb.b_interlaced = PARAM_INTERLACED && h->mb.field[h->mb.i_mb_xy];
338     h->mb.i_mb_top_y = mb_y - (1 << MB_INTERLACED);
339     h->mb.i_mb_top_xy = mb_x + h->mb.i_mb_stride*h->mb.i_mb_top_y;
340     h->mb.i_mb_left_xy[1] =
341     h->mb.i_mb_left_xy[0] = h->mb.i_mb_xy - 1;
342     if( SLICE_MBAFF )
343     {
344         if( mb_y&1 )
345         {
346             if( mb_x && h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED )
347                 h->mb.i_mb_left_xy[0] -= h->mb.i_mb_stride;
348         }
349         else
350         {
351             if( h->mb.i_mb_top_xy >= 0 && MB_INTERLACED && !h->mb.field[h->mb.i_mb_top_xy] )
352             {
353                 h->mb.i_mb_top_xy += h->mb.i_mb_stride;
354                 h->mb.i_mb_top_y++;
355             }
356             if( mb_x && h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED )
357                 h->mb.i_mb_left_xy[1] += h->mb.i_mb_stride;
358         }
359     }
360
361     if( mb_x > 0 && (deblock_on_slice_edges ||
362         h->mb.slice_table[h->mb.i_mb_left_xy[0]] == h->mb.slice_table[h->mb.i_mb_xy]) )
363         h->mb.i_neighbour |= MB_LEFT;
364     if( mb_y > MB_INTERLACED && (deblock_on_slice_edges
365         || h->mb.slice_table[h->mb.i_mb_top_xy] == h->mb.slice_table[h->mb.i_mb_xy]) )
366         h->mb.i_neighbour |= MB_TOP;
367 }
368
369 void x264_frame_deblock_row( x264_t *h, int mb_y )
370 {
371     int b_interlaced = SLICE_MBAFF;
372     int a = h->sh.i_alpha_c0_offset - QP_BD_OFFSET;
373     int b = h->sh.i_beta_offset - QP_BD_OFFSET;
374     int qp_thresh = 15 - X264_MIN( a, b ) - X264_MAX( 0, h->pps->i_chroma_qp_index_offset );
375     int stridey   = h->fdec->i_stride[0];
376     int strideuv  = h->fdec->i_stride[1];
377     int chroma444 = CHROMA444;
378     intptr_t uvdiff = chroma444 ? h->fdec->plane[2] - h->fdec->plane[1] : 1;
379
380     for( int mb_x = 0; mb_x < h->mb.i_mb_width; mb_x += (~b_interlaced | mb_y)&1, mb_y ^= b_interlaced )
381     {
382         x264_prefetch_fenc( h, h->fdec, mb_x, mb_y );
383         x264_macroblock_cache_load_neighbours_deblock( h, mb_x, mb_y );
384
385         int mb_xy = h->mb.i_mb_xy;
386         int transform_8x8 = h->mb.mb_transform_size[h->mb.i_mb_xy];
387         int intra_cur = IS_INTRA( h->mb.type[mb_xy] );
388         uint8_t (*bs)[8][4] = h->deblock_strength[mb_y&1][mb_x];
389
390         pixel *pixy = h->fdec->plane[0] + 16*mb_y*stridey  + 16*mb_x;
391         pixel *pixuv = h->fdec->plane[1] + (8<<chroma444)*mb_y*strideuv + 16*mb_x;
392
393         if( mb_y & MB_INTERLACED )
394         {
395             pixy -= 15*stridey;
396             pixuv -= ((8<<chroma444)-1)*strideuv;
397         }
398
399         int stride2y  = stridey << MB_INTERLACED;
400         int stride2uv = strideuv << MB_INTERLACED;
401         int qp = h->mb.qp[mb_xy];
402         int qpc = h->chroma_qp_table[qp];
403         int first_edge_only = (h->mb.partition[mb_xy] == D_16x16 && !h->mb.cbp[mb_xy] && !intra_cur) || qp <= qp_thresh;
404
405         #define FILTER( intra, dir, edge, qp, chroma_qp )\
406         do\
407         {\
408             deblock_edge##intra( h, pixy + 4*edge*(dir?stride2y:1),\
409                                  stride2y, bs[dir][edge], qp, a, b, 0,\
410                                  h->loopf.deblock_luma##intra[dir] );\
411             if( chroma444 )\
412             {\
413                 deblock_edge##intra( h, pixuv          + 4*edge*(dir?stride2uv:1),\
414                                      stride2uv, bs[dir][edge], chroma_qp, a, b, 0,\
415                                      h->loopf.deblock_luma##intra[dir] );\
416                 deblock_edge##intra( h, pixuv + uvdiff + 4*edge*(dir?stride2uv:1),\
417                                      stride2uv, bs[dir][edge], chroma_qp, a, b, 0,\
418                                      h->loopf.deblock_luma##intra[dir] );\
419             }\
420             else if( !(edge & 1) )\
421                 deblock_edge##intra( h, pixuv + 2*edge*(dir?stride2uv:2),\
422                                      stride2uv, bs[dir][edge], chroma_qp, a, b, 1,\
423                                      h->loopf.deblock_chroma##intra[dir] );\
424         } while(0)
425
426         if( h->mb.i_neighbour & MB_LEFT )
427         {
428             if( b_interlaced && h->mb.field[h->mb.i_mb_left_xy[0]] != MB_INTERLACED )
429             {
430                 int luma_qp[2];
431                 int chroma_qp[2];
432                 int left_qp[2];
433                 x264_deblock_inter_t luma_deblock = h->loopf.deblock_luma_mbaff;
434                 x264_deblock_inter_t chroma_deblock = chroma444 ? h->loopf.deblock_luma_mbaff : h->loopf.deblock_chroma_mbaff;
435                 x264_deblock_intra_t luma_intra_deblock = h->loopf.deblock_luma_intra_mbaff;
436                 x264_deblock_intra_t chroma_intra_deblock = chroma444 ? h->loopf.deblock_luma_intra_mbaff : h->loopf.deblock_chroma_intra_mbaff;
437                 int c = chroma444 ? 0 : 1;
438
439                 left_qp[0] = h->mb.qp[h->mb.i_mb_left_xy[0]];
440                 luma_qp[0] = (qp + left_qp[0] + 1) >> 1;
441                 chroma_qp[0] = (qpc + h->chroma_qp_table[left_qp[0]] + 1) >> 1;
442                 if( intra_cur || IS_INTRA( h->mb.type[h->mb.i_mb_left_xy[0]] ) )
443                 {
444                     deblock_edge_intra( h, pixy,           2*stridey,  bs[0][0], luma_qp[0],   a, b, 0, luma_intra_deblock );
445                     deblock_edge_intra( h, pixuv,          2*strideuv, bs[0][0], chroma_qp[0], a, b, c, chroma_intra_deblock );
446                     deblock_edge_intra( h, pixuv + uvdiff, 2*strideuv, bs[0][0], chroma_qp[0], a, b, c, chroma_intra_deblock );
447                 }
448                 else
449                 {
450                     deblock_edge( h, pixy,           2*stridey,  bs[0][0], luma_qp[0],   a, b, 0, luma_deblock );
451                     deblock_edge( h, pixuv,          2*strideuv, bs[0][0], chroma_qp[0], a, b, c, chroma_deblock );
452                     deblock_edge( h, pixuv + uvdiff, 2*strideuv, bs[0][0], chroma_qp[0], a, b, c, chroma_deblock );
453                 }
454
455                 int offy = MB_INTERLACED ? 4 : 0;
456                 int offuv = MB_INTERLACED ? 3 : 0;
457                 if( chroma444 ) offuv = offy;
458                 left_qp[1] = h->mb.qp[h->mb.i_mb_left_xy[1]];
459                 luma_qp[1] = (qp + left_qp[1] + 1) >> 1;
460                 chroma_qp[1] = (qpc + h->chroma_qp_table[left_qp[1]] + 1) >> 1;
461                 if( intra_cur || IS_INTRA( h->mb.type[h->mb.i_mb_left_xy[1]] ) )
462                 {
463                     deblock_edge_intra( h, pixy           + (stridey<<offy),   2*stridey,  bs[0][4], luma_qp[1],   a, b, 0, luma_intra_deblock );
464                     deblock_edge_intra( h, pixuv          + (strideuv<<offuv), 2*strideuv, bs[0][4], chroma_qp[1], a, b, c, chroma_intra_deblock );
465                     deblock_edge_intra( h, pixuv + uvdiff + (strideuv<<offuv), 2*strideuv, bs[0][4], chroma_qp[1], a, b, c, chroma_intra_deblock );
466                 }
467                 else
468                 {
469                     deblock_edge( h, pixy           + (stridey<<offy),   2*stridey,  bs[0][4], luma_qp[1],   a, b, 0, luma_deblock );
470                     deblock_edge( h, pixuv          + (strideuv<<offuv), 2*strideuv, bs[0][4], chroma_qp[1], a, b, c, chroma_deblock );
471                     deblock_edge( h, pixuv + uvdiff + (strideuv<<offuv), 2*strideuv, bs[0][4], chroma_qp[1], a, b, c, chroma_deblock );
472                 }
473             }
474             else
475             {
476                 int qpl = h->mb.qp[h->mb.i_mb_xy-1];
477                 int qp_left = (qp + qpl + 1) >> 1;
478                 int qpc_left = (qpc + h->chroma_qp_table[qpl] + 1) >> 1;
479                 int intra_left = IS_INTRA( h->mb.type[h->mb.i_mb_xy-1] );
480
481                 if( intra_cur || intra_left )
482                     FILTER( _intra, 0, 0, qp_left, qpc_left );
483                 else
484                     FILTER(       , 0, 0, qp_left, qpc_left );
485             }
486         }
487         if( !first_edge_only )
488         {
489             if( !transform_8x8 ) FILTER( , 0, 1, qp, qpc );
490                                  FILTER( , 0, 2, qp, qpc );
491             if( !transform_8x8 ) FILTER( , 0, 3, qp, qpc );
492         }
493
494         if( h->mb.i_neighbour & MB_TOP )
495         {
496             if( b_interlaced && !(mb_y&1) && !MB_INTERLACED && h->mb.field[h->mb.i_mb_top_xy] )
497             {
498                 int mbn_xy = mb_xy - 2 * h->mb.i_mb_stride;
499
500                 for( int j = 0; j < 2; j++, mbn_xy += h->mb.i_mb_stride )
501                 {
502                     int qpt = h->mb.qp[mbn_xy];
503                     int qp_top = (qp + qpt + 1) >> 1;
504                     int qpc_top = (qpc + h->chroma_qp_table[qpt] + 1) >> 1;
505                     int intra_top = IS_INTRA( h->mb.type[mbn_xy] );
506                     if( intra_cur || intra_top )
507                         M32( bs[1][4*j] ) = 0x03030303;
508
509                     // deblock the first horizontal edge of the even rows, then the first horizontal edge of the odd rows
510                     deblock_edge( h, pixy      + j*stridey,  2* stridey, bs[1][4*j], qp_top, a, b, 0, h->loopf.deblock_luma[1] );
511                     if( chroma444 )
512                     {
513                         deblock_edge( h, pixuv          + j*strideuv, 2*strideuv, bs[1][4*j], qpc_top, a, b, 0, h->loopf.deblock_luma[1] );
514                         deblock_edge( h, pixuv + uvdiff + j*strideuv, 2*strideuv, bs[1][4*j], qpc_top, a, b, 0, h->loopf.deblock_luma[1] );
515                     }
516                     else
517                         deblock_edge( h, pixuv          + j*strideuv, 2*strideuv, bs[1][4*j], qpc_top, a, b, 1, h->loopf.deblock_chroma[1] );
518                 }
519             }
520             else
521             {
522                 int qpt = h->mb.qp[h->mb.i_mb_top_xy];
523                 int qp_top = (qp + qpt + 1) >> 1;
524                 int qpc_top = (qpc + h->chroma_qp_table[qpt] + 1) >> 1;
525                 int intra_top = IS_INTRA( h->mb.type[h->mb.i_mb_top_xy] );
526
527                 if( (!b_interlaced || (!MB_INTERLACED && !h->mb.field[h->mb.i_mb_top_xy]))
528                     && (intra_cur || intra_top) )
529                 {
530                     FILTER( _intra, 1, 0, qp_top, qpc_top );
531                 }
532                 else
533                 {
534                     if( intra_cur || intra_top )
535                         M32( bs[1][0] ) = 0x03030303;
536                     FILTER(       , 1, 0, qp_top, qpc_top );
537                 }
538             }
539         }
540
541         if( !first_edge_only )
542         {
543             if( !transform_8x8 ) FILTER( , 1, 1, qp, qpc );
544                                  FILTER( , 1, 2, qp, qpc );
545             if( !transform_8x8 ) FILTER( , 1, 3, qp, qpc );
546         }
547
548         #undef FILTER
549     }
550 }
551
552 /* For deblock-aware RD.
553  * TODO:
554  *  deblock macroblock edges
555  *  support analysis partitions smaller than 16x16
556  *  deblock chroma for 4:2:0
557  *  handle duplicate refs correctly
558  *  handle cavlc+8x8dct correctly
559  */
560 void x264_macroblock_deblock( x264_t *h )
561 {
562     int a = h->sh.i_alpha_c0_offset - QP_BD_OFFSET;
563     int b = h->sh.i_beta_offset - QP_BD_OFFSET;
564     int qp_thresh = 15 - X264_MIN( a, b ) - X264_MAX( 0, h->pps->i_chroma_qp_index_offset );
565     int intra_cur = IS_INTRA( h->mb.i_type );
566     int qp = h->mb.i_qp;
567     int qpc = h->mb.i_chroma_qp;
568     if( (h->mb.i_partition == D_16x16 && !h->mb.i_cbp_luma && !intra_cur) || qp <= qp_thresh )
569         return;
570
571     uint8_t (*bs)[8][4] = h->deblock_strength[h->mb.i_mb_y&1][h->mb.i_mb_x];
572     if( intra_cur )
573     {
574         memset( &bs[0][1], 3, 3*4*sizeof(uint8_t) );
575         memset( &bs[1][1], 3, 3*4*sizeof(uint8_t) );
576     }
577     else
578         h->loopf.deblock_strength( h->mb.cache.non_zero_count, h->mb.cache.ref, h->mb.cache.mv,
579                                    bs, 4 >> MB_INTERLACED, h->sh.i_type == SLICE_TYPE_B );
580
581     int transform_8x8 = h->mb.b_transform_8x8;
582
583     #define FILTER( dir, edge )\
584     do\
585     {\
586         deblock_edge( h, h->mb.pic.p_fdec[0] + 4*edge*(dir?FDEC_STRIDE:1),\
587                       FDEC_STRIDE, bs[dir][edge], qp, a, b, 0,\
588                       h->loopf.deblock_luma[dir] );\
589         if( CHROMA444 )\
590         {\
591             deblock_edge( h, h->mb.pic.p_fdec[1] + 4*edge*(dir?FDEC_STRIDE:1),\
592                           FDEC_STRIDE, bs[dir][edge], qpc, a, b, 0,\
593                           h->loopf.deblock_luma[dir] );\
594             deblock_edge( h, h->mb.pic.p_fdec[2] + 4*edge*(dir?FDEC_STRIDE:1),\
595                           FDEC_STRIDE, bs[dir][edge], qpc, a, b, 0,\
596                           h->loopf.deblock_luma[dir] );\
597         }\
598     } while(0)
599
600     if( !transform_8x8 ) FILTER( 0, 1 );
601                          FILTER( 0, 2 );
602     if( !transform_8x8 ) FILTER( 0, 3 );
603
604     if( !transform_8x8 ) FILTER( 1, 1 );
605                          FILTER( 1, 2 );
606     if( !transform_8x8 ) FILTER( 1, 3 );
607
608     #undef FILTER
609 }
610
611 #if HAVE_MMX
612 void x264_deblock_v_luma_sse2( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
613 void x264_deblock_v_luma_avx ( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
614 void x264_deblock_h_luma_sse2( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
615 void x264_deblock_h_luma_avx ( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
616 void x264_deblock_v_chroma_sse2( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
617 void x264_deblock_v_chroma_avx ( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
618 void x264_deblock_h_chroma_sse2( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
619 void x264_deblock_h_chroma_avx ( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
620 void x264_deblock_v_luma_intra_sse2( pixel *pix, int stride, int alpha, int beta );
621 void x264_deblock_v_luma_intra_avx ( pixel *pix, int stride, int alpha, int beta );
622 void x264_deblock_h_luma_intra_sse2( pixel *pix, int stride, int alpha, int beta );
623 void x264_deblock_h_luma_intra_avx ( pixel *pix, int stride, int alpha, int beta );
624 void x264_deblock_v_chroma_intra_sse2( pixel *pix, int stride, int alpha, int beta );
625 void x264_deblock_v_chroma_intra_avx ( pixel *pix, int stride, int alpha, int beta );
626 void x264_deblock_h_chroma_intra_sse2( pixel *pix, int stride, int alpha, int beta );
627 void x264_deblock_h_chroma_intra_avx ( pixel *pix, int stride, int alpha, int beta );
628 void x264_deblock_strength_mmx2 ( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
629                                   int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
630                                   int mvy_limit, int bframe );
631 void x264_deblock_strength_sse2 ( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
632                                   int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
633                                   int mvy_limit, int bframe );
634 void x264_deblock_strength_ssse3( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
635                                   int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
636                                   int mvy_limit, int bframe );
637 void x264_deblock_strength_avx  ( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
638                                   int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
639                                   int mvy_limit, int bframe );
640 #if ARCH_X86
641 void x264_deblock_h_luma_mmx2( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
642 void x264_deblock_v8_luma_mmx2( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 );
643 void x264_deblock_v_chroma_mmx2( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
644 void x264_deblock_h_chroma_mmx2( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
645 void x264_deblock_h_luma_intra_mmx2( pixel *pix, int stride, int alpha, int beta );
646 void x264_deblock_v8_luma_intra_mmx2( uint8_t *pix, int stride, int alpha, int beta );
647 void x264_deblock_v_chroma_intra_mmx2( pixel *pix, int stride, int alpha, int beta );
648 void x264_deblock_h_chroma_intra_mmx2( pixel *pix, int stride, int alpha, int beta );
649
650 #if HIGH_BIT_DEPTH
651 void x264_deblock_v_luma_mmx2( pixel *pix, int stride, int alpha, int beta, int8_t *tc0 );
652 void x264_deblock_v_luma_intra_mmx2( pixel *pix, int stride, int alpha, int beta );
653 #else
654 // FIXME this wrapper has a significant cpu cost
655 static void x264_deblock_v_luma_mmx2( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 )
656 {
657     x264_deblock_v8_luma_mmx2( pix,   stride, alpha, beta, tc0   );
658     x264_deblock_v8_luma_mmx2( pix+8, stride, alpha, beta, tc0+2 );
659 }
660 static void x264_deblock_v_luma_intra_mmx2( uint8_t *pix, int stride, int alpha, int beta )
661 {
662     x264_deblock_v8_luma_intra_mmx2( pix,   stride, alpha, beta );
663     x264_deblock_v8_luma_intra_mmx2( pix+8, stride, alpha, beta );
664 }
665 #endif // HIGH_BIT_DEPTH
666 #endif
667 #endif
668
669 #if ARCH_PPC
670 void x264_deblock_v_luma_altivec( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 );
671 void x264_deblock_h_luma_altivec( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 );
672 #endif // ARCH_PPC
673
674 #if HAVE_ARMV6
675 void x264_deblock_v_luma_neon( uint8_t *, int, int, int, int8_t * );
676 void x264_deblock_h_luma_neon( uint8_t *, int, int, int, int8_t * );
677 void x264_deblock_v_chroma_neon( uint8_t *, int, int, int, int8_t * );
678 void x264_deblock_h_chroma_neon( uint8_t *, int, int, int, int8_t * );
679 #endif
680
681 void x264_deblock_init( int cpu, x264_deblock_function_t *pf, int b_mbaff )
682 {
683     pf->deblock_luma[1] = deblock_v_luma_c;
684     pf->deblock_luma[0] = deblock_h_luma_c;
685     pf->deblock_chroma[1] = deblock_v_chroma_c;
686     pf->deblock_chroma[0] = deblock_h_chroma_c;
687     pf->deblock_luma_intra[1] = deblock_v_luma_intra_c;
688     pf->deblock_luma_intra[0] = deblock_h_luma_intra_c;
689     pf->deblock_chroma_intra[1] = deblock_v_chroma_intra_c;
690     pf->deblock_chroma_intra[0] = deblock_h_chroma_intra_c;
691     pf->deblock_luma_mbaff = deblock_v_luma_mbaff_c;
692     pf->deblock_chroma_mbaff = deblock_v_chroma_mbaff_c;
693     pf->deblock_luma_intra_mbaff = deblock_v_luma_intra_mbaff_c;
694     pf->deblock_chroma_intra_mbaff = deblock_v_chroma_intra_mbaff_c;
695     pf->deblock_strength = deblock_strength_c;
696
697 #if HAVE_MMX
698     if( cpu&X264_CPU_MMX2 )
699     {
700 #if ARCH_X86
701         pf->deblock_luma[1] = x264_deblock_v_luma_mmx2;
702         pf->deblock_luma[0] = x264_deblock_h_luma_mmx2;
703         pf->deblock_chroma[1] = x264_deblock_v_chroma_mmx2;
704         pf->deblock_chroma[0] = x264_deblock_h_chroma_mmx2;
705         pf->deblock_luma_intra[1] = x264_deblock_v_luma_intra_mmx2;
706         pf->deblock_luma_intra[0] = x264_deblock_h_luma_intra_mmx2;
707         pf->deblock_chroma_intra[1] = x264_deblock_v_chroma_intra_mmx2;
708         pf->deblock_chroma_intra[0] = x264_deblock_h_chroma_intra_mmx2;
709 #endif
710         pf->deblock_strength = x264_deblock_strength_mmx2;
711         if( cpu&X264_CPU_SSE2 )
712         {
713             pf->deblock_strength = x264_deblock_strength_sse2;
714             if( !(cpu&X264_CPU_STACK_MOD4) )
715             {
716                 pf->deblock_luma[1] = x264_deblock_v_luma_sse2;
717                 pf->deblock_luma[0] = x264_deblock_h_luma_sse2;
718                 pf->deblock_chroma[1] = x264_deblock_v_chroma_sse2;
719                 pf->deblock_chroma[0] = x264_deblock_h_chroma_sse2;
720                 pf->deblock_luma_intra[1] = x264_deblock_v_luma_intra_sse2;
721                 pf->deblock_luma_intra[0] = x264_deblock_h_luma_intra_sse2;
722                 pf->deblock_chroma_intra[1] = x264_deblock_v_chroma_intra_sse2;
723                 pf->deblock_chroma_intra[0] = x264_deblock_h_chroma_intra_sse2;
724             }
725         }
726         if( cpu&X264_CPU_SSSE3 )
727             pf->deblock_strength = x264_deblock_strength_ssse3;
728         if( cpu&X264_CPU_AVX )
729         {
730             pf->deblock_strength = x264_deblock_strength_avx;
731             if( !(cpu&X264_CPU_STACK_MOD4) )
732             {
733                 pf->deblock_luma[1] = x264_deblock_v_luma_avx;
734                 pf->deblock_luma[0] = x264_deblock_h_luma_avx;
735                 pf->deblock_chroma[1] = x264_deblock_v_chroma_avx;
736                 pf->deblock_chroma[0] = x264_deblock_h_chroma_avx;
737                 pf->deblock_luma_intra[1] = x264_deblock_v_luma_intra_avx;
738                 pf->deblock_luma_intra[0] = x264_deblock_h_luma_intra_avx;
739                 pf->deblock_chroma_intra[1] = x264_deblock_v_chroma_intra_avx;
740                 pf->deblock_chroma_intra[0] = x264_deblock_h_chroma_intra_avx;
741             }
742         }
743     }
744 #endif
745
746 #if !HIGH_BIT_DEPTH
747 #if HAVE_ALTIVEC
748     if( cpu&X264_CPU_ALTIVEC )
749     {
750         pf->deblock_luma[1] = x264_deblock_v_luma_altivec;
751         pf->deblock_luma[0] = x264_deblock_h_luma_altivec;
752    }
753 #endif // HAVE_ALTIVEC
754
755 #if HAVE_ARMV6
756    if( cpu&X264_CPU_NEON )
757    {
758         pf->deblock_luma[1] = x264_deblock_v_luma_neon;
759         pf->deblock_luma[0] = x264_deblock_h_luma_neon;
760 //      pf->deblock_chroma[1] = x264_deblock_v_chroma_neon;
761 //      pf->deblock_chroma[0] = x264_deblock_h_chroma_neon;
762    }
763 #endif
764 #endif // !HIGH_BIT_DEPTH
765 }