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