]> git.sesse.net Git - x264/blob - common/macroblock.c
MBAFF: Template cache_load and cache_load_neighbours
[x264] / common / macroblock.c
1 /*****************************************************************************
2  * macroblock.c: macroblock common functions
3  *****************************************************************************
4  * Copyright (C) 2003-2011 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  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include "common.h"
29 #include "encoder/me.h"
30
31 static NOINLINE void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
32 {
33     int i8    = x264_scan8[0]+x+8*y;
34     int i_ref = h->mb.cache.ref[0][i8];
35     int mvx   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
36     int mvy   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
37
38     h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
39                    h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
40                    mvx, mvy, 4*width, 4*height, &h->sh.weight[i_ref][0] );
41
42     // chroma is offset if MCing from a field of opposite parity
43     if( MB_INTERLACED & i_ref )
44         mvy += (h->mb.i_mb_y & 1)*4 - 2;
45
46     h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x],
47                      &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
48                      h->mb.pic.p_fref[0][i_ref][4], h->mb.pic.i_stride[1],
49                      mvx, mvy, 2*width, 2*height );
50
51     if( h->sh.weight[i_ref][1].weightfn )
52         h->sh.weight[i_ref][1].weightfn[width>>1]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
53                                                    &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
54                                                    &h->sh.weight[i_ref][1], height*2 );
55     if( h->sh.weight[i_ref][2].weightfn )
56         h->sh.weight[i_ref][2].weightfn[width>>1]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
57                                                    &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
58                                                    &h->sh.weight[i_ref][2],height*2 );
59
60 }
61 static NOINLINE void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
62 {
63     int i8    = x264_scan8[0]+x+8*y;
64     int i_ref = h->mb.cache.ref[1][i8];
65     int mvx   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
66     int mvy   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
67
68     h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
69                    h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
70                    mvx, mvy, 4*width, 4*height, weight_none );
71
72     if( MB_INTERLACED & i_ref )
73         mvy += (h->mb.i_mb_y & 1)*4 - 2;
74
75     h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x],
76                      &h->mb.pic.p_fdec[2][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
81 static NOINLINE void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
82 {
83     int i8 = x264_scan8[0]+x+8*y;
84     int i_ref0 = h->mb.cache.ref[0][i8];
85     int i_ref1 = h->mb.cache.ref[1][i8];
86     int weight = h->mb.bipred_weight[i_ref0][i_ref1];
87     int mvx0   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
88     int mvx1   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
89     int mvy0   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
90     int mvy1   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
91     int i_mode = x264_size2pixel[height][width];
92     int i_stride0 = 16, i_stride1 = 16;
93     ALIGNED_ARRAY_16( pixel, tmp0,[16*16] );
94     ALIGNED_ARRAY_16( pixel, tmp1,[16*16] );
95     pixel *src0, *src1;
96
97     src0 = h->mc.get_ref( tmp0, &i_stride0, h->mb.pic.p_fref[0][i_ref0], h->mb.pic.i_stride[0],
98                           mvx0, mvy0, 4*width, 4*height, weight_none );
99     src1 = h->mc.get_ref( tmp1, &i_stride1, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
100                           mvx1, mvy1, 4*width, 4*height, weight_none );
101     h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
102                        src0, i_stride0, src1, i_stride1, weight );
103
104     if( MB_INTERLACED & i_ref0 )
105         mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
106     if( MB_INTERLACED & i_ref1 )
107         mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
108
109     h->mc.mc_chroma( tmp0, tmp0+8, 16, h->mb.pic.p_fref[0][i_ref0][4], h->mb.pic.i_stride[1],
110                      mvx0, mvy0, 2*width, 2*height );
111     h->mc.mc_chroma( tmp1, tmp1+8, 16, h->mb.pic.p_fref[1][i_ref1][4], h->mb.pic.i_stride[1],
112                      mvx1, mvy1, 2*width, 2*height );
113     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 );
114     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 );
115 }
116
117 void x264_mb_mc_8x8( x264_t *h, int i8 )
118 {
119     int x = 2*(i8&1);
120     int y = 2*(i8>>1);
121
122     if( h->sh.i_type == SLICE_TYPE_P )
123     {
124         switch( h->mb.i_sub_partition[i8] )
125         {
126             case D_L0_8x8:
127                 x264_mb_mc_0xywh( h, x, y, 2, 2 );
128                 break;
129             case D_L0_8x4:
130                 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
131                 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
132                 break;
133             case D_L0_4x8:
134                 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
135                 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
136                 break;
137             case D_L0_4x4:
138                 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
139                 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
140                 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
141                 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
142                 break;
143         }
144     }
145     else
146     {
147         int scan8 = x264_scan8[0] + x + 8*y;
148
149         if( h->mb.cache.ref[0][scan8] >= 0 )
150             if( h->mb.cache.ref[1][scan8] >= 0 )
151                 x264_mb_mc_01xywh( h, x, y, 2, 2 );
152             else
153                 x264_mb_mc_0xywh( h, x, y, 2, 2 );
154         else
155             x264_mb_mc_1xywh( h, x, y, 2, 2 );
156     }
157 }
158
159 void x264_mb_mc( x264_t *h )
160 {
161     if( h->mb.i_partition == D_8x8 )
162     {
163         for( int i = 0; i < 4; i++ )
164             x264_mb_mc_8x8( h, i );
165     }
166     else
167     {
168         int ref0a = h->mb.cache.ref[0][x264_scan8[ 0]];
169         int ref0b = h->mb.cache.ref[0][x264_scan8[12]];
170         int ref1a = h->mb.cache.ref[1][x264_scan8[ 0]];
171         int ref1b = h->mb.cache.ref[1][x264_scan8[12]];
172
173         if( h->mb.i_partition == D_16x16 )
174         {
175             if( ref0a >= 0 )
176                 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
177                 else             x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
178             else                 x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
179         }
180         else if( h->mb.i_partition == D_16x8 )
181         {
182             if( ref0a >= 0 )
183                 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
184                 else             x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
185             else                 x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
186
187             if( ref0b >= 0 )
188                 if( ref1b >= 0 ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
189                 else             x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
190             else                 x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
191         }
192         else if( h->mb.i_partition == D_8x16 )
193         {
194             if( ref0a >= 0 )
195                 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
196                 else             x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
197             else                 x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
198
199             if( ref0b >= 0 )
200                 if( ref1b >= 0 ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
201                 else             x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
202             else                 x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
203         }
204     }
205 }
206
207 int x264_macroblock_cache_allocate( x264_t *h )
208 {
209     int i_mb_count = h->mb.i_mb_count;
210
211     h->mb.i_mb_stride = h->mb.i_mb_width;
212     h->mb.i_b8_stride = h->mb.i_mb_width * 2;
213     h->mb.i_b4_stride = h->mb.i_mb_width * 4;
214
215     h->mb.b_interlaced = PARAM_INTERLACED;
216
217     CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
218     CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
219     CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
220     CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
221     CHECKED_MALLOC( h->mb.slice_table, i_mb_count * sizeof(uint16_t) );
222     memset( h->mb.slice_table, -1, i_mb_count * sizeof(uint16_t) );
223
224     /* 0 -> 3 top(4), 4 -> 6 : left(3) */
225     CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
226
227     /* all coeffs */
228     CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
229
230     if( h->param.b_cabac )
231     {
232         CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
233         CHECKED_MALLOC( h->mb.mvd[0], i_mb_count * sizeof( **h->mb.mvd ) );
234         CHECKED_MALLOC( h->mb.mvd[1], i_mb_count * sizeof( **h->mb.mvd ) );
235     }
236
237     for( int i = 0; i < 2; i++ )
238     {
239         int i_refs = X264_MIN(X264_REF_MAX, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << PARAM_INTERLACED;
240         if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
241             i_refs = X264_MIN(X264_REF_MAX, i_refs + 1 + (BIT_DEPTH == 8)); //smart weights add two duplicate frames, one in >8-bit
242
243         for( int j = !i; j < i_refs; j++ )
244         {
245             CHECKED_MALLOC( h->mb.mvr[i][j], 2 * (i_mb_count + 1) * sizeof(int16_t) );
246             M32( h->mb.mvr[i][j][0] ) = 0;
247             h->mb.mvr[i][j]++;
248         }
249     }
250
251     if( h->param.analyse.i_weighted_pred )
252     {
253         int i_padv = PADV << PARAM_INTERLACED;
254         int 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                 luma_plane_size = h->fdec->i_stride_lowres * (h->mb.i_mb_height*8+2*i_padv);
264                 // Only need 1 buffer for analysis
265                 numweightbuf = 1;
266             }
267             else
268                 numweightbuf = 0;
269         }
270         else
271         {
272             luma_plane_size = h->fdec->i_stride[0] * (h->mb.i_mb_height*16+2*i_padv);
273
274             if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
275                 //smart can weight one ref and one offset -1 in 8-bit
276                 numweightbuf = 1 + (BIT_DEPTH == 8);
277             else
278                 //simple only has one weighted ref
279                 numweightbuf = 1;
280         }
281
282         for( int i = 0; i < numweightbuf; i++ )
283             CHECKED_MALLOC( h->mb.p_weight_buf[i], luma_plane_size * sizeof(pixel) );
284     }
285
286     return 0;
287 fail:
288     return -1;
289 }
290 void x264_macroblock_cache_free( x264_t *h )
291 {
292     for( int i = 0; i < 2; i++ )
293         for( int j = !i; j < X264_REF_MAX*2; j++ )
294             if( h->mb.mvr[i][j] )
295                 x264_free( h->mb.mvr[i][j]-1 );
296     for( int i = 0; i < X264_REF_MAX; i++ )
297         x264_free( h->mb.p_weight_buf[i] );
298
299     if( h->param.b_cabac )
300     {
301         x264_free( h->mb.chroma_pred_mode );
302         x264_free( h->mb.mvd[0] );
303         x264_free( h->mb.mvd[1] );
304     }
305     x264_free( h->mb.slice_table );
306     x264_free( h->mb.intra4x4_pred_mode );
307     x264_free( h->mb.non_zero_count );
308     x264_free( h->mb.mb_transform_size );
309     x264_free( h->mb.skipbp );
310     x264_free( h->mb.cbp );
311     x264_free( h->mb.qp );
312 }
313
314 int x264_macroblock_thread_allocate( x264_t *h, int b_lookahead )
315 {
316     if( !b_lookahead )
317     {
318         for( int i = 0; i <= 4*PARAM_INTERLACED; i++ )
319             for( int j = 0; j < 2; j++ )
320             {
321                 /* shouldn't really be initialized, just silences a valgrind false-positive in predict_8x8_filter_mmx */
322                 CHECKED_MALLOCZERO( h->intra_border_backup[i][j], (h->sps->i_mb_width*16+32) * sizeof(pixel) );
323                 h->intra_border_backup[i][j] += 16;
324                 if( !PARAM_INTERLACED )
325                     h->intra_border_backup[1][j] = h->intra_border_backup[i][j];
326             }
327         for( int i = 0; i <= PARAM_INTERLACED; i++ )
328         {
329             CHECKED_MALLOC( h->deblock_strength[i], sizeof(**h->deblock_strength) * h->mb.i_mb_width );
330             h->deblock_strength[1] = h->deblock_strength[i];
331         }
332     }
333
334     /* Allocate scratch buffer */
335     int scratch_size = 0;
336     if( !b_lookahead )
337     {
338         int buf_hpel = (h->thread[0]->fdec->i_width[0]+48) * sizeof(int16_t);
339         int buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int);
340         int me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range);
341         int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) *
342             ((me_range*2+24) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t));
343         scratch_size = X264_MAX3( buf_hpel, buf_ssim, buf_tesa );
344     }
345     int buf_mbtree = h->param.rc.b_mb_tree * ((h->mb.i_mb_width+3)&~3) * sizeof(int);
346     scratch_size = X264_MAX( scratch_size, buf_mbtree );
347     if( scratch_size )
348         CHECKED_MALLOC( h->scratch_buffer, scratch_size );
349     else
350         h->scratch_buffer = NULL;
351
352     return 0;
353 fail:
354     return -1;
355 }
356
357 void x264_macroblock_thread_free( x264_t *h, int b_lookahead )
358 {
359     if( !b_lookahead )
360     {
361         for( int i = 0; i <= PARAM_INTERLACED; i++ )
362             x264_free( h->deblock_strength[i] );
363         for( int i = 0; i <= 4*PARAM_INTERLACED; i++ )
364             for( int j = 0; j < 2; j++ )
365                 x264_free( h->intra_border_backup[i][j] - 16 );
366     }
367     x264_free( h->scratch_buffer );
368 }
369
370 void x264_macroblock_slice_init( x264_t *h )
371 {
372     h->mb.mv[0] = h->fdec->mv[0];
373     h->mb.mv[1] = h->fdec->mv[1];
374     h->mb.mvr[0][0] = h->fdec->mv16x16;
375     h->mb.ref[0] = h->fdec->ref[0];
376     h->mb.ref[1] = h->fdec->ref[1];
377     h->mb.type = h->fdec->mb_type;
378     h->mb.partition = h->fdec->mb_partition;
379     h->mb.field = h->fdec->field;
380
381     h->fdec->i_ref[0] = h->i_ref[0];
382     h->fdec->i_ref[1] = h->i_ref[1];
383     for( int i = 0; i < h->i_ref[0]; i++ )
384         h->fdec->ref_poc[0][i] = h->fref[0][i]->i_poc;
385     if( h->sh.i_type == SLICE_TYPE_B )
386     {
387         for( int i = 0; i < h->i_ref[1]; i++ )
388             h->fdec->ref_poc[1][i] = h->fref[1][i]->i_poc;
389
390         map_col_to_list0(-1) = -1;
391         map_col_to_list0(-2) = -2;
392         for( int i = 0; i < h->fref[1][0]->i_ref[0]; i++ )
393         {
394             int poc = h->fref[1][0]->ref_poc[0][i];
395             map_col_to_list0(i) = -2;
396             for( int j = 0; j < h->i_ref[0]; j++ )
397                 if( h->fref[0][j]->i_poc == poc )
398                 {
399                     map_col_to_list0(i) = j;
400                     break;
401                 }
402         }
403     }
404     else if( h->sh.i_type == SLICE_TYPE_P )
405     {
406         memset( h->mb.cache.skip, 0, sizeof( h->mb.cache.skip ) );
407
408         if( h->sh.i_disable_deblocking_filter_idc != 1 && h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
409         {
410             deblock_ref_table(-2) = -2;
411             deblock_ref_table(-1) = -1;
412             for( int i = 0; i < h->i_ref[0] << SLICE_MBAFF; i++ )
413             {
414                 /* Mask off high bits to avoid frame num collisions with -1/-2.
415                  * In current x264 frame num values don't cover a range of more
416                  * than 32, so 6 bits is enough for uniqueness. */
417                 if( !MB_INTERLACED )
418                     deblock_ref_table(i) = h->fref[0][i]->i_frame_num&63;
419                 else
420                     deblock_ref_table(i) = ((h->fref[0][i>>1]->i_frame_num&63)<<1) + (i&1);
421             }
422         }
423     }
424
425     /* init with not available (for top right idx=7,15) */
426     memset( h->mb.cache.ref, -2, sizeof( h->mb.cache.ref ) );
427
428     if( h->i_ref[0] > 0 )
429         for( int field = 0; field <= SLICE_MBAFF; field++ )
430         {
431             int curpoc = h->fdec->i_poc + h->fdec->i_delta_poc[field];
432             int refpoc = h->fref[0][0]->i_poc + h->fref[0][0]->i_delta_poc[field];
433             int delta = curpoc - refpoc;
434
435             h->fdec->inv_ref_poc[field] = (256 + delta/2) / delta;
436         }
437
438     h->mb.i_neighbour4[6] =
439     h->mb.i_neighbour4[9] =
440     h->mb.i_neighbour4[12] =
441     h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
442     h->mb.i_neighbour4[3] =
443     h->mb.i_neighbour4[7] =
444     h->mb.i_neighbour4[11] =
445     h->mb.i_neighbour4[13] =
446     h->mb.i_neighbour4[15] =
447     h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
448 }
449
450 void x264_macroblock_thread_init( x264_t *h )
451 {
452     h->mb.i_me_method = h->param.analyse.i_me_method;
453     h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
454     if( h->sh.i_type == SLICE_TYPE_B && (h->mb.i_subpel_refine == 6 || h->mb.i_subpel_refine == 8) )
455         h->mb.i_subpel_refine--;
456     h->mb.b_chroma_me = h->param.analyse.b_chroma_me &&
457                         ((h->sh.i_type == SLICE_TYPE_P && h->mb.i_subpel_refine >= 5) ||
458                          (h->sh.i_type == SLICE_TYPE_B && h->mb.i_subpel_refine >= 9));
459     h->mb.b_dct_decimate = h->sh.i_type == SLICE_TYPE_B ||
460                           (h->param.analyse.b_dct_decimate && h->sh.i_type != SLICE_TYPE_I);
461     h->mb.i_mb_prev_xy = -1;
462
463     /* fdec:      fenc:
464      * yyyyyyy
465      * yYYYY      YYYY
466      * yYYYY      YYYY
467      * yYYYY      YYYY
468      * yYYYY      YYYY
469      * uuu vvv    UUVV
470      * uUU vVV    UUVV
471      * uUU vVV
472      */
473     h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
474     h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
475     h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
476     h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
477     h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
478     h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
479 }
480
481 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
482 {
483     int stride_y  = fenc->i_stride[0];
484     int stride_uv = fenc->i_stride[1];
485     int off_y  = 16 * i_mb_x + 16 * i_mb_y * stride_y;
486     int off_uv = 16 * i_mb_x + 8 * i_mb_y * stride_uv;
487     h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
488                          fenc->plane[1]+off_uv, stride_uv, i_mb_x );
489 }
490
491 NOINLINE void x264_copy_column8( pixel *dst, pixel *src )
492 {
493     // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
494     for( int i = -4; i < 4; i++ )
495         dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
496 }
497
498 static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int mb_x, int mb_y, int i, int b_mbaff )
499 {
500     int w = (i ? 8 : 16);
501     int i_stride = h->fdec->i_stride[i];
502     int i_stride2 = i_stride << MB_INTERLACED;
503     int i_pix_offset = MB_INTERLACED
504                      ? 16 * mb_x + w * (mb_y&~1) * i_stride + (mb_y&1) * i_stride
505                      : 16 * mb_x + w * mb_y * i_stride;
506     pixel *plane_fdec = &h->fdec->plane[i][i_pix_offset];
507     int fdec_idx = b_mbaff ? (MB_INTERLACED ? (3 + (mb_y&1)) : (mb_y&1) ? 2 : 4) : 0;
508     pixel *intra_fdec = &h->intra_border_backup[fdec_idx][i][mb_x*16];
509     int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
510     /* ref_pix_offset[0] references the current field and [1] the opposite field. */
511     if( MB_INTERLACED )
512         ref_pix_offset[1] += (1-2*(mb_y&1)) * i_stride;
513     h->mb.pic.i_stride[i] = i_stride2;
514     h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
515     if( i )
516     {
517         h->mc.load_deinterleave_8x8x2_fenc( h->mb.pic.p_fenc[1], h->mb.pic.p_fenc_plane[1], i_stride2 );
518         memcpy( h->mb.pic.p_fdec[1]-FDEC_STRIDE, intra_fdec, 8*sizeof(pixel) );
519         memcpy( h->mb.pic.p_fdec[2]-FDEC_STRIDE, intra_fdec+8, 8*sizeof(pixel) );
520         if( b_mbaff )
521         {
522             h->mb.pic.p_fdec[1][-FDEC_STRIDE-1] = intra_fdec[-1-8];
523             h->mb.pic.p_fdec[2][-FDEC_STRIDE-1] = intra_fdec[-1];
524         }
525     }
526     else
527     {
528         h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, h->mb.pic.p_fenc_plane[0], i_stride2, 16 );
529         memcpy( h->mb.pic.p_fdec[0]-FDEC_STRIDE, intra_fdec, 24*sizeof(pixel) );
530         if( b_mbaff )
531             h->mb.pic.p_fdec[0][-FDEC_STRIDE-1] = intra_fdec[-1];
532     }
533     if( b_mbaff )
534     {
535         for( int j = 0; j < w; j++ )
536             if( i )
537             {
538                 h->mb.pic.p_fdec[1][-1+j*FDEC_STRIDE] = plane_fdec[-2+j*i_stride2];
539                 h->mb.pic.p_fdec[2][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
540             }
541             else
542                 h->mb.pic.p_fdec[0][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
543     }
544     pixel *plane_src, **filtered_src;
545     for( int j = 0; j < h->mb.pic.i_fref[0]; j++ )
546     {
547         // Interpolate between pixels in same field.
548         if( MB_INTERLACED )
549         {
550             plane_src = h->fref[0][j>>1]->plane_fld[i];
551             filtered_src = h->fref[0][j>>1]->filtered_fld;
552         }
553         else
554         {
555             plane_src = h->fref[0][j]->plane[i];
556             filtered_src = h->fref[0][j]->filtered;
557         }
558         h->mb.pic.p_fref[0][j][i?4:0] = plane_src + ref_pix_offset[j&1];
559
560         if( !i )
561         {
562             for( int k = 1; k < 4; k++ )
563                 h->mb.pic.p_fref[0][j][k] = filtered_src[k] + ref_pix_offset[j&1];
564             if( h->sh.weight[j][0].weightfn )
565                 h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> MB_INTERLACED][ref_pix_offset[j&1]];
566             else
567                 h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
568         }
569     }
570     if( h->sh.i_type == SLICE_TYPE_B )
571         for( int j = 0; j < h->mb.pic.i_fref[1]; j++ )
572         {
573             if( MB_INTERLACED )
574             {
575                 plane_src = h->fref[1][j>>1]->plane_fld[i];
576                 filtered_src = h->fref[1][j>>1]->filtered_fld;
577             }
578             else
579             {
580                 plane_src = h->fref[1][j]->plane[i];
581                 filtered_src = h->fref[1][j]->filtered;
582             }
583             h->mb.pic.p_fref[1][j][i?4:0] = plane_src + ref_pix_offset[j&1];
584
585             if( !i )
586                 for( int k = 1; k < 4; k++ )
587                     h->mb.pic.p_fref[1][j][k] = filtered_src[k] + ref_pix_offset[j&1];
588         }
589 }
590
591 x264_left_table_t left_indices[4] =
592 {
593     /* Current is progressive */
594     {{ 4, 4, 5, 5}, { 3,  3,  7,  7}, {16+1, 16+1, 16+4+1, 16+4+1}, {0, 0, 1, 1}, {0, 0, 0, 0}},
595     {{ 6, 6, 3, 3}, {11, 11, 15, 15}, {16+3, 16+3, 16+4+3, 16+4+3}, {2, 2, 3, 3}, {1, 1, 1, 1}},
596     /* Current is interlaced */
597     {{ 4, 6, 4, 6}, { 3, 11,  3, 11}, {16+1, 16+1, 16+4+1, 16+4+1}, {0, 2, 0, 2}, {0, 1, 0, 1}},
598     /* Both same */
599     {{ 4, 5, 6, 3}, { 3,  7, 11, 15}, {16+1, 16+3, 16+4+1, 16+4+3}, {0, 1, 2, 3}, {0, 0, 1, 1}}
600 };
601
602 static void ALWAYS_INLINE x264_macroblock_cache_load_neighbours( x264_t *h, int mb_x, int mb_y, int b_interlaced )
603 {
604     const int mb_interlaced = b_interlaced && MB_INTERLACED;
605     int top_y = mb_y - (1 << mb_interlaced);
606     int top = top_y * h->mb.i_mb_stride + mb_x;
607
608     h->mb.i_mb_x = mb_x;
609     h->mb.i_mb_y = mb_y;
610     h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
611     h->mb.i_b8_xy = 2*(mb_y * h->mb.i_b8_stride + mb_x);
612     h->mb.i_b4_xy = 4*(mb_y * h->mb.i_b4_stride + mb_x);
613     h->mb.left_b8[0] =
614     h->mb.left_b8[1] = -1;
615     h->mb.left_b4[0] =
616     h->mb.left_b4[1] = -1;
617     h->mb.i_neighbour = 0;
618     h->mb.i_neighbour_intra = 0;
619     h->mb.i_neighbour_frame = 0;
620     h->mb.i_mb_top_xy = -1;
621     h->mb.i_mb_top_y = -1;
622     h->mb.i_mb_left_xy[0] = h->mb.i_mb_left_xy[1] = -1;
623     h->mb.i_mb_topleft_xy = -1;
624     h->mb.i_mb_topright_xy = -1;
625     h->mb.i_mb_type_top = -1;
626     h->mb.i_mb_type_left[0] = h->mb.i_mb_type_left[1] = -1;
627     h->mb.i_mb_type_topleft = -1;
628     h->mb.i_mb_type_topright = -1;
629     h->mb.left_index_table = &left_indices[3];
630     h->mb.topleft_partition = 0;
631
632     int topleft_y = top_y;
633     int topright_y = top_y;
634     int left[2];
635
636     left[0] = left[1] = h->mb.i_mb_xy - 1;
637     h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2;
638     h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4;
639
640     if( b_interlaced )
641     {
642         h->mb.i_mb_top_mbpair_xy = h->mb.i_mb_xy - 2*h->mb.i_mb_stride;
643         h->mb.i_mb_topleft_y = -1;
644         h->mb.i_mb_topright_y = -1;
645
646         if( mb_y&1 )
647         {
648             if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
649             {
650                 left[0] = left[1] = h->mb.i_mb_xy - 1 - h->mb.i_mb_stride;
651                 h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2 - 2*h->mb.i_b8_stride;
652                 h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4 - 4*h->mb.i_b4_stride;
653
654                 if( mb_interlaced )
655                 {
656                     h->mb.left_index_table = &left_indices[2];
657                     left[1] += h->mb.i_mb_stride;
658                     h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
659                     h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
660                 }
661                 else
662                 {
663                     h->mb.left_index_table = &left_indices[1];
664                     topleft_y++;
665                     h->mb.topleft_partition = 1;
666                 }
667             }
668             if( !mb_interlaced )
669                 topright_y = -1;
670         }
671         else
672         {
673             if( mb_interlaced && top >= 0 )
674             {
675                 if( !h->mb.field[top] )
676                 {
677                     top += h->mb.i_mb_stride;
678                     top_y++;
679                 }
680                 if( mb_x )
681                     topleft_y += !h->mb.field[h->mb.i_mb_stride*topleft_y + mb_x - 1];
682                 if( mb_x < h->mb.i_mb_width-1 )
683                     topright_y += !h->mb.field[h->mb.i_mb_stride*topright_y + mb_x + 1];
684             }
685             if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
686             {
687                 if( mb_interlaced )
688                 {
689                     h->mb.left_index_table = &left_indices[2];
690                     left[1] += h->mb.i_mb_stride;
691                     h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
692                     h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
693                 }
694                 else
695                     h->mb.left_index_table = &left_indices[0];
696             }
697         }
698     }
699
700     if( mb_x > 0 )
701     {
702         h->mb.i_neighbour_frame |= MB_LEFT;
703         h->mb.i_mb_left_xy[0] = left[0];
704         h->mb.i_mb_left_xy[1] = left[1];
705         h->mb.i_mb_type_left[0] = h->mb.type[h->mb.i_mb_left_xy[0]];
706         h->mb.i_mb_type_left[1] = h->mb.type[h->mb.i_mb_left_xy[1]];
707         if( h->mb.slice_table[left[0]] == h->sh.i_first_mb )
708         {
709             h->mb.i_neighbour |= MB_LEFT;
710
711             // FIXME: We don't currently support constrained intra + mbaff.
712             if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_left[0] ) )
713                 h->mb.i_neighbour_intra |= MB_LEFT;
714         }
715     }
716
717     /* We can't predict from the previous threadslice since it hasn't been encoded yet. */
718     if( (h->i_threadslice_start >> mb_interlaced) != (mb_y >> mb_interlaced) )
719     {
720         if( top >= 0 )
721         {
722             h->mb.i_neighbour_frame |= MB_TOP;
723             h->mb.i_mb_top_xy = top;
724             h->mb.i_mb_top_y = top_y;
725             h->mb.i_mb_type_top = h->mb.type[h->mb.i_mb_top_xy];
726             if( h->mb.slice_table[top] == h->sh.i_first_mb )
727             {
728                 h->mb.i_neighbour |= MB_TOP;
729
730                 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_top ) )
731                     h->mb.i_neighbour_intra |= MB_TOP;
732
733                 /* We only need to prefetch the top blocks because the left was just written
734                  * to as part of the previous cache_save.  Since most target CPUs use write-allocate
735                  * caches, left blocks are near-guaranteed to be in L1 cache.  Top--not so much. */
736                 x264_prefetch( &h->mb.cbp[top] );
737                 x264_prefetch( h->mb.intra4x4_pred_mode[top] );
738                 x264_prefetch( &h->mb.non_zero_count[top][12] );
739                 /* These aren't always allocated, but prefetching an invalid address can't hurt. */
740                 x264_prefetch( &h->mb.mb_transform_size[top] );
741                 x264_prefetch( &h->mb.skipbp[top] );
742             }
743         }
744
745         if( mb_x > 0 && topleft_y >= 0  )
746         {
747             h->mb.i_neighbour_frame |= MB_TOPLEFT;
748             h->mb.i_mb_topleft_xy = h->mb.i_mb_stride*topleft_y + mb_x - 1;
749             h->mb.i_mb_topleft_y = topleft_y;
750             h->mb.i_mb_type_topleft = h->mb.type[h->mb.i_mb_topleft_xy];
751             if( h->mb.slice_table[h->mb.i_mb_topleft_xy] == h->sh.i_first_mb )
752             {
753                 h->mb.i_neighbour |= MB_TOPLEFT;
754
755                 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
756                     h->mb.i_neighbour_intra |= MB_TOPLEFT;
757             }
758         }
759
760         if( mb_x < h->mb.i_mb_width - 1 && topright_y >= 0 )
761         {
762             h->mb.i_neighbour_frame |= MB_TOPRIGHT;
763             h->mb.i_mb_topright_xy = h->mb.i_mb_stride*topright_y + mb_x + 1;
764             h->mb.i_mb_topright_y = topright_y;
765             h->mb.i_mb_type_topright = h->mb.type[h->mb.i_mb_topright_xy];
766             if( h->mb.slice_table[h->mb.i_mb_topright_xy] == h->sh.i_first_mb )
767             {
768                 h->mb.i_neighbour |= MB_TOPRIGHT;
769
770                 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
771                     h->mb.i_neighbour_intra |= MB_TOPRIGHT;
772             }
773         }
774     }
775 }
776
777 #define LTOP 0
778 #if HAVE_INTERLACED
779 #   define LBOT 1
780 #else
781 #   define LBOT 0
782 #endif
783
784 void ALWAYS_INLINE x264_macroblock_cache_load( x264_t *h, int mb_x, int mb_y, int b_mbaff )
785 {
786     x264_macroblock_cache_load_neighbours( h, mb_x, mb_y, b_mbaff );
787
788     int *left = h->mb.i_mb_left_xy;
789     int top  = h->mb.i_mb_top_xy;
790     int top_y = h->mb.i_mb_top_y;
791     int s8x8 = h->mb.i_b8_stride;
792     int s4x4 = h->mb.i_b4_stride;
793     int top_8x8 = (2*top_y+1) * s8x8 + 2*mb_x;
794     int top_4x4 = (4*top_y+3) * s4x4 + 4*mb_x;
795     int lists = (1 << h->sh.i_type) & 3;
796
797     /* GCC pessimizes direct loads from heap-allocated arrays due to aliasing. */
798     /* By only dereferencing them once, we avoid this issue. */
799     int8_t (*i4x4)[8] = h->mb.intra4x4_pred_mode;
800     uint8_t (*nnz)[24] = h->mb.non_zero_count;
801     int16_t *cbp = h->mb.cbp;
802
803     x264_left_table_t *left_index_table = h->mb.left_index_table;
804
805     /* load cache */
806     if( h->mb.i_neighbour & MB_TOP )
807     {
808         h->mb.cache.i_cbp_top = cbp[top];
809         /* load intra4x4 */
810         CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &i4x4[top][0] );
811
812         /* load non_zero_count */
813         CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &nnz[top][12] );
814         /* shift because x264_scan8[16] is misaligned */
815         M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = M16( &nnz[top][18] ) << 8;
816         M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = M16( &nnz[top][22] ) << 8;
817
818         /* Finish the prefetching */
819         for( int l = 0; l < lists; l++ )
820         {
821             x264_prefetch( &h->mb.mv[l][top_4x4-1] );
822             /* Top right being not in the same cacheline as top left will happen
823              * once every 4 MBs, so one extra prefetch is worthwhile */
824             x264_prefetch( &h->mb.mv[l][top_4x4+4] );
825             x264_prefetch( &h->mb.ref[l][top_8x8-1] );
826             x264_prefetch( &h->mb.mvd[l][top] );
827         }
828     }
829     else
830     {
831         h->mb.cache.i_cbp_top = -1;
832
833         /* load intra4x4 */
834         M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
835
836         /* load non_zero_count */
837         M32( &h->mb.cache.non_zero_count[x264_scan8[   0] - 8] ) = 0x80808080U;
838         M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = 0x80808080U;
839         M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = 0x80808080U;
840     }
841
842     if( h->mb.i_neighbour & MB_LEFT )
843     {
844         if( b_mbaff )
845         {
846             const int16_t top_luma = (cbp[left[LTOP]] >> (left_index_table->mv[0]&(~1))) & 2;
847             const int16_t bot_luma = (cbp[left[LBOT]] >> (left_index_table->mv[2]&(~1))) & 2;
848             h->mb.cache.i_cbp_left = (cbp[left[LTOP]] & 0xfff0) | (bot_luma<<2) | top_luma;
849         }
850         else
851             h->mb.cache.i_cbp_left = cbp[left[0]];
852         if( b_mbaff )
853         {
854             /* load intra4x4 */
855             h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = i4x4[left[LTOP]][left_index_table->intra[0]];
856             h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = i4x4[left[LTOP]][left_index_table->intra[1]];
857             h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = i4x4[left[LBOT]][left_index_table->intra[2]];
858             h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = i4x4[left[LBOT]][left_index_table->intra[3]];
859
860             /* load non_zero_count */
861             h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = nnz[left[LTOP]][left_index_table->nnz[0]];
862             h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = nnz[left[LTOP]][left_index_table->nnz[1]];
863             h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = nnz[left[LBOT]][left_index_table->nnz[2]];
864             h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[left[LBOT]][left_index_table->nnz[3]];
865
866             h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = nnz[left[LTOP]][left_index_table->nnz_chroma[0]];
867             h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = nnz[left[LBOT]][left_index_table->nnz_chroma[1]];
868
869             h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = nnz[left[LTOP]][left_index_table->nnz_chroma[2]];
870             h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = nnz[left[LBOT]][left_index_table->nnz_chroma[3]];
871         }
872         else
873         {
874             int l = left[0];
875             h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = i4x4[l][left_index_table->intra[0]];
876             h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = i4x4[l][left_index_table->intra[1]];
877             h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = i4x4[l][left_index_table->intra[2]];
878             h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = i4x4[l][left_index_table->intra[3]];
879
880             h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = nnz[l][left_index_table->nnz[0]];
881             h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = nnz[l][left_index_table->nnz[1]];
882             h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = nnz[l][left_index_table->nnz[2]];
883             h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[l][left_index_table->nnz[3]];
884
885             h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = nnz[l][left_index_table->nnz_chroma[0]];
886             h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = nnz[l][left_index_table->nnz_chroma[1]];
887
888             h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = nnz[l][left_index_table->nnz_chroma[2]];
889             h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = nnz[l][left_index_table->nnz_chroma[3]];
890         }
891     }
892     else
893     {
894         h->mb.cache.i_cbp_left = -1;
895
896         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
897         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
898         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
899         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
900
901         /* load non_zero_count */
902         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
903         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
904         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
905         h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
906         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
907         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
908         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
909         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
910     }
911
912     if( h->pps->b_transform_8x8_mode )
913     {
914         h->mb.cache.i_neighbour_transform_size =
915             ( (h->mb.i_neighbour & MB_LEFT) && h->mb.mb_transform_size[left[0]] )
916           + ( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top]  );
917     }
918
919     if( b_mbaff )
920     {
921         h->mb.pic.i_fref[0] = h->i_ref[0] << MB_INTERLACED;
922         h->mb.pic.i_fref[1] = h->i_ref[1] << MB_INTERLACED;
923     }
924
925     if( !b_mbaff )
926     {
927         x264_copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
928         x264_copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
929         x264_copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
930         x264_copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
931         x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 0, 0 );
932         x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 0 );
933     }
934     else
935     {
936         x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 0, 1 );
937         x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 1 );
938     }
939
940     if( h->fdec->integral )
941     {
942         int offset = 16 * (mb_x + mb_y * h->fdec->i_stride[0]);
943         for( int list = 0; list < 2; list++ )
944             for( int i = 0; i < h->mb.pic.i_fref[list]; i++ )
945                 h->mb.pic.p_integral[list][i] = &h->fref[list][i]->integral[offset];
946     }
947
948     x264_prefetch_fenc( h, h->fenc, mb_x, mb_y );
949
950     /* load ref/mv/mvd */
951     for( int l = 0; l < lists; l++ )
952     {
953         int16_t (*mv)[2] = h->mb.mv[l];
954         int8_t *ref = h->mb.ref[l];
955
956         int i8 = x264_scan8[0] - 1 - 1*8;
957         if( h->mb.i_neighbour & MB_TOPLEFT )
958         {
959             int ir = b_mbaff ? 2*(s8x8*h->mb.i_mb_topleft_y + mb_x-1)+1+s8x8 : top_8x8 - 1;
960             int iv = b_mbaff ? 4*(s4x4*h->mb.i_mb_topleft_y + mb_x-1)+3+3*s4x4 : top_4x4 - 1;
961             if( b_mbaff && h->mb.topleft_partition )
962             {
963                 /* Take motion vector from the middle of macroblock instead of
964                  * the bottom right as usual. */
965                 iv -= 2*s4x4;
966                 ir -= s8x8;
967             }
968             h->mb.cache.ref[l][i8] = ref[ir];
969             CP32( h->mb.cache.mv[l][i8], mv[iv] );
970         }
971         else
972         {
973             h->mb.cache.ref[l][i8] = -2;
974             M32( h->mb.cache.mv[l][i8] ) = 0;
975         }
976
977         i8 = x264_scan8[0] - 8;
978         if( h->mb.i_neighbour & MB_TOP )
979         {
980             h->mb.cache.ref[l][i8+0] =
981             h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
982             h->mb.cache.ref[l][i8+2] =
983             h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
984             CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
985         }
986         else
987         {
988             M128( h->mb.cache.mv[l][i8] ) = M128_ZERO;
989             M32( &h->mb.cache.ref[l][i8] ) = (uint8_t)(-2) * 0x01010101U;
990         }
991
992         i8 = x264_scan8[0] + 4 - 1*8;
993         if( h->mb.i_neighbour & MB_TOPRIGHT )
994         {
995             int ir = b_mbaff ? 2*(s8x8*h->mb.i_mb_topright_y + (mb_x+1))+s8x8 : top_8x8 + 2;
996             int iv = b_mbaff ? 4*(s4x4*h->mb.i_mb_topright_y + (mb_x+1))+3*s4x4 : top_4x4 + 4;
997             h->mb.cache.ref[l][i8] = ref[ir];
998             CP32( h->mb.cache.mv[l][i8], mv[iv] );
999         }
1000         else
1001              h->mb.cache.ref[l][i8] = -2;
1002
1003         i8 = x264_scan8[0] - 1;
1004         if( h->mb.i_neighbour & MB_LEFT )
1005         {
1006             if( b_mbaff )
1007             {
1008                 h->mb.cache.ref[l][i8+0*8] = ref[h->mb.left_b8[LTOP] + 1 + s8x8*left_index_table->ref[0]];
1009                 h->mb.cache.ref[l][i8+1*8] = ref[h->mb.left_b8[LTOP] + 1 + s8x8*left_index_table->ref[1]];
1010                 h->mb.cache.ref[l][i8+2*8] = ref[h->mb.left_b8[LBOT] + 1 + s8x8*left_index_table->ref[2]];
1011                 h->mb.cache.ref[l][i8+3*8] = ref[h->mb.left_b8[LBOT] + 1 + s8x8*left_index_table->ref[3]];
1012
1013                 CP32( h->mb.cache.mv[l][i8+0*8], mv[h->mb.left_b4[LTOP] + 3 + s4x4*left_index_table->mv[0]] );
1014                 CP32( h->mb.cache.mv[l][i8+1*8], mv[h->mb.left_b4[LTOP] + 3 + s4x4*left_index_table->mv[1]] );
1015                 CP32( h->mb.cache.mv[l][i8+2*8], mv[h->mb.left_b4[LBOT] + 3 + s4x4*left_index_table->mv[2]] );
1016                 CP32( h->mb.cache.mv[l][i8+3*8], mv[h->mb.left_b4[LBOT] + 3 + s4x4*left_index_table->mv[3]] );
1017             }
1018             else
1019             {
1020                 const int ir = h->mb.i_b8_xy - 1;
1021                 const int iv = h->mb.i_b4_xy - 1;
1022                 h->mb.cache.ref[l][i8+0*8] =
1023                 h->mb.cache.ref[l][i8+1*8] = ref[ir + 0*s8x8];
1024                 h->mb.cache.ref[l][i8+2*8] =
1025                 h->mb.cache.ref[l][i8+3*8] = ref[ir + 1*s8x8];
1026
1027                 CP32( h->mb.cache.mv[l][i8+0*8], mv[iv + 0*s4x4] );
1028                 CP32( h->mb.cache.mv[l][i8+1*8], mv[iv + 1*s4x4] );
1029                 CP32( h->mb.cache.mv[l][i8+2*8], mv[iv + 2*s4x4] );
1030                 CP32( h->mb.cache.mv[l][i8+3*8], mv[iv + 3*s4x4] );
1031             }
1032         }
1033         else
1034         {
1035             for( int i = 0; i < 4; i++ )
1036             {
1037                 h->mb.cache.ref[l][i8+i*8] = -2;
1038                 M32( h->mb.cache.mv[l][i8+i*8] ) = 0;
1039             }
1040         }
1041
1042         /* Extra logic for top right mv in mbaff.
1043          * . . . d  . . a .
1044          * . . . e  . . . .
1045          * . . . f  b . c .
1046          * . . . .  . . . .
1047          *
1048          * If the top right of the 4x4 partitions labeled a, b and c in the
1049          * above diagram do not exist, but the entries d, e and f exist (in
1050          * the macroblock to the left) then use those instead.
1051          */
1052         if( b_mbaff && (h->mb.i_neighbour & MB_LEFT) )
1053         {
1054             if( MB_INTERLACED && !h->mb.field[h->mb.i_mb_xy-1] )
1055             {
1056                 h->mb.cache.topright_ref[l][0] = ref[h->mb.left_b8[0] + 1 + s8x8*0];
1057                 h->mb.cache.topright_ref[l][1] = ref[h->mb.left_b8[0] + 1 + s8x8*1];
1058                 h->mb.cache.topright_ref[l][2] = ref[h->mb.left_b8[1] + 1 + s8x8*0];
1059                 CP32( h->mb.cache.topright_mv[l][0], mv[h->mb.left_b4[0] + 3 + s4x4*(left_index_table->mv[0]+1)] );
1060                 CP32( h->mb.cache.topright_mv[l][1], mv[h->mb.left_b4[0] + 3 + s4x4*(left_index_table->mv[1]+1)] );
1061                 CP32( h->mb.cache.topright_mv[l][2], mv[h->mb.left_b4[1] + 3 + s4x4*(left_index_table->mv[2]+1)] );
1062             }
1063             else if( !MB_INTERLACED && h->mb.field[h->mb.i_mb_xy-1] )
1064             {
1065                 // Looking at the bottom field so always take the bottom macroblock of the pair.
1066                 h->mb.cache.topright_ref[l][0] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[0]];
1067                 h->mb.cache.topright_ref[l][1] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[0]];
1068                 h->mb.cache.topright_ref[l][2] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[2]];
1069                 CP32( h->mb.cache.topright_mv[l][0], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[0]] );
1070                 CP32( h->mb.cache.topright_mv[l][1], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[1]] );
1071                 CP32( h->mb.cache.topright_mv[l][2], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[2]] );
1072             }
1073         }
1074
1075         if( h->param.b_cabac )
1076         {
1077             uint8_t (*mvd)[8][2] = h->mb.mvd[l];
1078             if( h->mb.i_neighbour & MB_TOP )
1079                 CP64( h->mb.cache.mvd[l][x264_scan8[0] - 8], mvd[top][0] );
1080             else
1081                 M64( h->mb.cache.mvd[l][x264_scan8[0] - 8] ) = 0;
1082
1083             if( h->mb.i_neighbour & MB_LEFT && (!b_mbaff || h->mb.cache.ref[l][x264_scan8[0]-1] >= 0) )
1084             {
1085                 CP16( h->mb.cache.mvd[l][x264_scan8[0 ] - 1], mvd[left[LTOP]][left_index_table->intra[0]] );
1086                 CP16( h->mb.cache.mvd[l][x264_scan8[2 ] - 1], mvd[left[LTOP]][left_index_table->intra[1]] );
1087             }
1088             else
1089             {
1090                 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+0*8] ) = 0;
1091                 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+1*8] ) = 0;
1092             }
1093             if( h->mb.i_neighbour & MB_LEFT && (!b_mbaff || h->mb.cache.ref[l][x264_scan8[0]-1+2*8] >=0) )
1094             {
1095                 CP16( h->mb.cache.mvd[l][x264_scan8[8 ] - 1], mvd[left[LBOT]][left_index_table->intra[2]] );
1096                 CP16( h->mb.cache.mvd[l][x264_scan8[10] - 1], mvd[left[LBOT]][left_index_table->intra[3]] );
1097             }
1098             else
1099             {
1100                 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+2*8] ) = 0;
1101                 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+3*8] ) = 0;
1102             }
1103         }
1104
1105         /* If motion vectors are cached from frame macroblocks but this
1106          * macroblock is a field macroblock then the motion vector must be
1107          * halved. Similarly, motion vectors from field macroblocks are doubled. */
1108         if( b_mbaff )
1109         {
1110 #define MAP_MVS\
1111                 if( FIELD_DIFFERENT(h->mb.i_mb_topleft_xy) )\
1112                     MAP_F2F(mv, ref, x264_scan8[0] - 1 - 1*8)\
1113                 if( FIELD_DIFFERENT(top) )\
1114                 {\
1115                     MAP_F2F(mv, ref, x264_scan8[0] + 0 - 1*8)\
1116                     MAP_F2F(mv, ref, x264_scan8[0] + 1 - 1*8)\
1117                     MAP_F2F(mv, ref, x264_scan8[0] + 2 - 1*8)\
1118                     MAP_F2F(mv, ref, x264_scan8[0] + 3 - 1*8)\
1119                 }\
1120                 if( FIELD_DIFFERENT(h->mb.i_mb_topright_xy) )\
1121                     MAP_F2F(mv, ref, x264_scan8[0] + 4 - 1*8)\
1122                 if( FIELD_DIFFERENT(left[0]) )\
1123                 {\
1124                     MAP_F2F(mv, ref, x264_scan8[0] - 1 + 0*8)\
1125                     MAP_F2F(mv, ref, x264_scan8[0] - 1 + 1*8)\
1126                     MAP_F2F(mv, ref, x264_scan8[0] - 1 + 2*8)\
1127                     MAP_F2F(mv, ref, x264_scan8[0] - 1 + 3*8)\
1128                     MAP_F2F(topright_mv, topright_ref, 0)\
1129                     MAP_F2F(topright_mv, topright_ref, 1)\
1130                     MAP_F2F(topright_mv, topright_ref, 2)\
1131                 }
1132
1133             if( MB_INTERLACED )
1134             {
1135 #define FIELD_DIFFERENT(macroblock) (macroblock >= 0 && !h->mb.field[macroblock])
1136 #define MAP_F2F(varmv, varref, index)\
1137                 if( h->mb.cache.varref[l][index] >= 0 )\
1138                 {\
1139                     h->mb.cache.varref[l][index] <<= 1;\
1140                     h->mb.cache.varmv[l][index][1] /= 2;\
1141                     h->mb.cache.mvd[l][index][1] >>= 1;\
1142                 }
1143                 MAP_MVS
1144 #undef MAP_F2F
1145 #undef FIELD_DIFFERENT
1146             }
1147             else
1148             {
1149 #define FIELD_DIFFERENT(macroblock) (macroblock >= 0 && h->mb.field[macroblock])
1150 #define MAP_F2F(varmv, varref, index)\
1151                 if( h->mb.cache.varref[l][index] >= 0 )\
1152                 {\
1153                     h->mb.cache.varref[l][index] >>= 1;\
1154                     h->mb.cache.varmv[l][index][1] <<= 1;\
1155                     h->mb.cache.mvd[l][index][1] <<= 1;\
1156                 }
1157                 MAP_MVS
1158 #undef MAP_F2F
1159 #undef FIELD_DIFFERENT
1160             }
1161         }
1162     }
1163
1164     if( b_mbaff && mb_x == 0 && !(mb_y&1) && mb_y > 0 )
1165         h->mb.field_decoding_flag = h->mb.field[h->mb.i_mb_xy - h->mb.i_mb_stride];
1166
1167     /* Check whether skip here would cause decoder to predict interlace mode incorrectly.
1168      * FIXME: It might be better to change the interlace type rather than forcing a skip to be non-skip. */
1169     h->mb.b_allow_skip = 1;
1170     if( b_mbaff )
1171     {
1172         if( MB_INTERLACED != h->mb.field_decoding_flag &&
1173             h->mb.i_mb_prev_xy >= 0 && IS_SKIP(h->mb.type[h->mb.i_mb_prev_xy]) )
1174             h->mb.b_allow_skip = 0;
1175         if( (mb_y&1) && IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride]) )
1176         {
1177             if( h->mb.i_neighbour & MB_LEFT )
1178             {
1179                 if( h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED )
1180                     h->mb.b_allow_skip = 0;
1181             }
1182             else if( h->mb.i_neighbour & MB_TOP )
1183             {
1184                 if( h->mb.field[h->mb.i_mb_top_xy] != MB_INTERLACED )
1185                     h->mb.b_allow_skip = 0;
1186             }
1187             else // Frame mb pair is predicted
1188             {
1189                 if( MB_INTERLACED )
1190                     h->mb.b_allow_skip = 0;
1191             }
1192         }
1193     }
1194
1195     if( h->param.b_cabac )
1196     {
1197         if( b_mbaff )
1198         {
1199             int left_xy, top_xy;
1200             /* Neighbours here are calculated based on field_decoding_flag */
1201             int mb_xy = mb_x + (mb_y&~1)*h->mb.i_mb_stride;
1202             left_xy = mb_xy - 1;
1203             if( (mb_y&1) && mb_x > 0 && h->mb.field_decoding_flag == h->mb.field[left_xy] )
1204                 left_xy += h->mb.i_mb_stride;
1205             if( h->mb.field_decoding_flag )
1206             {
1207                 top_xy = mb_xy - h->mb.i_mb_stride;
1208                 if( !(mb_y&1) && top_xy >= 0 && h->mb.slice_table[top_xy] == h->sh.i_first_mb && h->mb.field[top_xy] )
1209                     top_xy -= h->mb.i_mb_stride;
1210             }
1211             else
1212                 top_xy = mb_x + (mb_y-1)*h->mb.i_mb_stride;
1213
1214             h->mb.cache.i_neighbour_skip =   (mb_x >  0 && h->mb.slice_table[left_xy] == h->sh.i_first_mb && !IS_SKIP( h->mb.type[left_xy] ))
1215                                          + (top_xy >= 0 && h->mb.slice_table[top_xy]  == h->sh.i_first_mb && !IS_SKIP( h->mb.type[top_xy] ));
1216         }
1217         else
1218         {
1219             h->mb.cache.i_neighbour_skip = ((h->mb.i_neighbour & MB_LEFT) && !IS_SKIP( h->mb.i_mb_type_left[0] ))
1220                                          + ((h->mb.i_neighbour & MB_TOP)  && !IS_SKIP( h->mb.i_mb_type_top ));
1221         }
1222     }
1223
1224     /* load skip */
1225     if( h->sh.i_type == SLICE_TYPE_B )
1226     {
1227         h->mb.bipred_weight = h->mb.bipred_weight_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
1228         h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
1229         if( h->param.b_cabac )
1230         {
1231             uint8_t skipbp;
1232             x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1233             if( b_mbaff )
1234             {
1235                 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[LTOP]] : 0;
1236                 h->mb.cache.skip[x264_scan8[0] - 1] = (skipbp >> (1+(left_index_table->mv[0]&~1))) & 1;
1237                 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[LBOT]] : 0;
1238                 h->mb.cache.skip[x264_scan8[8] - 1] = (skipbp >> (1+(left_index_table->mv[2]&~1))) & 1;
1239             }
1240             else
1241             {
1242                 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[0]] : 0;
1243                 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
1244                 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
1245             }
1246             skipbp = (h->mb.i_neighbour & MB_TOP) ? h->mb.skipbp[top] : 0;
1247             h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
1248             h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
1249         }
1250     }
1251
1252     if( h->sh.i_type == SLICE_TYPE_P )
1253         x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1254
1255     h->mb.i_neighbour4[0] =
1256     h->mb.i_neighbour8[0] = (h->mb.i_neighbour_intra & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1257                             | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOPRIGHT : 0);
1258     h->mb.i_neighbour4[4] =
1259     h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour_intra & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1260     h->mb.i_neighbour4[2] =
1261     h->mb.i_neighbour4[8] =
1262     h->mb.i_neighbour4[10] =
1263     h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour_intra & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1264     h->mb.i_neighbour4[5] =
1265     h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour_intra & MB_TOPRIGHT)
1266                             | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1267 }
1268
1269 void x264_macroblock_cache_load_progressive( x264_t *h, int mb_x, int mb_y )
1270 {
1271     x264_macroblock_cache_load( h, mb_x, mb_y, 0 );
1272 }
1273
1274 void x264_macroblock_cache_load_interlaced( x264_t *h, int mb_x, int mb_y )
1275 {
1276     x264_macroblock_cache_load( h, mb_x, mb_y, 1 );
1277 }
1278
1279 void x264_macroblock_cache_load_neighbours_deblock( x264_t *h, int mb_x, int mb_y )
1280 {
1281     int deblock_on_slice_edges = h->sh.i_disable_deblocking_filter_idc != 2;
1282
1283     h->mb.i_neighbour = 0;
1284     h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
1285     h->mb.b_interlaced = PARAM_INTERLACED && h->mb.field[h->mb.i_mb_xy];
1286     h->mb.i_mb_top_y = mb_y - (1 << MB_INTERLACED);
1287     h->mb.i_mb_top_xy = mb_x + h->mb.i_mb_stride*h->mb.i_mb_top_y;
1288     h->mb.i_mb_left_xy[1] =
1289     h->mb.i_mb_left_xy[0] = h->mb.i_mb_xy - 1;
1290     if( SLICE_MBAFF )
1291     {
1292         if( mb_y&1 )
1293         {
1294             if( mb_x && h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED )
1295                 h->mb.i_mb_left_xy[0] -= h->mb.i_mb_stride;
1296         }
1297         else
1298         {
1299             if( h->mb.i_mb_top_xy >= 0 && MB_INTERLACED && !h->mb.field[h->mb.i_mb_top_xy] )
1300             {
1301                 h->mb.i_mb_top_xy += h->mb.i_mb_stride;
1302                 h->mb.i_mb_top_y++;
1303             }
1304             if( mb_x && h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED )
1305                 h->mb.i_mb_left_xy[1] += h->mb.i_mb_stride;
1306         }
1307     }
1308
1309     if( mb_x > 0 && (deblock_on_slice_edges ||
1310         h->mb.slice_table[h->mb.i_mb_left_xy[0]] == h->mb.slice_table[h->mb.i_mb_xy]) )
1311         h->mb.i_neighbour |= MB_LEFT;
1312     if( mb_y > MB_INTERLACED && (deblock_on_slice_edges
1313         || h->mb.slice_table[h->mb.i_mb_top_xy] == h->mb.slice_table[h->mb.i_mb_xy]) )
1314         h->mb.i_neighbour |= MB_TOP;
1315 }
1316
1317 void x264_macroblock_deblock_strength( x264_t *h )
1318 {
1319     uint8_t (*bs)[8][4] = h->deblock_strength[h->mb.i_mb_y&1][h->mb.i_mb_x];
1320     if( IS_INTRA( h->mb.type[h->mb.i_mb_xy] ) )
1321     {
1322         memset( bs[0], 3, 4*4*sizeof(uint8_t) );
1323         memset( bs[1], 3, 4*4*sizeof(uint8_t) );
1324         if( !SLICE_MBAFF ) return;
1325     }
1326
1327     /* If we have multiple slices and we're deblocking on slice edges, we
1328      * have to reload neighbour data. */
1329     if( SLICE_MBAFF || (h->sh.i_first_mb && h->sh.i_disable_deblocking_filter_idc != 2) )
1330     {
1331         int old_neighbour = h->mb.i_neighbour;
1332         int mb_x = h->mb.i_mb_x;
1333         int mb_y = h->mb.i_mb_y;
1334         x264_macroblock_cache_load_neighbours_deblock( h, mb_x, mb_y );
1335         int new_neighbour = h->mb.i_neighbour;
1336         h->mb.i_neighbour &= ~old_neighbour;
1337         if( h->mb.i_neighbour )
1338         {
1339             int top_y = h->mb.i_mb_top_y;
1340             int top_8x8 = (2*top_y+1) * h->mb.i_b8_stride + 2*mb_x;
1341             int top_4x4 = (4*top_y+3) * h->mb.i_b4_stride + 4*mb_x;
1342             int s8x8 = h->mb.i_b8_stride;
1343             int s4x4 = h->mb.i_b4_stride;
1344
1345             uint8_t (*nnz)[24] = h->mb.non_zero_count;
1346             x264_left_table_t *left_index_table = SLICE_MBAFF ? h->mb.left_index_table : &left_indices[3];
1347
1348             if( h->mb.i_neighbour & MB_TOP )
1349                 CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &nnz[h->mb.i_mb_top_xy][12] );
1350
1351             if( h->mb.i_neighbour & MB_LEFT )
1352             {
1353                 int *left = h->mb.i_mb_left_xy;
1354                 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = nnz[left[0]][left_index_table->nnz[0]];
1355                 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = nnz[left[0]][left_index_table->nnz[1]];
1356                 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = nnz[left[1]][left_index_table->nnz[2]];
1357                 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[left[1]][left_index_table->nnz[3]];
1358             }
1359
1360             for( int l = 0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
1361             {
1362                 int16_t (*mv)[2] = h->mb.mv[l];
1363                 int8_t *ref = h->mb.ref[l];
1364
1365                 int i8 = x264_scan8[0] - 8;
1366                 if( h->mb.i_neighbour & MB_TOP )
1367                 {
1368                     h->mb.cache.ref[l][i8+0] =
1369                     h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
1370                     h->mb.cache.ref[l][i8+2] =
1371                     h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
1372                     CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
1373                 }
1374
1375                 i8 = x264_scan8[0] - 1;
1376                 if( h->mb.i_neighbour & MB_LEFT )
1377                 {
1378                     h->mb.cache.ref[l][i8+0*8] =
1379                     h->mb.cache.ref[l][i8+1*8] = ref[h->mb.left_b8[0] + 1 + s8x8*left_index_table->ref[0]];
1380                     h->mb.cache.ref[l][i8+2*8] =
1381                     h->mb.cache.ref[l][i8+3*8] = ref[h->mb.left_b8[1] + 1 + s8x8*left_index_table->ref[2]];
1382
1383                     CP32( h->mb.cache.mv[l][i8+0*8], mv[h->mb.left_b4[0] + 3 + s4x4*left_index_table->mv[0]] );
1384                     CP32( h->mb.cache.mv[l][i8+1*8], mv[h->mb.left_b4[0] + 3 + s4x4*left_index_table->mv[1]] );
1385                     CP32( h->mb.cache.mv[l][i8+2*8], mv[h->mb.left_b4[1] + 3 + s4x4*left_index_table->mv[2]] );
1386                     CP32( h->mb.cache.mv[l][i8+3*8], mv[h->mb.left_b4[1] + 3 + s4x4*left_index_table->mv[3]] );
1387                 }
1388             }
1389         }
1390         h->mb.i_neighbour = new_neighbour;
1391     }
1392
1393     if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART && h->sh.i_type == SLICE_TYPE_P )
1394     {
1395         /* Handle reference frame duplicates */
1396         int i8 = x264_scan8[0] - 8;
1397         h->mb.cache.ref[0][i8+0] =
1398         h->mb.cache.ref[0][i8+1] = deblock_ref_table(h->mb.cache.ref[0][i8+0]);
1399         h->mb.cache.ref[0][i8+2] =
1400         h->mb.cache.ref[0][i8+3] = deblock_ref_table(h->mb.cache.ref[0][i8+2]);
1401
1402         i8 = x264_scan8[0] - 1;
1403         h->mb.cache.ref[0][i8+0*8] =
1404         h->mb.cache.ref[0][i8+1*8] = deblock_ref_table(h->mb.cache.ref[0][i8+0*8]);
1405         h->mb.cache.ref[0][i8+2*8] =
1406         h->mb.cache.ref[0][i8+3*8] = deblock_ref_table(h->mb.cache.ref[0][i8+2*8]);
1407
1408         int ref0 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 0]]);
1409         int ref1 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 4]]);
1410         int ref2 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 8]]);
1411         int ref3 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[12]]);
1412         uint32_t reftop = pack16to32( (uint8_t)ref0, (uint8_t)ref1 ) * 0x0101;
1413         uint32_t refbot = pack16to32( (uint8_t)ref2, (uint8_t)ref3 ) * 0x0101;
1414
1415         M32( &h->mb.cache.ref[0][x264_scan8[0]+8*0] ) = reftop;
1416         M32( &h->mb.cache.ref[0][x264_scan8[0]+8*1] ) = reftop;
1417         M32( &h->mb.cache.ref[0][x264_scan8[0]+8*2] ) = refbot;
1418         M32( &h->mb.cache.ref[0][x264_scan8[0]+8*3] ) = refbot;
1419     }
1420
1421     /* Munge NNZ for cavlc + 8x8dct */
1422     if( !h->param.b_cabac && h->pps->b_transform_8x8_mode )
1423     {
1424         uint8_t (*nnz)[24] = h->mb.non_zero_count;
1425         int top = h->mb.i_mb_top_xy;
1426         int *left = h->mb.i_mb_left_xy;
1427
1428         if( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top] )
1429         {
1430             int i8 = x264_scan8[0] - 8;
1431             int nnz_top0 = M16( &nnz[top][8] ) | M16( &nnz[top][12] );
1432             int nnz_top1 = M16( &nnz[top][10] ) | M16( &nnz[top][14] );
1433             M16( &h->mb.cache.non_zero_count[i8+0] ) = nnz_top0 ? 0x0101 : 0;
1434             M16( &h->mb.cache.non_zero_count[i8+2] ) = nnz_top1 ? 0x0101 : 0;
1435         }
1436
1437         if( h->mb.i_neighbour & MB_LEFT )
1438         {
1439             int i8 = x264_scan8[0] - 1;
1440             if( h->mb.mb_transform_size[left[0]] )
1441             {
1442                 int nnz_left0 = M16( &nnz[left[0]][2] ) | M16( &nnz[left[0]][6] );
1443                 h->mb.cache.non_zero_count[i8+8*0] = !!nnz_left0;
1444                 h->mb.cache.non_zero_count[i8+8*1] = !!nnz_left0;
1445             }
1446             if( h->mb.mb_transform_size[left[1]] )
1447             {
1448                 int nnz_left1 = M16( &nnz[left[1]][10] ) | M16( &nnz[left[1]][14] );
1449                 h->mb.cache.non_zero_count[i8+8*2] = !!nnz_left1;
1450                 h->mb.cache.non_zero_count[i8+8*3] = !!nnz_left1;
1451             }
1452         }
1453
1454         if( h->mb.mb_transform_size[h->mb.i_mb_xy] )
1455         {
1456             int nnz0 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 0]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 2]] );
1457             int nnz1 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 4]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 6]] );
1458             int nnz2 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 8]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[10]] );
1459             int nnz3 = M16( &h->mb.cache.non_zero_count[x264_scan8[12]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[14]] );
1460             uint32_t nnztop = pack16to32( !!nnz0, !!nnz1 ) * 0x0101;
1461             uint32_t nnzbot = pack16to32( !!nnz2, !!nnz3 ) * 0x0101;
1462
1463             M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*0] ) = nnztop;
1464             M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*1] ) = nnztop;
1465             M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*2] ) = nnzbot;
1466             M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*3] ) = nnzbot;
1467         }
1468     }
1469
1470     int mvy_limit = 4 >> MB_INTERLACED;
1471     h->loopf.deblock_strength( h->mb.cache.non_zero_count, h->mb.cache.ref, h->mb.cache.mv,
1472                                bs, mvy_limit, h->sh.i_type == SLICE_TYPE_B, h );
1473 }
1474
1475 static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int mb_x, int mb_y, int i, int b_mbaff )
1476 {
1477     int w = i ? 8 : 16;
1478     int i_stride = h->fdec->i_stride[i];
1479     int i_stride2 = i_stride << (b_mbaff && MB_INTERLACED);
1480     int i_pix_offset = (b_mbaff && MB_INTERLACED)
1481                      ? 16 * mb_x + w * (mb_y&~1) * i_stride + (mb_y&1) * i_stride
1482                      : 16 * mb_x + w * mb_y * i_stride;
1483     if( i )
1484         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] );
1485     else
1486         h->mc.copy[PIXEL_16x16]( &h->fdec->plane[0][i_pix_offset], i_stride2, h->mb.pic.p_fdec[0], FDEC_STRIDE, 16 );
1487 }
1488
1489 static void ALWAYS_INLINE x264_macroblock_backup_intra( x264_t *h, int mb_x, int mb_y, int b_mbaff )
1490 {
1491     /* In MBAFF we store the last two rows in intra_border_backup[0] and [1].
1492      * For progressive mbs this is the bottom two rows, and for interlaced the
1493      * bottom row of each field. We also store samples needed for the next
1494      * mbpair in intra_border_backup[2]. */
1495     int backup_dst = !b_mbaff ? 0 : (mb_y&1) ? 1 : MB_INTERLACED ? 0 : 2;
1496     memcpy( &h->intra_border_backup[backup_dst][0][mb_x*16  ], h->mb.pic.p_fdec[0]+FDEC_STRIDE*15, 16*sizeof(pixel) );
1497     memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16  ], h->mb.pic.p_fdec[1]+FDEC_STRIDE*7,   8*sizeof(pixel) );
1498     memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16+8], h->mb.pic.p_fdec[2]+FDEC_STRIDE*7,   8*sizeof(pixel) );
1499     if( b_mbaff )
1500     {
1501         if( mb_y&1 )
1502         {
1503             int backup_src = (MB_INTERLACED ? 7 : 14) * FDEC_STRIDE;
1504             backup_dst = MB_INTERLACED ? 2 : 0;
1505             memcpy( &h->intra_border_backup[backup_dst][0][mb_x*16  ], h->mb.pic.p_fdec[0]+backup_src, 16*sizeof(pixel) );
1506             backup_src = (MB_INTERLACED ? 3 : 6) * FDEC_STRIDE;
1507             memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16  ], h->mb.pic.p_fdec[1]+backup_src,  8*sizeof(pixel) );
1508             memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16+8], h->mb.pic.p_fdec[2]+backup_src,  8*sizeof(pixel) );
1509         }
1510     }
1511     else
1512     {
1513         /* In progressive we update intra_border_backup in-place, so the topleft neighbor will
1514          * no longer exist there when load_pic_pointers wants it. Move it within p_fdec instead. */
1515         h->mb.pic.p_fdec[0][-FDEC_STRIDE-1] = h->mb.pic.p_fdec[0][-FDEC_STRIDE+15];
1516         h->mb.pic.p_fdec[1][-FDEC_STRIDE-1] = h->mb.pic.p_fdec[1][-FDEC_STRIDE+7];
1517         h->mb.pic.p_fdec[2][-FDEC_STRIDE-1] = h->mb.pic.p_fdec[2][-FDEC_STRIDE+7];
1518     }
1519 }
1520
1521 void x264_macroblock_cache_save( x264_t *h )
1522 {
1523     const int i_mb_xy = h->mb.i_mb_xy;
1524     const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1525     const int s8x8 = h->mb.i_b8_stride;
1526     const int s4x4 = h->mb.i_b4_stride;
1527     const int i_mb_4x4 = h->mb.i_b4_xy;
1528     const int i_mb_8x8 = h->mb.i_b8_xy;
1529
1530     /* GCC pessimizes direct stores to heap-allocated arrays due to aliasing. */
1531     /* By only dereferencing them once, we avoid this issue. */
1532     int8_t *i4x4 = h->mb.intra4x4_pred_mode[i_mb_xy];
1533     uint8_t *nnz = h->mb.non_zero_count[i_mb_xy];
1534
1535     if( SLICE_MBAFF )
1536     {
1537         x264_macroblock_backup_intra( h, h->mb.i_mb_x, h->mb.i_mb_y, 1 );
1538         x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 0, 1 );
1539         x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 1 );
1540     }
1541     else
1542     {
1543         x264_macroblock_backup_intra( h, h->mb.i_mb_x, h->mb.i_mb_y, 0 );
1544         x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 0, 0 );
1545         x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 0 );
1546     }
1547
1548     x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1549
1550     h->mb.type[i_mb_xy] = i_mb_type;
1551     h->mb.slice_table[i_mb_xy] = h->sh.i_first_mb;
1552     h->mb.partition[i_mb_xy] = IS_INTRA( i_mb_type ) ? D_16x16 : h->mb.i_partition;
1553     h->mb.i_mb_prev_xy = i_mb_xy;
1554
1555     /* save intra4x4 */
1556     if( i_mb_type == I_4x4 )
1557     {
1558         CP32( &i4x4[0], &h->mb.cache.intra4x4_pred_mode[x264_scan8[10]] );
1559         M32( &i4x4[4] ) = pack8to32( h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
1560                                      h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
1561                                      h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
1562     }
1563     else if( !h->param.b_constrained_intra || IS_INTRA(i_mb_type) )
1564         M64( i4x4 ) = I_PRED_4x4_DC * 0x0101010101010101ULL;
1565     else
1566         M64( i4x4 ) = (uint8_t)(-1) * 0x0101010101010101ULL;
1567
1568
1569     if( i_mb_type == I_PCM )
1570     {
1571         h->mb.qp[i_mb_xy] = 0;
1572         h->mb.i_last_dqp = 0;
1573         h->mb.i_cbp_chroma = 2;
1574         h->mb.i_cbp_luma = 0xf;
1575         h->mb.cbp[i_mb_xy] = 0x72f;   /* all set */
1576         h->mb.b_transform_8x8 = 0;
1577         for( int i = 0; i < 24; i++ )
1578             h->mb.cache.non_zero_count[x264_scan8[i]] = h->param.b_cabac ? 1 : 16;
1579     }
1580     else
1581     {
1582         if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1583             h->mb.i_qp = h->mb.i_last_qp;
1584         h->mb.qp[i_mb_xy] = h->mb.i_qp;
1585         h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1586         h->mb.i_last_qp = h->mb.i_qp;
1587     }
1588
1589     /* save non zero count */
1590     CP32( &nnz[0*4], &h->mb.cache.non_zero_count[x264_scan8[0]+0*8] );
1591     CP32( &nnz[1*4], &h->mb.cache.non_zero_count[x264_scan8[0]+1*8] );
1592     CP32( &nnz[2*4], &h->mb.cache.non_zero_count[x264_scan8[0]+2*8] );
1593     CP32( &nnz[3*4], &h->mb.cache.non_zero_count[x264_scan8[0]+3*8] );
1594     M16( &nnz[16+0*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+0*2]-1] ) >> 8;
1595     M16( &nnz[16+1*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+1*2]-1] ) >> 8;
1596     M16( &nnz[16+2*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+2*2]-1] ) >> 8;
1597     M16( &nnz[16+3*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+3*2]-1] ) >> 8;
1598
1599     if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1600         h->mb.b_transform_8x8 = 0;
1601     h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1602
1603     if( h->sh.i_type != SLICE_TYPE_I )
1604     {
1605         int16_t (*mv0)[2] = &h->mb.mv[0][i_mb_4x4];
1606         int16_t (*mv1)[2] = &h->mb.mv[1][i_mb_4x4];
1607         int8_t *ref0 = &h->mb.ref[0][i_mb_8x8];
1608         int8_t *ref1 = &h->mb.ref[1][i_mb_8x8];
1609         if( !IS_INTRA( i_mb_type ) )
1610         {
1611             ref0[0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
1612             ref0[1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
1613             ref0[0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
1614             ref0[1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
1615             CP128( &mv0[0*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*0] );
1616             CP128( &mv0[1*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*1] );
1617             CP128( &mv0[2*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*2] );
1618             CP128( &mv0[3*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*3] );
1619             if( h->sh.i_type == SLICE_TYPE_B )
1620             {
1621                 ref1[0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
1622                 ref1[1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
1623                 ref1[0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
1624                 ref1[1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
1625                 CP128( &mv1[0*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*0] );
1626                 CP128( &mv1[1*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*1] );
1627                 CP128( &mv1[2*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*2] );
1628                 CP128( &mv1[3*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*3] );
1629             }
1630         }
1631         else
1632         {
1633             M16( &ref0[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1634             M16( &ref0[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1635             M128( &mv0[0*s4x4] ) = M128_ZERO;
1636             M128( &mv0[1*s4x4] ) = M128_ZERO;
1637             M128( &mv0[2*s4x4] ) = M128_ZERO;
1638             M128( &mv0[3*s4x4] ) = M128_ZERO;
1639             if( h->sh.i_type == SLICE_TYPE_B )
1640             {
1641                 M16( &ref1[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1642                 M16( &ref1[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1643                 M128( &mv1[0*s4x4] ) = M128_ZERO;
1644                 M128( &mv1[1*s4x4] ) = M128_ZERO;
1645                 M128( &mv1[2*s4x4] ) = M128_ZERO;
1646                 M128( &mv1[3*s4x4] ) = M128_ZERO;
1647             }
1648         }
1649     }
1650
1651     if( h->param.b_cabac )
1652     {
1653         uint8_t (*mvd0)[2] = h->mb.mvd[0][i_mb_xy];
1654         uint8_t (*mvd1)[2] = h->mb.mvd[1][i_mb_xy];
1655         if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
1656             h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1657         else
1658             h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1659
1660         if( (0x3FF30 >> i_mb_type) & 1 ) /* !INTRA && !SKIP && !DIRECT */
1661         {
1662             CP64( mvd0[0], h->mb.cache.mvd[0][x264_scan8[10]] );
1663             CP16( mvd0[4], h->mb.cache.mvd[0][x264_scan8[5 ]] );
1664             CP16( mvd0[5], h->mb.cache.mvd[0][x264_scan8[7 ]] );
1665             CP16( mvd0[6], h->mb.cache.mvd[0][x264_scan8[13]] );
1666             if( h->sh.i_type == SLICE_TYPE_B )
1667             {
1668                 CP64( mvd1[0], h->mb.cache.mvd[1][x264_scan8[10]] );
1669                 CP16( mvd1[4], h->mb.cache.mvd[1][x264_scan8[5 ]] );
1670                 CP16( mvd1[5], h->mb.cache.mvd[1][x264_scan8[7 ]] );
1671                 CP16( mvd1[6], h->mb.cache.mvd[1][x264_scan8[13]] );
1672             }
1673         }
1674         else
1675         {
1676             M128( mvd0[0] ) = M128_ZERO;
1677             if( h->sh.i_type == SLICE_TYPE_B )
1678                 M128( mvd1[0] ) = M128_ZERO;
1679         }
1680
1681         if( h->sh.i_type == SLICE_TYPE_B )
1682         {
1683             if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1684                 h->mb.skipbp[i_mb_xy] = 0xf;
1685             else if( i_mb_type == B_8x8 )
1686             {
1687                 int skipbp = ( h->mb.i_sub_partition[0] == D_DIRECT_8x8 ) << 0;
1688                 skipbp    |= ( h->mb.i_sub_partition[1] == D_DIRECT_8x8 ) << 1;
1689                 skipbp    |= ( h->mb.i_sub_partition[2] == D_DIRECT_8x8 ) << 2;
1690                 skipbp    |= ( h->mb.i_sub_partition[3] == D_DIRECT_8x8 ) << 3;
1691                 h->mb.skipbp[i_mb_xy] = skipbp;
1692             }
1693             else
1694                 h->mb.skipbp[i_mb_xy] = 0;
1695         }
1696     }
1697 }
1698
1699
1700 void x264_macroblock_bipred_init( x264_t *h )
1701 {
1702     for( int mbfield = 0; mbfield <= SLICE_MBAFF; mbfield++ )
1703         for( int field = 0; field <= SLICE_MBAFF; field++ )
1704             for( int i_ref0 = 0; i_ref0 < (h->i_ref[0]<<mbfield); i_ref0++ )
1705             {
1706                 x264_frame_t *l0 = h->fref[0][i_ref0>>mbfield];
1707                 int poc0 = l0->i_poc + mbfield*l0->i_delta_poc[field^(i_ref0&1)];
1708                 for( int i_ref1 = 0; i_ref1 < (h->i_ref[1]<<mbfield); i_ref1++ )
1709                 {
1710                     int dist_scale_factor;
1711                     x264_frame_t *l1 = h->fref[1][i_ref1>>mbfield];
1712                     int cur_poc = h->fdec->i_poc + mbfield*h->fdec->i_delta_poc[field];
1713                     int poc1 = l1->i_poc + mbfield*l1->i_delta_poc[field^(i_ref1&1)];
1714                     int td = x264_clip3( poc1 - poc0, -128, 127 );
1715                     if( td == 0 /* || pic0 is a long-term ref */ )
1716                         dist_scale_factor = 256;
1717                     else
1718                     {
1719                         int tb = x264_clip3( cur_poc - poc0, -128, 127 );
1720                         int tx = (16384 + (abs(td) >> 1)) / td;
1721                         dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1722                     }
1723
1724                     h->mb.dist_scale_factor_buf[mbfield][field][i_ref0][i_ref1] = dist_scale_factor;
1725
1726                     dist_scale_factor >>= 2;
1727                     if( h->param.analyse.b_weighted_bipred
1728                           && dist_scale_factor >= -64
1729                           && dist_scale_factor <= 128 )
1730                     {
1731                         h->mb.bipred_weight_buf[mbfield][field][i_ref0][i_ref1] = 64 - dist_scale_factor;
1732                         // ssse3 implementation of biweight doesn't support the extrema.
1733                         // if we ever generate them, we'll have to drop that optimization.
1734                         assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
1735                     }
1736                     else
1737                         h->mb.bipred_weight_buf[mbfield][field][i_ref0][i_ref1] = 32;
1738                 }
1739             }
1740 }
1741