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