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