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