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 $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
29 #include "macroblock.h"
31 static const uint8_t block_idx_x[16] =
33 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
35 static const uint8_t block_idx_y[16] =
37 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
39 static const uint8_t block_idx_xy[4][4] =
47 static const int dequant_mf[6][4][4] =
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} }
57 static const int dequant8_mf[6][8][8] =
60 {20, 19, 25, 19, 20, 19, 25, 19},
61 {19, 18, 24, 18, 19, 18, 24, 18},
62 {25, 24, 32, 24, 25, 24, 32, 24},
63 {19, 18, 24, 18, 19, 18, 24, 18},
64 {20, 19, 25, 19, 20, 19, 25, 19},
65 {19, 18, 24, 18, 19, 18, 24, 18},
66 {25, 24, 32, 24, 25, 24, 32, 24},
67 {19, 18, 24, 18, 19, 18, 24, 18}
69 {22, 21, 28, 21, 22, 21, 28, 21},
70 {21, 19, 26, 19, 21, 19, 26, 19},
71 {28, 26, 35, 26, 28, 26, 35, 26},
72 {21, 19, 26, 19, 21, 19, 26, 19},
73 {22, 21, 28, 21, 22, 21, 28, 21},
74 {21, 19, 26, 19, 21, 19, 26, 19},
75 {28, 26, 35, 26, 28, 26, 35, 26},
76 {21, 19, 26, 19, 21, 19, 26, 19}
78 {26, 24, 33, 24, 26, 24, 33, 24},
79 {24, 23, 31, 23, 24, 23, 31, 23},
80 {33, 31, 42, 31, 33, 31, 42, 31},
81 {24, 23, 31, 23, 24, 23, 31, 23},
82 {26, 24, 33, 24, 26, 24, 33, 24},
83 {24, 23, 31, 23, 24, 23, 31, 23},
84 {33, 31, 42, 31, 33, 31, 42, 31},
85 {24, 23, 31, 23, 24, 23, 31, 23}
87 {28, 26, 35, 26, 28, 26, 35, 26},
88 {26, 25, 33, 25, 26, 25, 33, 25},
89 {35, 33, 45, 33, 35, 33, 45, 33},
90 {26, 25, 33, 25, 26, 25, 33, 25},
91 {28, 26, 35, 26, 28, 26, 35, 26},
92 {26, 25, 33, 25, 26, 25, 33, 25},
93 {35, 33, 45, 33, 35, 33, 45, 33},
94 {26, 25, 33, 25, 26, 25, 33, 25}
96 {32, 30, 40, 30, 32, 30, 40, 30},
97 {30, 28, 38, 28, 30, 28, 38, 28},
98 {40, 38, 51, 38, 40, 38, 51, 38},
99 {30, 28, 38, 28, 30, 28, 38, 28},
100 {32, 30, 40, 30, 32, 30, 40, 30},
101 {30, 28, 38, 28, 30, 28, 38, 28},
102 {40, 38, 51, 38, 40, 38, 51, 38},
103 {30, 28, 38, 28, 30, 28, 38, 28}
105 {36, 34, 46, 34, 36, 34, 46, 34},
106 {34, 32, 43, 32, 34, 32, 43, 32},
107 {46, 43, 58, 43, 46, 43, 58, 43},
108 {34, 32, 43, 32, 34, 32, 43, 32},
109 {36, 34, 46, 34, 36, 34, 46, 34},
110 {34, 32, 43, 32, 34, 32, 43, 32},
111 {46, 43, 58, 43, 46, 43, 58, 43},
112 {34, 32, 43, 32, 34, 32, 43, 32}
116 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
118 const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
119 const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
120 const int m = X264_MIN( x264_mb_pred_mode4x4_fix(ma),
121 x264_mb_pred_mode4x4_fix(mb) );
124 return I_PRED_4x4_DC;
129 int x264_mb_predict_non_zero_code( x264_t *h, int idx )
131 const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1];
132 const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8];
138 i_ret = ( i_ret + 1 ) >> 1;
143 int x264_mb_transform_8x8_allowed( x264_t *h )
145 if( IS_SKIP( h->mb.i_type ) )
147 if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
150 for( i = 0; i < 4; i++ )
151 if( !IS_SUB8x8(h->mb.i_sub_partition[i])
152 || ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 && !h->sps->b_direct8x8_inference ) )
157 if( h->mb.i_type == B_DIRECT && !h->sps->b_direct8x8_inference )
163 /****************************************************************************
164 * Scan and Quant functions
165 ****************************************************************************/
166 void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int i_qscale )
168 const int i_qbits = i_qscale/6 - 1;
172 const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
174 dct[0][0] = dct[0][0] * i_dmf;
175 dct[0][1] = dct[0][1] * i_dmf;
176 dct[1][0] = dct[1][0] * i_dmf;
177 dct[1][1] = dct[1][1] * i_dmf;
181 const int i_dmf = dequant_mf[i_qscale%6][0][0];
183 dct[0][0] = ( dct[0][0] * i_dmf ) >> 1;
184 dct[0][1] = ( dct[0][1] * i_dmf ) >> 1;
185 dct[1][0] = ( dct[1][0] * i_dmf ) >> 1;
186 dct[1][1] = ( dct[1][1] * i_dmf ) >> 1;
190 void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int i_qscale )
192 const int i_qbits = i_qscale/6 - 2;
197 const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
199 for( y = 0; y < 4; y++ )
201 for( x = 0; x < 4; x++ )
203 dct[y][x] = dct[y][x] * i_dmf;
209 const int i_dmf = dequant_mf[i_qscale%6][0][0];
210 const int f = -i_qbits; // 1 << (-1-i_qbits)
212 for( y = 0; y < 4; y++ )
214 for( x = 0; x < 4; x++ )
216 dct[y][x] = ( dct[y][x] * i_dmf + f ) >> (-i_qbits);
222 void x264_mb_dequant_4x4( int16_t dct[4][4], int i_qscale )
224 const int i_mf = i_qscale%6;
225 const int i_qbits = i_qscale/6;
228 for( y = 0; y < 4; y++ )
230 dct[y][0] = ( dct[y][0] * dequant_mf[i_mf][y][0] ) << i_qbits;
231 dct[y][1] = ( dct[y][1] * dequant_mf[i_mf][y][1] ) << i_qbits;
232 dct[y][2] = ( dct[y][2] * dequant_mf[i_mf][y][2] ) << i_qbits;
233 dct[y][3] = ( dct[y][3] * dequant_mf[i_mf][y][3] ) << i_qbits;
237 void x264_mb_dequant_8x8( int16_t dct[8][8], int i_qscale )
239 const int i_mf = i_qscale%6;
244 const int i_qbits = (i_qscale/6) - 2;
245 for( y = 0; y < 8; y++ )
247 dct[y][0] = ( dct[y][0] * dequant8_mf[i_mf][y][0] ) << i_qbits;
248 dct[y][1] = ( dct[y][1] * dequant8_mf[i_mf][y][1] ) << i_qbits;
249 dct[y][2] = ( dct[y][2] * dequant8_mf[i_mf][y][2] ) << i_qbits;
250 dct[y][3] = ( dct[y][3] * dequant8_mf[i_mf][y][3] ) << i_qbits;
251 dct[y][4] = ( dct[y][4] * dequant8_mf[i_mf][y][4] ) << i_qbits;
252 dct[y][5] = ( dct[y][5] * dequant8_mf[i_mf][y][5] ) << i_qbits;
253 dct[y][6] = ( dct[y][6] * dequant8_mf[i_mf][y][6] ) << i_qbits;
254 dct[y][7] = ( dct[y][7] * dequant8_mf[i_mf][y][7] ) << i_qbits;
259 const int i_qbits = 2 - (i_qscale/6);
260 const int i_round = i_qbits; // 1<<(i_qbits-1)
261 for( y = 0; y < 8; y++ )
263 dct[y][0] = ( dct[y][0] * dequant8_mf[i_mf][y][0] + i_round ) >> i_qbits;
264 dct[y][1] = ( dct[y][1] * dequant8_mf[i_mf][y][1] + i_round ) >> i_qbits;
265 dct[y][2] = ( dct[y][2] * dequant8_mf[i_mf][y][2] + i_round ) >> i_qbits;
266 dct[y][3] = ( dct[y][3] * dequant8_mf[i_mf][y][3] + i_round ) >> i_qbits;
267 dct[y][4] = ( dct[y][4] * dequant8_mf[i_mf][y][4] + i_round ) >> i_qbits;
268 dct[y][5] = ( dct[y][5] * dequant8_mf[i_mf][y][5] + i_round ) >> i_qbits;
269 dct[y][6] = ( dct[y][6] * dequant8_mf[i_mf][y][6] + i_round ) >> i_qbits;
270 dct[y][7] = ( dct[y][7] * dequant8_mf[i_mf][y][7] + i_round ) >> i_qbits;
275 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
277 const int i8 = x264_scan8[idx];
278 const int i_ref= h->mb.cache.ref[i_list][i8];
279 int i_refa = h->mb.cache.ref[i_list][i8 - 1];
280 int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
281 int i_refb = h->mb.cache.ref[i_list][i8 - 8];
282 int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
283 int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
284 int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
288 if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
290 i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
291 mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
294 if( h->mb.i_partition == D_16x8 )
296 if( idx == 0 && i_refb == i_ref )
302 else if( idx != 0 && i_refa == i_ref )
309 else if( h->mb.i_partition == D_8x16 )
311 if( idx == 0 && i_refa == i_ref )
317 else if( idx != 0 && i_refc == i_ref )
326 if( i_refa == i_ref ) i_count++;
327 if( i_refb == i_ref ) i_count++;
328 if( i_refc == i_ref ) i_count++;
332 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
333 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
335 else if( i_count == 1 )
337 if( i_refa == i_ref )
342 else if( i_refb == i_ref )
353 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
360 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
361 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
365 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int mvp[2] )
367 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
368 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
369 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
370 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
371 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
372 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
378 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
379 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
383 if( i_refa == i_ref ) i_count++;
384 if( i_refb == i_ref ) i_count++;
385 if( i_refc == i_ref ) i_count++;
389 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
390 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
392 else if( i_count == 1 )
394 if( i_refa == i_ref )
399 else if( i_refb == i_ref )
410 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
417 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
418 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
423 void x264_mb_predict_mv_pskip( x264_t *h, int mv[2] )
425 int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
426 int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
427 int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
428 int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
430 if( i_refa == -2 || i_refb == -2 ||
431 ( i_refa == 0 && mv_a[0] == 0 && mv_a[1] == 0 ) ||
432 ( i_refb == 0 && mv_b[0] == 0 && mv_b[1] == 0 ) )
438 x264_mb_predict_mv_16x16( h, 0, 0, mv );
442 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
444 int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
445 int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
448 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
450 if( IS_INTRA( h->fref1[0]->mb_type[ h->mb.i_mb_xy ] ) )
452 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
453 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0, 0 );
454 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0, 0 );
458 /* FIXME: optimize per block size */
459 for( i = 0; i < 4; i++ )
461 const int x8 = 2*(i%2);
462 const int y8 = 2*(i/2);
463 const int i_part_8x8 = i_mb_8x8 + x8/2 + y8 * h->mb.i_mb_stride;
464 const int i_ref = h->mb.map_col_to_list0[ h->fref1[0]->ref[0][ i_part_8x8 ] ];
468 const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
471 x264_macroblock_cache_ref( h, x8, y8, 2, 2, 0, i_ref );
473 for( y4 = y8; y4 < y8+2; y4++ )
474 for( x4 = x8; x4 < x8+2; x4++ )
476 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * 4 * h->mb.i_mb_stride ];
478 mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
479 mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
480 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, mv_l0[0], mv_l0[1] );
481 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
486 /* the colocated ref isn't in the current list0 */
487 /* FIXME: we might still be able to use direct_8x8 on some partitions */
495 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
501 const int8_t *l1ref = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
502 const int16_t (*l1mv)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
504 for( i_list=0; i_list<2; i_list++ )
506 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
507 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
508 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
510 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
512 ref[i_list] = i_refa;
513 if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
514 ref[i_list] = i_refb;
515 if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
516 ref[i_list] = i_refc;
517 if( ref[i_list] < 0 )
521 if( ref[0] < 0 && ref[1] < 0 )
532 for( i_list=0; i_list<2; i_list++ )
534 if( ref[i_list] >= 0 )
535 x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
537 mv[i_list][0] = mv[i_list][1] = 0;
541 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
542 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
543 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, mv[0][0], mv[0][1] );
544 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, mv[1][0], mv[1][1] );
547 for( i8=0; i8<4; i8++ )
551 if( l1ref[ x8 + y8 * h->mb.i_b8_stride ] == 0 )
553 for( i4=0; i4<4; i4++ )
555 const int x4 = i4%2 + 2*x8;
556 const int y4 = i4/2 + 2*y8;
557 const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
558 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
561 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0, 0 );
563 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0, 0 );
572 int x264_mb_predict_mv_direct16x16( x264_t *h )
575 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
577 else if( h->sh.b_direct_spatial_mv_pred )
578 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
580 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
586 for( l = 0; l < 2; l++ )
587 for( i = 0; i < 4; i++ )
588 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
589 memcpy(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
595 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
597 const int x = 2*(idx%2);
598 const int y = 2*(idx/2);
600 x264_macroblock_cache_ref( h, x, y, 2, 2, 0, h->mb.cache.direct_ref[0][idx] );
601 x264_macroblock_cache_ref( h, x, y, 2, 2, 1, h->mb.cache.direct_ref[1][idx] );
602 for( l = 0; l < 2; l++ )
604 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]] =
605 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]];
606 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]+8] =
607 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]+8];
611 /* This just improves encoder performance, it's not part of the spec */
612 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[5][2], int *i_mvc )
614 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
618 if( h->sh.i_type == SLICE_TYPE_B )
620 if( h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
622 /* FIXME: use direct_mv to be clearer? */
623 int16_t *mvp = h->mb.cache.mv[i_list][x264_scan8[12]];
631 if( h->mb.i_neighbour & MB_LEFT )
633 int i_mb_l = h->mb.i_mb_xy - 1;
634 /* skip MBs didn't go through the whole search process, so mvr is undefined */
635 if( !IS_SKIP( h->mb.type[i_mb_l] ) )
637 mvc[i][0] = mvr[i_mb_l][0];
638 mvc[i][1] = mvr[i_mb_l][1];
642 if( h->mb.i_neighbour & MB_TOP )
644 int i_mb_t = h->mb.i_mb_xy - h->mb.i_mb_stride;
645 if( !IS_SKIP( h->mb.type[i_mb_t] ) )
647 mvc[i][0] = mvr[i_mb_t][0];
648 mvc[i][1] = mvr[i_mb_t][1];
652 if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
654 mvc[i][0] = mvr[i_mb_t - 1][0];
655 mvc[i][1] = mvr[i_mb_t - 1][1];
658 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
660 mvc[i][0] = mvr[i_mb_t + 1][0];
661 mvc[i][1] = mvr[i_mb_t + 1][1];
668 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
670 const int i8 = x264_scan8[0]+x+8*y;
671 const int i_ref = h->mb.cache.ref[0][i8];
672 const int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
673 const int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
675 h->mc.mc_luma( h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
676 &h->mb.pic.p_fdec[0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
677 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
679 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],
680 &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
681 mvx, mvy, 2*width, 2*height );
683 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],
684 &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
685 mvx, mvy, 2*width, 2*height );
687 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
689 const int i8 = x264_scan8[0]+x+8*y;
690 const int i_ref = h->mb.cache.ref[1][i8];
691 const int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
692 const int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
694 h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
695 &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
696 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
698 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],
699 &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
700 mvx, mvy, 2*width, 2*height );
702 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],
703 &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
704 mvx, mvy, 2*width, 2*height );
707 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
709 const int i8 = x264_scan8[0]+x+8*y;
711 const int i_ref1 = h->mb.cache.ref[1][i8];
712 const int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
713 const int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
714 DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
715 int i_mode = x264_size2pixel[height][width];
717 x264_mb_mc_0xywh( h, x, y, width, height );
719 h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
720 tmp, 16, mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
722 if( h->param.analyse.b_weighted_bipred )
724 const int i_ref0 = h->mb.cache.ref[0][i8];
725 const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
727 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 );
729 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],
730 tmp, 16, mvx1, mvy1, 2*width, 2*height );
731 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 );
733 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],
734 tmp, 16, mvx1, mvy1, 2*width, 2*height );
735 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 );
739 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 );
741 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],
742 tmp, 16, mvx1, mvy1, 2*width, 2*height );
743 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 );
745 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],
746 tmp, 16, mvx1, mvy1, 2*width, 2*height );
747 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 );
751 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
753 const int i8 = x264_scan8[0] + x + 8*y;
755 /* FIXME: optimize based on current block size, not global settings? */
756 if( h->sps->b_direct8x8_inference )
758 if( h->mb.cache.ref[0][i8] >= 0 )
759 if( h->mb.cache.ref[1][i8] >= 0 )
760 x264_mb_mc_01xywh( h, x, y, 2, 2 );
762 x264_mb_mc_0xywh( h, x, y, 2, 2 );
764 x264_mb_mc_1xywh( h, x, y, 2, 2 );
768 if( h->mb.cache.ref[0][i8] >= 0 )
770 if( h->mb.cache.ref[1][i8] >= 0 )
772 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
773 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
774 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
775 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
779 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
780 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
781 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
782 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
787 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
788 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
789 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
790 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
795 void x264_mb_mc( x264_t *h )
797 if( h->mb.i_type == P_L0 )
799 if( h->mb.i_partition == D_16x16 )
801 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
803 else if( h->mb.i_partition == D_16x8 )
805 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
806 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
808 else if( h->mb.i_partition == D_8x16 )
810 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
811 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
814 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
817 for( i = 0; i < 4; i++ )
819 const int x = 2*(i%2);
820 const int y = 2*(i/2);
821 switch( h->mb.i_sub_partition[i] )
824 x264_mb_mc_0xywh( h, x, y, 2, 2 );
827 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
828 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
831 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
832 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
835 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
836 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
837 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
838 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
841 x264_mb_mc_1xywh( h, x, y, 2, 2 );
844 x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
845 x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
848 x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
849 x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
852 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
853 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
854 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
855 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
858 x264_mb_mc_01xywh( h, x, y, 2, 2 );
861 x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
862 x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
865 x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
866 x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
869 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
870 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
871 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
872 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
875 x264_mb_mc_direct8x8( h, x, y );
880 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
882 x264_mb_mc_direct8x8( h, 0, 0 );
883 x264_mb_mc_direct8x8( h, 2, 0 );
884 x264_mb_mc_direct8x8( h, 0, 2 );
885 x264_mb_mc_direct8x8( h, 2, 2 );
894 /* init ref list utilisations */
895 for( i = 0; i < 2; i++ )
897 b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
898 b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
900 if( h->mb.i_partition == D_16x16 )
902 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
903 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
904 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
906 else if( h->mb.i_partition == D_16x8 )
908 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
909 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
910 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
912 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
913 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
914 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
916 else if( h->mb.i_partition == D_8x16 )
918 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
919 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
920 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
922 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
923 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
924 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
929 void x264_macroblock_cache_init( x264_t *h )
932 int i_mb_count = h->mb.i_mb_count;
934 h->mb.i_mb_stride = h->sps->i_mb_width;
935 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
936 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
938 h->mb.qp = x264_malloc( i_mb_count * sizeof(int8_t) );
939 h->mb.cbp = x264_malloc( i_mb_count * sizeof(int16_t) );
940 h->mb.skipbp = x264_malloc( i_mb_count * sizeof(int8_t) );
941 h->mb.mb_transform_size = x264_malloc( i_mb_count * sizeof(int8_t) );
943 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
944 h->mb.intra4x4_pred_mode = x264_malloc( i_mb_count * 7 * sizeof( int8_t ) );
947 h->mb.non_zero_count = x264_malloc( i_mb_count * 24 * sizeof( uint8_t ) );
949 if( h->param.b_cabac )
951 h->mb.chroma_pred_mode = x264_malloc( i_mb_count * sizeof( int8_t) );
952 h->mb.mvd[0] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
953 h->mb.mvd[1] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
958 int i_refs = i ? 1 + h->param.b_bframe_pyramid : h->param.i_frame_reference;
959 for( j=0; j < i_refs; j++ )
960 h->mb.mvr[i][j] = x264_malloc( 2 * i_mb_count * sizeof( int16_t ) );
963 /* init with not avaiable (for top right idx=7,15) */
964 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
965 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
967 void x264_macroblock_cache_end( x264_t *h )
972 int i_refs = i ? 1 + h->param.b_bframe_pyramid : h->param.i_frame_reference;
973 for( j=0; j < i_refs; j++ )
974 x264_free( h->mb.mvr[i][j] );
976 if( h->param.b_cabac )
978 x264_free( h->mb.chroma_pred_mode );
979 x264_free( h->mb.mvd[0] );
980 x264_free( h->mb.mvd[1] );
982 x264_free( h->mb.intra4x4_pred_mode );
983 x264_free( h->mb.non_zero_count );
984 x264_free( h->mb.mb_transform_size );
985 x264_free( h->mb.skipbp );
986 x264_free( h->mb.cbp );
987 x264_free( h->mb.qp );
989 void x264_macroblock_slice_init( x264_t *h )
993 h->mb.mv[0] = h->fdec->mv[0];
994 h->mb.mv[1] = h->fdec->mv[1];
995 h->mb.ref[0] = h->fdec->ref[0];
996 h->mb.ref[1] = h->fdec->ref[1];
997 h->mb.type = h->fdec->mb_type;
999 h->fdec->i_ref[0] = h->i_ref0;
1000 h->fdec->i_ref[1] = h->i_ref1;
1001 for( i = 0; i < h->i_ref0; i++ )
1002 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
1003 if( h->sh.i_type == SLICE_TYPE_B )
1005 for( i = 0; i < h->i_ref1; i++ )
1006 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
1008 h->mb.map_col_to_list0[-1] = -1;
1009 h->mb.map_col_to_list0[-2] = -2;
1010 for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
1012 int poc = h->fref1[0]->ref_poc[0][i];
1013 h->mb.map_col_to_list0[i] = -2;
1014 for( j = 0; j < h->i_ref0; j++ )
1015 if( h->fref0[j]->i_poc == poc )
1017 h->mb.map_col_to_list0[i] = j;
1022 if( h->sh.i_type == SLICE_TYPE_P )
1023 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1027 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
1029 const int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
1030 const int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
1032 int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
1033 int i_top_xy = i_mb_xy - h->mb.i_mb_stride;
1035 int i_top_type = -1; /* gcc warn */
1036 int i_left_type= -1;
1041 h->mb.i_mb_x = i_mb_x;
1042 h->mb.i_mb_y = i_mb_y;
1043 h->mb.i_mb_xy = i_mb_xy;
1044 h->mb.i_b8_xy = i_mb_8x8;
1045 h->mb.i_b4_xy = i_mb_4x4;
1046 h->mb.i_neighbour = 0;
1048 /* load picture pointers */
1049 for( i = 0; i < 3; i++ )
1051 const int w = (i == 0 ? 16 : 8);
1052 const int i_stride = h->fdec->i_stride[i];
1055 h->mb.pic.i_stride[i] = i_stride;
1057 h->mb.pic.p_fenc[i] = &h->fenc->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
1058 h->mb.pic.p_fdec[i] = &h->fdec->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
1060 for( j = 0; j < h->i_ref0; j++ )
1062 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 )];
1063 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] )];
1065 for( j = 0; j < h->i_ref1; j++ )
1067 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 )];
1068 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] )];
1073 if( i_mb_xy >= h->sh.i_first_mb + h->mb.i_mb_stride )
1075 h->mb.i_mb_type_top =
1076 i_top_type= h->mb.type[i_top_xy];
1078 h->mb.i_neighbour |= MB_TOP;
1081 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
1082 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
1083 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
1084 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
1086 /* load non_zero_count */
1087 h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
1088 h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
1089 h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
1090 h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
1092 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
1093 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
1095 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
1096 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
1100 h->mb.i_mb_type_top = -1;
1103 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
1104 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
1105 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
1106 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
1108 /* load non_zero_count */
1109 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
1110 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
1111 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
1112 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
1113 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
1114 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1115 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1116 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1120 if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1122 i_left_xy = i_mb_xy - 1;
1123 h->mb.i_mb_type_left =
1124 i_left_type = h->mb.type[i_left_xy];
1126 h->mb.i_neighbour |= MB_LEFT;
1129 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1130 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1131 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1132 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1134 /* load non_zero_count */
1135 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
1136 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1137 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
1138 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1140 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1141 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1143 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1144 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1148 h->mb.i_mb_type_left = -1;
1150 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1151 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1152 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1153 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1155 /* load non_zero_count */
1156 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1157 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1158 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1159 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1160 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1161 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1162 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1163 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1166 if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1168 h->mb.i_neighbour |= MB_TOPRIGHT;
1169 h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1172 h->mb.i_mb_type_topright = -1;
1173 if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1175 h->mb.i_neighbour |= MB_TOPLEFT;
1176 h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1179 h->mb.i_mb_type_topleft = -1;
1181 if( h->param.analyse.b_transform_8x8 )
1183 h->mb.cache.i_neighbour_transform_size =
1184 ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1185 + ( i_top_type >= 0 && h->mb.mb_transform_size[i_top_xy] );
1188 /* load ref/mv/mvd */
1189 if( h->sh.i_type != SLICE_TYPE_I )
1191 const int s8x8 = h->mb.i_b8_stride;
1192 const int s4x4 = h->mb.i_b4_stride;
1196 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1199 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1200 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1201 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1204 if( h->mb.i_neighbour & MB_TOPLEFT )
1206 const int i8 = x264_scan8[0] - 1 - 1*8;
1207 const int ir = i_mb_8x8 - s8x8 - 1;
1208 const int iv = i_mb_4x4 - s4x4 - 1;
1209 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1210 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1211 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1215 const int i8 = x264_scan8[0] - 1 - 1*8;
1216 h->mb.cache.ref[i_list][i8] = -2;
1217 h->mb.cache.mv[i_list][i8][0] = 0;
1218 h->mb.cache.mv[i_list][i8][1] = 0;
1221 if( h->mb.i_neighbour & MB_TOP )
1223 const int i8 = x264_scan8[0] - 8;
1224 const int ir = i_mb_8x8 - s8x8;
1225 const int iv = i_mb_4x4 - s4x4;
1227 h->mb.cache.ref[i_list][i8+0] =
1228 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1229 h->mb.cache.ref[i_list][i8+2] =
1230 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1232 for( i = 0; i < 4; i++ )
1234 h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
1235 h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
1240 const int i8 = x264_scan8[0] - 8;
1241 for( i = 0; i < 4; i++ )
1243 h->mb.cache.ref[i_list][i8+i] = -2;
1244 h->mb.cache.mv[i_list][i8+i][0] =
1245 h->mb.cache.mv[i_list][i8+i][1] = 0;
1249 if( h->mb.i_neighbour & MB_TOPRIGHT )
1251 const int i8 = x264_scan8[0] + 4 - 1*8;
1252 const int ir = i_mb_8x8 - s8x8 + 2;
1253 const int iv = i_mb_4x4 - s4x4 + 4;
1255 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1256 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1257 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1261 const int i8 = x264_scan8[0] + 4 - 1*8;
1262 h->mb.cache.ref[i_list][i8] = -2;
1263 h->mb.cache.mv[i_list][i8][0] = 0;
1264 h->mb.cache.mv[i_list][i8][1] = 0;
1267 if( h->mb.i_neighbour & MB_LEFT )
1269 const int i8 = x264_scan8[0] - 1;
1270 const int ir = i_mb_8x8 - 1;
1271 const int iv = i_mb_4x4 - 1;
1273 h->mb.cache.ref[i_list][i8+0*8] =
1274 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1275 h->mb.cache.ref[i_list][i8+2*8] =
1276 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1278 for( i = 0; i < 4; i++ )
1280 h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
1281 h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
1286 const int i8 = x264_scan8[0] - 1;
1287 for( i = 0; i < 4; i++ )
1289 h->mb.cache.ref[i_list][i8+i*8] = -2;
1290 h->mb.cache.mv[i_list][i8+i*8][0] =
1291 h->mb.cache.mv[i_list][i8+i*8][1] = 0;
1295 if( h->param.b_cabac )
1297 if( i_top_type >= 0 )
1299 const int i8 = x264_scan8[0] - 8;
1300 const int iv = i_mb_4x4 - s4x4;
1301 for( i = 0; i < 4; i++ )
1303 h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
1304 h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
1309 const int i8 = x264_scan8[0] - 8;
1310 for( i = 0; i < 4; i++ )
1312 h->mb.cache.mvd[i_list][i8+i][0] =
1313 h->mb.cache.mvd[i_list][i8+i][1] = 0;
1317 if( i_left_type >= 0 )
1319 const int i8 = x264_scan8[0] - 1;
1320 const int iv = i_mb_4x4 - 1;
1321 for( i = 0; i < 4; i++ )
1323 h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
1324 h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
1329 const int i8 = x264_scan8[0] - 1;
1330 for( i = 0; i < 4; i++ )
1332 h->mb.cache.mvd[i_list][i8+i*8][0] =
1333 h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
1340 if( h->sh.i_type == SLICE_TYPE_B && h->param.b_cabac )
1342 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1343 if( i_left_type >= 0 )
1345 h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
1346 h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
1348 if( i_top_type >= 0 )
1350 h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
1351 h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
1356 h->mb.i_neighbour4[0] =
1357 h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1358 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0);
1359 h->mb.i_neighbour4[4] =
1360 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1361 h->mb.i_neighbour4[2] =
1362 h->mb.i_neighbour4[8] =
1363 h->mb.i_neighbour4[10] =
1364 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1365 h->mb.i_neighbour4[3] =
1366 h->mb.i_neighbour4[7] =
1367 h->mb.i_neighbour4[11] =
1368 h->mb.i_neighbour4[13] =
1369 h->mb.i_neighbour4[15] =
1370 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
1371 h->mb.i_neighbour4[5] =
1372 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour & MB_TOPRIGHT)
1373 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1374 h->mb.i_neighbour4[6] =
1375 h->mb.i_neighbour4[9] =
1376 h->mb.i_neighbour4[12] =
1377 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
1380 void x264_macroblock_cache_save( x264_t *h )
1382 const int i_mb_xy = h->mb.i_mb_xy;
1383 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1384 const int s8x8 = h->mb.i_b8_stride;
1385 const int s4x4 = h->mb.i_b4_stride;
1386 const int i_mb_4x4 = h->mb.i_b4_xy;
1387 const int i_mb_8x8 = h->mb.i_b8_xy;
1391 h->mb.type[i_mb_xy] = i_mb_type;
1393 if( IS_SKIP( h->mb.i_type ) )
1394 h->mb.qp[i_mb_xy] = h->mb.i_last_qp;
1396 h->mb.i_last_dqp = h->mb.qp[i_mb_xy] - h->mb.i_last_qp;
1397 h->mb.i_last_qp = h->mb.qp[i_mb_xy];
1400 if( i_mb_type == I_4x4 )
1402 h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1403 h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
1404 h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
1405 h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
1406 h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
1407 h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
1408 h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
1412 h->mb.intra4x4_pred_mode[i_mb_xy][0] =
1413 h->mb.intra4x4_pred_mode[i_mb_xy][1] =
1414 h->mb.intra4x4_pred_mode[i_mb_xy][2] =
1415 h->mb.intra4x4_pred_mode[i_mb_xy][3] =
1416 h->mb.intra4x4_pred_mode[i_mb_xy][4] =
1417 h->mb.intra4x4_pred_mode[i_mb_xy][5] =
1418 h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
1421 if( i_mb_type == I_PCM )
1423 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1424 for( i = 0; i < 16 + 2*4; i++ )
1426 h->mb.non_zero_count[i_mb_xy][i] = 16;
1431 /* save non zero count */
1432 for( i = 0; i < 16 + 2*4; i++ )
1434 h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
1438 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1440 if( !IS_INTRA( i_mb_type ) )
1443 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1447 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
1448 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
1449 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
1450 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
1452 for( y = 0; y < 4; y++ )
1454 for( x = 0; x < 4; x++ )
1456 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];
1457 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];
1465 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1469 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
1470 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
1471 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
1472 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
1474 for( y = 0; y < 4; y++ )
1476 for( x = 0; x < 4; x++ )
1478 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1479 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1485 if( h->param.b_cabac )
1487 if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1488 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1490 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1492 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1495 for( i_list = 0; i_list < 2; i_list++ )
1497 const int s4x4 = 4 * h->mb.i_mb_stride;
1499 for( y = 0; y < 4; y++ )
1501 for( x = 0; x < 4; x++ )
1503 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];
1504 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];
1512 for( i_list = 0; i_list < 2; i_list++ )
1514 const int s4x4 = 4 * h->mb.i_mb_stride;
1516 for( y = 0; y < 4; y++ )
1518 for( x = 0; x < 4; x++ )
1520 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1521 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1526 if( h->sh.i_type == SLICE_TYPE_B )
1528 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1529 h->mb.skipbp[i_mb_xy] = 0xf;
1530 else if( i_mb_type == B_8x8 )
1533 for( i = 0; i < 4; i++ )
1534 skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1535 h->mb.skipbp[i_mb_xy] = skipbp;
1538 h->mb.skipbp[i_mb_xy] = 0;
1543 void x264_macroblock_bipred_init( x264_t *h )
1546 for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1548 int poc0 = h->fref0[i_ref0]->i_poc;
1549 for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1551 int dist_scale_factor;
1552 int poc1 = h->fref1[i_ref1]->i_poc;
1553 int td = x264_clip3( poc1 - poc0, -128, 127 );
1554 if( td == 0 /* || pic0 is a long-term ref */ )
1555 dist_scale_factor = 256;
1558 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1559 int tx = (16384 + (abs(td) >> 1)) / td;
1560 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1562 h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1564 dist_scale_factor >>= 2;
1565 if( h->param.analyse.b_weighted_bipred
1566 && dist_scale_factor >= -64
1567 && dist_scale_factor <= 128 )
1568 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1570 h->mb.bipred_weight[i_ref0][i_ref1] = 32;