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