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