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