]> git.sesse.net Git - x264/blob - common/deblock.c
arm: Implement x264_deblock_h_chroma_mbaff_neon
[x264] / common / deblock.c
1 /*****************************************************************************
2  * deblock.c: deblocking
3  *****************************************************************************
4  * Copyright (C) 2003-2015 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 <henrik@gramner.com>
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, intptr_t 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, intptr_t xstride, intptr_t 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, intptr_t 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, intptr_t 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, intptr_t 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, intptr_t 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, intptr_t xstride, intptr_t 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, intptr_t 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_v_chroma_c( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 )
171 {
172     deblock_chroma_c( pix, 2, stride, 2, alpha, beta, tc0 );
173 }
174 static void deblock_h_chroma_c( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 )
175 {
176     deblock_chroma_c( pix, 2, 2, stride, alpha, beta, tc0 );
177 }
178 static void deblock_h_chroma_422_c( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 )
179 {
180     deblock_chroma_c( pix, 4, 2, stride, alpha, beta, tc0 );
181 }
182
183 static ALWAYS_INLINE void deblock_edge_luma_intra_c( pixel *pix, intptr_t xstride, int alpha, int beta )
184 {
185     int p2 = pix[-3*xstride];
186     int p1 = pix[-2*xstride];
187     int p0 = pix[-1*xstride];
188     int q0 = pix[ 0*xstride];
189     int q1 = pix[ 1*xstride];
190     int q2 = pix[ 2*xstride];
191
192     if( abs( p0 - q0 ) < alpha && abs( p1 - p0 ) < beta && abs( q1 - q0 ) < beta )
193     {
194         if( abs( p0 - q0 ) < ((alpha >> 2) + 2) )
195         {
196             if( abs( p2 - p0 ) < beta ) /* p0', p1', p2' */
197             {
198                 const int p3 = pix[-4*xstride];
199                 pix[-1*xstride] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
200                 pix[-2*xstride] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
201                 pix[-3*xstride] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
202             }
203             else /* p0' */
204                 pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
205             if( abs( q2 - q0 ) < beta ) /* q0', q1', q2' */
206             {
207                 const int q3 = pix[3*xstride];
208                 pix[0*xstride] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
209                 pix[1*xstride] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
210                 pix[2*xstride] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
211             }
212             else /* q0' */
213                 pix[0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
214         }
215         else /* p0', q0' */
216         {
217             pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
218             pix[ 0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
219         }
220     }
221 }
222 static inline void deblock_luma_intra_c( pixel *pix, intptr_t xstride, intptr_t ystride, int alpha, int beta )
223 {
224     for( int d = 0; d < 16; d++, pix += ystride )
225         deblock_edge_luma_intra_c( pix, xstride, alpha, beta );
226 }
227 static void deblock_h_luma_intra_mbaff_c( pixel *pix, intptr_t ystride, int alpha, int beta )
228 {
229     for( int d = 0; d < 8; d++, pix += ystride )
230         deblock_edge_luma_intra_c( pix, 1, alpha, beta );
231 }
232 static void deblock_v_luma_intra_c( pixel *pix, intptr_t stride, int alpha, int beta )
233 {
234     deblock_luma_intra_c( pix, stride, 1, alpha, beta );
235 }
236 static void deblock_h_luma_intra_c( pixel *pix, intptr_t stride, int alpha, int beta )
237 {
238     deblock_luma_intra_c( pix, 1, stride, alpha, beta );
239 }
240
241 static ALWAYS_INLINE void deblock_edge_chroma_intra_c( pixel *pix, intptr_t xstride, int alpha, int beta )
242 {
243     int p1 = pix[-2*xstride];
244     int p0 = pix[-1*xstride];
245     int q0 = pix[ 0*xstride];
246     int q1 = pix[ 1*xstride];
247
248     if( abs( p0 - q0 ) < alpha && abs( p1 - p0 ) < beta && abs( q1 - q0 ) < beta )
249     {
250         pix[-1*xstride] = (2*p1 + p0 + q1 + 2) >> 2;   /* p0' */
251         pix[ 0*xstride] = (2*q1 + q0 + p1 + 2) >> 2;   /* q0' */
252     }
253 }
254 static ALWAYS_INLINE void deblock_chroma_intra_c( pixel *pix, int width, int height, intptr_t xstride, intptr_t ystride, int alpha, int beta )
255 {
256     for( int d = 0; d < height; d++, pix += ystride-2 )
257         for( int e = 0; e < width; e++, pix++ )
258             deblock_edge_chroma_intra_c( pix, xstride, alpha, beta );
259 }
260 static void deblock_h_chroma_intra_mbaff_c( pixel *pix, intptr_t stride, int alpha, int beta )
261 {
262     deblock_chroma_intra_c( pix, 2, 4, 2, stride, alpha, beta );
263 }
264 static void deblock_v_chroma_intra_c( pixel *pix, intptr_t stride, int alpha, int beta )
265 {
266     deblock_chroma_intra_c( pix, 1, 16, stride, 2, alpha, beta );
267 }
268 static void deblock_h_chroma_intra_c( pixel *pix, intptr_t stride, int alpha, int beta )
269 {
270     deblock_chroma_intra_c( pix, 2, 8, 2, stride, alpha, beta );
271 }
272 static void deblock_h_chroma_422_intra_c( pixel *pix, intptr_t stride, int alpha, int beta )
273 {
274     deblock_chroma_intra_c( pix, 2, 16, 2, stride, alpha, beta );
275 }
276
277 static void deblock_strength_c( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
278                                 int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4], int mvy_limit,
279                                 int bframe )
280 {
281     for( int dir = 0; dir < 2; dir++ )
282     {
283         int s1 = dir ? 1 : 8;
284         int s2 = dir ? 8 : 1;
285         for( int edge = 0; edge < 4; edge++ )
286             for( int i = 0, loc = X264_SCAN8_0+edge*s2; i < 4; i++, loc += s1 )
287             {
288                 int locn = loc - s2;
289                 if( nnz[loc] || nnz[locn] )
290                     bs[dir][edge][i] = 2;
291                 else if( ref[0][loc] != ref[0][locn] ||
292                          abs( mv[0][loc][0] - mv[0][locn][0] ) >= 4 ||
293                          abs( mv[0][loc][1] - mv[0][locn][1] ) >= mvy_limit ||
294                         (bframe && (ref[1][loc] != ref[1][locn] ||
295                          abs( mv[1][loc][0] - mv[1][locn][0] ) >= 4 ||
296                          abs( mv[1][loc][1] - mv[1][locn][1] ) >= mvy_limit )))
297                 {
298                     bs[dir][edge][i] = 1;
299                 }
300                 else
301                     bs[dir][edge][i] = 0;
302             }
303     }
304 }
305
306 static ALWAYS_INLINE void deblock_edge( x264_t *h, pixel *pix, intptr_t i_stride, uint8_t bS[4], int i_qp,
307                                         int a, int b, int b_chroma, x264_deblock_inter_t pf_inter )
308 {
309     int index_a = i_qp + a;
310     int index_b = i_qp + b;
311     int alpha = alpha_table(index_a) << (BIT_DEPTH-8);
312     int beta  = beta_table(index_b) << (BIT_DEPTH-8);
313     int8_t tc[4];
314
315     if( !M32(bS) || !alpha || !beta )
316         return;
317
318     tc[0] = (tc0_table(index_a)[bS[0]] << (BIT_DEPTH-8)) + b_chroma;
319     tc[1] = (tc0_table(index_a)[bS[1]] << (BIT_DEPTH-8)) + b_chroma;
320     tc[2] = (tc0_table(index_a)[bS[2]] << (BIT_DEPTH-8)) + b_chroma;
321     tc[3] = (tc0_table(index_a)[bS[3]] << (BIT_DEPTH-8)) + b_chroma;
322
323     pf_inter( pix, i_stride, alpha, beta, tc );
324 }
325
326 static ALWAYS_INLINE void deblock_edge_intra( x264_t *h, pixel *pix, intptr_t i_stride, uint8_t bS[4], int i_qp,
327                                               int a, int b, int b_chroma, x264_deblock_intra_t pf_intra )
328 {
329     int index_a = i_qp + a;
330     int index_b = i_qp + b;
331     int alpha = alpha_table(index_a) << (BIT_DEPTH-8);
332     int beta  = beta_table(index_b) << (BIT_DEPTH-8);
333
334     if( !alpha || !beta )
335         return;
336
337     pf_intra( pix, i_stride, alpha, beta );
338 }
339
340 static ALWAYS_INLINE void x264_macroblock_cache_load_neighbours_deblock( x264_t *h, int mb_x, int mb_y )
341 {
342     int deblock_on_slice_edges = h->sh.i_disable_deblocking_filter_idc != 2;
343
344     h->mb.i_neighbour = 0;
345     h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
346     h->mb.b_interlaced = PARAM_INTERLACED && h->mb.field[h->mb.i_mb_xy];
347     h->mb.i_mb_top_y = mb_y - (1 << MB_INTERLACED);
348     h->mb.i_mb_top_xy = mb_x + h->mb.i_mb_stride*h->mb.i_mb_top_y;
349     h->mb.i_mb_left_xy[1] =
350     h->mb.i_mb_left_xy[0] = h->mb.i_mb_xy - 1;
351     if( SLICE_MBAFF )
352     {
353         if( mb_y&1 )
354         {
355             if( mb_x && h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED )
356                 h->mb.i_mb_left_xy[0] -= h->mb.i_mb_stride;
357         }
358         else
359         {
360             if( h->mb.i_mb_top_xy >= 0 && MB_INTERLACED && !h->mb.field[h->mb.i_mb_top_xy] )
361             {
362                 h->mb.i_mb_top_xy += h->mb.i_mb_stride;
363                 h->mb.i_mb_top_y++;
364             }
365             if( mb_x && h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED )
366                 h->mb.i_mb_left_xy[1] += h->mb.i_mb_stride;
367         }
368     }
369
370     if( mb_x > 0 && (deblock_on_slice_edges ||
371         h->mb.slice_table[h->mb.i_mb_left_xy[0]] == h->mb.slice_table[h->mb.i_mb_xy]) )
372         h->mb.i_neighbour |= MB_LEFT;
373     if( mb_y > MB_INTERLACED && (deblock_on_slice_edges
374         || h->mb.slice_table[h->mb.i_mb_top_xy] == h->mb.slice_table[h->mb.i_mb_xy]) )
375         h->mb.i_neighbour |= MB_TOP;
376 }
377
378 void x264_frame_deblock_row( x264_t *h, int mb_y )
379 {
380     int b_interlaced = SLICE_MBAFF;
381     int a = h->sh.i_alpha_c0_offset - QP_BD_OFFSET;
382     int b = h->sh.i_beta_offset - QP_BD_OFFSET;
383     int qp_thresh = 15 - X264_MIN( a, b ) - X264_MAX( 0, h->pps->i_chroma_qp_index_offset );
384     int stridey   = h->fdec->i_stride[0];
385     int strideuv  = h->fdec->i_stride[1];
386     int chroma444 = CHROMA444;
387     int chroma_height = 16 >> CHROMA_V_SHIFT;
388     intptr_t uvdiff = chroma444 ? h->fdec->plane[2] - h->fdec->plane[1] : 1;
389
390     for( int mb_x = 0; mb_x < h->mb.i_mb_width; mb_x += (~b_interlaced | mb_y)&1, mb_y ^= b_interlaced )
391     {
392         x264_prefetch_fenc( h, h->fdec, mb_x, mb_y );
393         x264_macroblock_cache_load_neighbours_deblock( h, mb_x, mb_y );
394
395         int mb_xy = h->mb.i_mb_xy;
396         int transform_8x8 = h->mb.mb_transform_size[mb_xy];
397         int intra_cur = IS_INTRA( h->mb.type[mb_xy] );
398         uint8_t (*bs)[8][4] = h->deblock_strength[mb_y&1][h->param.b_sliced_threads?mb_xy:mb_x];
399
400         pixel *pixy = h->fdec->plane[0] + 16*mb_y*stridey  + 16*mb_x;
401         pixel *pixuv = h->fdec->plane[1] + chroma_height*mb_y*strideuv + 16*mb_x;
402
403         if( mb_y & MB_INTERLACED )
404         {
405             pixy -= 15*stridey;
406             pixuv -= (chroma_height-1)*strideuv;
407         }
408
409         int stride2y  = stridey << MB_INTERLACED;
410         int stride2uv = strideuv << MB_INTERLACED;
411         int qp = h->mb.qp[mb_xy];
412         int qpc = h->chroma_qp_table[qp];
413         int first_edge_only = (h->mb.partition[mb_xy] == D_16x16 && !h->mb.cbp[mb_xy] && !intra_cur) || qp <= qp_thresh;
414
415         #define FILTER( intra, dir, edge, qp, chroma_qp )\
416         do\
417         {\
418             if( !(edge & 1) || !transform_8x8 )\
419             {\
420                 deblock_edge##intra( h, pixy + 4*edge*(dir?stride2y:1),\
421                                      stride2y, bs[dir][edge], qp, a, b, 0,\
422                                      h->loopf.deblock_luma##intra[dir] );\
423                 if( CHROMA_FORMAT == CHROMA_444 )\
424                 {\
425                     deblock_edge##intra( h, pixuv          + 4*edge*(dir?stride2uv:1),\
426                                          stride2uv, bs[dir][edge], chroma_qp, a, b, 0,\
427                                          h->loopf.deblock_luma##intra[dir] );\
428                     deblock_edge##intra( h, pixuv + uvdiff + 4*edge*(dir?stride2uv:1),\
429                                          stride2uv, bs[dir][edge], chroma_qp, a, b, 0,\
430                                          h->loopf.deblock_luma##intra[dir] );\
431                 }\
432                 else if( CHROMA_FORMAT == CHROMA_420 && !(edge & 1) )\
433                 {\
434                     deblock_edge##intra( h, pixuv + edge*(dir?2*stride2uv:4),\
435                                          stride2uv, bs[dir][edge], chroma_qp, a, b, 1,\
436                                          h->loopf.deblock_chroma##intra[dir] );\
437                 }\
438             }\
439             if( CHROMA_FORMAT == CHROMA_422 && (dir || !(edge & 1)) )\
440             {\
441                 deblock_edge##intra( h, pixuv + edge*(dir?4*stride2uv:4),\
442                                      stride2uv, bs[dir][edge], chroma_qp, a, b, 1,\
443                                      h->loopf.deblock_chroma##intra[dir] );\
444             }\
445         } while(0)
446
447         if( h->mb.i_neighbour & MB_LEFT )
448         {
449             if( b_interlaced && h->mb.field[h->mb.i_mb_left_xy[0]] != MB_INTERLACED )
450             {
451                 int luma_qp[2];
452                 int chroma_qp[2];
453                 int left_qp[2];
454                 x264_deblock_inter_t luma_deblock = h->loopf.deblock_luma_mbaff;
455                 x264_deblock_inter_t chroma_deblock = h->loopf.deblock_chroma_mbaff;
456                 x264_deblock_intra_t luma_intra_deblock = h->loopf.deblock_luma_intra_mbaff;
457                 x264_deblock_intra_t chroma_intra_deblock = h->loopf.deblock_chroma_intra_mbaff;
458                 int c = chroma444 ? 0 : 1;
459
460                 left_qp[0] = h->mb.qp[h->mb.i_mb_left_xy[0]];
461                 luma_qp[0] = (qp + left_qp[0] + 1) >> 1;
462                 chroma_qp[0] = (qpc + h->chroma_qp_table[left_qp[0]] + 1) >> 1;
463                 if( intra_cur || IS_INTRA( h->mb.type[h->mb.i_mb_left_xy[0]] ) )
464                 {
465                     deblock_edge_intra( h, pixy,           2*stridey,  bs[0][0], luma_qp[0],   a, b, 0, luma_intra_deblock );
466                     deblock_edge_intra( h, pixuv,          2*strideuv, bs[0][0], chroma_qp[0], a, b, c, chroma_intra_deblock );
467                     if( chroma444 )
468                         deblock_edge_intra( h, pixuv + uvdiff, 2*strideuv, bs[0][0], chroma_qp[0], a, b, c, chroma_intra_deblock );
469                 }
470                 else
471                 {
472                     deblock_edge( h, pixy,           2*stridey,  bs[0][0], luma_qp[0],   a, b, 0, luma_deblock );
473                     deblock_edge( h, pixuv,          2*strideuv, bs[0][0], chroma_qp[0], a, b, c, chroma_deblock );
474                     if( chroma444 )
475                         deblock_edge( h, pixuv + uvdiff, 2*strideuv, bs[0][0], chroma_qp[0], a, b, c, chroma_deblock );
476                 }
477
478                 int offy = MB_INTERLACED ? 4 : 0;
479                 int offuv = MB_INTERLACED ? 4-CHROMA_V_SHIFT : 0;
480                 left_qp[1] = h->mb.qp[h->mb.i_mb_left_xy[1]];
481                 luma_qp[1] = (qp + left_qp[1] + 1) >> 1;
482                 chroma_qp[1] = (qpc + h->chroma_qp_table[left_qp[1]] + 1) >> 1;
483                 if( intra_cur || IS_INTRA( h->mb.type[h->mb.i_mb_left_xy[1]] ) )
484                 {
485                     deblock_edge_intra( h, pixy           + (stridey<<offy),   2*stridey,  bs[0][4], luma_qp[1],   a, b, 0, luma_intra_deblock );
486                     deblock_edge_intra( h, pixuv          + (strideuv<<offuv), 2*strideuv, bs[0][4], chroma_qp[1], a, b, c, chroma_intra_deblock );
487                     if( chroma444 )
488                         deblock_edge_intra( h, pixuv + uvdiff + (strideuv<<offuv), 2*strideuv, bs[0][4], chroma_qp[1], a, b, c, chroma_intra_deblock );
489                 }
490                 else
491                 {
492                     deblock_edge( h, pixy           + (stridey<<offy),   2*stridey,  bs[0][4], luma_qp[1],   a, b, 0, luma_deblock );
493                     deblock_edge( h, pixuv          + (strideuv<<offuv), 2*strideuv, bs[0][4], chroma_qp[1], a, b, c, chroma_deblock );
494                     if( chroma444 )
495                         deblock_edge( h, pixuv + uvdiff + (strideuv<<offuv), 2*strideuv, bs[0][4], chroma_qp[1], a, b, c, chroma_deblock );
496                 }
497             }
498             else
499             {
500                 int qpl = h->mb.qp[h->mb.i_mb_xy-1];
501                 int qp_left = (qp + qpl + 1) >> 1;
502                 int qpc_left = (qpc + h->chroma_qp_table[qpl] + 1) >> 1;
503                 int intra_left = IS_INTRA( h->mb.type[h->mb.i_mb_xy-1] );
504                 int intra_deblock = intra_cur || intra_left;
505
506                 /* Any MB that was coded, or that analysis decided to skip, has quality commensurate with its QP.
507                  * But if deblocking affects neighboring MBs that were force-skipped, blur might accumulate there.
508                  * So reset their effective QP to max, to indicate that lack of guarantee. */
509                 if( h->fdec->mb_info && M32( bs[0][0] ) )
510                 {
511 #define RESET_EFFECTIVE_QP(xy) h->fdec->effective_qp[xy] |= 0xff * !!(h->fdec->mb_info[xy] & X264_MBINFO_CONSTANT);
512                     RESET_EFFECTIVE_QP(mb_xy);
513                     RESET_EFFECTIVE_QP(h->mb.i_mb_left_xy[0]);
514                 }
515
516                 if( intra_deblock )
517                     FILTER( _intra, 0, 0, qp_left, qpc_left );
518                 else
519                     FILTER(       , 0, 0, qp_left, qpc_left );
520             }
521         }
522         if( !first_edge_only )
523         {
524             FILTER( , 0, 1, qp, qpc );
525             FILTER( , 0, 2, qp, qpc );
526             FILTER( , 0, 3, qp, qpc );
527         }
528
529         if( h->mb.i_neighbour & MB_TOP )
530         {
531             if( b_interlaced && !(mb_y&1) && !MB_INTERLACED && h->mb.field[h->mb.i_mb_top_xy] )
532             {
533                 int mbn_xy = mb_xy - 2 * h->mb.i_mb_stride;
534
535                 for( int j = 0; j < 2; j++, mbn_xy += h->mb.i_mb_stride )
536                 {
537                     int qpt = h->mb.qp[mbn_xy];
538                     int qp_top = (qp + qpt + 1) >> 1;
539                     int qpc_top = (qpc + h->chroma_qp_table[qpt] + 1) >> 1;
540                     int intra_top = IS_INTRA( h->mb.type[mbn_xy] );
541                     if( intra_cur || intra_top )
542                         M32( bs[1][4*j] ) = 0x03030303;
543
544                     // deblock the first horizontal edge of the even rows, then the first horizontal edge of the odd rows
545                     deblock_edge( h, pixy      + j*stridey,  2* stridey, bs[1][4*j], qp_top, a, b, 0, h->loopf.deblock_luma[1] );
546                     if( chroma444 )
547                     {
548                         deblock_edge( h, pixuv          + j*strideuv, 2*strideuv, bs[1][4*j], qpc_top, a, b, 0, h->loopf.deblock_luma[1] );
549                         deblock_edge( h, pixuv + uvdiff + j*strideuv, 2*strideuv, bs[1][4*j], qpc_top, a, b, 0, h->loopf.deblock_luma[1] );
550                     }
551                     else
552                         deblock_edge( h, pixuv          + j*strideuv, 2*strideuv, bs[1][4*j], qpc_top, a, b, 1, h->loopf.deblock_chroma[1] );
553                 }
554             }
555             else
556             {
557                 int qpt = h->mb.qp[h->mb.i_mb_top_xy];
558                 int qp_top = (qp + qpt + 1) >> 1;
559                 int qpc_top = (qpc + h->chroma_qp_table[qpt] + 1) >> 1;
560                 int intra_top = IS_INTRA( h->mb.type[h->mb.i_mb_top_xy] );
561                 int intra_deblock = intra_cur || intra_top;
562
563                 /* This edge has been modified, reset effective qp to max. */
564                 if( h->fdec->mb_info && M32( bs[1][0] ) )
565                 {
566                     RESET_EFFECTIVE_QP(mb_xy);
567                     RESET_EFFECTIVE_QP(h->mb.i_mb_top_xy);
568                 }
569
570                 if( (!b_interlaced || (!MB_INTERLACED && !h->mb.field[h->mb.i_mb_top_xy])) && intra_deblock )
571                 {
572                     FILTER( _intra, 1, 0, qp_top, qpc_top );
573                 }
574                 else
575                 {
576                     if( intra_deblock )
577                         M32( bs[1][0] ) = 0x03030303;
578                     FILTER(       , 1, 0, qp_top, qpc_top );
579                 }
580             }
581         }
582
583         if( !first_edge_only )
584         {
585             FILTER( , 1, 1, qp, qpc );
586             FILTER( , 1, 2, qp, qpc );
587             FILTER( , 1, 3, qp, qpc );
588         }
589
590         #undef FILTER
591     }
592 }
593
594 /* For deblock-aware RD.
595  * TODO:
596  *  deblock macroblock edges
597  *  support analysis partitions smaller than 16x16
598  *  deblock chroma for 4:2:0/4:2:2
599  *  handle duplicate refs correctly
600  */
601 void x264_macroblock_deblock( x264_t *h )
602 {
603     int a = h->sh.i_alpha_c0_offset - QP_BD_OFFSET;
604     int b = h->sh.i_beta_offset - QP_BD_OFFSET;
605     int qp_thresh = 15 - X264_MIN( a, b ) - X264_MAX( 0, h->pps->i_chroma_qp_index_offset );
606     int intra_cur = IS_INTRA( h->mb.i_type );
607     int qp = h->mb.i_qp;
608     int qpc = h->mb.i_chroma_qp;
609     if( (h->mb.i_partition == D_16x16 && !h->mb.i_cbp_luma && !intra_cur) || qp <= qp_thresh )
610         return;
611
612     uint8_t (*bs)[8][4] = h->mb.cache.deblock_strength;
613     if( intra_cur )
614     {
615         M32( bs[0][1] ) = 0x03030303;
616         M64( bs[0][2] ) = 0x0303030303030303ULL;
617         M32( bs[1][1] ) = 0x03030303;
618         M64( bs[1][2] ) = 0x0303030303030303ULL;
619     }
620     else
621         h->loopf.deblock_strength( h->mb.cache.non_zero_count, h->mb.cache.ref, h->mb.cache.mv,
622                                    bs, 4 >> MB_INTERLACED, h->sh.i_type == SLICE_TYPE_B );
623
624     int transform_8x8 = h->mb.b_transform_8x8;
625
626     #define FILTER( dir, edge )\
627     do\
628     {\
629         deblock_edge( h, h->mb.pic.p_fdec[0] + 4*edge*(dir?FDEC_STRIDE:1),\
630                       FDEC_STRIDE, bs[dir][edge], qp, a, b, 0,\
631                       h->loopf.deblock_luma[dir] );\
632         if( CHROMA444 )\
633         {\
634             deblock_edge( h, h->mb.pic.p_fdec[1] + 4*edge*(dir?FDEC_STRIDE:1),\
635                           FDEC_STRIDE, bs[dir][edge], qpc, a, b, 0,\
636                           h->loopf.deblock_luma[dir] );\
637             deblock_edge( h, h->mb.pic.p_fdec[2] + 4*edge*(dir?FDEC_STRIDE:1),\
638                           FDEC_STRIDE, bs[dir][edge], qpc, a, b, 0,\
639                           h->loopf.deblock_luma[dir] );\
640         }\
641     } while(0)
642
643     if( !transform_8x8 ) FILTER( 0, 1 );
644                          FILTER( 0, 2 );
645     if( !transform_8x8 ) FILTER( 0, 3 );
646
647     if( !transform_8x8 ) FILTER( 1, 1 );
648                          FILTER( 1, 2 );
649     if( !transform_8x8 ) FILTER( 1, 3 );
650
651     #undef FILTER
652 }
653
654 #if HAVE_MMX
655 void x264_deblock_v_luma_sse2( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
656 void x264_deblock_v_luma_avx ( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
657 void x264_deblock_h_luma_sse2( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
658 void x264_deblock_h_luma_avx ( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
659 void x264_deblock_v_chroma_sse2( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
660 void x264_deblock_v_chroma_avx ( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
661 void x264_deblock_h_chroma_sse2( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
662 void x264_deblock_h_chroma_avx ( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
663 void x264_deblock_h_chroma_mbaff_sse2( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
664 void x264_deblock_h_chroma_mbaff_avx ( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
665 void x264_deblock_h_chroma_422_mmx2( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
666 void x264_deblock_h_chroma_422_sse2( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
667 void x264_deblock_h_chroma_422_avx ( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
668 void x264_deblock_v_luma_intra_sse2( pixel *pix, intptr_t stride, int alpha, int beta );
669 void x264_deblock_v_luma_intra_avx ( pixel *pix, intptr_t stride, int alpha, int beta );
670 void x264_deblock_h_luma_intra_sse2( pixel *pix, intptr_t stride, int alpha, int beta );
671 void x264_deblock_h_luma_intra_avx ( pixel *pix, intptr_t stride, int alpha, int beta );
672 void x264_deblock_v_chroma_intra_sse2( pixel *pix, intptr_t stride, int alpha, int beta );
673 void x264_deblock_v_chroma_intra_avx ( pixel *pix, intptr_t stride, int alpha, int beta );
674 void x264_deblock_h_chroma_intra_sse2( pixel *pix, intptr_t stride, int alpha, int beta );
675 void x264_deblock_h_chroma_intra_avx ( pixel *pix, intptr_t stride, int alpha, int beta );
676 void x264_deblock_h_chroma_422_intra_mmx2( pixel *pix, intptr_t stride, int alpha, int beta );
677 void x264_deblock_h_chroma_422_intra_sse2( pixel *pix, intptr_t stride, int alpha, int beta );
678 void x264_deblock_h_chroma_422_intra_avx ( pixel *pix, intptr_t stride, int alpha, int beta );
679 void x264_deblock_strength_mmx2 ( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
680                                   int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
681                                   int mvy_limit, int bframe );
682 void x264_deblock_strength_sse2 ( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
683                                   int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
684                                   int mvy_limit, int bframe );
685 void x264_deblock_strength_ssse3( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
686                                   int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
687                                   int mvy_limit, int bframe );
688 void x264_deblock_strength_avx  ( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
689                                   int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
690                                   int mvy_limit, int bframe );
691 void x264_deblock_strength_avx2 ( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
692                                   int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
693                                   int mvy_limit, int bframe );
694
695 void x264_deblock_h_chroma_intra_mbaff_mmx2( pixel *pix, intptr_t stride, int alpha, int beta );
696 void x264_deblock_h_chroma_intra_mbaff_sse2( pixel *pix, intptr_t stride, int alpha, int beta );
697 void x264_deblock_h_chroma_intra_mbaff_avx ( pixel *pix, intptr_t stride, int alpha, int beta );
698 #if ARCH_X86
699 void x264_deblock_h_luma_mmx2( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
700 void x264_deblock_v8_luma_mmx2( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
701 void x264_deblock_v_chroma_mmx2( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
702 void x264_deblock_h_chroma_mmx2( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
703 void x264_deblock_h_chroma_mbaff_mmx2( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
704 void x264_deblock_h_luma_intra_mmx2( pixel *pix, intptr_t stride, int alpha, int beta );
705 void x264_deblock_v8_luma_intra_mmx2( uint8_t *pix, intptr_t stride, int alpha, int beta );
706 void x264_deblock_v_chroma_intra_mmx2( pixel *pix, intptr_t stride, int alpha, int beta );
707 void x264_deblock_h_chroma_intra_mmx2( pixel *pix, intptr_t stride, int alpha, int beta );
708 void x264_deblock_h_chroma_intra_mbaff_mmx2( pixel *pix, intptr_t stride, int alpha, int beta );
709
710 #if HIGH_BIT_DEPTH
711 void x264_deblock_v_luma_mmx2( pixel *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
712 void x264_deblock_v_luma_intra_mmx2( pixel *pix, intptr_t stride, int alpha, int beta );
713 #else
714 // FIXME this wrapper has a significant cpu cost
715 static void x264_deblock_v_luma_mmx2( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 )
716 {
717     x264_deblock_v8_luma_mmx2( pix,   stride, alpha, beta, tc0   );
718     x264_deblock_v8_luma_mmx2( pix+8, stride, alpha, beta, tc0+2 );
719 }
720 static void x264_deblock_v_luma_intra_mmx2( uint8_t *pix, intptr_t stride, int alpha, int beta )
721 {
722     x264_deblock_v8_luma_intra_mmx2( pix,   stride, alpha, beta );
723     x264_deblock_v8_luma_intra_mmx2( pix+8, stride, alpha, beta );
724 }
725 #endif // HIGH_BIT_DEPTH
726 #endif
727 #endif
728
729 #if ARCH_PPC
730 void x264_deblock_v_luma_altivec( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
731 void x264_deblock_h_luma_altivec( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
732 #endif // ARCH_PPC
733
734 #if HAVE_ARMV6 || ARCH_AARCH64
735 void x264_deblock_v_luma_neon  ( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
736 void x264_deblock_h_luma_neon  ( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
737 void x264_deblock_v_chroma_neon( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
738 void x264_deblock_h_chroma_neon( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
739 void x264_deblock_strength_neon( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
740                                  int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4],
741                                  int mvy_limit, int bframe );
742 void x264_deblock_h_chroma_422_neon( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
743 void x264_deblock_h_chroma_mbaff_neon( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
744 #if ARCH_AARCH64
745 void x264_deblock_h_chroma_intra_mbaff_neon( uint8_t *pix, intptr_t stride, int alpha, int beta );
746 void x264_deblock_h_chroma_intra_neon( uint8_t *pix, intptr_t stride, int alpha, int beta );
747 void x264_deblock_h_chroma_422_intra_neon( uint8_t *pix, intptr_t stride, int alpha, int beta );
748 void x264_deblock_v_chroma_intra_neon( uint8_t *pix, intptr_t stride, int alpha, int beta );
749 void x264_deblock_h_luma_intra_neon( uint8_t *pix, intptr_t stride, int alpha, int beta );
750 void x264_deblock_v_luma_intra_neon( uint8_t *pix, intptr_t stride, int alpha, int beta );
751 #endif
752 #endif
753
754 #if !HIGH_BIT_DEPTH
755 #if HAVE_MSA
756 void x264_deblock_v_luma_msa( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
757 void x264_deblock_h_luma_msa( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
758 void x264_deblock_v_chroma_msa( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
759 void x264_deblock_h_chroma_msa( uint8_t *pix, intptr_t stride, int alpha, int beta, int8_t *tc0 );
760 void x264_deblock_v_luma_intra_msa( uint8_t *pix, intptr_t stride, int alpha, int beta );
761 void x264_deblock_h_luma_intra_msa( uint8_t *pix, intptr_t stride, int alpha, int beta );
762 void x264_deblock_v_chroma_intra_msa( uint8_t *pix, intptr_t stride, int alpha, int beta );
763 void x264_deblock_h_chroma_intra_msa( uint8_t *pix, intptr_t stride, int alpha, int beta );
764 void x264_deblock_strength_msa( uint8_t nnz[X264_SCAN8_SIZE], int8_t ref[2][X264_SCAN8_LUMA_SIZE],
765                                 int16_t mv[2][X264_SCAN8_LUMA_SIZE][2], uint8_t bs[2][8][4], int mvy_limit,
766                                 int bframe );
767 #endif
768 #endif
769
770 void x264_deblock_init( int cpu, x264_deblock_function_t *pf, int b_mbaff )
771 {
772     pf->deblock_luma[1] = deblock_v_luma_c;
773     pf->deblock_luma[0] = deblock_h_luma_c;
774     pf->deblock_chroma[1] = deblock_v_chroma_c;
775     pf->deblock_h_chroma_420 = deblock_h_chroma_c;
776     pf->deblock_h_chroma_422 = deblock_h_chroma_422_c;
777     pf->deblock_luma_intra[1] = deblock_v_luma_intra_c;
778     pf->deblock_luma_intra[0] = deblock_h_luma_intra_c;
779     pf->deblock_chroma_intra[1] = deblock_v_chroma_intra_c;
780     pf->deblock_h_chroma_420_intra = deblock_h_chroma_intra_c;
781     pf->deblock_h_chroma_422_intra = deblock_h_chroma_422_intra_c;
782     pf->deblock_luma_mbaff = deblock_h_luma_mbaff_c;
783     pf->deblock_chroma_420_mbaff = deblock_h_chroma_mbaff_c;
784     pf->deblock_luma_intra_mbaff = deblock_h_luma_intra_mbaff_c;
785     pf->deblock_chroma_420_intra_mbaff = deblock_h_chroma_intra_mbaff_c;
786     pf->deblock_strength = deblock_strength_c;
787
788 #if HAVE_MMX
789     if( cpu&X264_CPU_MMX2 )
790     {
791 #if ARCH_X86
792         pf->deblock_luma[1] = x264_deblock_v_luma_mmx2;
793         pf->deblock_luma[0] = x264_deblock_h_luma_mmx2;
794         pf->deblock_chroma[1] = x264_deblock_v_chroma_mmx2;
795         pf->deblock_h_chroma_420 = x264_deblock_h_chroma_mmx2;
796         pf->deblock_chroma_420_mbaff = x264_deblock_h_chroma_mbaff_mmx2;
797         pf->deblock_h_chroma_422 = x264_deblock_h_chroma_422_mmx2;
798         pf->deblock_h_chroma_422_intra = x264_deblock_h_chroma_422_intra_mmx2;
799         pf->deblock_luma_intra[1] = x264_deblock_v_luma_intra_mmx2;
800         pf->deblock_luma_intra[0] = x264_deblock_h_luma_intra_mmx2;
801         pf->deblock_chroma_intra[1] = x264_deblock_v_chroma_intra_mmx2;
802         pf->deblock_h_chroma_420_intra = x264_deblock_h_chroma_intra_mmx2;
803         pf->deblock_chroma_420_intra_mbaff = x264_deblock_h_chroma_intra_mbaff_mmx2;
804 #endif
805 #if !HIGH_BIT_DEPTH
806         pf->deblock_chroma_420_intra_mbaff = x264_deblock_h_chroma_intra_mbaff_mmx2;
807 #endif
808         pf->deblock_strength = x264_deblock_strength_mmx2;
809         if( cpu&X264_CPU_SSE2 )
810         {
811             pf->deblock_strength = x264_deblock_strength_sse2;
812             pf->deblock_h_chroma_420 = x264_deblock_h_chroma_sse2;
813             pf->deblock_h_chroma_422 = x264_deblock_h_chroma_422_sse2;
814             pf->deblock_h_chroma_422_intra = x264_deblock_h_chroma_422_intra_sse2;
815             pf->deblock_chroma_420_mbaff = x264_deblock_h_chroma_mbaff_sse2;
816             pf->deblock_luma[1] = x264_deblock_v_luma_sse2;
817             pf->deblock_luma[0] = x264_deblock_h_luma_sse2;
818             pf->deblock_luma_intra[1] = x264_deblock_v_luma_intra_sse2;
819             pf->deblock_luma_intra[0] = x264_deblock_h_luma_intra_sse2;
820             if( !(cpu&X264_CPU_STACK_MOD4) )
821             {
822                 pf->deblock_chroma[1] = x264_deblock_v_chroma_sse2;
823                 pf->deblock_chroma_intra[1] = x264_deblock_v_chroma_intra_sse2;
824                 pf->deblock_h_chroma_420_intra = x264_deblock_h_chroma_intra_sse2;
825 #if HIGH_BIT_DEPTH
826                 pf->deblock_chroma_420_intra_mbaff= x264_deblock_h_chroma_intra_mbaff_sse2;
827 #endif
828             }
829         }
830         if( cpu&X264_CPU_SSSE3 )
831             pf->deblock_strength = x264_deblock_strength_ssse3;
832         if( cpu&X264_CPU_AVX )
833         {
834             pf->deblock_strength = x264_deblock_strength_avx;
835             pf->deblock_h_chroma_420 = x264_deblock_h_chroma_avx;
836             pf->deblock_h_chroma_422 = x264_deblock_h_chroma_422_avx;
837             pf->deblock_h_chroma_422_intra = x264_deblock_h_chroma_422_intra_avx;
838             pf->deblock_luma[1] = x264_deblock_v_luma_avx;
839             pf->deblock_luma[0] = x264_deblock_h_luma_avx;
840             pf->deblock_luma_intra[1] = x264_deblock_v_luma_intra_avx;
841             pf->deblock_luma_intra[0] = x264_deblock_h_luma_intra_avx;
842             if( !(cpu&X264_CPU_STACK_MOD4) )
843             {
844                 pf->deblock_chroma[1] = x264_deblock_v_chroma_avx;
845                 pf->deblock_chroma_intra[1] = x264_deblock_v_chroma_intra_avx;
846                 pf->deblock_h_chroma_420_intra = x264_deblock_h_chroma_intra_avx;
847 #if HIGH_BIT_DEPTH
848                 pf->deblock_chroma_420_mbaff = x264_deblock_h_chroma_mbaff_avx;
849                 pf->deblock_chroma_420_intra_mbaff = x264_deblock_h_chroma_intra_mbaff_avx;
850 #endif
851             }
852         }
853         if( cpu&X264_CPU_AVX2 )
854         {
855             pf->deblock_strength = x264_deblock_strength_avx2;
856         }
857     }
858 #endif
859
860 #if !HIGH_BIT_DEPTH
861 #if HAVE_ALTIVEC
862     if( cpu&X264_CPU_ALTIVEC )
863     {
864         pf->deblock_luma[1] = x264_deblock_v_luma_altivec;
865         pf->deblock_luma[0] = x264_deblock_h_luma_altivec;
866     }
867 #endif // HAVE_ALTIVEC
868
869 #if HAVE_ARMV6 || ARCH_AARCH64
870     if( cpu&X264_CPU_NEON )
871     {
872         pf->deblock_luma[1] = x264_deblock_v_luma_neon;
873         pf->deblock_luma[0] = x264_deblock_h_luma_neon;
874         pf->deblock_chroma[1] = x264_deblock_v_chroma_neon;
875         pf->deblock_h_chroma_420 = x264_deblock_h_chroma_neon;
876         pf->deblock_h_chroma_422 = x264_deblock_h_chroma_422_neon;
877         pf->deblock_chroma_420_mbaff = x264_deblock_h_chroma_mbaff_neon;
878 #if ARCH_AARCH64
879         pf->deblock_chroma_420_intra_mbaff = x264_deblock_h_chroma_intra_mbaff_neon;
880         pf->deblock_h_chroma_420_intra = x264_deblock_h_chroma_intra_neon;
881         pf->deblock_h_chroma_422_intra = x264_deblock_h_chroma_422_intra_neon;
882         pf->deblock_chroma_intra[1] = x264_deblock_v_chroma_intra_neon;
883         pf->deblock_luma_intra[0] = x264_deblock_h_luma_intra_neon;
884         pf->deblock_luma_intra[1] = x264_deblock_v_luma_intra_neon;
885 #endif
886         pf->deblock_strength     = x264_deblock_strength_neon;
887     }
888 #endif
889
890 #if HAVE_MSA
891     if( cpu&X264_CPU_MSA )
892     {
893         pf->deblock_luma[1] = x264_deblock_v_luma_msa;
894         pf->deblock_luma[0] = x264_deblock_h_luma_msa;
895         pf->deblock_chroma[1] = x264_deblock_v_chroma_msa;
896         pf->deblock_h_chroma_420 = x264_deblock_h_chroma_msa;
897         pf->deblock_luma_intra[1] = x264_deblock_v_luma_intra_msa;
898         pf->deblock_luma_intra[0] = x264_deblock_h_luma_intra_msa;
899         pf->deblock_chroma_intra[1] = x264_deblock_v_chroma_intra_msa;
900         pf->deblock_h_chroma_420_intra = x264_deblock_h_chroma_intra_msa;
901         pf->deblock_strength = x264_deblock_strength_msa;
902     }
903 #endif
904 #endif // !HIGH_BIT_DEPTH
905
906     /* These functions are equivalent, so don't duplicate them. */
907     pf->deblock_chroma_422_mbaff = pf->deblock_h_chroma_420;
908     pf->deblock_chroma_422_intra_mbaff = pf->deblock_h_chroma_420_intra;
909 }