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