]> git.sesse.net Git - x264/blob - common/macroblock.c
a2f176fc154c48f68392d5a85999df93b527f90c
[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 <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "common.h"
29 #include "macroblock.h"
30
31 static const uint8_t block_idx_x[16] =
32 {
33     0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
34 };
35 static const uint8_t block_idx_y[16] =
36 {
37     0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
38 };
39 static const uint8_t block_idx_xy[4][4] =
40 {
41     { 0, 2, 8,  10},
42     { 1, 3, 9,  11},
43     { 4, 6, 12, 14},
44     { 5, 7, 13, 15}
45 };
46
47 static const int dequant_mf[6][4][4] =
48 {
49     { {10, 13, 10, 13}, {13, 16, 13, 16}, {10, 13, 10, 13}, {13, 16, 13, 16} },
50     { {11, 14, 11, 14}, {14, 18, 14, 18}, {11, 14, 11, 14}, {14, 18, 14, 18} },
51     { {13, 16, 13, 16}, {16, 20, 16, 20}, {13, 16, 13, 16}, {16, 20, 16, 20} },
52     { {14, 18, 14, 18}, {18, 23, 18, 23}, {14, 18, 14, 18}, {18, 23, 18, 23} },
53     { {16, 20, 16, 20}, {20, 25, 20, 25}, {16, 20, 16, 20}, {20, 25, 20, 25} },
54     { {18, 23, 18, 23}, {23, 29, 23, 29}, {18, 23, 18, 23}, {23, 29, 23, 29} }
55 };
56
57 #if 0
58 static const int i_chroma_qp_table[52] =
59 {
60      0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
61     10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
62     20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
63     29, 30, 31, 32, 32, 33, 34, 34, 35, 35,
64     36, 36, 37, 37, 37, 38, 38, 38, 39, 39,
65     39, 39
66 };
67 #endif
68
69 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
70 {
71     const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
72     const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
73     const int m  = X264_MIN( ma, mb );
74
75     if( m < 0 )
76         return I_PRED_4x4_DC;
77
78     return m;
79 }
80
81 int x264_mb_predict_non_zero_code( x264_t *h, int idx )
82 {
83     const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1];
84     const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8];
85
86     int i_ret = za + zb;
87
88     if( i_ret < 0x80 )
89     {
90         i_ret = ( i_ret + 1 ) >> 1;
91     }
92     return i_ret & 0x7f;
93 }
94
95 /****************************************************************************
96  * Scan and Quant functions
97  ****************************************************************************/
98 void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int i_qscale )
99 {
100     const int i_qbits = i_qscale/6 - 1;
101
102     if( i_qbits >= 0 )
103     {
104         const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
105
106         dct[0][0] = dct[0][0] * i_dmf;
107         dct[0][1] = dct[0][1] * i_dmf;
108         dct[1][0] = dct[1][0] * i_dmf;
109         dct[1][1] = dct[1][1] * i_dmf;
110     }
111     else
112     {
113         const int i_dmf = dequant_mf[i_qscale%6][0][0];
114
115         dct[0][0] = ( dct[0][0] * i_dmf ) >> 1;
116         dct[0][1] = ( dct[0][1] * i_dmf ) >> 1;
117         dct[1][0] = ( dct[1][0] * i_dmf ) >> 1;
118         dct[1][1] = ( dct[1][1] * i_dmf ) >> 1;
119     }
120 }
121
122 void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int i_qscale )
123 {
124     const int i_qbits = i_qscale/6 - 2;
125     int x,y;
126
127     if( i_qbits >= 0 )
128     {
129         const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
130
131         for( y = 0; y < 4; y++ )
132         {
133             for( x = 0; x < 4; x++ )
134             {
135                 dct[y][x] = dct[y][x] * i_dmf;
136             }
137         }
138     }
139     else
140     {
141         const int i_dmf = dequant_mf[i_qscale%6][0][0];
142         const int f = 1 << ( 1 + i_qbits );
143
144         for( y = 0; y < 4; y++ )
145         {
146             for( x = 0; x < 4; x++ )
147             {
148                 dct[y][x] = ( dct[y][x] * i_dmf + f ) >> (-i_qbits);
149             }
150         }
151     }
152 }
153
154 void x264_mb_dequant_4x4( int16_t dct[4][4], int i_qscale )
155 {
156     const int i_mf = i_qscale%6;
157     const int i_qbits = i_qscale/6;
158     int y;
159
160     for( y = 0; y < 4; y++ )
161     {
162         dct[y][0] = ( dct[y][0] * dequant_mf[i_mf][y][0] ) << i_qbits;
163         dct[y][1] = ( dct[y][1] * dequant_mf[i_mf][y][1] ) << i_qbits;
164         dct[y][2] = ( dct[y][2] * dequant_mf[i_mf][y][2] ) << i_qbits;
165         dct[y][3] = ( dct[y][3] * dequant_mf[i_mf][y][3] ) << i_qbits;
166     }
167 }
168
169 static inline int x264_median( int a, int b, int c )
170 {
171     int min = a, max =a;
172     if( b < min )
173         min = b;
174     else
175         max = b;    /* no need to do 'b > max' (more consuming than always doing affectation) */
176
177     if( c < min )
178         min = c;
179     else if( c > max )
180         max = c;
181
182     return a + b + c - min - max;
183 }
184
185 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
186 {
187     const int i8 = x264_scan8[idx];
188     const int i_ref= h->mb.cache.ref[i_list][i8];
189     int     i_refa = h->mb.cache.ref[i_list][i8 - 1];
190     int16_t *mv_a  = h->mb.cache.mv[i_list][i8 - 1];
191     int     i_refb = h->mb.cache.ref[i_list][i8 - 8];
192     int16_t *mv_b  = h->mb.cache.mv[i_list][i8 - 8];
193     int     i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
194     int16_t *mv_c  = h->mb.cache.mv[i_list][i8 - 8 + i_width];
195
196     int i_count;
197
198     if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
199     {
200         i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
201         mv_c   = h->mb.cache.mv[i_list][i8 - 8 - 1];
202     }
203
204     if( h->mb.i_partition == D_16x8 )
205     {
206         if( idx == 0 && i_refb == i_ref )
207         {
208             mvp[0] = mv_b[0];
209             mvp[1] = mv_b[1];
210             return;
211         }
212         else if( idx != 0 && i_refa == i_ref )
213         {
214             mvp[0] = mv_a[0];
215             mvp[1] = mv_a[1];
216             return;
217         }
218     }
219     else if( h->mb.i_partition == D_8x16 )
220     {
221         if( idx == 0 && i_refa == i_ref )
222         {
223             mvp[0] = mv_a[0];
224             mvp[1] = mv_a[1];
225             return;
226         }
227         else if( idx != 0 && i_refc == i_ref )
228         {
229             mvp[0] = mv_c[0];
230             mvp[1] = mv_c[1];
231             return;
232         }
233     }
234
235     i_count = 0;
236     if( i_refa == i_ref ) i_count++;
237     if( i_refb == i_ref ) i_count++;
238     if( i_refc == i_ref ) i_count++;
239
240     if( i_count > 1 )
241     {
242         mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
243         mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
244     }
245     else if( i_count == 1 )
246     {
247         if( i_refa == i_ref )
248         {
249             mvp[0] = mv_a[0];
250             mvp[1] = mv_a[1];
251         }
252         else if( i_refb == i_ref )
253         {
254             mvp[0] = mv_b[0];
255             mvp[1] = mv_b[1];
256         }
257         else
258         {
259             mvp[0] = mv_c[0];
260             mvp[1] = mv_c[1];
261         }
262     }
263     else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
264     {
265         mvp[0] = mv_a[0];
266         mvp[1] = mv_a[1];
267     }
268     else
269     {
270         mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
271         mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
272     }
273 }
274
275 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int mvp[2] )
276 {
277     int     i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
278     int16_t *mv_a  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
279     int     i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
280     int16_t *mv_b  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
281     int     i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
282     int16_t *mv_c  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
283
284     int i_count;
285
286     if( i_refc == -2 )
287     {
288         i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
289         mv_c   = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
290     }
291
292     i_count = 0;
293     if( i_refa == i_ref ) i_count++;
294     if( i_refb == i_ref ) i_count++;
295     if( i_refc == i_ref ) i_count++;
296
297     if( i_count > 1 )
298     {
299         mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
300         mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
301     }
302     else if( i_count == 1 )
303     {
304         if( i_refa == i_ref )
305         {
306             mvp[0] = mv_a[0];
307             mvp[1] = mv_a[1];
308         }
309         else if( i_refb == i_ref )
310         {
311             mvp[0] = mv_b[0];
312             mvp[1] = mv_b[1];
313         }
314         else
315         {
316             mvp[0] = mv_c[0];
317             mvp[1] = mv_c[1];
318         }
319     }
320     else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
321     {
322         mvp[0] = mv_a[0];
323         mvp[1] = mv_a[1];
324     }
325     else
326     {
327         mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
328         mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
329     }
330 }
331
332
333 void x264_mb_predict_mv_pskip( x264_t *h, int mv[2] )
334 {
335     int     i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
336     int     i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
337     int16_t *mv_a  = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
338     int16_t *mv_b  = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
339
340     if( i_refa == -2 || i_refb == -2 ||
341         ( i_refa == 0 && mv_a[0] == 0 && mv_a[1] == 0 ) ||
342         ( i_refb == 0 && mv_b[0] == 0 && mv_b[1] == 0 ) )
343     {
344         mv[0] = mv[1] = 0;
345     }
346     else
347     {
348         x264_mb_predict_mv_16x16( h, 0, 0, mv );
349     }
350 }
351
352 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
353 {
354     int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
355     int i_mb_8x8 =  4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
356     int i;
357     
358     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
359     
360     /* FIXME: optimize per block size */
361     for( i = 0; i < 4; i++ )
362     {
363         const int x8 = 2*(i%2);
364         const int y8 = 2*(i/2);
365         /* TODO: MapColToList0 */
366         const int i_ref = h->fref1[0]->ref[0][ i_mb_8x8 + x8/2 + y8 * h->mb.i_mb_stride ];
367
368         if( i_ref == -1 )
369         {
370             x264_macroblock_cache_ref( h, x8, y8, 2, 2, 0, 0 );
371             x264_macroblock_cache_mv(  h, x8, y8, 2, 2, 0, 0, 0 );
372             x264_macroblock_cache_mv(  h, x8, y8, 2, 2, 1, 0, 0 );
373         }
374         else
375         {
376             int poc0 = h->fref0[i_ref]->i_poc;
377             int poc1 = h->fref1[0]->i_poc;
378             int td = x264_clip3( poc1 - poc0, -128, 127 );
379             int dist_scale_factor;
380             int x4, y4;
381             if( td == 0 /* || pic0 is a long-term ref */ )
382                 dist_scale_factor = 256;
383             else
384             {
385                 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
386                 int tx = (16384 + (abs(td) >> 1)) / td;
387                 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
388             }
389
390             x264_macroblock_cache_ref( h, x8, y8, 2, 2, 0, i_ref );
391
392             for( y4 = y8; y4 < y8+2; y4++ )
393                 for( x4 = x8; x4 < x8+2; x4++ )
394                 {
395                     const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * 4 * h->mb.i_mb_stride ];
396                     int mv_l0[2];
397                     mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
398                     mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
399                     x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, mv_l0[0], mv_l0[1] );
400                     x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
401                 }
402         }
403     }
404
405     return 1;
406 }
407
408 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
409 {
410     int ref[2];
411     int mv[2][2];
412     int i_list;
413     int i8, i4;
414     const int8_t *l1ref = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
415     const int16_t (*l1mv)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
416
417     for( i_list=0; i_list<2; i_list++ )
418     {
419         int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
420         int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
421         int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
422         if( i_refc == -2 )
423             i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
424
425         ref[i_list] = i_refa;
426         if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
427             ref[i_list] = i_refb;
428         if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
429             ref[i_list] = i_refc;
430         if( ref[i_list] < 0 )
431             ref[i_list] = -1;
432     }
433
434     if( ref[0] < 0 && ref[1] < 0 )
435     {
436         ref[0] = 
437         ref[1] = 0;
438         mv[0][0] = 
439         mv[0][1] = 
440         mv[1][0] = 
441         mv[1][1] = 0;
442     }
443     else
444     {
445         for( i_list=0; i_list<2; i_list++ )
446         {
447             if( ref[i_list] >= 0 )
448                 x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
449             else
450                 mv[i_list][0] = mv[i_list][1] = 0;
451         }
452     }
453
454     /* FIXME: clip mv ? */
455     
456     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
457     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
458     x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 0, mv[0][0], mv[0][1] );
459     x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 1, mv[1][0], mv[1][1] );
460
461     /* col_zero_flag */
462     for( i8=0; i8<4; i8++ )
463     {
464         const int x8 = i8%2;
465         const int y8 = i8/2;
466         if( l1ref[ x8 + y8 * h->mb.i_b8_stride ] == 0 )
467         {
468             for( i4=0; i4<4; i4++ )
469             {
470                 const int x4 = i4%2 + 2*x8;
471                 const int y4 = i4/2 + 2*y8;
472                 const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
473                 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
474                 {
475                     if( ref[0] == 0 )
476                         x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0, 0 );
477                     if( ref[1] == 0 )
478                         x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0, 0 );
479                 }
480             }
481         }
482     }
483
484     return 1;
485 }
486
487 int x264_mb_predict_mv_direct16x16( x264_t *h )
488 {
489     int b_available;
490     if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
491         return 0;
492     else if( h->sh.b_direct_spatial_mv_pred )
493         b_available = x264_mb_predict_mv_direct16x16_spatial( h );
494     else
495         b_available = x264_mb_predict_mv_direct16x16_temporal( h );
496
497     /* cache ref & mv */
498     if( b_available )
499     {
500         int i, l;
501         for( l = 0; l < 2; l++ )
502             for( i = 0; i < 4; i++ )
503                 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
504         memcpy(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
505     }
506
507     return b_available;
508 }
509
510 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
511 {
512     const int x = 2*(idx%2);
513     const int y = 2*(idx/2);
514     int l;
515     x264_macroblock_cache_ref( h, x, y, 2, 2, 0, h->mb.cache.direct_ref[0][idx] );
516     x264_macroblock_cache_ref( h, x, y, 2, 2, 1, h->mb.cache.direct_ref[1][idx] );
517     for( l = 0; l < 2; l++ )
518     {
519         *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]] =
520         *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]];
521         *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]+8] =
522         *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]+8];
523     }
524 }
525
526 /* This just improves encoder performance, it's not part of the spec */
527 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[5][2], int *i_mvc )
528 {
529     int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
530     int i = 0;
531
532     /* temporal */
533     if( h->sh.i_type == SLICE_TYPE_B )
534     {
535         if( h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
536         {
537             int16_t *mvp = h->mb.cache.mv[i_list][x264_scan8[12]];
538             mvc[i][0] = mvp[0];
539             mvc[i][1] = mvp[1];
540             i++;
541         }
542     }
543
544     /* spatial */
545     if( h->mb.i_mb_x > 0 )
546     {
547         int i_mb_l = h->mb.i_mb_xy - 1;
548         mvc[i][0] = mvr[i_mb_l][0];
549         mvc[i][1] = mvr[i_mb_l][1];
550         i++;
551     }
552     if( h->mb.i_mb_y > 0 )
553     {
554         int i_mb_t = h->mb.i_mb_xy - h->mb.i_mb_stride;
555         mvc[i][0] = mvr[i_mb_t][0];
556         mvc[i][1] = mvr[i_mb_t][1];
557         i++;
558
559         if( h->mb.i_mb_x > 0 )
560         {
561             mvc[i][0] = mvr[i_mb_t - 1][0];
562             mvc[i][1] = mvr[i_mb_t - 1][1];
563             i++;
564         }
565         if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 )
566         {
567             mvc[i][0] = mvr[i_mb_t + 1][0];
568             mvc[i][1] = mvr[i_mb_t + 1][1];
569             i++;
570         }
571     }
572     *i_mvc = i;
573 }
574
575 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
576 {
577     const int i8 = x264_scan8[0]+x+8*y;
578     const int i_ref = h->mb.cache.ref[0][i8];
579     const int mvx   = h->mb.cache.mv[0][i8][0];
580     const int mvy   = h->mb.cache.mv[0][i8][1];
581
582     h->mc[MC_LUMA]( &h->mb.pic.p_fref[0][i_ref][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
583                     &h->mb.pic.p_fdec[0][4*y * h->mb.pic.i_stride[0]+4*x],           h->mb.pic.i_stride[0],
584                     mvx, mvy, 4*width, 4*height );
585
586     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
587                       &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x],           h->mb.pic.i_stride[1],
588                       mvx, mvy, 2*width, 2*height );
589
590     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
591                       &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x],           h->mb.pic.i_stride[2],
592                       mvx, mvy, 2*width, 2*height );
593 }
594 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
595 {
596     const int i8 = x264_scan8[0]+x+8*y;
597     const int i_ref = h->mb.cache.ref[1][i8];
598     const int mvx   = h->mb.cache.mv[1][i8][0];
599     const int mvy   = h->mb.cache.mv[1][i8][1];
600
601     h->mc[MC_LUMA]( &h->mb.pic.p_fref[1][i_ref][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
602                     &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x],            h->mb.pic.i_stride[0],
603                     mvx, mvy, 4*width, 4*height );
604
605     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
606                       &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x],           h->mb.pic.i_stride[1],
607                       mvx, mvy, 2*width, 2*height );
608
609     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
610                       &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x],           h->mb.pic.i_stride[2],
611                       mvx, mvy, 2*width, 2*height );
612 }
613
614 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
615 {
616     const int i8 = x264_scan8[0]+x+8*y;
617
618     const int i_ref0 = h->mb.cache.ref[0][i8];
619     const int mvx0   = h->mb.cache.mv[0][i8][0];
620     const int mvy0   = h->mb.cache.mv[0][i8][1];
621
622     const int i_ref1 = h->mb.cache.ref[1][i8];
623     const int mvx1   = h->mb.cache.mv[1][i8][0];
624     const int mvy1   = h->mb.cache.mv[1][i8][1];
625     DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
626     int     i_mode = 0;
627
628     if( width == 4 && height == 4 ) i_mode = PIXEL_16x16;
629     else if( width == 4 && height == 2 ) i_mode = PIXEL_16x8;
630     else if( width == 2 && height == 4 ) i_mode = PIXEL_8x16;
631     else if( width == 2 && height == 2 ) i_mode = PIXEL_8x8;
632     else if( width == 2 && height == 1 ) i_mode = PIXEL_8x4;
633     else if( width == 1 && height == 2 ) i_mode = PIXEL_4x8;
634     else if( width == 1 && height == 1 ) i_mode = PIXEL_4x4;
635
636     h->mc[MC_LUMA]( &h->mb.pic.p_fref[0][i_ref0][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
637                     &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x],             h->mb.pic.i_stride[0],
638                     mvx0, mvy0, 4*width, 4*height );
639     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref0][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
640                       &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x],            h->mb.pic.i_stride[1],
641                       mvx0, mvy0, 2*width, 2*height );
642     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref0][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
643                       &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x],            h->mb.pic.i_stride[2],
644                       mvx0, mvy0, 2*width, 2*height );
645
646
647     h->mc[MC_LUMA]( &h->mb.pic.p_fref[1][i_ref1][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
648                     tmp, 16, mvx1, mvy1, 4*width, 4*height );
649     h->pixf.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0], tmp, 16 );
650
651     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref1][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
652                       tmp, 16, mvx1, mvy1, 2*width, 2*height );
653     h->pixf.avg[i_mode]( &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1], tmp, 16 );
654
655     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref1][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
656                       tmp, 16, mvx1, mvy1, 2*width, 2*height );
657     h->pixf.avg[i_mode]( &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2], tmp, 16 );
658 }
659
660 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
661 {
662     const int i8 = x264_scan8[0] + x + 8*y;
663
664     /* FIXME: optimize based on current block size, not global settings? */
665     if( h->sps->b_direct8x8_inference )
666     {
667         if( h->mb.cache.ref[0][i8] >= 0 )
668             if( h->mb.cache.ref[1][i8] >= 0 )
669                 x264_mb_mc_01xywh( h, x, y, 2, 2 );
670             else
671                 x264_mb_mc_0xywh( h, x, y, 2, 2 );
672         else
673             x264_mb_mc_1xywh( h, x, y, 2, 2 );
674     }
675     else
676     {
677         if( h->mb.cache.ref[0][i8] >= 0 )
678         {
679             if( h->mb.cache.ref[1][i8] >= 0 )
680             {
681                 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
682                 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
683                 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
684                 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
685             }
686             else
687             {
688                 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
689                 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
690                 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
691                 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
692             }
693         }
694         else
695         {
696             x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
697             x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
698             x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
699             x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
700         }
701     }
702 }
703
704 void x264_mb_mc( x264_t *h )
705 {
706     if( h->mb.i_type == P_L0 )
707     {
708         if( h->mb.i_partition == D_16x16 )
709         {
710             x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
711         }
712         else if( h->mb.i_partition == D_16x8 )
713         {
714             x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
715             x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
716         }
717         else if( h->mb.i_partition == D_8x16 )
718         {
719             x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
720             x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
721         }
722     }
723     else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
724     {
725         int i;
726         for( i = 0; i < 4; i++ )
727         {
728             const int x = 2*(i%2);
729             const int y = 2*(i/2);
730             switch( h->mb.i_sub_partition[i] )
731             {
732                 case D_L0_8x8:
733                     x264_mb_mc_0xywh( h, x, y, 2, 2 );
734                     break;
735                 case D_L0_8x4:
736                     x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
737                     x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
738                     break;
739                 case D_L0_4x8:
740                     x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
741                     x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
742                     break;
743                 case D_L0_4x4:
744                     x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
745                     x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
746                     x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
747                     x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
748                     break;
749                 case D_L1_8x8:
750                     x264_mb_mc_1xywh( h, x, y, 2, 2 );
751                     break;
752                 case D_L1_8x4:
753                     x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
754                     x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
755                     break;
756                 case D_L1_4x8:
757                     x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
758                     x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
759                     break;
760                 case D_L1_4x4:
761                     x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
762                     x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
763                     x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
764                     x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
765                     break;
766                 case D_BI_8x8:
767                     x264_mb_mc_01xywh( h, x, y, 2, 2 );
768                     break;
769                 case D_BI_8x4:
770                     x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
771                     x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
772                     break;
773                 case D_BI_4x8:
774                     x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
775                     x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
776                     break;
777                 case D_BI_4x4:
778                     x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
779                     x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
780                     x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
781                     x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
782                     break;
783                 case D_DIRECT_8x8:
784                     x264_mb_mc_direct8x8( h, x, y );
785                     break;
786             }
787         }
788     }
789     else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
790     {
791         int i;
792         for( i = 0; i < 4; i++ )
793         {
794             const int x = 2*(i%2);
795             const int y = 2*(i/2);
796             x264_mb_mc_direct8x8( h, x, y );
797         }
798     }
799     else    /* B_*x* */
800     {
801         int b_list0[2];
802         int b_list1[2];
803
804         int i;
805
806         /* init ref list utilisations */
807         for( i = 0; i < 2; i++ )
808         {
809             b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
810             b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
811         }
812         if( h->mb.i_partition == D_16x16 )
813         {
814             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
815             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
816             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
817         }
818         else if( h->mb.i_partition == D_16x8 )
819         {
820             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
821             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
822             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
823
824             if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
825             else if( b_list0[1] )          x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
826             else if( b_list1[1] )          x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
827         }
828         else if( h->mb.i_partition == D_8x16 )
829         {
830             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
831             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
832             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
833
834             if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
835             else if( b_list0[1] )          x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
836             else if( b_list1[1] )          x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
837         }
838     }
839 }
840
841 void x264_macroblock_cache_init( x264_t *h )
842 {
843     int i, j;
844     int i_mb_count = h->mb.i_mb_count;
845
846     h->mb.i_mb_stride = h->sps->i_mb_width;
847     h->mb.i_b8_stride = h->sps->i_mb_width * 2;
848     h->mb.i_b4_stride = h->sps->i_mb_width * 4;
849
850     h->mb.type= x264_malloc( i_mb_count * sizeof( int8_t) );
851     h->mb.qp  = x264_malloc( i_mb_count * sizeof( int8_t) );
852     h->mb.cbp = x264_malloc( i_mb_count * sizeof( int16_t) );
853     h->mb.skipbp = x264_malloc( i_mb_count * sizeof( int8_t) );
854
855     /* 0 -> 3 top(4), 4 -> 6 : left(3) */
856     h->mb.intra4x4_pred_mode = x264_malloc( i_mb_count * 7 * sizeof( int8_t ) );
857
858     /* all coeffs */
859     h->mb.non_zero_count = x264_malloc( i_mb_count * 24 * sizeof( uint8_t ) );
860
861     if( h->param.b_cabac )
862     {
863         h->mb.chroma_pred_mode = x264_malloc( i_mb_count * sizeof( int8_t) );
864         h->mb.mvd[0] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
865         h->mb.mvd[1] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
866     }
867
868     for( i=0; i<2; i++ )
869         for( j=0; j < ( i ? 1 : h->param.i_frame_reference ); j++ )
870             h->mb.mvr[i][j] = x264_malloc( 2 * i_mb_count * sizeof( int16_t ) );
871
872     /* init with not avaiable (for top right idx=7,15) */
873     memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
874     memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
875 }
876 void x264_macroblock_cache_end( x264_t *h )
877 {
878     int i, j;
879     for( i=0; i<2; i++ )
880         for( j=0; j < ( i ? 1 : h->param.i_frame_reference ); j++ )
881             x264_free( h->mb.mvr[i][j] );
882     if( h->param.b_cabac )
883     {
884         x264_free( h->mb.chroma_pred_mode );
885         x264_free( h->mb.mvd[0] );
886         x264_free( h->mb.mvd[1] );
887     }
888     x264_free( h->mb.intra4x4_pred_mode );
889     x264_free( h->mb.non_zero_count );
890     x264_free( h->mb.skipbp );
891     x264_free( h->mb.cbp );
892     x264_free( h->mb.qp );
893     x264_free( h->mb.type );
894 }
895 void x264_macroblock_slice_init( x264_t *h )
896 {
897     int i;
898
899     h->mb.mv[0] = h->fdec->mv[0];
900     h->mb.mv[1] = h->fdec->mv[1];
901     h->mb.ref[0] = h->fdec->ref[0];
902     h->mb.ref[1] = h->fdec->ref[1];
903
904     h->fdec->i_ref[0] = h->i_ref0;
905     h->fdec->i_ref[1] = h->i_ref1;
906     for( i = 0; i < h->i_ref0; i++ )
907         h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
908     if( h->sh.i_type == SLICE_TYPE_B )
909     {
910         for( i = 0; i < h->i_ref1; i++ )
911             h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
912     }
913 }
914
915
916 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
917 {
918     const int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
919     const int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
920
921     int i_top_xy = -1;
922     int i_left_xy = -1;
923     int i_top_type = -1;    /* gcc warn */
924     int i_left_type= -1;
925
926     int i;
927
928     /* init index */
929     h->mb.i_mb_x = i_mb_x;
930     h->mb.i_mb_y = i_mb_y;
931     h->mb.i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
932     h->mb.i_b8_xy = i_mb_8x8;
933     h->mb.i_b4_xy = i_mb_4x4;
934     h->mb.i_neighbour = 0;
935
936     /* load picture pointers */
937     for( i = 0; i < 3; i++ )
938     {
939         const int w = (i == 0 ? 16 : 8);
940         const int i_stride = h->fdec->i_stride[i];
941         int   j;
942
943         h->mb.pic.i_stride[i] = i_stride;
944
945         h->mb.pic.p_fenc[i] = &h->fenc->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
946
947         h->mb.pic.p_fdec[i] = &h->fdec->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
948
949         for( j = 0; j < h->i_ref0; j++ )
950         {
951             h->mb.pic.p_fref[0][j][i] = &h->fref0[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
952         }
953         for( j = 0; j < h->i_ref1; j++ )
954         {
955             h->mb.pic.p_fref[1][j][i] = &h->fref1[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
956         }
957     }
958
959     /* load cache */
960     if( i_mb_y > 0 )
961     {
962         i_top_xy  = h->mb.i_mb_xy - h->mb.i_mb_stride;
963         i_top_type= h->mb.type[i_top_xy];
964
965         h->mb.i_neighbour |= MB_TOP;
966
967         /* load intra4x4 */
968         h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
969         h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
970         h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
971         h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
972
973         /* load non_zero_count */
974         h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
975         h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
976         h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
977         h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
978
979         h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
980         h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
981
982         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
983         h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
984     }
985     else
986     {
987         /* load intra4x4 */
988         h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
989         h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
990         h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
991         h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
992
993         /* load non_zero_count */
994         h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
995         h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
996         h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
997         h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
998         h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
999         h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1000         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1001         h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1002
1003     }
1004
1005     if( i_mb_x > 0 )
1006     {
1007         i_left_xy  = h->mb.i_mb_xy - 1;
1008         i_left_type= h->mb.type[i_left_xy];
1009
1010         h->mb.i_neighbour |= MB_LEFT;
1011
1012         /* load intra4x4 */
1013         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1014         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1015         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1016         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1017
1018         /* load non_zero_count */
1019         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
1020         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1021         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
1022         h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1023
1024         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1025         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1026
1027         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1028         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1029     }
1030     else
1031     {
1032         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1033         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1034         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1035         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1036
1037         /* load non_zero_count */
1038         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1039         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1040         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1041         h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1042         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1043         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1044         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1045         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1046     }
1047
1048     if( i_mb_y > 0 && i_mb_x < h->sps->i_mb_width - 1 )
1049     {
1050         h->mb.i_neighbour |= MB_TOPRIGHT;
1051     }
1052
1053     /* load ref/mv/mvd */
1054     if( h->sh.i_type != SLICE_TYPE_I )
1055     {
1056         const int s8x8 = h->mb.i_b8_stride;
1057         const int s4x4 = h->mb.i_b4_stride;
1058
1059         int i_top_left_xy   = -1;
1060         int i_top_right_xy  = -1;
1061
1062         int i_list;
1063
1064         if( h->mb.i_mb_y > 0 && h->mb.i_mb_x > 0 )
1065         {
1066             i_top_left_xy   = i_top_xy - 1;
1067         }
1068         if( h->mb.i_mb_y > 0 && h->mb.i_mb_x < h->sps->i_mb_width - 1 )
1069         {
1070             i_top_right_xy = i_top_xy + 1;
1071         }
1072
1073         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1074         {
1075             /*
1076             h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1077             h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1078             h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1079             */
1080
1081             if( i_top_left_xy >= 0 )
1082             {
1083                 const int i8 = x264_scan8[0] - 1 - 1*8;
1084                 const int ir = i_mb_8x8 - s8x8 - 1;
1085                 const int iv = i_mb_4x4 - s4x4 - 1;
1086                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
1087                 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1088                 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1089             }
1090             else
1091             {
1092                 const int i8 = x264_scan8[0] - 1 - 1*8;
1093                 h->mb.cache.ref[i_list][i8] = -2;
1094                 h->mb.cache.mv[i_list][i8][0] = 0;
1095                 h->mb.cache.mv[i_list][i8][1] = 0;
1096             }
1097
1098             if( i_top_xy >= 0 )
1099             {
1100                 const int i8 = x264_scan8[0] - 8;
1101                 const int ir = i_mb_8x8 - s8x8;
1102                 const int iv = i_mb_4x4 - s4x4;
1103
1104                 h->mb.cache.ref[i_list][i8+0] =
1105                 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1106                 h->mb.cache.ref[i_list][i8+2] =
1107                 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1108
1109                 for( i = 0; i < 4; i++ )
1110                 {
1111                     h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
1112                     h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
1113                 }
1114             }
1115             else
1116             {
1117                 const int i8 = x264_scan8[0] - 8;
1118                 for( i = 0; i < 4; i++ )
1119                 {
1120                     h->mb.cache.ref[i_list][i8+i] = -2;
1121                     h->mb.cache.mv[i_list][i8+i][0] =
1122                     h->mb.cache.mv[i_list][i8+i][1] = 0;
1123                 }
1124             }
1125
1126             if( i_top_right_xy >= 0 )
1127             {
1128                 const int i8 = x264_scan8[0] + 4 - 1*8;
1129                 const int ir = i_mb_8x8 - s8x8 + 2;
1130                 const int iv = i_mb_4x4 - s4x4 + 4;
1131
1132                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
1133                 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1134                 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1135             }
1136             else
1137             {
1138                 const int i8 = x264_scan8[0] + 4 - 1*8;
1139                 h->mb.cache.ref[i_list][i8] = -2;
1140                 h->mb.cache.mv[i_list][i8][0] = 0;
1141                 h->mb.cache.mv[i_list][i8][1] = 0;
1142             }
1143
1144             if( i_left_xy >= 0 )
1145             {
1146                 const int i8 = x264_scan8[0] - 1;
1147                 const int ir = i_mb_8x8 - 1;
1148                 const int iv = i_mb_4x4 - 1;
1149
1150                 h->mb.cache.ref[i_list][i8+0*8] =
1151                 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1152                 h->mb.cache.ref[i_list][i8+2*8] =
1153                 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1154
1155                 for( i = 0; i < 4; i++ )
1156                 {
1157                     h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
1158                     h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
1159                 }
1160             }
1161             else
1162             {
1163                 const int i8 = x264_scan8[0] - 1;
1164                 for( i = 0; i < 4; i++ )
1165                 {
1166                     h->mb.cache.ref[i_list][i8+i*8] = -2;
1167                     h->mb.cache.mv[i_list][i8+i*8][0] =
1168                     h->mb.cache.mv[i_list][i8+i*8][1] = 0;
1169                 }
1170             }
1171
1172             if( h->param.b_cabac )
1173             {
1174                 if( i_top_xy >= 0 )
1175                 {
1176                     const int i8 = x264_scan8[0] - 8;
1177                     const int iv = i_mb_4x4 - s4x4;
1178                     for( i = 0; i < 4; i++ )
1179                     {
1180                         h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
1181                         h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
1182                     }
1183                 }
1184                 else
1185                 {
1186                     const int i8 = x264_scan8[0] - 8;
1187                     for( i = 0; i < 4; i++ )
1188                     {
1189                         h->mb.cache.mvd[i_list][i8+i][0] =
1190                         h->mb.cache.mvd[i_list][i8+i][1] = 0;
1191                     }
1192                 }
1193
1194                 if( i_left_xy >= 0 )
1195                 {
1196                     const int i8 = x264_scan8[0] - 1;
1197                     const int iv = i_mb_4x4 - 1;
1198                     for( i = 0; i < 4; i++ )
1199                     {
1200                         h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
1201                         h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
1202                     }
1203                 }
1204                 else
1205                 {
1206                     const int i8 = x264_scan8[0] - 1;
1207                     for( i = 0; i < 4; i++ )
1208                     {
1209                         h->mb.cache.mvd[i_list][i8+i*8][0] =
1210                         h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
1211                     }
1212                 }
1213             }
1214         }
1215
1216         /* load skip */
1217         if( h->param.b_cabac )
1218         {
1219             if( h->sh.i_type == SLICE_TYPE_B )
1220             {
1221                 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1222                 if( i_left_xy >= 0 )
1223                 {
1224                     h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
1225                     h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
1226                 }
1227                 if( i_top_xy >= 0 )
1228                 {
1229                     h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
1230                     h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
1231                 }
1232             }
1233             else if( h->mb.i_mb_xy == 0 && h->sh.i_type == SLICE_TYPE_P )
1234             {
1235                 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1236             }
1237         }
1238     }
1239 }
1240
1241 void x264_macroblock_cache_save( x264_t *h )
1242 {
1243     const int i_mb_xy = h->mb.i_mb_xy;
1244     const int i_mb_type = h->mb.i_type;
1245     const int s8x8 = h->mb.i_b8_stride;
1246     const int s4x4 = h->mb.i_b4_stride;
1247     const int i_mb_4x4 = h->mb.i_b4_xy;
1248     const int i_mb_8x8 = h->mb.i_b8_xy;
1249
1250     int i;
1251
1252     if( IS_SKIP( h->mb.i_type ) )
1253         h->mb.qp[i_mb_xy] = h->mb.i_last_qp;
1254
1255     h->mb.i_last_dqp = h->mb.qp[i_mb_xy] - h->mb.i_last_qp;
1256     h->mb.i_last_qp = h->mb.qp[i_mb_xy];
1257
1258     /* save intra4x4 */
1259     if( i_mb_type == I_4x4 )
1260     {
1261         h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1262         h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
1263         h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
1264         h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
1265         h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
1266         h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
1267         h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
1268     }
1269     else
1270     {
1271         h->mb.intra4x4_pred_mode[i_mb_xy][0] =
1272         h->mb.intra4x4_pred_mode[i_mb_xy][1] =
1273         h->mb.intra4x4_pred_mode[i_mb_xy][2] =
1274         h->mb.intra4x4_pred_mode[i_mb_xy][3] =
1275         h->mb.intra4x4_pred_mode[i_mb_xy][4] =
1276         h->mb.intra4x4_pred_mode[i_mb_xy][5] =
1277         h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
1278     }
1279
1280     if( i_mb_type == I_PCM )
1281     {
1282         h->mb.cbp[i_mb_xy] = 0x72f;   /* all set */
1283         for( i = 0; i < 16 + 2*4; i++ )
1284         {
1285             h->mb.non_zero_count[i_mb_xy][i] = 16;
1286         }
1287     }
1288     else
1289     {
1290         /* save non zero count */
1291         for( i = 0; i < 16 + 2*4; i++ )
1292         {
1293             h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
1294         }
1295     }
1296
1297     if( !IS_INTRA( i_mb_type ) )
1298     {
1299         int i_list;
1300         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1301         {
1302             int y,x;
1303
1304             h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
1305             h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
1306             h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
1307             h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
1308
1309             for( y = 0; y < 4; y++ )
1310             {
1311                 for( x = 0; x < 4; x++ )
1312                 {
1313                     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];
1314                     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];
1315                 }
1316             }
1317         }
1318     }
1319     else
1320     {
1321         int i_list;
1322         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1323         {
1324             int y,x;
1325
1326             h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
1327             h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
1328             h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
1329             h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
1330
1331             for( y = 0; y < 4; y++ )
1332             {
1333                 for( x = 0; x < 4; x++ )
1334                 {
1335                     h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1336                     h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1337                 }
1338             }
1339         }
1340     }
1341
1342     if( h->param.b_cabac )
1343     {
1344         if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1345             h->mb.chroma_pred_mode[i_mb_xy] = h->mb.i_chroma_pred_mode;
1346         else
1347             h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1348
1349         if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1350         {
1351             int i_list;
1352             for( i_list  = 0; i_list < 2; i_list++ )
1353             {
1354                 const int s4x4 = 4 * h->mb.i_mb_stride;
1355                 int y,x;
1356                 for( y = 0; y < 4; y++ )
1357                 {
1358                     for( x = 0; x < 4; x++ )
1359                     {
1360                         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];
1361                         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];
1362                     }
1363                 }
1364             }
1365         }
1366         else
1367         {
1368             int i_list;
1369             for( i_list  = 0; i_list < 2; i_list++ )
1370             {
1371                 const int s4x4 = 4 * h->mb.i_mb_stride;
1372                 int y,x;
1373                 for( y = 0; y < 4; y++ )
1374                 {
1375                     for( x = 0; x < 4; x++ )
1376                     {
1377                         h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1378                         h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1379                     }
1380                 }
1381             }
1382         }
1383         if( h->sh.i_type == SLICE_TYPE_B )
1384         {
1385             if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1386                 h->mb.skipbp[i_mb_xy] = 0xf;
1387             else if( i_mb_type == B_8x8 )
1388             {
1389                 int skipbp = 0;
1390                 for( i = 0; i < 4; i++ )
1391                     skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1392                 h->mb.skipbp[i_mb_xy] = skipbp;
1393             }
1394             else
1395                 h->mb.skipbp[i_mb_xy] = 0;
1396         }
1397     }
1398 }