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