]> git.sesse.net Git - x264/blob - common/macroblock.c
Faster deblock strength asm on conroe/penryn
[x264] / common / macroblock.c
1 /*****************************************************************************
2  * macroblock.c: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2003-2008 x264 project
5  *
6  * Authors: Fiona Glaser <fiona@x264.com>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *          Loren Merritt <lorenm@u.washington.edu>
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
25 #include "common.h"
26 #include "encoder/me.h"
27
28 static NOINLINE void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
29 {
30     int i8    = x264_scan8[0]+x+8*y;
31     int i_ref = h->mb.cache.ref[0][i8];
32     int mvx   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
33     int mvy   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
34
35     h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
36                    h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
37                    mvx, mvy, 4*width, 4*height, &h->sh.weight[i_ref][0] );
38
39     // chroma is offset if MCing from a field of opposite parity
40     if( h->mb.b_interlaced & i_ref )
41         mvy += (h->mb.i_mb_y & 1)*4 - 2;
42
43     h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
44                      h->mb.pic.p_fref[0][i_ref][4], h->mb.pic.i_stride[1],
45                      mvx, mvy, 2*width, 2*height );
46
47     if( h->sh.weight[i_ref][1].weightfn )
48         h->sh.weight[i_ref][1].weightfn[width>>1]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
49                                                    &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
50                                                    &h->sh.weight[i_ref][1], height*2 );
51
52     h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
53                      h->mb.pic.p_fref[0][i_ref][5], h->mb.pic.i_stride[2],
54                      mvx, mvy, 2*width, 2*height );
55
56     if( h->sh.weight[i_ref][2].weightfn )
57         h->sh.weight[i_ref][2].weightfn[width>>1]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
58                                                    &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
59                                                    &h->sh.weight[i_ref][2],height*2 );
60
61 }
62 static NOINLINE void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
63 {
64     int i8    = x264_scan8[0]+x+8*y;
65     int i_ref = h->mb.cache.ref[1][i8];
66     int mvx   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
67     int mvy   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
68
69     h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
70                    h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
71                    mvx, mvy, 4*width, 4*height, weight_none );
72
73     if( h->mb.b_interlaced & i_ref )
74         mvy += (h->mb.i_mb_y & 1)*4 - 2;
75
76     h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
77                      h->mb.pic.p_fref[1][i_ref][4], h->mb.pic.i_stride[1],
78                      mvx, mvy, 2*width, 2*height );
79
80     h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
81                      h->mb.pic.p_fref[1][i_ref][5], h->mb.pic.i_stride[2],
82                      mvx, mvy, 2*width, 2*height );
83 }
84
85 static NOINLINE void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
86 {
87     int i8 = x264_scan8[0]+x+8*y;
88     int i_ref0 = h->mb.cache.ref[0][i8];
89     int i_ref1 = h->mb.cache.ref[1][i8];
90     int weight = h->mb.bipred_weight[i_ref0][i_ref1];
91     int mvx0   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
92     int mvx1   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
93     int mvy0   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
94     int mvy1   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
95     int i_mode = x264_size2pixel[height][width];
96     int i_stride0 = 16, i_stride1 = 16;
97     ALIGNED_ARRAY_16( uint8_t, tmp0,[16*16] );
98     ALIGNED_ARRAY_16( uint8_t, tmp1,[16*16] );
99     uint8_t *src0, *src1;
100
101     src0 = h->mc.get_ref( tmp0, &i_stride0, h->mb.pic.p_fref[0][i_ref0], h->mb.pic.i_stride[0],
102                           mvx0, mvy0, 4*width, 4*height, weight_none );
103     src1 = h->mc.get_ref( tmp1, &i_stride1, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
104                           mvx1, mvy1, 4*width, 4*height, weight_none );
105     h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
106                        src0, i_stride0, src1, i_stride1, weight );
107
108     if( h->mb.b_interlaced & i_ref0 )
109         mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
110     if( h->mb.b_interlaced & i_ref1 )
111         mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
112
113     h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][4], h->mb.pic.i_stride[1],
114                      mvx0, mvy0, 2*width, 2*height );
115     h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][4], h->mb.pic.i_stride[1],
116                      mvx1, mvy1, 2*width, 2*height );
117     h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp0, 16, tmp1, 16, weight );
118     h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][5], h->mb.pic.i_stride[2],
119                      mvx0, mvy0, 2*width, 2*height );
120     h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][5], h->mb.pic.i_stride[2],
121                      mvx1, mvy1, 2*width, 2*height );
122     h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp0, 16, tmp1, 16, weight );
123 }
124
125 void x264_mb_mc_8x8( x264_t *h, int i8 )
126 {
127     int x = 2*(i8&1);
128     int y = 2*(i8>>1);
129
130     if( h->sh.i_type == SLICE_TYPE_P )
131     {
132         switch( h->mb.i_sub_partition[i8] )
133         {
134             case D_L0_8x8:
135                 x264_mb_mc_0xywh( h, x, y, 2, 2 );
136                 break;
137             case D_L0_8x4:
138                 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
139                 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
140                 break;
141             case D_L0_4x8:
142                 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
143                 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
144                 break;
145             case D_L0_4x4:
146                 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
147                 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
148                 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
149                 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
150                 break;
151         }
152     }
153     else
154     {
155         int scan8 = x264_scan8[0] + x + 8*y;
156
157         if( h->mb.cache.ref[0][scan8] >= 0 )
158             if( h->mb.cache.ref[1][scan8] >= 0 )
159                 x264_mb_mc_01xywh( h, x, y, 2, 2 );
160             else
161                 x264_mb_mc_0xywh( h, x, y, 2, 2 );
162         else
163             x264_mb_mc_1xywh( h, x, y, 2, 2 );
164     }
165 }
166
167 void x264_mb_mc( x264_t *h )
168 {
169     if( h->mb.i_partition == D_8x8 )
170     {
171         for( int i = 0; i < 4; i++ )
172             x264_mb_mc_8x8( h, i );
173     }
174     else
175     {
176         int ref0a = h->mb.cache.ref[0][x264_scan8[ 0]];
177         int ref0b = h->mb.cache.ref[0][x264_scan8[12]];
178         int ref1a = h->mb.cache.ref[1][x264_scan8[ 0]];
179         int ref1b = h->mb.cache.ref[1][x264_scan8[12]];
180
181         if( h->mb.i_partition == D_16x16 )
182         {
183             if( ref0a >= 0 )
184                 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
185                 else             x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
186             else                 x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
187         }
188         else if( h->mb.i_partition == D_16x8 )
189         {
190             if( ref0a >= 0 )
191                 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
192                 else             x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
193             else                 x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
194
195             if( ref0b >= 0 )
196                 if( ref1b >= 0 ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
197                 else             x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
198             else                 x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
199         }
200         else if( h->mb.i_partition == D_8x16 )
201         {
202             if( ref0a >= 0 )
203                 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
204                 else             x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
205             else                 x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
206
207             if( ref0b >= 0 )
208                 if( ref1b >= 0 ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
209                 else             x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
210             else                 x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
211         }
212     }
213 }
214
215 int x264_macroblock_cache_allocate( x264_t *h )
216 {
217     int i_mb_count = h->mb.i_mb_count;
218
219     h->mb.i_mb_stride = h->sps->i_mb_width;
220     h->mb.i_b8_stride = h->sps->i_mb_width * 2;
221     h->mb.i_b4_stride = h->sps->i_mb_width * 4;
222
223     h->mb.b_interlaced = h->param.b_interlaced;
224
225     CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
226     CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
227     CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
228     CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
229     CHECKED_MALLOC( h->mb.slice_table, i_mb_count * sizeof(uint16_t) );
230     memset( h->mb.slice_table, -1, i_mb_count * sizeof(uint16_t) );
231
232     /* 0 -> 3 top(4), 4 -> 6 : left(3) */
233     CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
234
235     /* all coeffs */
236     CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
237
238     if( h->param.b_cabac )
239     {
240         CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
241         CHECKED_MALLOC( h->mb.mvd[0], i_mb_count * sizeof( **h->mb.mvd ) );
242         CHECKED_MALLOC( h->mb.mvd[1], i_mb_count * sizeof( **h->mb.mvd ) );
243     }
244
245     for( int i = 0; i < 2; i++ )
246     {
247         int i_refs = X264_MIN(16, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << h->param.b_interlaced;
248         if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
249             i_refs = X264_MIN(16, i_refs + 2); //smart weights add two duplicate frames
250         else if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_BLIND )
251             i_refs = X264_MIN(16, i_refs + 1); //blind weights add one duplicate frame
252
253         for( int j = !i; j < i_refs; j++ )
254             CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
255     }
256
257     if( h->param.analyse.i_weighted_pred )
258     {
259         int i_padv = PADV << h->param.b_interlaced;
260 #define ALIGN(x,a) (((x)+((a)-1))&~((a)-1))
261         int align = h->param.cpu&X264_CPU_CACHELINE_64 ? 64 : h->param.cpu&X264_CPU_CACHELINE_32 ? 32 : 16;
262         int i_stride, luma_plane_size = 0;
263         int numweightbuf;
264
265         if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_FAKE )
266         {
267             // only need buffer for lookahead
268             if( !h->param.i_sync_lookahead || h == h->thread[h->param.i_threads] )
269             {
270                 // Fake analysis only works on lowres
271                 i_stride = ALIGN( h->sps->i_mb_width*8 + 2*PADH, align );
272                 luma_plane_size = i_stride * (h->sps->i_mb_height*8+2*i_padv);
273                 // Only need 1 buffer for analysis
274                 numweightbuf = 1;
275             }
276             else
277                 numweightbuf = 0;
278         }
279         else
280         {
281             i_stride = ALIGN( h->sps->i_mb_width*16 + 2*PADH, align );
282             luma_plane_size = i_stride * (h->sps->i_mb_height*16+2*i_padv);
283
284             if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
285                 //SMART can weight one ref and one offset -1
286                 numweightbuf = 2;
287             else
288                 //blind only has one weighted copy (offset -1)
289                 numweightbuf = 1;
290         }
291
292         for( int i = 0; i < numweightbuf; i++ )
293             CHECKED_MALLOC( h->mb.p_weight_buf[i], luma_plane_size );
294 #undef ALIGN
295     }
296
297     return 0;
298 fail:
299     return -1;
300 }
301 void x264_macroblock_cache_free( x264_t *h )
302 {
303     for( int i = 0; i < 2; i++ )
304         for( int j = !i; j < 32; j++ )
305             x264_free( h->mb.mvr[i][j] );
306     for( int i = 0; i < 16; i++ )
307         x264_free( h->mb.p_weight_buf[i] );
308
309     if( h->param.b_cabac )
310     {
311         x264_free( h->mb.chroma_pred_mode );
312         x264_free( h->mb.mvd[0] );
313         x264_free( h->mb.mvd[1] );
314     }
315     x264_free( h->mb.slice_table );
316     x264_free( h->mb.intra4x4_pred_mode );
317     x264_free( h->mb.non_zero_count );
318     x264_free( h->mb.mb_transform_size );
319     x264_free( h->mb.skipbp );
320     x264_free( h->mb.cbp );
321     x264_free( h->mb.qp );
322 }
323
324 int x264_macroblock_thread_allocate( x264_t *h, int b_lookahead )
325 {
326     if( !b_lookahead )
327         for( int i = 0; i <= h->param.b_interlaced; i++ )
328             for( int j = 0; j < 3; j++ )
329             {
330                 /* shouldn't really be initialized, just silences a valgrind false-positive in predict_8x8_filter_mmx */
331                 CHECKED_MALLOCZERO( h->intra_border_backup[i][j], (h->sps->i_mb_width*16+32)>>!!j );
332                 h->intra_border_backup[i][j] += 8;
333             }
334
335     /* Allocate scratch buffer */
336     int scratch_size = 0;
337     if( !b_lookahead )
338     {
339         int buf_hpel = (h->thread[0]->fdec->i_width[0]+48) * sizeof(int16_t);
340         int buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int);
341         int me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range);
342         int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) *
343             ((me_range*2+18) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t));
344         int buf_nnz = !h->param.b_cabac * h->pps->b_transform_8x8_mode * (h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t));
345         scratch_size = X264_MAX4( buf_hpel, buf_ssim, buf_tesa, buf_nnz );
346     }
347     int buf_mbtree = h->param.rc.b_mb_tree * ((h->sps->i_mb_width+3)&~3) * sizeof(int);
348     scratch_size = X264_MAX( scratch_size, buf_mbtree );
349     CHECKED_MALLOC( h->scratch_buffer, scratch_size );
350
351     return 0;
352 fail:
353     return -1;
354 }
355
356 void x264_macroblock_thread_free( x264_t *h, int b_lookahead )
357 {
358     if( !b_lookahead )
359         for( int i = 0; i <= h->param.b_interlaced; i++ )
360             for( int j = 0; j < 3; j++ )
361                 x264_free( h->intra_border_backup[i][j] - 8 );
362     x264_free( h->scratch_buffer );
363 }
364
365 void x264_macroblock_slice_init( x264_t *h )
366 {
367     h->mb.mv[0] = h->fdec->mv[0];
368     h->mb.mv[1] = h->fdec->mv[1];
369     h->mb.mvr[0][0] = h->fdec->mv16x16;
370     h->mb.ref[0] = h->fdec->ref[0];
371     h->mb.ref[1] = h->fdec->ref[1];
372     h->mb.type = h->fdec->mb_type;
373     h->mb.partition = h->fdec->mb_partition;
374
375     h->fdec->i_ref[0] = h->i_ref0;
376     h->fdec->i_ref[1] = h->i_ref1;
377     for( int i = 0; i < h->i_ref0; i++ )
378         h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
379     if( h->sh.i_type == SLICE_TYPE_B )
380     {
381         for( int i = 0; i < h->i_ref1; i++ )
382             h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
383
384         map_col_to_list0(-1) = -1;
385         map_col_to_list0(-2) = -2;
386         for( int i = 0; i < h->fref1[0]->i_ref[0]; i++ )
387         {
388             int poc = h->fref1[0]->ref_poc[0][i];
389             map_col_to_list0(i) = -2;
390             for( int j = 0; j < h->i_ref0; j++ )
391                 if( h->fref0[j]->i_poc == poc )
392                 {
393                     map_col_to_list0(i) = j;
394                     break;
395                 }
396         }
397     }
398     if( h->sh.i_type == SLICE_TYPE_P )
399         memset( h->mb.cache.skip, 0, sizeof( h->mb.cache.skip ) );
400
401     /* init with not available (for top right idx=7,15) */
402     memset( h->mb.cache.ref, -2, sizeof( h->mb.cache.ref ) );
403
404     if( h->i_ref0 > 0 )
405         for( int field = 0; field <= h->sh.b_mbaff; field++ )
406         {
407             int curpoc = h->fdec->i_poc + field*h->sh.i_delta_poc_bottom;
408             int refpoc = h->fref0[0]->i_poc;
409             if( h->sh.b_mbaff && field )
410                 refpoc += h->sh.i_delta_poc_bottom;
411             int delta = curpoc - refpoc;
412
413             h->fdec->inv_ref_poc[field] = (256 + delta/2) / delta;
414         }
415
416     h->mb.i_neighbour4[6] =
417     h->mb.i_neighbour4[9] =
418     h->mb.i_neighbour4[12] =
419     h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
420     h->mb.i_neighbour4[3] =
421     h->mb.i_neighbour4[7] =
422     h->mb.i_neighbour4[11] =
423     h->mb.i_neighbour4[13] =
424     h->mb.i_neighbour4[15] =
425     h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
426 }
427
428 void x264_macroblock_thread_init( x264_t *h )
429 {
430     h->mb.i_me_method = h->param.analyse.i_me_method;
431     h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
432     if( h->sh.i_type == SLICE_TYPE_B && (h->mb.i_subpel_refine == 6 || h->mb.i_subpel_refine == 8) )
433         h->mb.i_subpel_refine--;
434     h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
435                         && h->mb.i_subpel_refine >= 5;
436     h->mb.b_dct_decimate = h->sh.i_type == SLICE_TYPE_B ||
437                           (h->param.analyse.b_dct_decimate && h->sh.i_type != SLICE_TYPE_I);
438
439
440     /* fdec:      fenc:
441      * yyyyyyy
442      * yYYYY      YYYY
443      * yYYYY      YYYY
444      * yYYYY      YYYY
445      * yYYYY      YYYY
446      * uuu vvv    UUVV
447      * uUU vVV    UUVV
448      * uUU vVV
449      */
450     h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
451     h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
452     h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
453     h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
454     h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
455     h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
456 }
457
458 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
459 {
460     int stride_y  = fenc->i_stride[0];
461     int stride_uv = fenc->i_stride[1];
462     int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
463     int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
464     h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
465                          fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
466 }
467
468 static NOINLINE void copy_column8( uint8_t *dst, uint8_t *src )
469 {
470     // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
471     for( int i = -4; i < 4; i++ )
472         dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
473 }
474
475 static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int mb_x, int mb_y, int i )
476 {
477     const int w = (i == 0 ? 16 : 8);
478     const int i_stride = h->fdec->i_stride[!!i];
479     const int i_stride2 = i_stride << h->mb.b_interlaced;
480     const int i_pix_offset = h->mb.b_interlaced
481                            ? w * (mb_x + (mb_y&~1) * i_stride) + (mb_y&1) * i_stride
482                            : w * (mb_x + mb_y * i_stride);
483     const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
484     const uint8_t *intra_fdec = &h->intra_border_backup[mb_y & h->sh.b_mbaff][i][mb_x*16>>!!i];
485     int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
486     x264_frame_t **fref[2] = { h->fref0, h->fref1 };
487     if( h->mb.b_interlaced )
488         ref_pix_offset[1] += (1-2*(mb_y&1)) * i_stride;
489     h->mb.pic.i_stride[i] = i_stride2;
490     h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
491     h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
492         h->mb.pic.p_fenc_plane[i], i_stride2, w );
493     memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
494     if( h->mb.b_interlaced )
495         for( int j = 0; j < w; j++ )
496             h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
497     for( int j = 0; j < h->mb.pic.i_fref[0]; j++ )
498     {
499         h->mb.pic.p_fref[0][j][i==0 ? 0:i+3] = &fref[0][j >> h->mb.b_interlaced]->plane[i][ref_pix_offset[j&1]];
500         if( i == 0 )
501         {
502             for( int k = 1; k < 4; k++ )
503                 h->mb.pic.p_fref[0][j][k] = &fref[0][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
504             if( h->sh.weight[j][0].weightfn )
505                 h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> h->mb.b_interlaced][ref_pix_offset[j&1]];
506             else
507                 h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
508         }
509     }
510     if( h->sh.i_type == SLICE_TYPE_B )
511         for( int j = 0; j < h->mb.pic.i_fref[1]; j++ )
512         {
513             h->mb.pic.p_fref[1][j][i==0 ? 0:i+3] = &fref[1][j >> h->mb.b_interlaced]->plane[i][ref_pix_offset[j&1]];
514             if( i == 0 )
515                 for( int k = 1; k < 4; k++ )
516                     h->mb.pic.p_fref[1][j][k] = &fref[1][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
517         }
518 }
519
520 static void inline x264_macroblock_cache_load_neighbours( x264_t *h, int mb_x, int mb_y )
521 {
522     int top = (mb_y - (1 << h->mb.b_interlaced)) * h->mb.i_mb_stride + mb_x;
523
524     h->mb.i_mb_x = mb_x;
525     h->mb.i_mb_y = mb_y;
526     h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
527     h->mb.i_b8_xy = 2*(mb_y * h->mb.i_b8_stride + mb_x);
528     h->mb.i_b4_xy = 4*(mb_y * h->mb.i_b4_stride + mb_x);
529     h->mb.i_neighbour = 0;
530     h->mb.i_neighbour_intra = 0;
531     h->mb.i_neighbour_frame = 0;
532     h->mb.i_mb_top_xy = -1;
533     h->mb.i_mb_left_xy = -1;
534     h->mb.i_mb_topleft_xy = -1;
535     h->mb.i_mb_topright_xy = -1;
536     h->mb.i_mb_type_top = -1;
537     h->mb.i_mb_type_left = -1;
538     h->mb.i_mb_type_topleft = -1;
539     h->mb.i_mb_type_topright = -1;
540
541     if( mb_x > 0 )
542     {
543         h->mb.i_neighbour_frame |= MB_LEFT;
544         h->mb.i_mb_left_xy = h->mb.i_mb_xy - 1;
545         h->mb.i_mb_type_left = h->mb.type[h->mb.i_mb_left_xy];
546         if( h->mb.i_mb_xy > h->sh.i_first_mb )
547         {
548             h->mb.i_neighbour |= MB_LEFT;
549
550             if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_left ) )
551                 h->mb.i_neighbour_intra |= MB_LEFT;
552         }
553     }
554
555     /* We can't predict from the previous threadslice since it hasn't been encoded yet. */
556     if( (h->i_threadslice_start >> h->mb.b_interlaced) != (mb_y >> h->mb.b_interlaced) )
557     {
558         if( top >= 0 )
559         {
560             h->mb.i_neighbour_frame |= MB_TOP;
561             h->mb.i_mb_top_xy = top;
562             h->mb.i_mb_type_top = h->mb.type[h->mb.i_mb_top_xy];
563             if( top >= h->sh.i_first_mb )
564             {
565                 h->mb.i_neighbour |= MB_TOP;
566
567                 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_top ) )
568                     h->mb.i_neighbour_intra |= MB_TOP;
569
570                 /* We only need to prefetch the top blocks because the left was just written
571                  * to as part of the previous cache_save.  Since most target CPUs use write-allocate
572                  * caches, left blocks are near-guaranteed to be in L1 cache.  Top--not so much. */
573                 x264_prefetch( &h->mb.cbp[top] );
574                 x264_prefetch( h->mb.intra4x4_pred_mode[top] );
575                 x264_prefetch( &h->mb.non_zero_count[top][12] );
576                 /* These aren't always allocated, but prefetching an invalid address can't hurt. */
577                 x264_prefetch( &h->mb.mb_transform_size[top] );
578                 x264_prefetch( &h->mb.skipbp[top] );
579             }
580         }
581
582         if( mb_x > 0 && top - 1 >= 0  )
583         {
584             h->mb.i_neighbour_frame |= MB_TOPLEFT;
585             h->mb.i_mb_topleft_xy = top - 1;
586             h->mb.i_mb_type_topleft = h->mb.type[h->mb.i_mb_topleft_xy];
587             if( top - 1 >= h->sh.i_first_mb )
588             {
589                 h->mb.i_neighbour |= MB_TOPLEFT;
590
591                 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
592                     h->mb.i_neighbour_intra |= MB_TOPLEFT;
593             }
594         }
595
596         if( mb_x < h->sps->i_mb_width - 1 && top + 1 >= 0 )
597         {
598             h->mb.i_neighbour_frame |= MB_TOPRIGHT;
599             h->mb.i_mb_topright_xy = top + 1;
600             h->mb.i_mb_type_topright = h->mb.type[h->mb.i_mb_topright_xy];
601             if( top + 1 >= h->sh.i_first_mb )
602             {
603                 h->mb.i_neighbour |= MB_TOPRIGHT;
604
605                 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
606                     h->mb.i_neighbour_intra |= MB_TOPRIGHT;
607             }
608         }
609     }
610 }
611
612 void x264_macroblock_cache_load( x264_t *h, int mb_x, int mb_y )
613 {
614     x264_macroblock_cache_load_neighbours( h, mb_x, mb_y );
615
616     int left = h->mb.i_mb_left_xy;
617     int top  = h->mb.i_mb_top_xy;
618     int top_y = mb_y - (1 << h->mb.b_interlaced);
619     int top_8x8 = (2*top_y+1) * h->mb.i_b8_stride + 2*mb_x;
620     int top_4x4 = (4*top_y+3) * h->mb.i_b4_stride + 4*mb_x;
621
622     /* GCC pessimizes direct loads from heap-allocated arrays due to aliasing. */
623     /* By only dereferencing them once, we avoid this issue. */
624     int8_t (*i4x4)[8] = h->mb.intra4x4_pred_mode;
625     uint8_t (*nnz)[24] = h->mb.non_zero_count;
626     int16_t *cbp = h->mb.cbp;
627
628     /* load cache */
629     if( h->mb.i_neighbour & MB_TOP )
630     {
631         h->mb.cache.i_cbp_top = cbp[top];
632         /* load intra4x4 */
633         CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &i4x4[top][0] );
634
635         /* load non_zero_count */
636         CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &nnz[top][12] );
637         /* shift because x264_scan8[16] is misaligned */
638         M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = M16( &nnz[top][18] ) << 8;
639         M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = M16( &nnz[top][22] ) << 8;
640     }
641     else
642     {
643         h->mb.cache.i_cbp_top = -1;
644
645         /* load intra4x4 */
646         M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
647
648         /* load non_zero_count */
649         M32( &h->mb.cache.non_zero_count[x264_scan8[   0] - 8] ) = 0x80808080U;
650         M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = 0x80808080U;
651         M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = 0x80808080U;
652     }
653
654     if( h->mb.i_neighbour & MB_LEFT )
655     {
656         h->mb.cache.i_cbp_left = cbp[left];
657
658         /* load intra4x4 */
659         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = i4x4[left][4];
660         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = i4x4[left][5];
661         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = i4x4[left][6];
662         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = i4x4[left][3];
663
664         /* load non_zero_count */
665         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = nnz[left][3];
666         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = nnz[left][7];
667         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = nnz[left][11];
668         h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[left][15];
669
670         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = nnz[left][16+1];
671         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = nnz[left][16+3];
672
673         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = nnz[left][16+4+1];
674         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = nnz[left][16+4+3];
675
676         /* Finish the prefetching */
677         if( h->sh.i_type != SLICE_TYPE_I )
678             for( int l = 0; l < (h->sh.i_type == SLICE_TYPE_B) + 1; l++ )
679             {
680                 x264_prefetch( &h->mb.mv[l][top_4x4-1] );
681                 /* Top right being not in the same cacheline as top left will happen
682                  * once every 4 MBs, so one extra prefetch is worthwhile */
683                 x264_prefetch( &h->mb.mv[l][top_4x4+4] );
684                 x264_prefetch( &h->mb.ref[l][top_8x8-1] );
685                 x264_prefetch( &h->mb.mvd[l][top] );
686             }
687     }
688     else
689     {
690         h->mb.cache.i_cbp_left = -1;
691
692         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
693         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
694         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
695         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
696
697         /* load non_zero_count */
698         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
699         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
700         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
701         h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
702         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
703         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
704         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
705         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
706     }
707
708     if( h->pps->b_transform_8x8_mode )
709     {
710         h->mb.cache.i_neighbour_transform_size =
711             ( (h->mb.i_neighbour & MB_LEFT) && h->mb.mb_transform_size[left] )
712           + ( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top]  );
713     }
714
715     if( h->sh.b_mbaff )
716     {
717         h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
718         h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
719         h->mb.cache.i_neighbour_interlaced =
720             !!(h->mb.i_neighbour & MB_LEFT)
721           + !!(h->mb.i_neighbour & MB_TOP);
722     }
723
724     if( !h->mb.b_interlaced )
725     {
726         copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
727         copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
728         copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
729         copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
730     }
731
732     /* load picture pointers */
733     x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 0 );
734     x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1 );
735     x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 2 );
736
737     if( h->fdec->integral )
738     {
739         int offset = 16 * (mb_x + mb_y * h->fdec->i_stride[0]);
740         for( int i = 0; i < h->mb.pic.i_fref[0]; i++ )
741             h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[offset];
742         for( int i = 0; i < h->mb.pic.i_fref[1]; i++ )
743             h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[offset];
744     }
745
746     x264_prefetch_fenc( h, h->fenc, mb_x, mb_y );
747
748     /* load ref/mv/mvd */
749     if( h->sh.i_type != SLICE_TYPE_I )
750     {
751         int s8x8 = h->mb.i_b8_stride;
752         int s4x4 = h->mb.i_b4_stride;
753
754         for( int l = 0; l < (h->sh.i_type == SLICE_TYPE_B) + 1; l++ )
755         {
756             int16_t (*mv)[2] = h->mb.mv[l];
757             int8_t *ref = h->mb.ref[l];
758
759             int i8 = x264_scan8[0] - 1 - 1*8;
760             if( h->mb.i_neighbour & MB_TOPLEFT )
761             {
762                 h->mb.cache.ref[l][i8] = ref[top_8x8 - 1];
763                 CP32( h->mb.cache.mv[l][i8], mv[top_4x4 - 1] );
764             }
765             else
766             {
767                 h->mb.cache.ref[l][i8] = -2;
768                 M32( h->mb.cache.mv[l][i8] ) = 0;
769             }
770
771             i8 = x264_scan8[0] - 8;
772             if( h->mb.i_neighbour & MB_TOP )
773             {
774                 h->mb.cache.ref[l][i8+0] =
775                 h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
776                 h->mb.cache.ref[l][i8+2] =
777                 h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
778                 CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
779             }
780             else
781             {
782                 M128( h->mb.cache.mv[l][i8] ) = M128_ZERO;
783                 M32( &h->mb.cache.ref[l][i8] ) = (uint8_t)(-2) * 0x01010101U;
784             }
785
786             i8 = x264_scan8[0] + 4 - 1*8;
787             if( h->mb.i_neighbour & MB_TOPRIGHT )
788             {
789                 h->mb.cache.ref[l][i8] = ref[top_8x8 + 2];
790                 CP32( h->mb.cache.mv[l][i8], mv[top_4x4 + 4] );
791             }
792             else
793                  h->mb.cache.ref[l][i8] = -2;
794
795             i8 = x264_scan8[0] - 1;
796             if( h->mb.i_neighbour & MB_LEFT )
797             {
798                 const int ir = h->mb.i_b8_xy - 1;
799                 const int iv = h->mb.i_b4_xy - 1;
800                 h->mb.cache.ref[l][i8+0*8] =
801                 h->mb.cache.ref[l][i8+1*8] = ref[ir + 0*s8x8];
802                 h->mb.cache.ref[l][i8+2*8] =
803                 h->mb.cache.ref[l][i8+3*8] = ref[ir + 1*s8x8];
804
805                 CP32( h->mb.cache.mv[l][i8+0*8], mv[iv + 0*s4x4] );
806                 CP32( h->mb.cache.mv[l][i8+1*8], mv[iv + 1*s4x4] );
807                 CP32( h->mb.cache.mv[l][i8+2*8], mv[iv + 2*s4x4] );
808                 CP32( h->mb.cache.mv[l][i8+3*8], mv[iv + 3*s4x4] );
809             }
810             else
811             {
812                 for( int i = 0; i < 4; i++ )
813                 {
814                     h->mb.cache.ref[l][i8+i*8] = -2;
815                     M32( h->mb.cache.mv[l][i8+i*8] ) = 0;
816                 }
817             }
818
819             if( h->param.b_cabac )
820             {
821                 uint8_t (*mvd)[8][2] = h->mb.mvd[l];
822                 if( h->mb.i_neighbour & MB_TOP )
823                     CP64( h->mb.cache.mvd[l][x264_scan8[0] - 8], mvd[top][0] );
824                 else
825                     M64( h->mb.cache.mvd[l][x264_scan8[0] - 8] ) = 0;
826
827                 if( h->mb.i_neighbour & MB_LEFT )
828                 {
829                     CP16( h->mb.cache.mvd[l][x264_scan8[0 ] - 1], mvd[left][4] );
830                     CP16( h->mb.cache.mvd[l][x264_scan8[2 ] - 1], mvd[left][5] );
831                     CP16( h->mb.cache.mvd[l][x264_scan8[8 ] - 1], mvd[left][6] );
832                     CP16( h->mb.cache.mvd[l][x264_scan8[10] - 1], mvd[left][3] );
833                 }
834                 else
835                     for( int i = 0; i < 4; i++ )
836                         M16( h->mb.cache.mvd[l][x264_scan8[0]-1+i*8] ) = 0;
837             }
838         }
839
840         /* load skip */
841         if( h->sh.i_type == SLICE_TYPE_B )
842         {
843             h->mb.bipred_weight = h->mb.bipred_weight_buf[h->mb.b_interlaced&(mb_y&1)];
844             h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[h->mb.b_interlaced&(mb_y&1)];
845             if( h->param.b_cabac )
846             {
847                 uint8_t skipbp;
848                 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
849                 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left] : 0;
850                 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
851                 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
852                 skipbp = (h->mb.i_neighbour & MB_TOP) ? h->mb.skipbp[top] : 0;
853                 h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
854                 h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
855             }
856         }
857
858         if( h->sh.i_type == SLICE_TYPE_P )
859             x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
860     }
861
862     h->mb.i_neighbour4[0] =
863     h->mb.i_neighbour8[0] = (h->mb.i_neighbour_intra & (MB_TOP|MB_LEFT|MB_TOPLEFT))
864                             | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOPRIGHT : 0);
865     h->mb.i_neighbour4[4] =
866     h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour_intra & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
867     h->mb.i_neighbour4[2] =
868     h->mb.i_neighbour4[8] =
869     h->mb.i_neighbour4[10] =
870     h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour_intra & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
871     h->mb.i_neighbour4[5] =
872     h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour_intra & MB_TOPRIGHT)
873                             | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
874 }
875
876 static void inline x264_macroblock_cache_load_neighbours_deblock( x264_t *h, int mb_x, int mb_y )
877 {
878     int top = (mb_y - (1 << h->mb.b_interlaced)) * h->mb.i_mb_stride + mb_x;
879     int deblock_on_slice_edges = h->sh.i_disable_deblocking_filter_idc != 2;
880
881     h->mb.i_neighbour = 0;
882     h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
883     h->mb.i_b8_xy = 2*(mb_y * h->mb.i_b8_stride + mb_x);
884     h->mb.i_b4_xy = 4*(mb_y * h->mb.i_b4_stride + mb_x);
885
886     if( mb_x > 0 )
887     {
888         h->mb.i_mb_left_xy = h->mb.i_mb_xy - 1;
889         if( deblock_on_slice_edges || h->mb.slice_table[h->mb.i_mb_left_xy] == h->mb.slice_table[h->mb.i_mb_xy] )
890             h->mb.i_neighbour |= MB_LEFT;
891     }
892
893     if( top >= 0 )
894     {
895         h->mb.i_mb_top_xy = top;
896         if( deblock_on_slice_edges || h->mb.slice_table[h->mb.i_mb_top_xy] == h->mb.slice_table[h->mb.i_mb_xy] )
897             h->mb.i_neighbour |= MB_TOP;
898     }
899 }
900
901 void x264_macroblock_cache_load_deblock( x264_t *h, int mb_x, int mb_y, int deblock_ref_table[2][34] )
902 {
903     x264_macroblock_cache_load_neighbours_deblock( h, mb_x, mb_y );
904
905     if( IS_INTRA( h->mb.type[h->mb.i_mb_xy] ) )
906         return;
907
908     int cur  = h->mb.i_mb_xy;
909     int left = h->mb.i_mb_left_xy;
910     int top  = h->mb.i_mb_top_xy;
911     int top_y = mb_y - (1 << h->mb.b_interlaced);
912     int top_8x8 = (2*top_y+1) * h->mb.i_b8_stride + 2*mb_x;
913     int top_4x4 = (4*top_y+3) * h->mb.i_b4_stride + 4*mb_x;
914     int s8x8 = h->mb.i_b8_stride;
915     int s4x4 = h->mb.i_b4_stride;
916
917     uint8_t (*nnz)[24] = h->mb.non_zero_count;
918
919     if( h->mb.i_neighbour & MB_TOP )
920         CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &nnz[top][12] );
921
922     if( h->mb.i_neighbour & MB_LEFT )
923     {
924         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = nnz[left][3];
925         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = nnz[left][7];
926         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = nnz[left][11];
927         h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[left][15];
928     }
929
930     CP32( &h->mb.cache.non_zero_count[x264_scan8[0]+0*8], &nnz[cur][0*4] );
931     CP32( &h->mb.cache.non_zero_count[x264_scan8[0]+1*8], &nnz[cur][1*4] );
932     CP32( &h->mb.cache.non_zero_count[x264_scan8[0]+2*8], &nnz[cur][2*4] );
933     CP32( &h->mb.cache.non_zero_count[x264_scan8[0]+3*8], &nnz[cur][3*4] );
934
935     for( int l = 0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
936     {
937         int16_t (*mv)[2] = h->mb.mv[l];
938         int8_t *ref = h->mb.ref[l];
939
940         int i8 = x264_scan8[0] - 8;
941         if( h->mb.i_neighbour & MB_TOP )
942         {
943             h->mb.cache.ref[l][i8+0] =
944             h->mb.cache.ref[l][i8+1] = deblock_ref_table(l,ref[top_8x8 + 0]);
945             h->mb.cache.ref[l][i8+2] =
946             h->mb.cache.ref[l][i8+3] = deblock_ref_table(l,ref[top_8x8 + 1]);
947             CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
948         }
949
950         i8 = x264_scan8[0] - 1;
951         if( h->mb.i_neighbour & MB_LEFT )
952         {
953             int ir = h->mb.i_b8_xy - 1;
954             int iv = h->mb.i_b4_xy - 1;
955             h->mb.cache.ref[l][i8+0*8] =
956             h->mb.cache.ref[l][i8+1*8] = deblock_ref_table(l,ref[ir + 0*s8x8]);
957             h->mb.cache.ref[l][i8+2*8] =
958             h->mb.cache.ref[l][i8+3*8] = deblock_ref_table(l,ref[ir + 1*s8x8]);
959
960             CP32( h->mb.cache.mv[l][i8+0*8], mv[iv + 0*s4x4] );
961             CP32( h->mb.cache.mv[l][i8+1*8], mv[iv + 1*s4x4] );
962             CP32( h->mb.cache.mv[l][i8+2*8], mv[iv + 2*s4x4] );
963             CP32( h->mb.cache.mv[l][i8+3*8], mv[iv + 3*s4x4] );
964         }
965
966         int ref0 = deblock_ref_table(l,ref[h->mb.i_b8_xy+0+0*s8x8]);
967         int ref1 = deblock_ref_table(l,ref[h->mb.i_b8_xy+1+0*s8x8]);
968         int ref2 = deblock_ref_table(l,ref[h->mb.i_b8_xy+0+1*s8x8]);
969         int ref3 = deblock_ref_table(l,ref[h->mb.i_b8_xy+1+1*s8x8]);
970         uint32_t reftop = pack16to32( (uint8_t)ref0, (uint8_t)ref1 ) * 0x0101;
971         uint32_t refbot = pack16to32( (uint8_t)ref2, (uint8_t)ref3 ) * 0x0101;
972
973         M32( &h->mb.cache.ref[l][x264_scan8[0]+8*0] ) = reftop;
974         M32( &h->mb.cache.ref[l][x264_scan8[0]+8*1] ) = reftop;
975         M32( &h->mb.cache.ref[l][x264_scan8[0]+8*2] ) = refbot;
976         M32( &h->mb.cache.ref[l][x264_scan8[0]+8*3] ) = refbot;
977         CP128( h->mb.cache.mv[l][x264_scan8[0]+8*0], mv[h->mb.i_b4_xy+0*s4x4] );
978         CP128( h->mb.cache.mv[l][x264_scan8[0]+8*1], mv[h->mb.i_b4_xy+1*s4x4] );
979         CP128( h->mb.cache.mv[l][x264_scan8[0]+8*2], mv[h->mb.i_b4_xy+2*s4x4] );
980         CP128( h->mb.cache.mv[l][x264_scan8[0]+8*3], mv[h->mb.i_b4_xy+3*s4x4] );
981     }
982 }
983
984 static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int i )
985 {
986     int w = i ? 8 : 16;
987     int i_stride = h->fdec->i_stride[!!i];
988     int i_stride2 = i_stride << h->mb.b_interlaced;
989     int i_pix_offset = h->mb.b_interlaced
990                      ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
991                      : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
992     h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( &h->fdec->plane[i][i_pix_offset], i_stride2,
993                                          h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
994 }
995
996 void x264_macroblock_cache_save( x264_t *h )
997 {
998     const int i_mb_xy = h->mb.i_mb_xy;
999     const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1000     const int s8x8 = h->mb.i_b8_stride;
1001     const int s4x4 = h->mb.i_b4_stride;
1002     const int i_mb_4x4 = h->mb.i_b4_xy;
1003     const int i_mb_8x8 = h->mb.i_b8_xy;
1004
1005     /* GCC pessimizes direct stores to heap-allocated arrays due to aliasing. */
1006     /* By only dereferencing them once, we avoid this issue. */
1007     int8_t *i4x4 = h->mb.intra4x4_pred_mode[i_mb_xy];
1008     uint8_t *nnz = h->mb.non_zero_count[i_mb_xy];
1009
1010     x264_macroblock_store_pic( h, 0 );
1011     x264_macroblock_store_pic( h, 1 );
1012     x264_macroblock_store_pic( h, 2 );
1013
1014     x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1015
1016     h->mb.type[i_mb_xy] = i_mb_type;
1017     h->mb.slice_table[i_mb_xy] = h->sh.i_first_mb;
1018     h->mb.partition[i_mb_xy] = IS_INTRA( i_mb_type ) ? D_16x16 : h->mb.i_partition;
1019     h->mb.i_mb_prev_xy = i_mb_xy;
1020
1021     /* save intra4x4 */
1022     if( i_mb_type == I_4x4 )
1023     {
1024         CP32( &i4x4[0], &h->mb.cache.intra4x4_pred_mode[x264_scan8[10]] );
1025         M32( &i4x4[4] ) = pack8to32( h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
1026                                      h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
1027                                      h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
1028     }
1029     else if( !h->param.b_constrained_intra || IS_INTRA(i_mb_type) )
1030         M64( i4x4 ) = I_PRED_4x4_DC * 0x0101010101010101ULL;
1031     else
1032         M64( i4x4 ) = (uint8_t)(-1) * 0x0101010101010101ULL;
1033
1034
1035     if( i_mb_type == I_PCM )
1036     {
1037         h->mb.qp[i_mb_xy] = 0;
1038         h->mb.i_last_dqp = 0;
1039         h->mb.i_cbp_chroma = 2;
1040         h->mb.i_cbp_luma = 0xf;
1041         h->mb.cbp[i_mb_xy] = 0x72f;   /* all set */
1042         h->mb.b_transform_8x8 = 0;
1043         memset( nnz, 16, sizeof( *h->mb.non_zero_count ) );
1044     }
1045     else
1046     {
1047         /* save non zero count */
1048         CP32( &nnz[0*4], &h->mb.cache.non_zero_count[x264_scan8[0]+0*8] );
1049         CP32( &nnz[1*4], &h->mb.cache.non_zero_count[x264_scan8[0]+1*8] );
1050         CP32( &nnz[2*4], &h->mb.cache.non_zero_count[x264_scan8[0]+2*8] );
1051         CP32( &nnz[3*4], &h->mb.cache.non_zero_count[x264_scan8[0]+3*8] );
1052         M16( &nnz[16+0*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+0*2]-1] ) >> 8;
1053         M16( &nnz[16+1*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+1*2]-1] ) >> 8;
1054         M16( &nnz[16+2*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+2*2]-1] ) >> 8;
1055         M16( &nnz[16+3*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+3*2]-1] ) >> 8;
1056
1057         if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1058             h->mb.i_qp = h->mb.i_last_qp;
1059         h->mb.qp[i_mb_xy] = h->mb.i_qp;
1060         h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1061         h->mb.i_last_qp = h->mb.i_qp;
1062     }
1063
1064     if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1065         h->mb.b_transform_8x8 = 0;
1066     h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1067
1068     if( h->sh.i_type != SLICE_TYPE_I )
1069     {
1070         int16_t (*mv0)[2] = &h->mb.mv[0][i_mb_4x4];
1071         int16_t (*mv1)[2] = &h->mb.mv[1][i_mb_4x4];
1072         int8_t *ref0 = &h->mb.ref[0][i_mb_8x8];
1073         int8_t *ref1 = &h->mb.ref[1][i_mb_8x8];
1074         if( !IS_INTRA( i_mb_type ) )
1075         {
1076             ref0[0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
1077             ref0[1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
1078             ref0[0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
1079             ref0[1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
1080             CP128( &mv0[0*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*0] );
1081             CP128( &mv0[1*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*1] );
1082             CP128( &mv0[2*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*2] );
1083             CP128( &mv0[3*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*3] );
1084             if( h->sh.i_type == SLICE_TYPE_B )
1085             {
1086                 ref1[0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
1087                 ref1[1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
1088                 ref1[0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
1089                 ref1[1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
1090                 CP128( &mv1[0*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*0] );
1091                 CP128( &mv1[1*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*1] );
1092                 CP128( &mv1[2*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*2] );
1093                 CP128( &mv1[3*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*3] );
1094             }
1095         }
1096         else
1097         {
1098             M16( &ref0[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1099             M16( &ref0[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1100             M128( &mv0[0*s4x4] ) = M128_ZERO;
1101             M128( &mv0[1*s4x4] ) = M128_ZERO;
1102             M128( &mv0[2*s4x4] ) = M128_ZERO;
1103             M128( &mv0[3*s4x4] ) = M128_ZERO;
1104             if( h->sh.i_type == SLICE_TYPE_B )
1105             {
1106                 M16( &ref1[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1107                 M16( &ref1[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1108                 M128( &mv1[0*s4x4] ) = M128_ZERO;
1109                 M128( &mv1[1*s4x4] ) = M128_ZERO;
1110                 M128( &mv1[2*s4x4] ) = M128_ZERO;
1111                 M128( &mv1[3*s4x4] ) = M128_ZERO;
1112             }
1113         }
1114     }
1115
1116     if( h->param.b_cabac )
1117     {
1118         uint8_t (*mvd0)[2] = h->mb.mvd[0][i_mb_xy];
1119         uint8_t (*mvd1)[2] = h->mb.mvd[1][i_mb_xy];
1120         if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
1121             h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1122         else
1123             h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1124
1125         if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1126         {
1127             CP64( mvd0[0], h->mb.cache.mvd[0][x264_scan8[10]] );
1128             CP16( mvd0[4], h->mb.cache.mvd[0][x264_scan8[5 ]] );
1129             CP16( mvd0[5], h->mb.cache.mvd[0][x264_scan8[7 ]] );
1130             CP16( mvd0[6], h->mb.cache.mvd[0][x264_scan8[13]] );
1131             if( h->sh.i_type == SLICE_TYPE_B )
1132             {
1133                 CP64( mvd1[0], h->mb.cache.mvd[1][x264_scan8[10]] );
1134                 CP16( mvd1[4], h->mb.cache.mvd[1][x264_scan8[5 ]] );
1135                 CP16( mvd1[5], h->mb.cache.mvd[1][x264_scan8[7 ]] );
1136                 CP16( mvd1[6], h->mb.cache.mvd[1][x264_scan8[13]] );
1137             }
1138         }
1139         else
1140         {
1141             M128( mvd0[0] ) = M128_ZERO;
1142             if( h->sh.i_type == SLICE_TYPE_B )
1143                 M128( mvd1[0] ) = M128_ZERO;
1144         }
1145
1146         if( h->sh.i_type == SLICE_TYPE_B )
1147         {
1148             if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1149                 h->mb.skipbp[i_mb_xy] = 0xf;
1150             else if( i_mb_type == B_8x8 )
1151             {
1152                 int skipbp = ( h->mb.i_sub_partition[0] == D_DIRECT_8x8 ) << 0;
1153                 skipbp    |= ( h->mb.i_sub_partition[1] == D_DIRECT_8x8 ) << 1;
1154                 skipbp    |= ( h->mb.i_sub_partition[2] == D_DIRECT_8x8 ) << 2;
1155                 skipbp    |= ( h->mb.i_sub_partition[3] == D_DIRECT_8x8 ) << 3;
1156                 h->mb.skipbp[i_mb_xy] = skipbp;
1157             }
1158             else
1159                 h->mb.skipbp[i_mb_xy] = 0;
1160         }
1161     }
1162 }
1163
1164
1165 void x264_macroblock_bipred_init( x264_t *h )
1166 {
1167     for( int field = 0; field <= h->sh.b_mbaff; field++ )
1168         for( int i_ref0 = 0; i_ref0 < (h->i_ref0<<h->sh.b_mbaff); i_ref0++ )
1169         {
1170             int poc0 = h->fref0[i_ref0>>h->sh.b_mbaff]->i_poc;
1171             if( h->sh.b_mbaff && field^(i_ref0&1) )
1172                 poc0 += h->sh.i_delta_poc_bottom;
1173             for( int i_ref1 = 0; i_ref1 < (h->i_ref1<<h->sh.b_mbaff); i_ref1++ )
1174             {
1175                 int dist_scale_factor;
1176                 int poc1 = h->fref1[i_ref1>>h->sh.b_mbaff]->i_poc;
1177                 if( h->sh.b_mbaff && field^(i_ref1&1) )
1178                     poc1 += h->sh.i_delta_poc_bottom;
1179                 int cur_poc = h->fdec->i_poc + field*h->sh.i_delta_poc_bottom;
1180                 int td = x264_clip3( poc1 - poc0, -128, 127 );
1181                 if( td == 0 /* || pic0 is a long-term ref */ )
1182                     dist_scale_factor = 256;
1183                 else
1184                 {
1185                     int tb = x264_clip3( cur_poc - poc0, -128, 127 );
1186                     int tx = (16384 + (abs(td) >> 1)) / td;
1187                     dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1188                 }
1189
1190                 h->mb.dist_scale_factor_buf[field][i_ref0][i_ref1] = dist_scale_factor;
1191
1192                 dist_scale_factor >>= 2;
1193                 if( h->param.analyse.b_weighted_bipred
1194                       && dist_scale_factor >= -64
1195                       && dist_scale_factor <= 128 )
1196                 {
1197                     h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 64 - dist_scale_factor;
1198                     // ssse3 implementation of biweight doesn't support the extrema.
1199                     // if we ever generate them, we'll have to drop that optimization.
1200                     assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
1201                 }
1202                 else
1203                     h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 32;
1204             }
1205         }
1206 }
1207