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