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