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