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