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