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