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