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