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