]> git.sesse.net Git - x264/blob - common/macroblock.c
3ecda66c05e25dd5b3b1007598441bd1d8067b4e
[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 collocated 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     if( h->param.i_threads > 1 )
312     {
313         int di = b8x8 ? 4 : 1;
314         for( i4=0; i4<16; i4+=di )
315         {
316             if( h->mb.cache.mv[0][x264_scan8[i4]][1] > h->mb.mv_max_spel[1]
317              || h->mb.cache.mv[1][x264_scan8[i4]][1] > h->mb.mv_max_spel[1] )
318             {
319 #if 0
320                 fprintf(stderr, "direct_temporal: (%d,%d) (%d,%d) > %d \n",
321                         h->mb.cache.mv[0][x264_scan8[i4]][0],
322                         h->mb.cache.mv[0][x264_scan8[i4]][1],
323                         h->mb.cache.mv[1][x264_scan8[i4]][0],
324                         h->mb.cache.mv[1][x264_scan8[i4]][1],
325                         h->mb.mv_max_spel[1]);
326 #endif
327                 return 0;
328             }
329         }
330     }
331
332     return 1;
333 }
334
335 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
336 {
337     int ref[2];
338     int mv[2][2];
339     int i_list;
340     int i8, i4;
341     int b8x8;
342     const int8_t *l1ref0 = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
343     const int8_t *l1ref1 = &h->fref1[0]->ref[1][ h->mb.i_b8_xy ];
344     const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
345     const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[1][ h->mb.i_b4_xy ];
346     const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
347
348     for( i_list=0; i_list<2; i_list++ )
349     {
350         int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
351         int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
352         int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
353         if( i_refc == -2 )
354             i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
355
356         ref[i_list] = i_refa;
357         if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
358             ref[i_list] = i_refb;
359         if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
360             ref[i_list] = i_refc;
361         if( ref[i_list] < 0 )
362             ref[i_list] = -1;
363     }
364
365     if( ref[0] < 0 && ref[1] < 0 )
366     {
367         ref[0] = 
368         ref[1] = 0;
369         mv[0][0] = 
370         mv[0][1] = 
371         mv[1][0] = 
372         mv[1][1] = 0;
373     }
374     else
375     {
376         for( i_list=0; i_list<2; i_list++ )
377         {
378             if( ref[i_list] >= 0 )
379                 x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
380             else
381                 mv[i_list][0] = mv[i_list][1] = 0;
382         }
383     }
384
385     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
386     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
387     x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 0, mv[0][0], mv[0][1] );
388     x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 1, mv[1][0], mv[1][1] );
389
390     if( IS_INTRA( type_col ) )
391         return 1;
392
393     if( h->param.i_threads > 1
394         && ( mv[0][1] > h->mb.mv_max_spel[1]
395           || mv[1][1] > h->mb.mv_max_spel[1] ) )
396     {
397 #if 0
398         fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
399                 mv[0][0], mv[0][1], mv[1][0], mv[1][1],
400                 h->mb.mv_max_spel[1]);
401 #endif
402         return 0;
403     }
404
405     b8x8 = h->sps->b_direct8x8_inference ||
406            (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
407
408     /* col_zero_flag */
409     for( i8=0; i8<4; i8++ )
410     {
411         const int x8 = i8%2;
412         const int y8 = i8/2;
413         const int o8 = x8 + y8 * h->mb.i_b8_stride;
414         if( l1ref0[o8] == 0 || ( l1ref0[o8] < 0 && l1ref1[o8] == 0 ) )
415         {
416             const int16_t (*l1mv)[2] = (l1ref0[o8] == 0) ? l1mv0 : l1mv1;
417             if( b8x8 )
418             {
419                 const int16_t *mvcol = l1mv[3*x8 + 3*y8 * h->mb.i_b4_stride];
420                 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
421                 {
422                     if( ref[0] == 0 )
423                         x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0, 0 );
424                     if( ref[1] == 0 )
425                         x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0, 0 );
426                 }
427             }
428             else
429             {
430                 for( i4=0; i4<4; i4++ )
431                 {
432                     const int x4 = i4%2 + 2*x8;
433                     const int y4 = i4/2 + 2*y8;
434                     const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
435                     if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
436                     {
437                         if( ref[0] == 0 )
438                             x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0, 0 );
439                         if( ref[1] == 0 )
440                             x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0, 0 );
441                     }
442                 }
443             }
444         }
445     }
446
447     return 1;
448 }
449
450 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
451 {
452     int b_available;
453     if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
454         return 0;
455     else if( h->sh.b_direct_spatial_mv_pred )
456         b_available = x264_mb_predict_mv_direct16x16_spatial( h );
457     else
458         b_available = x264_mb_predict_mv_direct16x16_temporal( h );
459
460     if( b_changed != NULL && b_available )
461     {
462         int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
463         if( IS_INTRA(type_col) || type_col == P_SKIP )
464         {
465             *b_changed = h->mb.cache.direct_ref[0][0] != h->mb.cache.ref[0][X264_SCAN8_0]
466                       || h->mb.cache.direct_ref[1][0] != h->mb.cache.ref[1][X264_SCAN8_0]
467                       || *(uint32_t*)h->mb.cache.direct_mv[0][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[0][X264_SCAN8_0]
468                       || *(uint32_t*)h->mb.cache.direct_mv[1][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[1][X264_SCAN8_0];
469         }
470         else
471         {
472             int i, l;
473             *b_changed = 0;
474             for( l = 0; l < 2; l++ )
475                 for( i = 0; i < 4; i++ )
476                     *b_changed |= h->mb.cache.direct_ref[l][i] != h->mb.cache.ref[l][x264_scan8[i*4]];
477             *b_changed = *b_changed || memcmp(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
478         }
479         if( !*b_changed )
480             return b_available;
481     }
482
483     /* cache ref & mv */
484     if( b_available )
485     {
486         int i, l;
487         for( l = 0; l < 2; l++ )
488             for( i = 0; i < 4; i++ )
489                 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
490         memcpy(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
491     }
492
493     return b_available;
494 }
495
496 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
497 {
498     const int x = 2*(idx%2);
499     const int y = 2*(idx/2);
500     int l;
501     x264_macroblock_cache_ref( h, x, y, 2, 2, 0, h->mb.cache.direct_ref[0][idx] );
502     x264_macroblock_cache_ref( h, x, y, 2, 2, 1, h->mb.cache.direct_ref[1][idx] );
503     for( l = 0; l < 2; l++ )
504     {
505         *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]] =
506         *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]];
507         *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]+8] =
508         *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]+8];
509     }
510 }
511
512 /* This just improves encoder performance, it's not part of the spec */
513 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[8][2], int *i_mvc )
514 {
515     int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
516     int i = 0;
517
518 #define SET_MVP(mvp) { \
519         mvc[i][0] = mvp[0]; \
520         mvc[i][1] = mvp[1]; \
521         i++; \
522     }
523
524     /* b_direct */
525     if( h->sh.i_type == SLICE_TYPE_B
526         && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
527     {
528         SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
529     }
530
531     /* spatial predictors */
532     if( h->mb.i_neighbour & MB_LEFT )
533     {
534         int i_mb_l = h->mb.i_mb_xy - 1;
535         /* skip MBs didn't go through the whole search process, so mvr is undefined */
536         if( !IS_SKIP( h->mb.type[i_mb_l] ) )
537             SET_MVP( mvr[i_mb_l] );
538     }
539     if( h->mb.i_neighbour & MB_TOP )
540     {
541         int i_mb_t = h->mb.i_mb_top_xy;
542         if( !IS_SKIP( h->mb.type[i_mb_t] ) )
543             SET_MVP( mvr[i_mb_t] );
544
545         if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
546             SET_MVP( mvr[i_mb_t-1] );
547         if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
548             SET_MVP( mvr[i_mb_t+1] );
549     }
550 #undef SET_MVP
551
552     /* temporal predictors */
553     /* FIXME temporal scaling w/ interlace */
554     if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
555     {
556         x264_frame_t *l0 = h->fref0[0];
557         int ref_col_cur, ref_col_prev = -1;
558         int scale = 0;
559
560 #define SET_TMVP(dx, dy) { \
561             int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
562             int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
563             ref_col_cur = l0->ref[0][i_b8]; \
564             if( ref_col_cur >= 0 ) \
565             { \
566                 /* TODO: calc once per frame and tablize? */\
567                 if( ref_col_cur != ref_col_prev ) \
568                     scale = 256 * (h->fenc->i_poc - h->fref0[i_ref]->i_poc) \
569                                 / (l0->i_poc - l0->ref_poc[0][ref_col_cur]); \
570                 mvc[i][0] = l0->mv[0][i_b4][0] * scale / 256; \
571                 mvc[i][1] = l0->mv[0][i_b4][1] * scale / 256; \
572                 i++; \
573                 ref_col_prev = ref_col_cur; \
574             } \
575         }
576
577         SET_TMVP(0,0);
578         if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
579             SET_TMVP(1,0);
580         if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
581             SET_TMVP(0,1);
582 #undef SET_TMVP
583     }
584
585     *i_mvc = i;
586 }
587
588 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
589 {
590     const int i8 = x264_scan8[0]+x+8*y;
591     const int i_ref = h->mb.cache.ref[0][i8];
592     const int mvx   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
593     int       mvy   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
594
595     h->mc.mc_luma( h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
596                     &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
597                     mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
598
599     // chroma is offset if MCing from a field of opposite parity
600     if( h->mb.b_interlaced & i_ref )
601         mvy += (h->mb.i_mb_y & 1)*4 - 2;
602
603     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],
604                       &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
605                       mvx, mvy, 2*width, 2*height );
606
607     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],
608                       &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
609                       mvx, mvy, 2*width, 2*height );
610 }
611 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
612 {
613     const int i8 = x264_scan8[0]+x+8*y;
614     const int i_ref = h->mb.cache.ref[1][i8];
615     const int mvx   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
616     int       mvy   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
617
618     h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
619                     &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
620                     mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
621
622     if( h->mb.b_interlaced & i_ref )
623         mvy += (h->mb.i_mb_y & 1)*4 - 2;
624
625     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],
626                       &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
627                       mvx, mvy, 2*width, 2*height );
628
629     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],
630                       &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
631                       mvx, mvy, 2*width, 2*height );
632 }
633
634 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
635 {
636     const int i8 = x264_scan8[0]+x+8*y;
637
638     const int i_ref1 = h->mb.cache.ref[1][i8];
639     const int mvx1   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
640     int       mvy1   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
641     DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
642     int i_mode = x264_size2pixel[height][width];
643
644     x264_mb_mc_0xywh( h, x, y, width, height );
645
646     h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
647                     tmp, 16, mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
648
649     if( h->mb.b_interlaced & i_ref1 )
650         mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
651
652     if( h->param.analyse.b_weighted_bipred )
653     {
654         const int i_ref0 = h->mb.cache.ref[0][i8];
655         const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
656
657         h->mc.avg_weight[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16, weight );
658
659         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],
660                           tmp, 16, mvx1, mvy1, 2*width, 2*height );
661         h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
662
663         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],
664                           tmp, 16, mvx1, mvy1, 2*width, 2*height );
665         h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
666     }
667     else
668     {
669         h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16 );
670
671         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],
672                           tmp, 16, mvx1, mvy1, 2*width, 2*height );
673         h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
674
675         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],
676                           tmp, 16, mvx1, mvy1, 2*width, 2*height );
677         h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
678     }
679 }
680
681 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
682 {
683     const int i8 = x264_scan8[0] + x + 8*y;
684
685     /* FIXME: optimize based on current block size, not global settings? */
686     if( h->sps->b_direct8x8_inference )
687     {
688         if( h->mb.cache.ref[0][i8] >= 0 )
689             if( h->mb.cache.ref[1][i8] >= 0 )
690                 x264_mb_mc_01xywh( h, x, y, 2, 2 );
691             else
692                 x264_mb_mc_0xywh( h, x, y, 2, 2 );
693         else
694             x264_mb_mc_1xywh( h, x, y, 2, 2 );
695     }
696     else
697     {
698         if( h->mb.cache.ref[0][i8] >= 0 )
699         {
700             if( h->mb.cache.ref[1][i8] >= 0 )
701             {
702                 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
703                 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
704                 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
705                 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
706             }
707             else
708             {
709                 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
710                 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
711                 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
712                 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
713             }
714         }
715         else
716         {
717             x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
718             x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
719             x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
720             x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
721         }
722     }
723 }
724
725 void x264_mb_mc_8x8( x264_t *h, int i8 )
726 {
727     const int x = 2*(i8&1);
728     const int y = 2*(i8>>1);
729     switch( h->mb.i_sub_partition[i8] )
730     {
731         case D_L0_8x8:
732             x264_mb_mc_0xywh( h, x, y, 2, 2 );
733             break;
734         case D_L0_8x4:
735             x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
736             x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
737             break;
738         case D_L0_4x8:
739             x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
740             x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
741             break;
742         case D_L0_4x4:
743             x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
744             x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
745             x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
746             x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
747             break;
748         case D_L1_8x8:
749             x264_mb_mc_1xywh( h, x, y, 2, 2 );
750             break;
751         case D_L1_8x4:
752             x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
753             x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
754             break;
755         case D_L1_4x8:
756             x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
757             x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
758             break;
759         case D_L1_4x4:
760             x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
761             x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
762             x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
763             x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
764             break;
765         case D_BI_8x8:
766             x264_mb_mc_01xywh( h, x, y, 2, 2 );
767             break;
768         case D_BI_8x4:
769             x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
770             x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
771             break;
772         case D_BI_4x8:
773             x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
774             x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
775             break;
776         case D_BI_4x4:
777             x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
778             x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
779             x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
780             x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
781             break;
782         case D_DIRECT_8x8:
783             x264_mb_mc_direct8x8( h, x, y );
784             break;
785     }
786 }
787
788 void x264_mb_mc( x264_t *h )
789 {
790     if( h->mb.i_type == P_L0 )
791     {
792         if( h->mb.i_partition == D_16x16 )
793         {
794             x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
795         }
796         else if( h->mb.i_partition == D_16x8 )
797         {
798             x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
799             x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
800         }
801         else if( h->mb.i_partition == D_8x16 )
802         {
803             x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
804             x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
805         }
806     }
807     else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
808     {
809         int i;
810         for( i = 0; i < 4; i++ )
811             x264_mb_mc_8x8( h, i );
812     }
813     else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
814     {
815         x264_mb_mc_direct8x8( h, 0, 0 );
816         x264_mb_mc_direct8x8( h, 2, 0 );
817         x264_mb_mc_direct8x8( h, 0, 2 );
818         x264_mb_mc_direct8x8( h, 2, 2 );
819     }
820     else    /* B_*x* */
821     {
822         int b_list0[2];
823         int b_list1[2];
824
825         int i;
826
827         /* init ref list utilisations */
828         for( i = 0; i < 2; i++ )
829         {
830             b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
831             b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
832         }
833         if( h->mb.i_partition == D_16x16 )
834         {
835             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
836             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
837             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
838         }
839         else if( h->mb.i_partition == D_16x8 )
840         {
841             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
842             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
843             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
844
845             if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
846             else if( b_list0[1] )          x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
847             else if( b_list1[1] )          x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
848         }
849         else if( h->mb.i_partition == D_8x16 )
850         {
851             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
852             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
853             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
854
855             if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
856             else if( b_list0[1] )          x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
857             else if( b_list1[1] )          x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
858         }
859     }
860 }
861
862 int x264_macroblock_cache_init( x264_t *h )
863 {
864     int i, j;
865     int i_mb_count = h->mb.i_mb_count;
866
867     h->mb.i_mb_stride = h->sps->i_mb_width;
868     h->mb.i_b8_stride = h->sps->i_mb_width * 2;
869     h->mb.i_b4_stride = h->sps->i_mb_width * 4;
870
871     h->mb.b_interlaced = h->param.b_interlaced;
872
873     CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
874     CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
875     CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
876     CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
877
878     /* 0 -> 3 top(4), 4 -> 6 : left(3) */
879     CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 7 * sizeof(int8_t) );
880
881     /* all coeffs */
882     CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
883     CHECKED_MALLOC( h->mb.nnz_backup, h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t) );
884
885     if( h->param.b_cabac )
886     {
887         CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
888         CHECKED_MALLOC( h->mb.mvd[0], 2*16 * i_mb_count * sizeof(int16_t) );
889         CHECKED_MALLOC( h->mb.mvd[1], 2*16 * i_mb_count * sizeof(int16_t) );
890     }
891
892     for( i=0; i<2; i++ )
893     {
894         int i_refs = X264_MIN(16, (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid) << h->param.b_interlaced;
895         for( j=0; j < i_refs; j++ )
896             CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
897     }
898
899     for( i=0; i<=h->param.b_interlaced; i++ )
900         for( j=0; j<3; j++ )
901         {
902             CHECKED_MALLOC( h->mb.intra_border_backup[i][j], h->fdec->i_stride[j] );
903             h->mb.intra_border_backup[i][j] += 8;
904         }
905
906     /* init with not available (for top right idx=7,15) */
907     memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
908     memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
909
910     return 0;
911 fail: return -1;
912 }
913 void x264_macroblock_cache_end( x264_t *h )
914 {
915     int i, j;
916     for( i=0; i<=h->param.b_interlaced; i++ )
917         for( j=0; j<3; j++ )
918             x264_free( h->mb.intra_border_backup[i][j] - 8 );
919     for( i=0; i<2; i++ )
920     {
921         int i_refs = i ? 1 + h->param.b_bframe_pyramid : h->param.i_frame_reference;
922         for( j=0; j < i_refs; j++ )
923             x264_free( h->mb.mvr[i][j] );
924     }
925     if( h->param.b_cabac )
926     {
927         x264_free( h->mb.chroma_pred_mode );
928         x264_free( h->mb.mvd[0] );
929         x264_free( h->mb.mvd[1] );
930     }
931     x264_free( h->mb.intra4x4_pred_mode );
932     x264_free( h->mb.non_zero_count );
933     x264_free( h->mb.nnz_backup );
934     x264_free( h->mb.mb_transform_size );
935     x264_free( h->mb.skipbp );
936     x264_free( h->mb.cbp );
937     x264_free( h->mb.qp );
938 }
939 void x264_macroblock_slice_init( x264_t *h )
940 {
941     int i, j;
942
943     h->mb.mv[0] = h->fdec->mv[0];
944     h->mb.mv[1] = h->fdec->mv[1];
945     h->mb.ref[0] = h->fdec->ref[0];
946     h->mb.ref[1] = h->fdec->ref[1];
947     h->mb.type = h->fdec->mb_type;
948
949     h->fdec->i_ref[0] = h->i_ref0;
950     h->fdec->i_ref[1] = h->i_ref1;
951     for( i = 0; i < h->i_ref0; i++ )
952         h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
953     if( h->sh.i_type == SLICE_TYPE_B )
954     {
955         for( i = 0; i < h->i_ref1; i++ )
956             h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
957
958         h->mb.map_col_to_list0[-1] = -1;
959         h->mb.map_col_to_list0[-2] = -2;
960         for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
961         {
962             int poc = h->fref1[0]->ref_poc[0][i];
963             h->mb.map_col_to_list0[i] = -2;
964             for( j = 0; j < h->i_ref0; j++ )
965                 if( h->fref0[j]->i_poc == poc )
966                 {
967                     h->mb.map_col_to_list0[i] = j;
968                     break;
969                 }
970         }
971     }
972     if( h->sh.i_type == SLICE_TYPE_P )
973         memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
974 }
975
976 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
977 {
978     int stride_y  = fenc->i_stride[0];
979     int stride_uv = fenc->i_stride[1];
980     int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
981     int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
982     h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
983                          fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
984 }
985
986 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
987 {
988     int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
989     int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
990     int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
991     int i_top_y = i_mb_y - (1 << h->mb.b_interlaced);
992     int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x;
993     int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x;
994     int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x;
995     int i_left_xy = -1;
996     int i_top_type = -1;    /* gcc warn */
997     int i_left_type= -1;
998
999     int i;
1000
1001     assert( h->mb.i_b8_stride == 2*h->mb.i_mb_stride );
1002     assert( h->mb.i_b4_stride == 4*h->mb.i_mb_stride );
1003
1004     /* init index */
1005     h->mb.i_mb_x = i_mb_x;
1006     h->mb.i_mb_y = i_mb_y;
1007     h->mb.i_mb_xy = i_mb_xy;
1008     h->mb.i_b8_xy = i_mb_8x8;
1009     h->mb.i_b4_xy = i_mb_4x4;
1010     h->mb.i_mb_top_xy = i_top_xy;
1011     h->mb.i_neighbour = 0;
1012
1013     /* load cache */
1014     if( i_top_xy >= h->sh.i_first_mb )
1015     {
1016         h->mb.i_mb_type_top =
1017         i_top_type= h->mb.type[i_top_xy];
1018
1019         h->mb.i_neighbour |= MB_TOP;
1020
1021         /* load intra4x4 */
1022         h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
1023         h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
1024         h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
1025         h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
1026
1027         /* load non_zero_count */
1028         h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
1029         h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
1030         h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
1031         h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
1032
1033         h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
1034         h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
1035
1036         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
1037         h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
1038     }
1039     else
1040     {
1041         h->mb.i_mb_type_top = -1;
1042         
1043         /* load intra4x4 */
1044         h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
1045         h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
1046         h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
1047         h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
1048
1049         /* load non_zero_count */
1050         h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
1051         h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
1052         h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
1053         h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
1054         h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
1055         h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1056         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1057         h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1058
1059     }
1060
1061     if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1062     {
1063         i_left_xy = i_mb_xy - 1;
1064         h->mb.i_mb_type_left =
1065         i_left_type = h->mb.type[i_left_xy];
1066
1067         h->mb.i_neighbour |= MB_LEFT;
1068
1069         /* load intra4x4 */
1070         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1071         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1072         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1073         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1074
1075         /* load non_zero_count */
1076         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
1077         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1078         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
1079         h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1080
1081         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1082         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1083
1084         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1085         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1086     }
1087     else
1088     {
1089         h->mb.i_mb_type_left = -1;
1090
1091         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1092         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1093         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1094         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1095
1096         /* load non_zero_count */
1097         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1098         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1099         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1100         h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1101         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1102         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1103         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1104         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1105     }
1106
1107     if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1108     {
1109         h->mb.i_neighbour |= MB_TOPRIGHT;
1110         h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1111     }
1112     else
1113         h->mb.i_mb_type_topright = -1;
1114     if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1115     {
1116         h->mb.i_neighbour |= MB_TOPLEFT;
1117         h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1118     }
1119     else
1120         h->mb.i_mb_type_topleft = -1;
1121
1122     if( h->pps->b_transform_8x8_mode )
1123     {
1124         h->mb.cache.i_neighbour_transform_size =
1125             ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1126           + ( i_top_type  >= 0 && h->mb.mb_transform_size[i_top_xy]  );
1127     }
1128
1129     if( h->sh.b_mbaff )
1130     {
1131         h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1132         h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1133         h->mb.cache.i_neighbour_interlaced =
1134             !!(h->mb.i_neighbour & MB_LEFT)
1135           + !!(h->mb.i_neighbour & MB_TOP);
1136     }
1137
1138     /* fdec:      fenc:
1139      * yyyyyyy
1140      * yYYYY      YYYY
1141      * yYYYY      YYYY
1142      * yYYYY      YYYY
1143      * yYYYY      YYYY
1144      * uuu vvv    UUVV
1145      * uUU vVV    UUVV
1146      * uUU vVV
1147      */
1148     h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
1149     h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
1150     h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
1151     h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
1152     h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
1153     h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
1154
1155     /* load picture pointers */
1156     for( i = 0; i < 3; i++ )
1157     {
1158         const int w = (i == 0 ? 16 : 8);
1159         const int i_stride = h->fdec->i_stride[i];
1160         const int i_stride2 = i_stride << h->mb.b_interlaced;
1161         const int i_pix_offset = h->mb.b_interlaced
1162                                ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride
1163                                : w * (i_mb_x + i_mb_y * i_stride);
1164         int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
1165         const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
1166         const uint8_t *intra_fdec = &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
1167         x264_frame_t **fref[2] = { h->fref0, h->fref1 };
1168         int j, k, l;
1169
1170         if( h->mb.b_interlaced )
1171             ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
1172
1173         h->mb.pic.i_stride[i] = i_stride2;
1174
1175         h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
1176             &h->fenc->plane[i][i_pix_offset], i_stride2, w );
1177         memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
1178         for( j = 0; j < w; j++ )
1179             h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
1180
1181         for( l=0; l<2; l++ )
1182         {
1183             for( j=0; j<h->mb.pic.i_fref[l]; j++ )
1184             {
1185                 h->mb.pic.p_fref[l][j][i==0 ? 0:i+3] = &fref[l][j >> h->mb.b_interlaced]->plane[i][ref_pix_offset[j&1]];
1186                 if( i == 0 )
1187                     for( k = 1; k < 4; k++ )
1188                         h->mb.pic.p_fref[l][j][k] = &fref[l][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
1189             }
1190         }
1191     }
1192
1193     if( h->fdec->integral )
1194     {
1195         assert( !h->mb.b_interlaced );
1196         for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
1197             h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1198         for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
1199             h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1200     }
1201
1202     x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
1203
1204     /* load ref/mv/mvd */
1205     if( h->sh.i_type != SLICE_TYPE_I )
1206     {
1207         const int s8x8 = h->mb.i_b8_stride;
1208         const int s4x4 = h->mb.i_b4_stride;
1209
1210         int i_list;
1211
1212         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1213         {
1214             /*
1215             h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1216             h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1217             h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1218             */
1219
1220             if( h->mb.i_neighbour & MB_TOPLEFT )
1221             {
1222                 const int i8 = x264_scan8[0] - 1 - 1*8;
1223                 const int ir = i_top_8x8 - 1;
1224                 const int iv = i_top_4x4 - 1;
1225                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
1226                 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1227                 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1228             }
1229             else
1230             {
1231                 const int i8 = x264_scan8[0] - 1 - 1*8;
1232                 h->mb.cache.ref[i_list][i8] = -2;
1233                 h->mb.cache.mv[i_list][i8][0] = 0;
1234                 h->mb.cache.mv[i_list][i8][1] = 0;
1235             }
1236
1237             if( h->mb.i_neighbour & MB_TOP )
1238             {
1239                 const int i8 = x264_scan8[0] - 8;
1240                 const int ir = i_top_8x8;
1241                 const int iv = i_top_4x4;
1242                 h->mb.cache.ref[i_list][i8+0] =
1243                 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1244                 h->mb.cache.ref[i_list][i8+2] =
1245                 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1246
1247                 for( i = 0; i < 4; i++ )
1248                 {
1249                     h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
1250                     h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
1251                 }
1252             }
1253             else
1254             {
1255                 const int i8 = x264_scan8[0] - 8;
1256                 for( i = 0; i < 4; i++ )
1257                 {
1258                     h->mb.cache.ref[i_list][i8+i] = -2;
1259                     h->mb.cache.mv[i_list][i8+i][0] =
1260                     h->mb.cache.mv[i_list][i8+i][1] = 0;
1261                 }
1262             }
1263
1264             if( h->mb.i_neighbour & MB_TOPRIGHT )
1265             {
1266                 const int i8 = x264_scan8[0] + 4 - 1*8;
1267                 const int ir = i_top_8x8 + 2;
1268                 const int iv = i_top_4x4 + 4;
1269                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
1270                 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1271                 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1272             }
1273             else
1274             {
1275                 const int i8 = x264_scan8[0] + 4 - 1*8;
1276                 h->mb.cache.ref[i_list][i8] = -2;
1277                 h->mb.cache.mv[i_list][i8][0] = 0;
1278                 h->mb.cache.mv[i_list][i8][1] = 0;
1279             }
1280
1281             if( h->mb.i_neighbour & MB_LEFT )
1282             {
1283                 const int i8 = x264_scan8[0] - 1;
1284                 const int ir = i_mb_8x8 - 1;
1285                 const int iv = i_mb_4x4 - 1;
1286                 h->mb.cache.ref[i_list][i8+0*8] =
1287                 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1288                 h->mb.cache.ref[i_list][i8+2*8] =
1289                 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1290
1291                 for( i = 0; i < 4; i++ )
1292                 {
1293                     h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
1294                     h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
1295                 }
1296             }
1297             else
1298             {
1299                 const int i8 = x264_scan8[0] - 1;
1300                 for( i = 0; i < 4; i++ )
1301                 {
1302                     h->mb.cache.ref[i_list][i8+i*8] = -2;
1303                     h->mb.cache.mv[i_list][i8+i*8][0] =
1304                     h->mb.cache.mv[i_list][i8+i*8][1] = 0;
1305                 }
1306             }
1307
1308             if( h->param.b_cabac )
1309             {
1310                 if( i_top_type >= 0 )
1311                 {
1312                     const int i8 = x264_scan8[0] - 8;
1313                     const int iv = i_top_4x4;
1314                     for( i = 0; i < 4; i++ )
1315                     {
1316                         h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
1317                         h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
1318                     }
1319                 }
1320                 else
1321                 {
1322                     const int i8 = x264_scan8[0] - 8;
1323                     for( i = 0; i < 4; i++ )
1324                     {
1325                         h->mb.cache.mvd[i_list][i8+i][0] =
1326                         h->mb.cache.mvd[i_list][i8+i][1] = 0;
1327                     }
1328                 }
1329
1330                 if( i_left_type >= 0 )
1331                 {
1332                     const int i8 = x264_scan8[0] - 1;
1333                     const int iv = i_mb_4x4 - 1;
1334                     for( i = 0; i < 4; i++ )
1335                     {
1336                         h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
1337                         h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
1338                     }
1339                 }
1340                 else
1341                 {
1342                     const int i8 = x264_scan8[0] - 1;
1343                     for( i = 0; i < 4; i++ )
1344                     {
1345                         h->mb.cache.mvd[i_list][i8+i*8][0] =
1346                         h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
1347                     }
1348                 }
1349             }
1350         }
1351
1352         /* load skip */
1353         if( h->sh.i_type == SLICE_TYPE_B && h->param.b_cabac )
1354         {
1355             memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1356             if( i_left_type >= 0 )
1357             {
1358                 h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
1359                 h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
1360             }
1361             if( i_top_type >= 0 )
1362             {
1363                 h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
1364                 h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
1365             }
1366         }
1367
1368         if( h->sh.i_type == SLICE_TYPE_P )
1369             x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1370     }
1371
1372     h->mb.i_neighbour4[0] =
1373     h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1374                             | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0);
1375     h->mb.i_neighbour4[4] =
1376     h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1377     h->mb.i_neighbour4[2] =
1378     h->mb.i_neighbour4[8] =
1379     h->mb.i_neighbour4[10] =
1380     h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1381     h->mb.i_neighbour4[3] =
1382     h->mb.i_neighbour4[7] =
1383     h->mb.i_neighbour4[11] =
1384     h->mb.i_neighbour4[13] =
1385     h->mb.i_neighbour4[15] =
1386     h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
1387     h->mb.i_neighbour4[5] =
1388     h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour & MB_TOPRIGHT)
1389                             | ((h->mb.i_neighbour & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1390     h->mb.i_neighbour4[6] =
1391     h->mb.i_neighbour4[9] =
1392     h->mb.i_neighbour4[12] =
1393     h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
1394 }
1395
1396 void x264_macroblock_cache_save( x264_t *h )
1397 {
1398     const int i_mb_xy = h->mb.i_mb_xy;
1399     const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1400     const int s8x8 = h->mb.i_b8_stride;
1401     const int s4x4 = h->mb.i_b4_stride;
1402     const int i_mb_4x4 = h->mb.i_b4_xy;
1403     const int i_mb_8x8 = h->mb.i_b8_xy;
1404
1405     int i;
1406
1407     for( i = 0; i < 3; i++ )
1408     {
1409         int w = i ? 8 : 16;
1410         int i_stride = h->fdec->i_stride[i];
1411         int i_stride2 = i_stride << h->mb.b_interlaced;
1412         int i_pix_offset = h->mb.b_interlaced
1413                          ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1414                          : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1415         h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1416             &h->fdec->plane[i][i_pix_offset], i_stride2,
1417             h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1418     }
1419
1420     x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1421
1422     h->mb.type[i_mb_xy] = i_mb_type;
1423
1424     if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1425         h->mb.i_qp = h->mb.i_last_qp;
1426     h->mb.qp[i_mb_xy] = h->mb.i_qp;
1427
1428     h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1429     h->mb.i_last_qp = h->mb.i_qp;
1430     h->mb.i_mb_prev_xy = h->mb.i_mb_xy;
1431
1432     /* save intra4x4 */
1433     if( i_mb_type == I_4x4 )
1434     {
1435         h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1436         h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
1437         h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
1438         h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
1439         h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
1440         h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
1441         h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
1442     }
1443     else
1444     {
1445         h->mb.intra4x4_pred_mode[i_mb_xy][0] =
1446         h->mb.intra4x4_pred_mode[i_mb_xy][1] =
1447         h->mb.intra4x4_pred_mode[i_mb_xy][2] =
1448         h->mb.intra4x4_pred_mode[i_mb_xy][3] =
1449         h->mb.intra4x4_pred_mode[i_mb_xy][4] =
1450         h->mb.intra4x4_pred_mode[i_mb_xy][5] =
1451         h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
1452     }
1453
1454     if( i_mb_type == I_PCM )
1455     {
1456         h->mb.cbp[i_mb_xy] = 0x72f;   /* all set */
1457         for( i = 0; i < 16 + 2*4; i++ )
1458         {
1459             h->mb.non_zero_count[i_mb_xy][i] = 16;
1460         }
1461     }
1462     else
1463     {
1464         /* save non zero count */
1465         for( i = 0; i < 16 + 2*4; i++ )
1466         {
1467             h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
1468         }
1469     }
1470
1471     if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1472         h->mb.b_transform_8x8 = 0;
1473     h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1474
1475     if( !IS_INTRA( i_mb_type ) )
1476     {
1477         int i_list;
1478         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1479         {
1480             int y,x;
1481
1482             h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
1483             h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
1484             h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
1485             h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
1486
1487             for( y = 0; y < 4; y++ )
1488             {
1489                 for( x = 0; x < 4; x++ )
1490                 {
1491                     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];
1492                     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];
1493                 }
1494             }
1495         }
1496     }
1497     else
1498     {
1499         int i_list;
1500         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1501         {
1502             int y,x;
1503
1504             h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
1505             h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
1506             h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
1507             h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
1508
1509             for( y = 0; y < 4; y++ )
1510             {
1511                 for( x = 0; x < 4; x++ )
1512                 {
1513                     h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1514                     h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1515                 }
1516             }
1517         }
1518     }
1519
1520     if( h->param.b_cabac )
1521     {
1522         if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1523             h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1524         else
1525             h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1526
1527         if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1528         {
1529             int i_list;
1530             for( i_list  = 0; i_list < 2; i_list++ )
1531             {
1532                 const int s4x4 = 4 * h->mb.i_mb_stride;
1533                 int y,x;
1534                 for( y = 0; y < 4; y++ )
1535                 {
1536                     for( x = 0; x < 4; x++ )
1537                     {
1538                         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];
1539                         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];
1540                     }
1541                 }
1542             }
1543         }
1544         else
1545         {
1546             int i_list;
1547             for( i_list  = 0; i_list < 2; i_list++ )
1548             {
1549                 const int s4x4 = 4 * h->mb.i_mb_stride;
1550                 int y,x;
1551                 for( y = 0; y < 4; y++ )
1552                 {
1553                     for( x = 0; x < 4; x++ )
1554                     {
1555                         h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1556                         h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1557                     }
1558                 }
1559             }
1560         }
1561         if( h->sh.i_type == SLICE_TYPE_B )
1562         {
1563             if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1564                 h->mb.skipbp[i_mb_xy] = 0xf;
1565             else if( i_mb_type == B_8x8 )
1566             {
1567                 int skipbp = 0;
1568                 for( i = 0; i < 4; i++ )
1569                     skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1570                 h->mb.skipbp[i_mb_xy] = skipbp;
1571             }
1572             else
1573                 h->mb.skipbp[i_mb_xy] = 0;
1574         }
1575     }
1576 }
1577
1578 void x264_macroblock_bipred_init( x264_t *h )
1579 {
1580     int i_ref0, i_ref1;
1581     for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1582     {
1583         int poc0 = h->fref0[i_ref0]->i_poc;
1584         for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1585         {
1586             int dist_scale_factor;
1587             int poc1 = h->fref1[i_ref1]->i_poc;
1588             int td = x264_clip3( poc1 - poc0, -128, 127 );
1589             if( td == 0 /* || pic0 is a long-term ref */ )
1590                 dist_scale_factor = 256;
1591             else
1592             {
1593                 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1594                 int tx = (16384 + (abs(td) >> 1)) / td;
1595                 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1596             }
1597             h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1598
1599             dist_scale_factor >>= 2;
1600             if( h->param.analyse.b_weighted_bipred
1601                   && dist_scale_factor >= -64
1602                   && dist_scale_factor <= 128 )
1603                 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1604             else
1605                 h->mb.bipred_weight[i_ref0][i_ref1] = 32;
1606         }
1607     }
1608     if( h->sh.b_mbaff )
1609     {
1610         for( i_ref0 = 2*h->i_ref0-1; i_ref0 >= 0; i_ref0-- )
1611             for( i_ref1 = 2*h->i_ref1-1; i_ref1 >= 0; i_ref1-- )
1612                 h->mb.bipred_weight[i_ref0][i_ref1] = h->mb.bipred_weight[i_ref0>>1][i_ref1>>1];
1613     }
1614 }