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