]> git.sesse.net Git - x264/blob - common/macroblock.c
49f6de5893de550bebc95e1d15ba9eb2abc25967
[x264] / common / macroblock.c
1 /*****************************************************************************
2  * macroblock.c: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2003-2008 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *          Fiona Glaser <fiona@x264.com>
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 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int16_t mvp[2] )
29 {
30     const int i8 = x264_scan8[idx];
31     const int i_ref= h->mb.cache.ref[i_list][i8];
32     int     i_refa = h->mb.cache.ref[i_list][i8 - 1];
33     int16_t *mv_a  = h->mb.cache.mv[i_list][i8 - 1];
34     int     i_refb = h->mb.cache.ref[i_list][i8 - 8];
35     int16_t *mv_b  = h->mb.cache.mv[i_list][i8 - 8];
36     int     i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width];
37     int16_t *mv_c  = h->mb.cache.mv[i_list][i8 - 8 + i_width];
38
39     if( (idx&3) >= 2 + (i_width&1) || i_refc == -2 )
40     {
41         i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
42         mv_c   = h->mb.cache.mv[i_list][i8 - 8 - 1];
43     }
44
45     if( h->mb.i_partition == D_16x8 )
46     {
47         if( idx == 0 )
48         {
49             if( i_refb == i_ref )
50             {
51                 CP32( mvp, mv_b );
52                 return;
53             }
54         }
55         else
56         {
57             if( i_refa == i_ref )
58             {
59                 CP32( mvp, mv_a );
60                 return;
61             }
62         }
63     }
64     else if( h->mb.i_partition == D_8x16 )
65     {
66         if( idx == 0 )
67         {
68             if( i_refa == i_ref )
69             {
70                 CP32( mvp, mv_a );
71                 return;
72             }
73         }
74         else
75         {
76             if( i_refc == i_ref )
77             {
78                 CP32( mvp, mv_c );
79                 return;
80             }
81         }
82     }
83
84     int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
85
86     if( i_count > 1 )
87     {
88 median:
89         x264_median_mv( mvp, mv_a, mv_b, mv_c );
90     }
91     else if( i_count == 1 )
92     {
93         if( i_refa == i_ref )
94             CP32( mvp, mv_a );
95         else if( i_refb == i_ref )
96             CP32( mvp, mv_b );
97         else
98             CP32( mvp, mv_c );
99     }
100     else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
101         CP32( mvp, mv_a );
102     else
103         goto median;
104 }
105
106 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int16_t mvp[2] )
107 {
108     int     i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
109     int16_t *mv_a  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
110     int     i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
111     int16_t *mv_b  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
112     int     i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
113     int16_t *mv_c  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
114     if( i_refc == -2 )
115     {
116         i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
117         mv_c   = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
118     }
119
120     int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
121
122     if( i_count > 1 )
123     {
124 median:
125         x264_median_mv( mvp, mv_a, mv_b, mv_c );
126     }
127     else if( i_count == 1 )
128     {
129         if( i_refa == i_ref )
130             CP32( mvp, mv_a );
131         else if( i_refb == i_ref )
132             CP32( mvp, mv_b );
133         else
134             CP32( mvp, mv_c );
135     }
136     else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
137         CP32( mvp, mv_a );
138     else
139         goto median;
140 }
141
142
143 void x264_mb_predict_mv_pskip( x264_t *h, int16_t mv[2] )
144 {
145     int     i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
146     int     i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
147     int16_t *mv_a  = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
148     int16_t *mv_b  = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
149
150     if( i_refa == -2 || i_refb == -2 ||
151         !( i_refa | M32( mv_a ) ) ||
152         !( i_refb | M32( mv_b ) ) )
153     {
154         M32( mv ) = 0;
155     }
156     else
157     {
158         x264_mb_predict_mv_16x16( h, 0, 0, mv );
159     }
160 }
161
162 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
163 {
164     int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
165     int i_mb_8x8 =  4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
166     int i8;
167     const int type_col = h->fref1[0]->mb_type[h->mb.i_mb_xy];
168
169     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
170
171     if( IS_INTRA( type_col ) )
172     {
173         x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
174         x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 0, 0 );
175         x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 1, 0 );
176         return 1;
177     }
178
179     for( i8 = 0; i8 < 4; i8++ )
180     {
181         const int x8 = i8%2;
182         const int y8 = i8/2;
183         const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
184         const int i_ref1_ref = h->fref1[0]->ref[0][i_part_8x8];
185         const int i_ref = (map_col_to_list0(i_ref1_ref>>h->sh.b_mbaff) << h->sh.b_mbaff) + (i_ref1_ref&h->sh.b_mbaff);
186
187         if( i_ref >= 0 )
188         {
189             const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
190             const int16_t *mv_col = h->fref1[0]->mv[0][i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
191             const int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
192             const int l0y = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
193             if( h->param.i_threads > 1 && (l0y > h->mb.mv_max_spel[1] || l0y-mv_col[1] > h->mb.mv_max_spel[1]) )
194                 return 0;
195             x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
196             x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, pack16to32_mask(l0x, l0y) );
197             x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_col[1]) );
198         }
199         else
200         {
201             /* the collocated ref isn't in the current list0 */
202             /* FIXME: we might still be able to use direct_8x8 on some partitions */
203             /* FIXME: with B-pyramid + extensive ref list reordering
204              *   (not currently used), we would also have to check
205              *   l1mv1 like in spatial mode */
206             return 0;
207         }
208     }
209
210     return 1;
211 }
212
213 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
214 {
215     int8_t ref[2];
216     ALIGNED_ARRAY_8( int16_t, mv,[2],[2] );
217     int i_list, i8, i_ref;
218     const int8_t *l1ref0 = &h->fref1[0]->ref[0][h->mb.i_b8_xy];
219     const int8_t *l1ref1 = &h->fref1[0]->ref[1][h->mb.i_b8_xy];
220     const int16_t (*l1mv[2])[2] = { (const int16_t (*)[2]) &h->fref1[0]->mv[0][h->mb.i_b4_xy],
221                                     (const int16_t (*)[2]) &h->fref1[0]->mv[1][h->mb.i_b4_xy] };
222     const int type_col = h->fref1[0]->mb_type[h->mb.i_mb_xy];
223
224     for( i_list = 0; i_list < 2; i_list++ )
225     {
226         int     i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
227         int16_t *mv_a  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
228         int     i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
229         int16_t *mv_b  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
230         int     i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
231         int16_t *mv_c  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
232         if( i_refc == -2 )
233         {
234             i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
235             mv_c   = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
236         }
237
238         i_ref = X264_MIN3( (unsigned)i_refa, (unsigned)i_refb, (unsigned)i_refc );
239         if( i_ref < 0 )
240         {
241             i_ref = -1;
242             M32( mv[i_list] ) = 0;
243         }
244         else
245         {
246             /* Same as x264_mb_predict_mv_16x16, but simplified to eliminate cases
247              * not relevant to spatial direct. */
248             int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
249
250             if( i_count > 1 )
251                 x264_median_mv( mv[i_list], mv_a, mv_b, mv_c );
252             else
253             {
254                 if( i_refa == i_ref )
255                     CP32( mv[i_list], mv_a );
256                 else if( i_refb == i_ref )
257                     CP32( mv[i_list], mv_b );
258                 else
259                     CP32( mv[i_list], mv_c );
260             }
261         }
262
263         x264_macroblock_cache_ref( h, 0, 0, 4, 4, i_list, i_ref );
264         x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, i_list, mv[i_list] );
265         ref[i_list] = i_ref;
266     }
267
268     if( (M16( ref ) & 0x8080) == 0x8080 ) /* if( ref[0] < 0 && ref[1] < 0 ) */
269     {
270         x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
271         x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
272         return 1;
273     }
274
275     if( !M64( mv ) || IS_INTRA( type_col ) || (ref[0]&&ref[1]) )
276         return 1;
277
278     if( h->param.i_threads > 1
279         && ( mv[0][1] > h->mb.mv_max_spel[1]
280           || mv[1][1] > h->mb.mv_max_spel[1] ) )
281     {
282 #if 0
283         fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
284                 mv[0][0], mv[0][1], mv[1][0], mv[1][1],
285                 h->mb.mv_max_spel[1]);
286 #endif
287         return 0;
288     }
289
290     /* col_zero_flag */
291     for( i8 = 0; i8 < 4; i8++ )
292     {
293         const int x8 = i8&1;
294         const int y8 = i8>>1;
295         const int o8 = x8 + y8 * h->mb.i_b8_stride;
296         const int o4 = 3*(x8 + y8 * h->mb.i_b4_stride);
297         int idx;
298         if( l1ref0[o8] == 0 )
299             idx = 0;
300         else if( l1ref0[o8] < 0 && l1ref1[o8] == 0 )
301             idx = 1;
302         else
303             continue;
304
305         if( abs( l1mv[idx][o4][0] ) <= 1 && abs( l1mv[idx][o4][1] ) <= 1 )
306         {
307             if( ref[0] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0 );
308             if( ref[1] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0 );
309         }
310     }
311
312     return 1;
313 }
314
315 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
316 {
317     int b_available;
318     if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
319         return 0;
320     else if( h->sh.b_direct_spatial_mv_pred )
321         b_available = x264_mb_predict_mv_direct16x16_spatial( h );
322     else
323         b_available = x264_mb_predict_mv_direct16x16_temporal( h );
324
325     if( b_changed != NULL && b_available )
326     {
327         int type_col = h->fref1[0]->mb_type[h->mb.i_mb_xy];
328         int changed = 0;
329
330         if( IS_INTRA( type_col ) || type_col == P_SKIP )
331         {
332             changed |= M32( h->mb.cache.direct_mv[0][0] ) ^ M32( h->mb.cache.mv[0][X264_SCAN8_0] );
333             changed |= M32( h->mb.cache.direct_mv[1][0] ) ^ M32( h->mb.cache.mv[1][X264_SCAN8_0] );
334             changed |= h->mb.cache.direct_ref[0][0] ^ h->mb.cache.ref[0][X264_SCAN8_0];
335             changed |= h->mb.cache.direct_ref[1][0] ^ h->mb.cache.ref[1][X264_SCAN8_0];
336         }
337         else
338         {
339             int l;
340             for( l = 0; l < 2; l++ )
341             {
342                 changed |= M32( h->mb.cache.direct_mv[l][0] ) ^ M32( h->mb.cache.mv[l][x264_scan8[ 0]] );
343                 if( changed ) break;
344                 changed |= M32( h->mb.cache.direct_mv[l][1] ) ^ M32( h->mb.cache.mv[l][x264_scan8[ 4]] );
345                 changed |= M32( h->mb.cache.direct_mv[l][2] ) ^ M32( h->mb.cache.mv[l][x264_scan8[ 8]] );
346                 changed |= M32( h->mb.cache.direct_mv[l][3] ) ^ M32( h->mb.cache.mv[l][x264_scan8[12]] );
347                 if( changed ) break;
348                 changed |= h->mb.cache.direct_ref[l][0] ^ h->mb.cache.ref[l][x264_scan8[ 0]];
349                 changed |= h->mb.cache.direct_ref[l][1] ^ h->mb.cache.ref[l][x264_scan8[ 4]];
350                 changed |= h->mb.cache.direct_ref[l][2] ^ h->mb.cache.ref[l][x264_scan8[ 8]];
351                 changed |= h->mb.cache.direct_ref[l][3] ^ h->mb.cache.ref[l][x264_scan8[12]];
352             }
353         }
354         *b_changed = changed;
355         if( !changed )
356             return b_available;
357     }
358
359     /* cache ref & mv */
360     if( b_available )
361     {
362         int l;
363         for( l = 0; l < 2; l++ )
364         {
365             CP32( h->mb.cache.direct_mv[l][0], h->mb.cache.mv[l][x264_scan8[ 0]] );
366             CP32( h->mb.cache.direct_mv[l][1], h->mb.cache.mv[l][x264_scan8[ 4]] );
367             CP32( h->mb.cache.direct_mv[l][2], h->mb.cache.mv[l][x264_scan8[ 8]] );
368             CP32( h->mb.cache.direct_mv[l][3], h->mb.cache.mv[l][x264_scan8[12]] );
369             h->mb.cache.direct_ref[l][0] = h->mb.cache.ref[l][x264_scan8[ 0]];
370             h->mb.cache.direct_ref[l][1] = h->mb.cache.ref[l][x264_scan8[ 4]];
371             h->mb.cache.direct_ref[l][2] = h->mb.cache.ref[l][x264_scan8[ 8]];
372             h->mb.cache.direct_ref[l][3] = h->mb.cache.ref[l][x264_scan8[12]];
373         }
374     }
375
376     return b_available;
377 }
378
379 /* This just improves encoder performance, it's not part of the spec */
380 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int16_t mvc[9][2], int *i_mvc )
381 {
382     int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
383     int i = 0;
384
385 #define SET_MVP(mvp) { \
386         CP32( mvc[i], mvp ); \
387         i++; \
388     }
389
390     /* b_direct */
391     if( h->sh.i_type == SLICE_TYPE_B
392         && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
393     {
394         SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
395     }
396
397     if( i_ref == 0 && h->frames.b_have_lowres )
398     {
399         int16_t (*lowres_mv)[2] = i_list ? h->fenc->lowres_mvs[1][h->fref1[0]->i_frame-h->fenc->i_frame-1]
400                                          : h->fenc->lowres_mvs[0][h->fenc->i_frame-h->fref0[0]->i_frame-1];
401         if( lowres_mv[0][0] != 0x7fff )
402         {
403             M32( mvc[i] ) = (M32( lowres_mv[h->mb.i_mb_xy] )*2)&0xfffeffff;
404             i++;
405         }
406     }
407
408     /* spatial predictors */
409     if( h->mb.i_neighbour & MB_LEFT )
410     {
411         int i_mb_l = h->mb.i_mb_xy - 1;
412         /* skip MBs didn't go through the whole search process, so mvr is undefined */
413         if( !IS_SKIP( h->mb.type[i_mb_l] ) )
414             SET_MVP( mvr[i_mb_l] );
415     }
416     if( h->mb.i_neighbour & MB_TOP )
417     {
418         int i_mb_t = h->mb.i_mb_top_xy;
419         if( !IS_SKIP( h->mb.type[i_mb_t] ) )
420             SET_MVP( mvr[i_mb_t] );
421
422         if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
423             SET_MVP( mvr[i_mb_t-1] );
424         if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
425             SET_MVP( mvr[i_mb_t+1] );
426     }
427 #undef SET_MVP
428
429     /* temporal predictors */
430     /* FIXME temporal scaling w/ interlace */
431     if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
432     {
433         x264_frame_t *l0 = h->fref0[0];
434
435 #define SET_TMVP(dx, dy) { \
436             int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
437             int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
438             int ref_col = l0->ref[0][i_b8]; \
439             if( ref_col >= 0 ) \
440             { \
441                 int scale = (h->fdec->i_poc - h->fdec->ref_poc[0][i_ref]) * l0->inv_ref_poc[ref_col];\
442                 mvc[i][0] = (l0->mv[0][i_b4][0]*scale + 128) >> 8;\
443                 mvc[i][1] = (l0->mv[0][i_b4][1]*scale + 128) >> 8;\
444                 i++; \
445             } \
446         }
447
448         SET_TMVP(0,0);
449         if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
450             SET_TMVP(1,0);
451         if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
452             SET_TMVP(0,1);
453 #undef SET_TMVP
454     }
455
456     *i_mvc = i;
457 }
458
459 /* Set up a lookup table for delta pocs to reduce an IDIV to an IMUL */
460 static void setup_inverse_delta_pocs( x264_t *h )
461 {
462     int i;
463     for( i = 0; i < h->i_ref0; i++ )
464     {
465         int delta = h->fdec->i_poc - h->fref0[i]->i_poc;
466         h->fdec->inv_ref_poc[i] = (256 + delta/2) / delta;
467     }
468 }
469
470 static NOINLINE void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
471 {
472     const int i8 = x264_scan8[0]+x+8*y;
473     const int i_ref = h->mb.cache.ref[0][i8];
474     const int mvx   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
475     int       mvy   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
476
477     h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
478                    h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
479                    mvx, mvy, 4*width, 4*height, &h->sh.weight[i_ref][0] );
480
481     // chroma is offset if MCing from a field of opposite parity
482     if( h->mb.b_interlaced & i_ref )
483         mvy += (h->mb.i_mb_y & 1)*4 - 2;
484
485     h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
486                      h->mb.pic.p_fref[0][i_ref][4], h->mb.pic.i_stride[1],
487                      mvx, mvy, 2*width, 2*height );
488
489     if( h->sh.weight[i_ref][1].weightfn )
490         h->sh.weight[i_ref][1].weightfn[width>>1]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
491                                                    &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
492                                                    &h->sh.weight[i_ref][1], height*2 );
493
494     h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
495                      h->mb.pic.p_fref[0][i_ref][5], h->mb.pic.i_stride[2],
496                      mvx, mvy, 2*width, 2*height );
497
498     if( h->sh.weight[i_ref][2].weightfn )
499         h->sh.weight[i_ref][2].weightfn[width>>1]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
500                                                    &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
501                                                    &h->sh.weight[i_ref][2],height*2 );
502
503 }
504 static NOINLINE void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
505 {
506     const int i8 = x264_scan8[0]+x+8*y;
507     const int i_ref = h->mb.cache.ref[1][i8];
508     const int mvx   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
509     int       mvy   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
510
511     h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
512                    h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
513                    mvx, mvy, 4*width, 4*height, weight_none );
514
515     if( h->mb.b_interlaced & i_ref )
516         mvy += (h->mb.i_mb_y & 1)*4 - 2;
517
518     h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
519                      h->mb.pic.p_fref[1][i_ref][4], h->mb.pic.i_stride[1],
520                      mvx, mvy, 2*width, 2*height );
521
522     h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
523                      h->mb.pic.p_fref[1][i_ref][5], h->mb.pic.i_stride[2],
524                      mvx, mvy, 2*width, 2*height );
525 }
526
527 static NOINLINE void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
528 {
529     const int i8 = x264_scan8[0]+x+8*y;
530     const int i_ref0 = h->mb.cache.ref[0][i8];
531     const int i_ref1 = h->mb.cache.ref[1][i8];
532     const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
533     const int mvx0   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
534     const int mvx1   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
535     int       mvy0   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
536     int       mvy1   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
537     int       i_mode = x264_size2pixel[height][width];
538     int       i_stride0 = 16, i_stride1 = 16;
539     ALIGNED_ARRAY_16( uint8_t, tmp0,[16*16] );
540     ALIGNED_ARRAY_16( uint8_t, tmp1,[16*16] );
541     uint8_t *src0, *src1;
542
543     src0 = h->mc.get_ref( tmp0, &i_stride0, h->mb.pic.p_fref[0][i_ref0], h->mb.pic.i_stride[0],
544                           mvx0, mvy0, 4*width, 4*height, weight_none );
545     src1 = h->mc.get_ref( tmp1, &i_stride1, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
546                           mvx1, mvy1, 4*width, 4*height, weight_none );
547     h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
548                        src0, i_stride0, src1, i_stride1, weight );
549
550     if( h->mb.b_interlaced & i_ref0 )
551         mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
552     if( h->mb.b_interlaced & i_ref1 )
553         mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
554
555     h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][4], h->mb.pic.i_stride[1],
556                      mvx0, mvy0, 2*width, 2*height );
557     h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][4], h->mb.pic.i_stride[1],
558                      mvx1, mvy1, 2*width, 2*height );
559     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 );
560     h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][5], h->mb.pic.i_stride[2],
561                      mvx0, mvy0, 2*width, 2*height );
562     h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][5], h->mb.pic.i_stride[2],
563                      mvx1, mvy1, 2*width, 2*height );
564     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 );
565 }
566
567 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
568 {
569     const int i8 = x264_scan8[0] + x + 8*y;
570
571     if( h->mb.cache.ref[0][i8] >= 0 )
572         if( h->mb.cache.ref[1][i8] >= 0 )
573             x264_mb_mc_01xywh( h, x, y, 2, 2 );
574         else
575             x264_mb_mc_0xywh( h, x, y, 2, 2 );
576     else
577         x264_mb_mc_1xywh( h, x, y, 2, 2 );
578 }
579
580 void x264_mb_mc_8x8( x264_t *h, int i8 )
581 {
582     const int x = 2*(i8&1);
583     const int y = 2*(i8>>1);
584     switch( h->mb.i_sub_partition[i8] )
585     {
586         case D_L0_8x8:
587             x264_mb_mc_0xywh( h, x, y, 2, 2 );
588             break;
589         case D_L0_8x4:
590             x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
591             x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
592             break;
593         case D_L0_4x8:
594             x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
595             x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
596             break;
597         case D_L0_4x4:
598             x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
599             x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
600             x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
601             x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
602             break;
603         case D_L1_8x8:
604             x264_mb_mc_1xywh( h, x, y, 2, 2 );
605             break;
606         case D_BI_8x8:
607             x264_mb_mc_01xywh( h, x, y, 2, 2 );
608             break;
609         case D_DIRECT_8x8:
610             x264_mb_mc_direct8x8( h, x, y );
611             break;
612     }
613 }
614
615 void x264_mb_mc( x264_t *h )
616 {
617     if( h->mb.i_type == P_L0 )
618     {
619         if( h->mb.i_partition == D_16x16 )
620         {
621             x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
622         }
623         else if( h->mb.i_partition == D_16x8 )
624         {
625             x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
626             x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
627         }
628         else if( h->mb.i_partition == D_8x16 )
629         {
630             x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
631             x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
632         }
633     }
634     else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
635     {
636         int i;
637         for( i = 0; i < 4; i++ )
638             x264_mb_mc_8x8( h, i );
639     }
640     else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
641     {
642         x264_mb_mc_direct8x8( h, 0, 0 );
643         x264_mb_mc_direct8x8( h, 2, 0 );
644         x264_mb_mc_direct8x8( h, 0, 2 );
645         x264_mb_mc_direct8x8( h, 2, 2 );
646     }
647     else    /* B_*x* */
648     {
649         const uint8_t *b_list0 = x264_mb_type_list_table[h->mb.i_type][0];
650         const uint8_t *b_list1 = x264_mb_type_list_table[h->mb.i_type][1];
651
652         if( h->mb.i_partition == D_16x16 )
653         {
654             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
655             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
656             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
657         }
658         else if( h->mb.i_partition == D_16x8 )
659         {
660             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
661             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
662             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
663
664             if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
665             else if( b_list0[1] )          x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
666             else if( b_list1[1] )          x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
667         }
668         else if( h->mb.i_partition == D_8x16 )
669         {
670             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
671             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
672             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
673
674             if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
675             else if( b_list0[1] )          x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
676             else if( b_list1[1] )          x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
677         }
678     }
679 }
680
681 int x264_macroblock_cache_init( x264_t *h )
682 {
683     int i, j;
684     int i_mb_count = h->mb.i_mb_count;
685
686     h->mb.i_mb_stride = h->sps->i_mb_width;
687     h->mb.i_b8_stride = h->sps->i_mb_width * 2;
688     h->mb.i_b4_stride = h->sps->i_mb_width * 4;
689
690     h->mb.b_interlaced = h->param.b_interlaced;
691
692     CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
693     CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
694     CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
695     CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
696
697     /* 0 -> 3 top(4), 4 -> 6 : left(3) */
698     CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
699
700     /* all coeffs */
701     CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
702
703     if( h->param.b_cabac )
704     {
705         CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
706         CHECKED_MALLOC( h->mb.mvd[0], 2*16 * i_mb_count * sizeof(int16_t) );
707         CHECKED_MALLOC( h->mb.mvd[1], 2*16 * i_mb_count * sizeof(int16_t) );
708     }
709
710     for( i=0; i<2; i++ )
711     {
712         int i_refs = X264_MIN(16, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << h->param.b_interlaced;
713         if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
714             i_refs = X264_MIN(16, i_refs + 2); //smart weights add two duplicate frames
715         else if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_BLIND )
716             i_refs = X264_MIN(16, i_refs + 1); //blind weights add one duplicate frame
717
718         for( j=0; j < i_refs; j++ )
719             CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
720     }
721
722     if( h->param.analyse.i_weighted_pred )
723     {
724         int i_padv = PADV << h->param.b_interlaced;
725 #define ALIGN(x,a) (((x)+((a)-1))&~((a)-1))
726         int align = h->param.cpu&X264_CPU_CACHELINE_64 ? 64 : h->param.cpu&X264_CPU_CACHELINE_32 ? 32 : 16;
727         int i_stride, luma_plane_size;
728         int numweightbuf;
729
730         if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_FAKE )
731         {
732             // only need buffer for lookahead
733             if( !h->param.i_sync_lookahead || h == h->thread[h->param.i_threads] )
734             {
735                 // Fake analysis only works on lowres
736                 i_stride = ALIGN( h->sps->i_mb_width*8 + 2*PADH, align );
737                 luma_plane_size = i_stride * (h->sps->i_mb_height*8+2*i_padv);
738                 // Only need 1 buffer for analysis
739                 numweightbuf = 1;
740             }
741             else
742                 numweightbuf = 0;
743         }
744         else
745         {
746             i_stride = ALIGN( h->sps->i_mb_width*16 + 2*PADH, align );
747             luma_plane_size = i_stride * (h->sps->i_mb_height*16+2*i_padv);
748
749             if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
750                 //SMART can weight one ref and one offset -1
751                 numweightbuf = 2;
752             else
753                 //blind only has one weighted copy (offset -1)
754                 numweightbuf = 1;
755         }
756
757         for( i = 0; i < numweightbuf; i++ )
758             CHECKED_MALLOC( h->mb.p_weight_buf[i], luma_plane_size );
759 #undef ALIGN
760     }
761
762     for( i=0; i<=h->param.b_interlaced; i++ )
763         for( j=0; j<3; j++ )
764         {
765             /* shouldn't really be initialized, just silences a valgrind false-positive in predict_8x8_filter_mmx */
766             CHECKED_MALLOCZERO( h->mb.intra_border_backup[i][j], (h->sps->i_mb_width*16+32)>>!!j );
767             h->mb.intra_border_backup[i][j] += 8;
768         }
769
770     /* init with not available (for top right idx=7,15) */
771     memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
772     memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
773
774     return 0;
775 fail: return -1;
776 }
777 void x264_macroblock_cache_end( x264_t *h )
778 {
779     int i, j;
780     for( i=0; i<=h->param.b_interlaced; i++ )
781         for( j=0; j<3; j++ )
782             x264_free( h->mb.intra_border_backup[i][j] - 8 );
783     for( i=0; i<2; i++ )
784         for( j=0; j<32; j++ )
785             x264_free( h->mb.mvr[i][j] );
786     for( i=0; i<16; i++ )
787         x264_free( h->mb.p_weight_buf[i] );
788
789     if( h->param.b_cabac )
790     {
791         x264_free( h->mb.chroma_pred_mode );
792         x264_free( h->mb.mvd[0] );
793         x264_free( h->mb.mvd[1] );
794     }
795     x264_free( h->mb.intra4x4_pred_mode );
796     x264_free( h->mb.non_zero_count );
797     x264_free( h->mb.mb_transform_size );
798     x264_free( h->mb.skipbp );
799     x264_free( h->mb.cbp );
800     x264_free( h->mb.qp );
801 }
802 void x264_macroblock_slice_init( x264_t *h )
803 {
804     int i, j;
805
806     h->mb.mv[0] = h->fdec->mv[0];
807     h->mb.mv[1] = h->fdec->mv[1];
808     h->mb.ref[0] = h->fdec->ref[0];
809     h->mb.ref[1] = h->fdec->ref[1];
810     h->mb.type = h->fdec->mb_type;
811
812     h->fdec->i_ref[0] = h->i_ref0;
813     h->fdec->i_ref[1] = h->i_ref1;
814     for( i = 0; i < h->i_ref0; i++ )
815         h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
816     if( h->sh.i_type == SLICE_TYPE_B )
817     {
818         for( i = 0; i < h->i_ref1; i++ )
819             h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
820
821         map_col_to_list0(-1) = -1;
822         map_col_to_list0(-2) = -2;
823         for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
824         {
825             int poc = h->fref1[0]->ref_poc[0][i];
826             map_col_to_list0(i) = -2;
827             for( j = 0; j < h->i_ref0; j++ )
828                 if( h->fref0[j]->i_poc == poc )
829                 {
830                     map_col_to_list0(i) = j;
831                     break;
832                 }
833         }
834     }
835     if( h->sh.i_type == SLICE_TYPE_P )
836         memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
837
838     setup_inverse_delta_pocs( h );
839
840     h->mb.i_neighbour4[6] =
841     h->mb.i_neighbour4[9] =
842     h->mb.i_neighbour4[12] =
843     h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
844     h->mb.i_neighbour4[3] =
845     h->mb.i_neighbour4[7] =
846     h->mb.i_neighbour4[11] =
847     h->mb.i_neighbour4[13] =
848     h->mb.i_neighbour4[15] =
849     h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
850 }
851
852 void x264_macroblock_thread_init( x264_t *h )
853 {
854     /* fdec:      fenc:
855      * yyyyyyy
856      * yYYYY      YYYY
857      * yYYYY      YYYY
858      * yYYYY      YYYY
859      * yYYYY      YYYY
860      * uuu vvv    UUVV
861      * uUU vVV    UUVV
862      * uUU vVV
863      */
864     h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
865     h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
866     h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
867     h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
868     h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
869     h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
870 }
871
872 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
873 {
874     int stride_y  = fenc->i_stride[0];
875     int stride_uv = fenc->i_stride[1];
876     int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
877     int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
878     h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
879                          fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
880 }
881
882 static NOINLINE void copy_column8( uint8_t *dst, uint8_t *src )
883 {
884     // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
885     int i;
886     for( i = -4; i < 4; i++ )
887         dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
888 }
889
890 static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int i_mb_x, int i_mb_y, int i)
891 {
892     const int w = (i == 0 ? 16 : 8);
893     const int i_stride = h->fdec->i_stride[!!i];
894     const int i_stride2 = i_stride << h->mb.b_interlaced;
895     const int i_pix_offset = h->mb.b_interlaced
896                            ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride
897                            : w * (i_mb_x + i_mb_y * i_stride);
898     const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
899     const uint8_t *intra_fdec = h->param.b_sliced_threads ? plane_fdec-i_stride2 :
900                                 &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
901     int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
902     x264_frame_t **fref[2] = { h->fref0, h->fref1 };
903     int j, k;
904     if( h->mb.b_interlaced )
905         ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
906     h->mb.pic.i_stride[i] = i_stride2;
907     h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
908     h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
909         h->mb.pic.p_fenc_plane[i], i_stride2, w );
910     if( i_mb_y > 0 )
911         memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
912     else
913         memset( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], 0, w*3/2+1 );
914     if( h->mb.b_interlaced || h->mb.b_reencode_mb )
915         for( j = 0; j < w; j++ )
916             h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
917     for( j = 0; j < h->mb.pic.i_fref[0]; j++ )
918     {
919         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]];
920         if( i == 0 )
921         {
922             for( k = 1; k < 4; k++ )
923                 h->mb.pic.p_fref[0][j][k] = &fref[0][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
924             if( h->sh.weight[j][0].weightfn )
925                 h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> h->mb.b_interlaced][ref_pix_offset[j&1]];
926             else
927                 h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
928         }
929     }
930     if( h->sh.i_type == SLICE_TYPE_B )
931         for( j = 0; j < h->mb.pic.i_fref[1]; j++ )
932         {
933             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]];
934             if( i == 0 )
935                 for( k = 1; k < 4; k++ )
936                     h->mb.pic.p_fref[1][j][k] = &fref[1][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
937         }
938 }
939
940 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
941 {
942     int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
943     int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
944     int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
945     int i_top_y = i_mb_y - (1 << h->mb.b_interlaced);
946     int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x;
947     int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x;
948     int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x;
949     int i_left_xy = -1;
950     int i_top_type = -1;    /* gcc warn */
951     int i_left_type= -1;
952
953     int i;
954
955     /* init index */
956     h->mb.i_mb_x = i_mb_x;
957     h->mb.i_mb_y = i_mb_y;
958     h->mb.i_mb_xy = i_mb_xy;
959     h->mb.i_b8_xy = i_mb_8x8;
960     h->mb.i_b4_xy = i_mb_4x4;
961     h->mb.i_mb_top_xy = i_top_xy;
962     h->mb.i_neighbour = 0;
963     h->mb.i_neighbour_intra = 0;
964
965     /* load cache */
966     if( i_top_xy >= h->sh.i_first_mb )
967     {
968         h->mb.i_mb_type_top =
969         i_top_type = h->mb.type[i_top_xy];
970         h->mb.cache.i_cbp_top = h->mb.cbp[i_top_xy];
971
972         h->mb.i_neighbour |= MB_TOP;
973
974         if( !h->param.b_constrained_intra || IS_INTRA( i_top_type ) )
975             h->mb.i_neighbour_intra |= MB_TOP;
976
977         /* load intra4x4 */
978         CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &h->mb.intra4x4_pred_mode[i_top_xy][0] );
979
980         /* load non_zero_count */
981         CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &h->mb.non_zero_count[i_top_xy][12] );
982         /* shift because x264_scan8[16] is misaligned */
983         M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = M16( &h->mb.non_zero_count[i_top_xy][18] ) << 8;
984         M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = M16( &h->mb.non_zero_count[i_top_xy][22] ) << 8;
985     }
986     else
987     {
988         h->mb.i_mb_type_top = -1;
989         h->mb.cache.i_cbp_top = -1;
990
991         /* load intra4x4 */
992         M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
993
994         /* load non_zero_count */
995         M32( &h->mb.cache.non_zero_count[x264_scan8[   0] - 8] ) = 0x80808080U;
996         M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = 0x80808080U;
997         M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = 0x80808080U;
998     }
999
1000     if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1001     {
1002         i_left_xy = i_mb_xy - 1;
1003         h->mb.i_mb_type_left =
1004         i_left_type = h->mb.type[i_left_xy];
1005         h->mb.cache.i_cbp_left = h->mb.cbp[h->mb.i_mb_xy - 1];
1006
1007         h->mb.i_neighbour |= MB_LEFT;
1008
1009         if( !h->param.b_constrained_intra || IS_INTRA( i_left_type ) )
1010             h->mb.i_neighbour_intra |= MB_LEFT;
1011
1012         /* load intra4x4 */
1013         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1014         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1015         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1016         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1017
1018         /* load non_zero_count */
1019         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][3];
1020         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1021         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][11];
1022         h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1023
1024         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1025         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1026
1027         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1028         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1029     }
1030     else
1031     {
1032         h->mb.i_mb_type_left = -1;
1033         h->mb.cache.i_cbp_left = -1;
1034
1035         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1036         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1037         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1038         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1039
1040         /* load non_zero_count */
1041         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1042         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1043         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1044         h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1045         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1046         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1047         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1048         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1049     }
1050
1051     if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1052     {
1053         h->mb.i_neighbour |= MB_TOPRIGHT;
1054         h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1055         if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
1056             h->mb.i_neighbour_intra |= MB_TOPRIGHT;
1057     }
1058     else
1059         h->mb.i_mb_type_topright = -1;
1060     if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1061     {
1062         h->mb.i_neighbour |= MB_TOPLEFT;
1063         h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1064         if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
1065             h->mb.i_neighbour_intra |= MB_TOPLEFT;
1066     }
1067     else
1068         h->mb.i_mb_type_topleft = -1;
1069
1070     if( h->pps->b_transform_8x8_mode )
1071     {
1072         h->mb.cache.i_neighbour_transform_size =
1073             ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1074           + ( i_top_type  >= 0 && h->mb.mb_transform_size[i_top_xy]  );
1075     }
1076
1077     if( h->sh.b_mbaff )
1078     {
1079         h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1080         h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1081         h->mb.cache.i_neighbour_interlaced =
1082             !!(h->mb.i_neighbour & MB_LEFT)
1083           + !!(h->mb.i_neighbour & MB_TOP);
1084     }
1085
1086     if( !h->mb.b_interlaced && !h->mb.b_reencode_mb )
1087     {
1088         copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
1089         copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
1090         copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
1091         copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
1092     }
1093
1094     /* load picture pointers */
1095     x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 0 );
1096     x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 1 );
1097     x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 2 );
1098
1099     if( h->fdec->integral )
1100     {
1101         assert( !h->mb.b_interlaced );
1102         for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
1103             h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1104         for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
1105             h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1106     }
1107
1108     x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
1109
1110     /* load ref/mv/mvd */
1111     if( h->sh.i_type != SLICE_TYPE_I )
1112     {
1113         const int s8x8 = h->mb.i_b8_stride;
1114         const int s4x4 = h->mb.i_b4_stride;
1115
1116         int i_list;
1117
1118         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1119         {
1120             /*
1121             h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1122             h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1123             h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1124             */
1125
1126             if( h->mb.i_neighbour & MB_TOPLEFT )
1127             {
1128                 const int i8 = x264_scan8[0] - 1 - 1*8;
1129                 const int ir = i_top_8x8 - 1;
1130                 const int iv = i_top_4x4 - 1;
1131                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
1132                 CP32( h->mb.cache.mv[i_list][i8], h->mb.mv[i_list][iv] );
1133             }
1134             else
1135             {
1136                 const int i8 = x264_scan8[0] - 1 - 1*8;
1137                 h->mb.cache.ref[i_list][i8] = -2;
1138                 M32( h->mb.cache.mv[i_list][i8] ) = 0;
1139             }
1140
1141             if( h->mb.i_neighbour & MB_TOP )
1142             {
1143                 const int i8 = x264_scan8[0] - 8;
1144                 const int ir = i_top_8x8;
1145                 const int iv = i_top_4x4;
1146                 h->mb.cache.ref[i_list][i8+0] =
1147                 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1148                 h->mb.cache.ref[i_list][i8+2] =
1149                 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1150                 CP64( h->mb.cache.mv[i_list][i8+0], h->mb.mv[i_list][iv+0] );
1151                 CP64( h->mb.cache.mv[i_list][i8+2], h->mb.mv[i_list][iv+2] );
1152             }
1153             else
1154             {
1155                 const int i8 = x264_scan8[0] - 8;
1156                 M64( h->mb.cache.mv[i_list][i8+0] ) = 0;
1157                 M64( h->mb.cache.mv[i_list][i8+2] ) = 0;
1158                 M32( &h->mb.cache.ref[i_list][i8] ) = (uint8_t)(-2) * 0x01010101U;
1159             }
1160
1161             if( h->mb.i_neighbour & MB_TOPRIGHT )
1162             {
1163                 const int i8 = x264_scan8[0] + 4 - 1*8;
1164                 const int ir = i_top_8x8 + 2;
1165                 const int iv = i_top_4x4 + 4;
1166                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
1167                 CP32( h->mb.cache.mv[i_list][i8], h->mb.mv[i_list][iv] );
1168             }
1169             else
1170             {
1171                 const int i8 = x264_scan8[0] + 4 - 1*8;
1172                 h->mb.cache.ref[i_list][i8] = -2;
1173                 M32( h->mb.cache.mv[i_list][i8] ) = 0;
1174             }
1175
1176             if( h->mb.i_neighbour & MB_LEFT )
1177             {
1178                 const int i8 = x264_scan8[0] - 1;
1179                 const int ir = i_mb_8x8 - 1;
1180                 const int iv = i_mb_4x4 - 1;
1181                 h->mb.cache.ref[i_list][i8+0*8] =
1182                 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1183                 h->mb.cache.ref[i_list][i8+2*8] =
1184                 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1185
1186                 CP32( h->mb.cache.mv[i_list][i8+0*8], h->mb.mv[i_list][iv + 0*s4x4] );
1187                 CP32( h->mb.cache.mv[i_list][i8+1*8], h->mb.mv[i_list][iv + 1*s4x4] );
1188                 CP32( h->mb.cache.mv[i_list][i8+2*8], h->mb.mv[i_list][iv + 2*s4x4] );
1189                 CP32( h->mb.cache.mv[i_list][i8+3*8], h->mb.mv[i_list][iv + 3*s4x4] );
1190             }
1191             else
1192             {
1193                 const int i8 = x264_scan8[0] - 1;
1194                 for( i = 0; i < 4; i++ )
1195                 {
1196                     h->mb.cache.ref[i_list][i8+i*8] = -2;
1197                     M32( h->mb.cache.mv[i_list][i8+i*8] ) = 0;
1198                 }
1199             }
1200
1201             if( h->param.b_cabac )
1202             {
1203                 if( i_top_type >= 0 )
1204                 {
1205                     const int i8 = x264_scan8[0] - 8;
1206                     const int iv = i_top_4x4;
1207                     CP64( h->mb.cache.mvd[i_list][i8+0], h->mb.mvd[i_list][iv+0] );
1208                     CP64( h->mb.cache.mvd[i_list][i8+2], h->mb.mvd[i_list][iv+2] );
1209                 }
1210                 else
1211                 {
1212                     const int i8 = x264_scan8[0] - 8;
1213                     M64( h->mb.cache.mvd[i_list][i8+0] ) = 0;
1214                     M64( h->mb.cache.mvd[i_list][i8+2] ) = 0;
1215                 }
1216
1217                 if( i_left_type >= 0 )
1218                 {
1219                     const int i8 = x264_scan8[0] - 1;
1220                     const int iv = i_mb_4x4 - 1;
1221                     CP32( h->mb.cache.mvd[i_list][i8+0*8], h->mb.mvd[i_list][iv + 0*s4x4] );
1222                     CP32( h->mb.cache.mvd[i_list][i8+1*8], h->mb.mvd[i_list][iv + 1*s4x4] );
1223                     CP32( h->mb.cache.mvd[i_list][i8+2*8], h->mb.mvd[i_list][iv + 2*s4x4] );
1224                     CP32( h->mb.cache.mvd[i_list][i8+3*8], h->mb.mvd[i_list][iv + 3*s4x4] );
1225                 }
1226                 else
1227                 {
1228                     const int i8 = x264_scan8[0] - 1;
1229                     for( i = 0; i < 4; i++ )
1230                         M32( h->mb.cache.mvd[i_list][i8+i*8] ) = 0;
1231                 }
1232             }
1233         }
1234
1235         /* load skip */
1236         if( h->sh.i_type == SLICE_TYPE_B )
1237         {
1238             h->mb.bipred_weight = h->mb.bipred_weight_buf[h->mb.b_interlaced&(i_mb_y&1)];
1239             h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[h->mb.b_interlaced&(i_mb_y&1)];
1240             if( h->param.b_cabac )
1241             {
1242                 uint8_t skipbp;
1243                 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1244                 skipbp = i_left_type >= 0 ? h->mb.skipbp[i_left_xy] : 0;
1245                 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
1246                 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
1247                 skipbp = i_top_type >= 0 ? h->mb.skipbp[i_top_xy] : 0;
1248                 h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
1249                 h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
1250             }
1251         }
1252
1253         if( h->sh.i_type == SLICE_TYPE_P )
1254             x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1255     }
1256
1257     h->mb.i_neighbour4[0] =
1258     h->mb.i_neighbour8[0] = (h->mb.i_neighbour_intra & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1259                             | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOPRIGHT : 0);
1260     h->mb.i_neighbour4[4] =
1261     h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour_intra & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1262     h->mb.i_neighbour4[2] =
1263     h->mb.i_neighbour4[8] =
1264     h->mb.i_neighbour4[10] =
1265     h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour_intra & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1266     h->mb.i_neighbour4[5] =
1267     h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour_intra & MB_TOPRIGHT)
1268                             | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1269 }
1270
1271 static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int i )
1272 {
1273     int w = i ? 8 : 16;
1274     int i_stride = h->fdec->i_stride[!!i];
1275     int i_stride2 = i_stride << h->mb.b_interlaced;
1276     int i_pix_offset = h->mb.b_interlaced
1277                      ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1278                      : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1279     h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1280         &h->fdec->plane[i][i_pix_offset], i_stride2,
1281         h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1282 }
1283
1284 void x264_macroblock_cache_save( x264_t *h )
1285 {
1286     const int i_mb_xy = h->mb.i_mb_xy;
1287     const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1288     const int s8x8 = h->mb.i_b8_stride;
1289     const int s4x4 = h->mb.i_b4_stride;
1290     const int i_mb_4x4 = h->mb.i_b4_xy;
1291     const int i_mb_8x8 = h->mb.i_b8_xy;
1292
1293     /* GCC pessimizes direct stores to heap-allocated 8-bit arrays due to aliasing.*/
1294     /* By only dereferencing them once, we avoid this issue. */
1295     int8_t *intra4x4_pred_mode = h->mb.intra4x4_pred_mode[i_mb_xy];
1296     uint8_t *non_zero_count = h->mb.non_zero_count[i_mb_xy];
1297
1298     int y;
1299
1300     x264_macroblock_store_pic( h, 0 );
1301     x264_macroblock_store_pic( h, 1 );
1302     x264_macroblock_store_pic( h, 2 );
1303
1304     x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1305
1306     h->mb.type[i_mb_xy] = i_mb_type;
1307     h->mb.i_mb_prev_xy = i_mb_xy;
1308
1309     /* save intra4x4 */
1310     if( i_mb_type == I_4x4 )
1311     {
1312         CP32( &intra4x4_pred_mode[0], &h->mb.cache.intra4x4_pred_mode[x264_scan8[10]] );
1313         M32( &intra4x4_pred_mode[4] ) = pack8to32(h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
1314                                                   h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
1315                                                   h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
1316     }
1317     else if( !h->param.b_constrained_intra || IS_INTRA(i_mb_type) )
1318         M64( intra4x4_pred_mode ) = I_PRED_4x4_DC * 0x0101010101010101ULL;
1319     else
1320         M64( intra4x4_pred_mode ) = (uint8_t)(-1) * 0x0101010101010101ULL;
1321
1322
1323     if( i_mb_type == I_PCM )
1324     {
1325         h->mb.qp[i_mb_xy] = 0;
1326         h->mb.i_last_dqp = 0;
1327         h->mb.i_cbp_chroma = 2;
1328         h->mb.i_cbp_luma = 0xf;
1329         h->mb.cbp[i_mb_xy] = 0x72f;   /* all set */
1330         h->mb.b_transform_8x8 = 0;
1331         memset( non_zero_count, 16, 24 );
1332     }
1333     else
1334     {
1335         /* save non zero count */
1336         CP32( &non_zero_count[0*4], &h->mb.cache.non_zero_count[x264_scan8[0]+0*8] );
1337         CP32( &non_zero_count[1*4], &h->mb.cache.non_zero_count[x264_scan8[0]+1*8] );
1338         CP32( &non_zero_count[2*4], &h->mb.cache.non_zero_count[x264_scan8[0]+2*8] );
1339         CP32( &non_zero_count[3*4], &h->mb.cache.non_zero_count[x264_scan8[0]+3*8] );
1340         M16( &non_zero_count[16+0*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+0*2]-1] ) >> 8;
1341         M16( &non_zero_count[16+1*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+1*2]-1] ) >> 8;
1342         M16( &non_zero_count[16+2*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+2*2]-1] ) >> 8;
1343         M16( &non_zero_count[16+3*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+3*2]-1] ) >> 8;
1344
1345         if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1346             h->mb.i_qp = h->mb.i_last_qp;
1347         h->mb.qp[i_mb_xy] = h->mb.i_qp;
1348         h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1349         h->mb.i_last_qp = h->mb.i_qp;
1350     }
1351
1352     if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1353         h->mb.b_transform_8x8 = 0;
1354     h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1355
1356     if( h->sh.i_type != SLICE_TYPE_I )
1357     {
1358         if( !IS_INTRA( i_mb_type ) )
1359         {
1360             h->mb.ref[0][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
1361             h->mb.ref[0][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
1362             h->mb.ref[0][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
1363             h->mb.ref[0][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
1364             for( y = 0; y < 4; y++ )
1365             {
1366                 CP64( h->mb.mv[0][i_mb_4x4+y*s4x4+0], h->mb.cache.mv[0][x264_scan8[0]+8*y+0] );
1367                 CP64( h->mb.mv[0][i_mb_4x4+y*s4x4+2], h->mb.cache.mv[0][x264_scan8[0]+8*y+2] );
1368             }
1369             if( h->sh.i_type == SLICE_TYPE_B )
1370             {
1371                 h->mb.ref[1][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
1372                 h->mb.ref[1][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
1373                 h->mb.ref[1][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
1374                 h->mb.ref[1][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
1375                 for( y = 0; y < 4; y++ )
1376                 {
1377                     CP64( h->mb.mv[1][i_mb_4x4+y*s4x4+0], h->mb.cache.mv[1][x264_scan8[0]+8*y+0] );
1378                     CP64( h->mb.mv[1][i_mb_4x4+y*s4x4+2], h->mb.cache.mv[1][x264_scan8[0]+8*y+2] );
1379                 }
1380             }
1381         }
1382         else
1383         {
1384             int i_list;
1385             for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1386             {
1387                 M16( &h->mb.ref[i_list][i_mb_8x8+0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1388                 M16( &h->mb.ref[i_list][i_mb_8x8+1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1389                 for( y = 0; y < 4; y++ )
1390                 {
1391                     M64( h->mb.mv[i_list][i_mb_4x4+y*s4x4+0] ) = 0;
1392                     M64( h->mb.mv[i_list][i_mb_4x4+y*s4x4+2] ) = 0;
1393                 }
1394             }
1395         }
1396     }
1397
1398     if( h->param.b_cabac )
1399     {
1400         if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
1401             h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1402         else
1403             h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1404
1405         if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1406         {
1407             for( y = 0; y < 4; y++ )
1408             {
1409                 CP64( h->mb.mvd[0][i_mb_4x4+y*s4x4+0], h->mb.cache.mvd[0][x264_scan8[0]+8*y+0] );
1410                 CP64( h->mb.mvd[0][i_mb_4x4+y*s4x4+2], h->mb.cache.mvd[0][x264_scan8[0]+8*y+2] );
1411             }
1412             if( h->sh.i_type == SLICE_TYPE_B )
1413                 for( y = 0; y < 4; y++ )
1414                 {
1415                     CP64( h->mb.mvd[1][i_mb_4x4+y*s4x4+0], h->mb.cache.mvd[1][x264_scan8[0]+8*y+0] );
1416                     CP64( h->mb.mvd[1][i_mb_4x4+y*s4x4+2], h->mb.cache.mvd[1][x264_scan8[0]+8*y+2] );
1417                 }
1418         }
1419         else
1420         {
1421             for( y = 0; y < 4; y++ )
1422             {
1423                 M64( h->mb.mvd[0][i_mb_4x4+y*s4x4+0] ) = 0;
1424                 M64( h->mb.mvd[0][i_mb_4x4+y*s4x4+2] ) = 0;
1425             }
1426             if( h->sh.i_type == SLICE_TYPE_B )
1427                 for( y = 0; y < 4; y++ )
1428                 {
1429                     M64( h->mb.mvd[1][i_mb_4x4+y*s4x4+0] ) = 0;
1430                     M64( h->mb.mvd[1][i_mb_4x4+y*s4x4+2] ) = 0;
1431                 }
1432         }
1433
1434         if( h->sh.i_type == SLICE_TYPE_B )
1435         {
1436             if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1437                 h->mb.skipbp[i_mb_xy] = 0xf;
1438             else if( i_mb_type == B_8x8 )
1439             {
1440                 int skipbp = ( h->mb.i_sub_partition[0] == D_DIRECT_8x8 ) << 0;
1441                 skipbp    |= ( h->mb.i_sub_partition[1] == D_DIRECT_8x8 ) << 1;
1442                 skipbp    |= ( h->mb.i_sub_partition[2] == D_DIRECT_8x8 ) << 2;
1443                 skipbp    |= ( h->mb.i_sub_partition[3] == D_DIRECT_8x8 ) << 3;
1444                 h->mb.skipbp[i_mb_xy] = skipbp;
1445             }
1446             else
1447                 h->mb.skipbp[i_mb_xy] = 0;
1448         }
1449     }
1450 }
1451
1452
1453 void x264_macroblock_bipred_init( x264_t *h )
1454 {
1455     int i_ref0, i_ref1, field;
1456     for( field = 0; field <= h->sh.b_mbaff; field++ )
1457         for( i_ref0 = 0; i_ref0 < (h->i_ref0<<h->sh.b_mbaff); i_ref0++ )
1458         {
1459             int poc0 = h->fref0[i_ref0>>h->sh.b_mbaff]->i_poc;
1460             if( h->sh.b_mbaff && field^(i_ref0&1) )
1461                 poc0 += h->sh.i_delta_poc_bottom;
1462             for( i_ref1 = 0; i_ref1 < (h->i_ref1<<h->sh.b_mbaff); i_ref1++ )
1463             {
1464                 int dist_scale_factor;
1465                 int poc1 = h->fref1[i_ref1>>h->sh.b_mbaff]->i_poc;
1466                 if( h->sh.b_mbaff && field^(i_ref1&1) )
1467                     poc1 += h->sh.i_delta_poc_bottom;
1468                 int cur_poc = h->fdec->i_poc + field*h->sh.i_delta_poc_bottom;
1469                 int td = x264_clip3( poc1 - poc0, -128, 127 );
1470                 if( td == 0 /* || pic0 is a long-term ref */ )
1471                     dist_scale_factor = 256;
1472                 else
1473                 {
1474                     int tb = x264_clip3( cur_poc - poc0, -128, 127 );
1475                     int tx = (16384 + (abs(td) >> 1)) / td;
1476                     dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1477                 }
1478
1479                 h->mb.dist_scale_factor_buf[field][i_ref0][i_ref1] = dist_scale_factor;
1480
1481                 dist_scale_factor >>= 2;
1482                 if( h->param.analyse.b_weighted_bipred
1483                       && dist_scale_factor >= -64
1484                       && dist_scale_factor <= 128 )
1485                 {
1486                     h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 64 - dist_scale_factor;
1487                     // ssse3 implementation of biweight doesn't support the extrema.
1488                     // if we ever generate them, we'll have to drop that optimization.
1489                     assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
1490                 }
1491                 else
1492                     h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 32;
1493             }
1494         }
1495 }
1496