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