]> git.sesse.net Git - x264/blob - common/macroblock.c
190d726662afe4285de2a7635213ea3defc3ebf4
[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 = -i_qbits; // 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_neighbour & MB_LEFT )
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_neighbour & MB_TOP )
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_neighbour & MB_TOPLEFT && !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         int   j;
943
944         h->mb.pic.i_stride[i] = i_stride;
945
946         h->mb.pic.p_fenc[i] = &h->fenc->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
947         h->mb.pic.p_fdec[i] = &h->fdec->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
948
949         for( j = 0; j < h->i_ref0; j++ )
950         {
951             h->mb.pic.p_fref[0][j][i==0 ? 0:i+3] = &h->fref0[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
952             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] )];
953         }
954         for( j = 0; j < h->i_ref1; j++ )
955         {
956             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 )];
957             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] )];
958         }
959     }
960
961     /* load cache */
962     if( h->mb.i_mb_xy >= h->sh.i_first_mb + h->mb.i_mb_stride )
963     {
964         i_top_xy  = h->mb.i_mb_xy - h->mb.i_mb_stride;
965         h->mb.i_mb_type_top =
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         h->mb.i_mb_type_top = -1;
991         
992         /* load intra4x4 */
993         h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
994         h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
995         h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
996         h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
997
998         /* load non_zero_count */
999         h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
1000         h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
1001         h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
1002         h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
1003         h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
1004         h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1005         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1006         h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1007
1008     }
1009
1010     if( i_mb_x > 0 && h->mb.i_mb_xy > h->sh.i_first_mb )
1011     {
1012         i_left_xy  = h->mb.i_mb_xy - 1;
1013         h->mb.i_mb_type_left =
1014         i_left_type= h->mb.type[i_left_xy];
1015
1016         h->mb.i_neighbour |= MB_LEFT;
1017
1018         /* load intra4x4 */
1019         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1020         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1021         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1022         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1023
1024         /* load non_zero_count */
1025         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
1026         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1027         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
1028         h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1029
1030         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1031         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1032
1033         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1034         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1035     }
1036     else
1037     {
1038         h->mb.i_mb_type_left = -1;
1039
1040         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1041         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1042         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1043         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1044
1045         /* load non_zero_count */
1046         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1047         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1048         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1049         h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1050         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1051         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1052         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1053         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1054     }
1055
1056     if( i_mb_x < h->sps->i_mb_width - 1 && h->mb.i_mb_xy >= h->sh.i_first_mb + h->mb.i_mb_stride - 1 )
1057         h->mb.i_neighbour |= MB_TOPRIGHT;
1058     if( i_mb_x > 0 && h->mb.i_mb_xy >= h->sh.i_first_mb + h->mb.i_mb_stride + 1 )
1059         h->mb.i_neighbour |= MB_TOPLEFT;
1060
1061     /* load ref/mv/mvd */
1062     if( h->sh.i_type != SLICE_TYPE_I )
1063     {
1064         const int s8x8 = h->mb.i_b8_stride;
1065         const int s4x4 = h->mb.i_b4_stride;
1066
1067         int i_list;
1068
1069         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1070         {
1071             /*
1072             h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1073             h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1074             h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1075             */
1076
1077             if( h->mb.i_neighbour & MB_TOPLEFT )
1078             {
1079                 const int i8 = x264_scan8[0] - 1 - 1*8;
1080                 const int ir = i_mb_8x8 - s8x8 - 1;
1081                 const int iv = i_mb_4x4 - s4x4 - 1;
1082                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
1083                 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1084                 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1085             }
1086             else
1087             {
1088                 const int i8 = x264_scan8[0] - 1 - 1*8;
1089                 h->mb.cache.ref[i_list][i8] = -2;
1090                 h->mb.cache.mv[i_list][i8][0] = 0;
1091                 h->mb.cache.mv[i_list][i8][1] = 0;
1092             }
1093
1094             if( i_top_xy >= 0 )
1095             {
1096                 const int i8 = x264_scan8[0] - 8;
1097                 const int ir = i_mb_8x8 - s8x8;
1098                 const int iv = i_mb_4x4 - s4x4;
1099
1100                 h->mb.cache.ref[i_list][i8+0] =
1101                 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1102                 h->mb.cache.ref[i_list][i8+2] =
1103                 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1104
1105                 for( i = 0; i < 4; i++ )
1106                 {
1107                     h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
1108                     h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
1109                 }
1110             }
1111             else
1112             {
1113                 const int i8 = x264_scan8[0] - 8;
1114                 for( i = 0; i < 4; i++ )
1115                 {
1116                     h->mb.cache.ref[i_list][i8+i] = -2;
1117                     h->mb.cache.mv[i_list][i8+i][0] =
1118                     h->mb.cache.mv[i_list][i8+i][1] = 0;
1119                 }
1120             }
1121
1122             if( h->mb.i_neighbour & MB_TOPRIGHT )
1123             {
1124                 const int i8 = x264_scan8[0] + 4 - 1*8;
1125                 const int ir = i_mb_8x8 - s8x8 + 2;
1126                 const int iv = i_mb_4x4 - s4x4 + 4;
1127
1128                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
1129                 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1130                 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1131             }
1132             else
1133             {
1134                 const int i8 = x264_scan8[0] + 4 - 1*8;
1135                 h->mb.cache.ref[i_list][i8] = -2;
1136                 h->mb.cache.mv[i_list][i8][0] = 0;
1137                 h->mb.cache.mv[i_list][i8][1] = 0;
1138             }
1139
1140             if( i_left_xy >= 0 )
1141             {
1142                 const int i8 = x264_scan8[0] - 1;
1143                 const int ir = i_mb_8x8 - 1;
1144                 const int iv = i_mb_4x4 - 1;
1145
1146                 h->mb.cache.ref[i_list][i8+0*8] =
1147                 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1148                 h->mb.cache.ref[i_list][i8+2*8] =
1149                 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1150
1151                 for( i = 0; i < 4; i++ )
1152                 {
1153                     h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
1154                     h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
1155                 }
1156             }
1157             else
1158             {
1159                 const int i8 = x264_scan8[0] - 1;
1160                 for( i = 0; i < 4; i++ )
1161                 {
1162                     h->mb.cache.ref[i_list][i8+i*8] = -2;
1163                     h->mb.cache.mv[i_list][i8+i*8][0] =
1164                     h->mb.cache.mv[i_list][i8+i*8][1] = 0;
1165                 }
1166             }
1167
1168             if( h->param.b_cabac )
1169             {
1170                 if( i_top_xy >= 0 )
1171                 {
1172                     const int i8 = x264_scan8[0] - 8;
1173                     const int iv = i_mb_4x4 - s4x4;
1174                     for( i = 0; i < 4; i++ )
1175                     {
1176                         h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
1177                         h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
1178                     }
1179                 }
1180                 else
1181                 {
1182                     const int i8 = x264_scan8[0] - 8;
1183                     for( i = 0; i < 4; i++ )
1184                     {
1185                         h->mb.cache.mvd[i_list][i8+i][0] =
1186                         h->mb.cache.mvd[i_list][i8+i][1] = 0;
1187                     }
1188                 }
1189
1190                 if( i_left_xy >= 0 )
1191                 {
1192                     const int i8 = x264_scan8[0] - 1;
1193                     const int iv = i_mb_4x4 - 1;
1194                     for( i = 0; i < 4; i++ )
1195                     {
1196                         h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
1197                         h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
1198                     }
1199                 }
1200                 else
1201                 {
1202                     const int i8 = x264_scan8[0] - 1;
1203                     for( i = 0; i < 4; i++ )
1204                     {
1205                         h->mb.cache.mvd[i_list][i8+i*8][0] =
1206                         h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
1207                     }
1208                 }
1209             }
1210         }
1211
1212         /* load skip */
1213         if( h->param.b_cabac )
1214         {
1215             if( h->sh.i_type == SLICE_TYPE_B )
1216             {
1217                 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1218                 if( i_left_xy >= 0 )
1219                 {
1220                     h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
1221                     h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
1222                 }
1223                 if( i_top_xy >= 0 )
1224                 {
1225                     h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
1226                     h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
1227                 }
1228             }
1229             else if( h->mb.i_mb_xy == 0 && h->sh.i_type == SLICE_TYPE_P )
1230             {
1231                 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1232             }
1233         }
1234     }
1235 }
1236
1237 void x264_macroblock_cache_save( x264_t *h )
1238 {
1239     const int i_mb_xy = h->mb.i_mb_xy;
1240     const int i_mb_type = h->mb.i_type;
1241     const int s8x8 = h->mb.i_b8_stride;
1242     const int s4x4 = h->mb.i_b4_stride;
1243     const int i_mb_4x4 = h->mb.i_b4_xy;
1244     const int i_mb_8x8 = h->mb.i_b8_xy;
1245
1246     int i;
1247
1248     if( IS_SKIP( h->mb.i_type ) )
1249         h->mb.qp[i_mb_xy] = h->mb.i_last_qp;
1250
1251     h->mb.i_last_dqp = h->mb.qp[i_mb_xy] - h->mb.i_last_qp;
1252     h->mb.i_last_qp = h->mb.qp[i_mb_xy];
1253
1254     /* save intra4x4 */
1255     if( i_mb_type == I_4x4 )
1256     {
1257         h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1258         h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
1259         h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
1260         h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
1261         h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
1262         h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
1263         h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
1264     }
1265     else
1266     {
1267         h->mb.intra4x4_pred_mode[i_mb_xy][0] =
1268         h->mb.intra4x4_pred_mode[i_mb_xy][1] =
1269         h->mb.intra4x4_pred_mode[i_mb_xy][2] =
1270         h->mb.intra4x4_pred_mode[i_mb_xy][3] =
1271         h->mb.intra4x4_pred_mode[i_mb_xy][4] =
1272         h->mb.intra4x4_pred_mode[i_mb_xy][5] =
1273         h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
1274     }
1275
1276     if( i_mb_type == I_PCM )
1277     {
1278         h->mb.cbp[i_mb_xy] = 0x72f;   /* all set */
1279         for( i = 0; i < 16 + 2*4; i++ )
1280         {
1281             h->mb.non_zero_count[i_mb_xy][i] = 16;
1282         }
1283     }
1284     else
1285     {
1286         /* save non zero count */
1287         for( i = 0; i < 16 + 2*4; i++ )
1288         {
1289             h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
1290         }
1291     }
1292
1293     if( !IS_INTRA( i_mb_type ) )
1294     {
1295         int i_list;
1296         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1297         {
1298             int y,x;
1299
1300             h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
1301             h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
1302             h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
1303             h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
1304
1305             for( y = 0; y < 4; y++ )
1306             {
1307                 for( x = 0; x < 4; x++ )
1308                 {
1309                     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];
1310                     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];
1311                 }
1312             }
1313         }
1314     }
1315     else
1316     {
1317         int i_list;
1318         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1319         {
1320             int y,x;
1321
1322             h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
1323             h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
1324             h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
1325             h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
1326
1327             for( y = 0; y < 4; y++ )
1328             {
1329                 for( x = 0; x < 4; x++ )
1330                 {
1331                     h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1332                     h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1333                 }
1334             }
1335         }
1336     }
1337
1338     if( h->param.b_cabac )
1339     {
1340         if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1341             h->mb.chroma_pred_mode[i_mb_xy] = h->mb.i_chroma_pred_mode;
1342         else
1343             h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1344
1345         if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1346         {
1347             int i_list;
1348             for( i_list  = 0; i_list < 2; i_list++ )
1349             {
1350                 const int s4x4 = 4 * h->mb.i_mb_stride;
1351                 int y,x;
1352                 for( y = 0; y < 4; y++ )
1353                 {
1354                     for( x = 0; x < 4; x++ )
1355                     {
1356                         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];
1357                         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];
1358                     }
1359                 }
1360             }
1361         }
1362         else
1363         {
1364             int i_list;
1365             for( i_list  = 0; i_list < 2; i_list++ )
1366             {
1367                 const int s4x4 = 4 * h->mb.i_mb_stride;
1368                 int y,x;
1369                 for( y = 0; y < 4; y++ )
1370                 {
1371                     for( x = 0; x < 4; x++ )
1372                     {
1373                         h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1374                         h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1375                     }
1376                 }
1377             }
1378         }
1379         if( h->sh.i_type == SLICE_TYPE_B )
1380         {
1381             if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1382                 h->mb.skipbp[i_mb_xy] = 0xf;
1383             else if( i_mb_type == B_8x8 )
1384             {
1385                 int skipbp = 0;
1386                 for( i = 0; i < 4; i++ )
1387                     skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1388                 h->mb.skipbp[i_mb_xy] = skipbp;
1389             }
1390             else
1391                 h->mb.skipbp[i_mb_xy] = 0;
1392         }
1393     }
1394 }
1395
1396 void x264_macroblock_bipred_init( x264_t *h )
1397 {
1398     int i_ref0, i_ref1;
1399     for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1400     {
1401         int poc0 = h->fref0[i_ref0]->i_poc;
1402         for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1403         {
1404             int dist_scale_factor;
1405             int poc1 = h->fref1[i_ref1]->i_poc;
1406             int td = x264_clip3( poc1 - poc0, -128, 127 );
1407             if( td == 0 /* || pic0 is a long-term ref */ )
1408                 dist_scale_factor = 256;
1409             else
1410             {
1411                 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1412                 int tx = (16384 + (abs(td) >> 1)) / td;
1413                 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1414             }
1415             h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1416
1417             dist_scale_factor >>= 2;
1418             if( h->param.analyse.b_weighted_bipred
1419                   && dist_scale_factor >= -64
1420                   && dist_scale_factor <= 128 )
1421                 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1422             else
1423                 h->mb.bipred_weight[i_ref0][i_ref1] = 32;
1424         }
1425     }
1426 }