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