]> git.sesse.net Git - x264/blob - common/macroblock.c
Faster 8x8dct+CAVLC interleave
[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
901         h->mb.i_neighbour |= MB_TOP;
902
903         /* load intra4x4 */
904         *(uint32_t*)&h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = *(uint32_t*)&h->mb.intra4x4_pred_mode[i_top_xy][0];
905
906         /* load non_zero_count */
907         *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0] - 8] = *(uint32_t*)&h->mb.non_zero_count[i_top_xy][12];
908         /* shift because x264_scan8[16] is misaligned */
909         *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16] - 9] = *(uint16_t*)&h->mb.non_zero_count[i_top_xy][18] << 8;
910         *(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;
911     }
912     else
913     {
914         h->mb.i_mb_type_top = -1;
915
916         /* load intra4x4 */
917         h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
918         h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
919         h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
920         h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
921
922         /* load non_zero_count */
923         h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
924         h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
925         h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
926         h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
927         h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
928         h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
929         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
930         h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
931     }
932
933     if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
934     {
935         i_left_xy = i_mb_xy - 1;
936         h->mb.i_mb_type_left =
937         i_left_type = h->mb.type[i_left_xy];
938
939         h->mb.i_neighbour |= MB_LEFT;
940
941         /* load intra4x4 */
942         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
943         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
944         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
945         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
946
947         /* load non_zero_count */
948         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][3];
949         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
950         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][11];
951         h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
952
953         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
954         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
955
956         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
957         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
958     }
959     else
960     {
961         h->mb.i_mb_type_left = -1;
962
963         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
964         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
965         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
966         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
967
968         /* load non_zero_count */
969         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
970         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
971         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
972         h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
973         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
974         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
975         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
976         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
977     }
978
979     if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
980     {
981         h->mb.i_neighbour |= MB_TOPRIGHT;
982         h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
983     }
984     else
985         h->mb.i_mb_type_topright = -1;
986     if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
987     {
988         h->mb.i_neighbour |= MB_TOPLEFT;
989         h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
990     }
991     else
992         h->mb.i_mb_type_topleft = -1;
993
994     if( h->pps->b_transform_8x8_mode )
995     {
996         h->mb.cache.i_neighbour_transform_size =
997             ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
998           + ( i_top_type  >= 0 && h->mb.mb_transform_size[i_top_xy]  );
999     }
1000
1001     if( h->sh.b_mbaff )
1002     {
1003         h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1004         h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1005         h->mb.cache.i_neighbour_interlaced =
1006             !!(h->mb.i_neighbour & MB_LEFT)
1007           + !!(h->mb.i_neighbour & MB_TOP);
1008     }
1009
1010     if( !h->mb.b_interlaced )
1011     {
1012         copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
1013         copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
1014         copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
1015         copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
1016     }
1017
1018     /* load picture pointers */
1019     x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 0 );
1020     x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 1 );
1021     x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 2 );
1022
1023     if( h->fdec->integral )
1024     {
1025         assert( !h->mb.b_interlaced );
1026         for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
1027             h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1028         for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
1029             h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1030     }
1031
1032     x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
1033
1034     /* load ref/mv/mvd */
1035     if( h->sh.i_type != SLICE_TYPE_I )
1036     {
1037         const int s8x8 = h->mb.i_b8_stride;
1038         const int s4x4 = h->mb.i_b4_stride;
1039
1040         int i_list;
1041
1042         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1043         {
1044             /*
1045             h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1046             h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1047             h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1048             */
1049
1050             if( h->mb.i_neighbour & MB_TOPLEFT )
1051             {
1052                 const int i8 = x264_scan8[0] - 1 - 1*8;
1053                 const int ir = i_top_8x8 - 1;
1054                 const int iv = i_top_4x4 - 1;
1055                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
1056                 *(uint32_t*)h->mb.cache.mv[i_list][i8] = *(uint32_t*)h->mb.mv[i_list][iv];
1057             }
1058             else
1059             {
1060                 const int i8 = x264_scan8[0] - 1 - 1*8;
1061                 h->mb.cache.ref[i_list][i8] = -2;
1062                 *(uint32_t*)h->mb.cache.mv[i_list][i8] = 0;
1063             }
1064
1065             if( h->mb.i_neighbour & MB_TOP )
1066             {
1067                 const int i8 = x264_scan8[0] - 8;
1068                 const int ir = i_top_8x8;
1069                 const int iv = i_top_4x4;
1070                 h->mb.cache.ref[i_list][i8+0] =
1071                 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1072                 h->mb.cache.ref[i_list][i8+2] =
1073                 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1074                 *(uint64_t*)h->mb.cache.mv[i_list][i8+0] = *(uint64_t*)h->mb.mv[i_list][iv+0];
1075                 *(uint64_t*)h->mb.cache.mv[i_list][i8+2] = *(uint64_t*)h->mb.mv[i_list][iv+2];
1076             }
1077             else
1078             {
1079                 const int i8 = x264_scan8[0] - 8;
1080                 *(uint64_t*)h->mb.cache.mv[i_list][i8+0] = 0;
1081                 *(uint64_t*)h->mb.cache.mv[i_list][i8+2] = 0;
1082                 *(uint32_t*)&h->mb.cache.ref[i_list][i8] = (uint8_t)(-2) * 0x01010101U;
1083             }
1084
1085             if( h->mb.i_neighbour & MB_TOPRIGHT )
1086             {
1087                 const int i8 = x264_scan8[0] + 4 - 1*8;
1088                 const int ir = i_top_8x8 + 2;
1089                 const int iv = i_top_4x4 + 4;
1090                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
1091                 *(uint32_t*)h->mb.cache.mv[i_list][i8] = *(uint32_t*)h->mb.mv[i_list][iv];
1092             }
1093             else
1094             {
1095                 const int i8 = x264_scan8[0] + 4 - 1*8;
1096                 h->mb.cache.ref[i_list][i8] = -2;
1097                 *(uint32_t*)h->mb.cache.mv[i_list][i8] = 0;
1098             }
1099
1100             if( h->mb.i_neighbour & MB_LEFT )
1101             {
1102                 const int i8 = x264_scan8[0] - 1;
1103                 const int ir = i_mb_8x8 - 1;
1104                 const int iv = i_mb_4x4 - 1;
1105                 h->mb.cache.ref[i_list][i8+0*8] =
1106                 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1107                 h->mb.cache.ref[i_list][i8+2*8] =
1108                 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1109
1110                 *(uint32_t*)h->mb.cache.mv[i_list][i8+0*8] = *(uint32_t*)h->mb.mv[i_list][iv + 0*s4x4];
1111                 *(uint32_t*)h->mb.cache.mv[i_list][i8+1*8] = *(uint32_t*)h->mb.mv[i_list][iv + 1*s4x4];
1112                 *(uint32_t*)h->mb.cache.mv[i_list][i8+2*8] = *(uint32_t*)h->mb.mv[i_list][iv + 2*s4x4];
1113                 *(uint32_t*)h->mb.cache.mv[i_list][i8+3*8] = *(uint32_t*)h->mb.mv[i_list][iv + 3*s4x4];
1114             }
1115             else
1116             {
1117                 const int i8 = x264_scan8[0] - 1;
1118                 for( i = 0; i < 4; i++ )
1119                 {
1120                     h->mb.cache.ref[i_list][i8+i*8] = -2;
1121                     *(uint32_t*)h->mb.cache.mv[i_list][i8+i*8] = 0;
1122                 }
1123             }
1124
1125             if( h->param.b_cabac )
1126             {
1127                 if( i_top_type >= 0 )
1128                 {
1129                     const int i8 = x264_scan8[0] - 8;
1130                     const int iv = i_top_4x4;
1131                     *(uint64_t*)h->mb.cache.mvd[i_list][i8+0] = *(uint64_t*)h->mb.mvd[i_list][iv+0];
1132                     *(uint64_t*)h->mb.cache.mvd[i_list][i8+2] = *(uint64_t*)h->mb.mvd[i_list][iv+2];
1133                 }
1134                 else
1135                 {
1136                     const int i8 = x264_scan8[0] - 8;
1137                     *(uint64_t*)h->mb.cache.mvd[i_list][i8+0] =
1138                     *(uint64_t*)h->mb.cache.mvd[i_list][i8+2] = 0;
1139                 }
1140
1141                 if( i_left_type >= 0 )
1142                 {
1143                     const int i8 = x264_scan8[0] - 1;
1144                     const int iv = i_mb_4x4 - 1;
1145                     *(uint32_t*)h->mb.cache.mvd[i_list][i8+0*8] = *(uint32_t*)h->mb.mvd[i_list][iv + 0*s4x4];
1146                     *(uint32_t*)h->mb.cache.mvd[i_list][i8+1*8] = *(uint32_t*)h->mb.mvd[i_list][iv + 1*s4x4];
1147                     *(uint32_t*)h->mb.cache.mvd[i_list][i8+2*8] = *(uint32_t*)h->mb.mvd[i_list][iv + 2*s4x4];
1148                     *(uint32_t*)h->mb.cache.mvd[i_list][i8+3*8] = *(uint32_t*)h->mb.mvd[i_list][iv + 3*s4x4];
1149                 }
1150                 else
1151                 {
1152                     const int i8 = x264_scan8[0] - 1;
1153                     for( i = 0; i < 4; i++ )
1154                         *(uint32_t*)h->mb.cache.mvd[i_list][i8+i*8] = 0;
1155                 }
1156             }
1157         }
1158
1159         /* load skip */
1160         if( h->sh.i_type == SLICE_TYPE_B && h->param.b_cabac )
1161         {
1162             uint8_t skipbp;
1163             x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1164             skipbp = i_left_type >= 0 ? h->mb.skipbp[i_left_xy] : 0;
1165             h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
1166             h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
1167             skipbp = i_top_type >= 0 ? h->mb.skipbp[i_top_xy] : 0;
1168             h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
1169             h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
1170         }
1171
1172         if( h->sh.i_type == SLICE_TYPE_P )
1173             x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1174     }
1175
1176     h->mb.i_neighbour4[0] =
1177     h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1178                             | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0);
1179     h->mb.i_neighbour4[4] =
1180     h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1181     h->mb.i_neighbour4[2] =
1182     h->mb.i_neighbour4[8] =
1183     h->mb.i_neighbour4[10] =
1184     h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1185     h->mb.i_neighbour4[5] =
1186     h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour & MB_TOPRIGHT)
1187                             | ((h->mb.i_neighbour & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1188 }
1189
1190 static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int i)
1191 {
1192     int w = i ? 8 : 16;
1193     int i_stride = h->fdec->i_stride[!!i];
1194     int i_stride2 = i_stride << h->mb.b_interlaced;
1195     int i_pix_offset = h->mb.b_interlaced
1196                      ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1197                      : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1198     h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1199         &h->fdec->plane[i][i_pix_offset], i_stride2,
1200         h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1201 }
1202
1203 void x264_macroblock_cache_save( x264_t *h )
1204 {
1205     const int i_mb_xy = h->mb.i_mb_xy;
1206     const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1207     const int s8x8 = h->mb.i_b8_stride;
1208     const int s4x4 = h->mb.i_b4_stride;
1209     const int i_mb_4x4 = h->mb.i_b4_xy;
1210     const int i_mb_8x8 = h->mb.i_b8_xy;
1211
1212     /* GCC pessimizes direct stores to heap-allocated 8-bit arrays due to aliasing.*/
1213     /* By only dereferencing them once, we avoid this issue. */
1214     int8_t *intra4x4_pred_mode = h->mb.intra4x4_pred_mode[i_mb_xy];
1215     uint8_t *non_zero_count = h->mb.non_zero_count[i_mb_xy];
1216
1217     int i, y;
1218
1219     x264_macroblock_store_pic( h, 0 );
1220     x264_macroblock_store_pic( h, 1 );
1221     x264_macroblock_store_pic( h, 2 );
1222
1223     x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1224
1225     h->mb.type[i_mb_xy] = i_mb_type;
1226     h->mb.i_mb_prev_xy = i_mb_xy;
1227
1228     /* save intra4x4 */
1229     if( i_mb_type == I_4x4 )
1230     {
1231         *(uint32_t*)&intra4x4_pred_mode[0] = *(uint32_t*)&h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1232         *(uint32_t*)&intra4x4_pred_mode[4] = pack8to32(h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
1233                                                        h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
1234                                                        h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
1235     }
1236     else
1237         *(uint64_t*)intra4x4_pred_mode = I_PRED_4x4_DC * 0x0101010101010101ULL;
1238
1239     if( i_mb_type == I_PCM )
1240     {
1241         h->mb.qp[i_mb_xy] = 0;
1242         h->mb.i_last_dqp = 0;
1243         h->mb.i_cbp_chroma = 2;
1244         h->mb.i_cbp_luma = 0xf;
1245         h->mb.cbp[i_mb_xy] = 0x72f;   /* all set */
1246         h->mb.b_transform_8x8 = 0;
1247         for( i = 0; i < 16 + 2*4; i++ )
1248             non_zero_count[i] = 16;
1249     }
1250     else
1251     {
1252         /* save non zero count */
1253         *(uint32_t*)&non_zero_count[0*4] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0]+0*8];
1254         *(uint32_t*)&non_zero_count[1*4] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0]+1*8];
1255         *(uint32_t*)&non_zero_count[2*4] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0]+2*8];
1256         *(uint32_t*)&non_zero_count[3*4] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0]+3*8];
1257         *(uint16_t*)&non_zero_count[16+0*2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16+0*2]-1] >> 8;
1258         *(uint16_t*)&non_zero_count[16+1*2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16+1*2]-1] >> 8;
1259         *(uint16_t*)&non_zero_count[16+2*2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16+2*2]-1] >> 8;
1260         *(uint16_t*)&non_zero_count[16+3*2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16+3*2]-1] >> 8;
1261
1262         if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1263             h->mb.i_qp = h->mb.i_last_qp;
1264         h->mb.qp[i_mb_xy] = h->mb.i_qp;
1265         h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1266         h->mb.i_last_qp = h->mb.i_qp;
1267     }
1268
1269     if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1270         h->mb.b_transform_8x8 = 0;
1271     h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1272
1273     if( h->sh.i_type != SLICE_TYPE_I )
1274     {
1275         if( !IS_INTRA( i_mb_type ) )
1276         {
1277             h->mb.ref[0][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
1278             h->mb.ref[0][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
1279             h->mb.ref[0][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
1280             h->mb.ref[0][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
1281             for( y = 0; y < 4; y++ )
1282             {
1283                 *(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];
1284                 *(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];
1285             }
1286             if( h->sh.i_type == SLICE_TYPE_B )
1287             {
1288                 h->mb.ref[1][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
1289                 h->mb.ref[1][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
1290                 h->mb.ref[1][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
1291                 h->mb.ref[1][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
1292                 for( y = 0; y < 4; y++ )
1293                 {
1294                     *(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];
1295                     *(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];
1296                 }
1297             }
1298         }
1299         else
1300         {
1301             int i_list;
1302             for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1303             {
1304                 *(uint16_t*)&h->mb.ref[i_list][i_mb_8x8+0*s8x8] = (uint8_t)(-1) * 0x0101;
1305                 *(uint16_t*)&h->mb.ref[i_list][i_mb_8x8+1*s8x8] = (uint8_t)(-1) * 0x0101;
1306                 for( y = 0; y < 4; y++ )
1307                 {
1308                     *(uint64_t*)h->mb.mv[i_list][i_mb_4x4+y*s4x4+0] = 0;
1309                     *(uint64_t*)h->mb.mv[i_list][i_mb_4x4+y*s4x4+2] = 0;
1310                 }
1311             }
1312         }
1313     }
1314
1315     if( h->param.b_cabac )
1316     {
1317         if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
1318             h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1319         else
1320             h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1321
1322         if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1323         {
1324             for( y = 0; y < 4; y++ )
1325             {
1326                 *(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];
1327                 *(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];
1328             }
1329             if( h->sh.i_type == SLICE_TYPE_B )
1330                 for( y = 0; y < 4; y++ )
1331                 {
1332                     *(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];
1333                     *(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];
1334                 }
1335         }
1336         else
1337         {
1338             for( y = 0; y < 4; y++ )
1339             {
1340                 *(uint64_t*)h->mb.mvd[0][i_mb_4x4+y*s4x4+0] = 0;
1341                 *(uint64_t*)h->mb.mvd[0][i_mb_4x4+y*s4x4+2] = 0;
1342             }
1343             if( h->sh.i_type == SLICE_TYPE_B )
1344                 for( y = 0; y < 4; y++ )
1345                 {
1346                     *(uint64_t*)h->mb.mvd[1][i_mb_4x4+y*s4x4+0] = 0;
1347                     *(uint64_t*)h->mb.mvd[1][i_mb_4x4+y*s4x4+2] = 0;
1348                 }
1349         }
1350
1351         if( h->sh.i_type == SLICE_TYPE_B )
1352         {
1353             if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1354                 h->mb.skipbp[i_mb_xy] = 0xf;
1355             else if( i_mb_type == B_8x8 )
1356             {
1357                 int skipbp = ( h->mb.i_sub_partition[0] == D_DIRECT_8x8 ) << 0;
1358                 skipbp    |= ( h->mb.i_sub_partition[1] == D_DIRECT_8x8 ) << 1;
1359                 skipbp    |= ( h->mb.i_sub_partition[2] == D_DIRECT_8x8 ) << 2;
1360                 skipbp    |= ( h->mb.i_sub_partition[3] == D_DIRECT_8x8 ) << 3;
1361                 h->mb.skipbp[i_mb_xy] = skipbp;
1362             }
1363             else
1364                 h->mb.skipbp[i_mb_xy] = 0;
1365         }
1366     }
1367 }
1368
1369 void x264_macroblock_bipred_init( x264_t *h )
1370 {
1371     int i_ref0, i_ref1;
1372     for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1373     {
1374         int poc0 = h->fref0[i_ref0]->i_poc;
1375         for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1376         {
1377             int dist_scale_factor;
1378             int poc1 = h->fref1[i_ref1]->i_poc;
1379             int td = x264_clip3( poc1 - poc0, -128, 127 );
1380             if( td == 0 /* || pic0 is a long-term ref */ )
1381                 dist_scale_factor = 256;
1382             else
1383             {
1384                 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1385                 int tx = (16384 + (abs(td) >> 1)) / td;
1386                 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1387             }
1388             h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1389
1390             dist_scale_factor >>= 2;
1391             if( h->param.analyse.b_weighted_bipred
1392                   && dist_scale_factor >= -64
1393                   && dist_scale_factor <= 128 )
1394             {
1395                 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1396                 // ssse3 implementation of biweight doesn't support the extrema.
1397                 // if we ever generate them, we'll have to drop that optimization.
1398                 assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
1399             }
1400             else
1401                 h->mb.bipred_weight[i_ref0][i_ref1] = 32;
1402         }
1403     }
1404     if( h->sh.b_mbaff )
1405     {
1406         for( i_ref0 = 2*h->i_ref0-1; i_ref0 >= 0; i_ref0-- )
1407             for( i_ref1 = 2*h->i_ref1-1; i_ref1 >= 0; i_ref1-- )
1408                 h->mb.bipred_weight[i_ref0][i_ref1] = h->mb.bipred_weight[i_ref0>>1][i_ref1>>1];
1409     }
1410 }