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} }
58 static const int i_chroma_qp_table[52] =
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,
69 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
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 );
81 int x264_mb_predict_non_zero_code( x264_t *h, int idx )
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];
90 i_ret = ( i_ret + 1 ) >> 1;
95 /****************************************************************************
96 * Scan and Quant functions
97 ****************************************************************************/
98 void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int i_qscale )
100 const int i_qbits = i_qscale/6 - 1;
104 const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
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;
113 const int i_dmf = dequant_mf[i_qscale%6][0][0];
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;
122 void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int i_qscale )
124 const int i_qbits = i_qscale/6 - 2;
129 const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
131 for( y = 0; y < 4; y++ )
133 for( x = 0; x < 4; x++ )
135 dct[y][x] = dct[y][x] * i_dmf;
141 const int i_dmf = dequant_mf[i_qscale%6][0][0];
142 const int f = 1 << ( 1 + i_qbits );
144 for( y = 0; y < 4; y++ )
146 for( x = 0; x < 4; x++ )
148 dct[y][x] = ( dct[y][x] * i_dmf + f ) >> (-i_qbits);
154 void x264_mb_dequant_4x4( int16_t dct[4][4], int i_qscale )
156 const int i_mf = i_qscale%6;
157 const int i_qbits = i_qscale/6;
160 for( y = 0; y < 4; y++ )
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;
169 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
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];
182 if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
184 i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
185 mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
188 if( h->mb.i_partition == D_16x8 )
190 if( idx == 0 && i_refb == i_ref )
196 else if( idx != 0 && i_refa == i_ref )
203 else if( h->mb.i_partition == D_8x16 )
205 if( idx == 0 && i_refa == i_ref )
211 else if( idx != 0 && i_refc == i_ref )
220 if( i_refa == i_ref ) i_count++;
221 if( i_refb == i_ref ) i_count++;
222 if( i_refc == i_ref ) i_count++;
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] );
229 else if( i_count == 1 )
231 if( i_refa == i_ref )
236 else if( i_refb == i_ref )
247 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
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] );
259 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int mvp[2] )
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];
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];
277 if( i_refa == i_ref ) i_count++;
278 if( i_refb == i_ref ) i_count++;
279 if( i_refc == i_ref ) i_count++;
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] );
286 else if( i_count == 1 )
288 if( i_refa == i_ref )
293 else if( i_refb == i_ref )
304 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
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] );
317 void x264_mb_predict_mv_pskip( x264_t *h, int mv[2] )
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];
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 ) )
332 x264_mb_predict_mv_16x16( h, 0, 0, mv );
336 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
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;
342 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
344 /* FIXME: optimize per block size */
345 for( i = 0; i < 4; i++ )
347 const int x8 = 2*(i%2);
348 const int y8 = 2*(i/2);
349 /* TODO: MapColToList0 */
350 const int i_ref = h->fref1[0]->ref[0][ i_mb_8x8 + x8/2 + y8 * h->mb.i_mb_stride ];
354 x264_macroblock_cache_ref( h, x8, y8, 2, 2, 0, 0 );
355 x264_macroblock_cache_mv( h, x8, y8, 2, 2, 0, 0, 0 );
356 x264_macroblock_cache_mv( h, x8, y8, 2, 2, 1, 0, 0 );
360 const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
363 x264_macroblock_cache_ref( h, x8, y8, 2, 2, 0, i_ref );
365 for( y4 = y8; y4 < y8+2; y4++ )
366 for( x4 = x8; x4 < x8+2; x4++ )
368 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * 4 * h->mb.i_mb_stride ];
370 mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
371 mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
372 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, mv_l0[0], mv_l0[1] );
373 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
381 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
387 const int8_t *l1ref = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
388 const int16_t (*l1mv)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
390 for( i_list=0; i_list<2; i_list++ )
392 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
393 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
394 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
396 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
398 ref[i_list] = i_refa;
399 if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
400 ref[i_list] = i_refb;
401 if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
402 ref[i_list] = i_refc;
403 if( ref[i_list] < 0 )
407 if( ref[0] < 0 && ref[1] < 0 )
418 for( i_list=0; i_list<2; i_list++ )
420 if( ref[i_list] >= 0 )
421 x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
423 mv[i_list][0] = mv[i_list][1] = 0;
427 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
428 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
429 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, mv[0][0], mv[0][1] );
430 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, mv[1][0], mv[1][1] );
433 for( i8=0; i8<4; i8++ )
437 if( l1ref[ x8 + y8 * h->mb.i_b8_stride ] == 0 )
439 for( i4=0; i4<4; i4++ )
441 const int x4 = i4%2 + 2*x8;
442 const int y4 = i4/2 + 2*y8;
443 const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
444 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
447 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0, 0 );
449 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0, 0 );
458 int x264_mb_predict_mv_direct16x16( x264_t *h )
461 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
463 else if( h->sh.b_direct_spatial_mv_pred )
464 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
466 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
472 for( l = 0; l < 2; l++ )
473 for( i = 0; i < 4; i++ )
474 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
475 memcpy(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
481 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
483 const int x = 2*(idx%2);
484 const int y = 2*(idx/2);
486 x264_macroblock_cache_ref( h, x, y, 2, 2, 0, h->mb.cache.direct_ref[0][idx] );
487 x264_macroblock_cache_ref( h, x, y, 2, 2, 1, h->mb.cache.direct_ref[1][idx] );
488 for( l = 0; l < 2; l++ )
490 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]] =
491 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]];
492 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]+8] =
493 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]+8];
497 /* This just improves encoder performance, it's not part of the spec */
498 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[5][2], int *i_mvc )
500 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
504 if( h->sh.i_type == SLICE_TYPE_B )
506 if( h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
508 /* FIXME: use direct_mv to be clearer? */
509 int16_t *mvp = h->mb.cache.mv[i_list][x264_scan8[12]];
517 if( h->mb.i_mb_x > 0 )
519 int i_mb_l = h->mb.i_mb_xy - 1;
520 /* skip MBs didn't go through the whole search process, so mvr is undefined */
521 if( !IS_SKIP( h->mb.type[i_mb_l] ) )
523 mvc[i][0] = mvr[i_mb_l][0];
524 mvc[i][1] = mvr[i_mb_l][1];
528 if( h->mb.i_mb_y > 0 )
530 int i_mb_t = h->mb.i_mb_xy - h->mb.i_mb_stride;
531 if( !IS_SKIP( h->mb.type[i_mb_t] ) )
533 mvc[i][0] = mvr[i_mb_t][0];
534 mvc[i][1] = mvr[i_mb_t][1];
538 if( h->mb.i_mb_x > 0 && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
540 mvc[i][0] = mvr[i_mb_t - 1][0];
541 mvc[i][1] = mvr[i_mb_t - 1][1];
544 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
546 mvc[i][0] = mvr[i_mb_t + 1][0];
547 mvc[i][1] = mvr[i_mb_t + 1][1];
554 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
556 const int i8 = x264_scan8[0]+x+8*y;
557 const int i_ref = h->mb.cache.ref[0][i8];
558 const int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
559 const int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
561 h->mc.mc_luma( h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
562 &h->mb.pic.p_fdec[0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
563 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
565 h->mc.mc_chroma( &h->mb.pic.p_fref[0][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
566 &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
567 mvx, mvy, 2*width, 2*height );
569 h->mc.mc_chroma( &h->mb.pic.p_fref[0][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
570 &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
571 mvx, mvy, 2*width, 2*height );
573 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
575 const int i8 = x264_scan8[0]+x+8*y;
576 const int i_ref = h->mb.cache.ref[1][i8];
577 const int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
578 const int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
580 h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
581 &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
582 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
584 h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
585 &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
586 mvx, mvy, 2*width, 2*height );
588 h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
589 &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
590 mvx, mvy, 2*width, 2*height );
593 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
595 const int i8 = x264_scan8[0]+x+8*y;
597 const int i_ref1 = h->mb.cache.ref[1][i8];
598 const int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
599 const int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
600 DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
601 int i_mode = x264_size2pixel[height][width];
603 x264_mb_mc_0xywh( h, x, y, width, height );
605 h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
606 tmp, 16, mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
608 if( h->param.analyse.b_weighted_bipred )
610 const int i_ref0 = h->mb.cache.ref[0][i8];
611 const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
613 h->pixf.avg_weight[i_mode]( &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0], tmp, 16, weight );
615 h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
616 tmp, 16, mvx1, mvy1, 2*width, 2*height );
617 h->pixf.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1], tmp, 16, weight );
619 h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
620 tmp, 16, mvx1, mvy1, 2*width, 2*height );
621 h->pixf.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2], tmp, 16, weight );
625 h->pixf.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0], tmp, 16 );
627 h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
628 tmp, 16, mvx1, mvy1, 2*width, 2*height );
629 h->pixf.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1], tmp, 16 );
631 h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
632 tmp, 16, mvx1, mvy1, 2*width, 2*height );
633 h->pixf.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2], tmp, 16 );
637 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
639 const int i8 = x264_scan8[0] + x + 8*y;
641 /* FIXME: optimize based on current block size, not global settings? */
642 if( h->sps->b_direct8x8_inference )
644 if( h->mb.cache.ref[0][i8] >= 0 )
645 if( h->mb.cache.ref[1][i8] >= 0 )
646 x264_mb_mc_01xywh( h, x, y, 2, 2 );
648 x264_mb_mc_0xywh( h, x, y, 2, 2 );
650 x264_mb_mc_1xywh( h, x, y, 2, 2 );
654 if( h->mb.cache.ref[0][i8] >= 0 )
656 if( h->mb.cache.ref[1][i8] >= 0 )
658 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
659 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
660 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
661 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
665 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
666 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
667 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
668 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
673 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
674 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
675 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
676 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
681 void x264_mb_mc( x264_t *h )
683 if( h->mb.i_type == P_L0 )
685 if( h->mb.i_partition == D_16x16 )
687 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
689 else if( h->mb.i_partition == D_16x8 )
691 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
692 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
694 else if( h->mb.i_partition == D_8x16 )
696 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
697 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
700 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
703 for( i = 0; i < 4; i++ )
705 const int x = 2*(i%2);
706 const int y = 2*(i/2);
707 switch( h->mb.i_sub_partition[i] )
710 x264_mb_mc_0xywh( h, x, y, 2, 2 );
713 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
714 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
717 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
718 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
721 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
722 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
723 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
724 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
727 x264_mb_mc_1xywh( h, x, y, 2, 2 );
730 x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
731 x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
734 x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
735 x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
738 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
739 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
740 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
741 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
744 x264_mb_mc_01xywh( h, x, y, 2, 2 );
747 x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
748 x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
751 x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
752 x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
755 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
756 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
757 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
758 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
761 x264_mb_mc_direct8x8( h, x, y );
766 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
768 x264_mb_mc_direct8x8( h, 0, 0 );
769 x264_mb_mc_direct8x8( h, 2, 0 );
770 x264_mb_mc_direct8x8( h, 0, 2 );
771 x264_mb_mc_direct8x8( h, 2, 2 );
780 /* init ref list utilisations */
781 for( i = 0; i < 2; i++ )
783 b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
784 b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
786 if( h->mb.i_partition == D_16x16 )
788 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
789 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
790 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
792 else if( h->mb.i_partition == D_16x8 )
794 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
795 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
796 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
798 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
799 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
800 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
802 else if( h->mb.i_partition == D_8x16 )
804 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
805 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
806 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
808 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
809 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
810 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
815 void x264_macroblock_cache_init( x264_t *h )
818 int i_mb_count = h->mb.i_mb_count;
820 h->mb.i_mb_stride = h->sps->i_mb_width;
821 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
822 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
824 h->mb.type= x264_malloc( i_mb_count * sizeof( int8_t) );
825 h->mb.qp = x264_malloc( i_mb_count * sizeof( int8_t) );
826 h->mb.cbp = x264_malloc( i_mb_count * sizeof( int16_t) );
827 h->mb.skipbp = x264_malloc( i_mb_count * sizeof( int8_t) );
829 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
830 h->mb.intra4x4_pred_mode = x264_malloc( i_mb_count * 7 * sizeof( int8_t ) );
833 h->mb.non_zero_count = x264_malloc( i_mb_count * 24 * sizeof( uint8_t ) );
835 if( h->param.b_cabac )
837 h->mb.chroma_pred_mode = x264_malloc( i_mb_count * sizeof( int8_t) );
838 h->mb.mvd[0] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
839 h->mb.mvd[1] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
843 for( j=0; j < ( i ? 1 : h->param.i_frame_reference ); j++ )
844 h->mb.mvr[i][j] = x264_malloc( 2 * i_mb_count * sizeof( int16_t ) );
846 /* init with not avaiable (for top right idx=7,15) */
847 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
848 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
850 void x264_macroblock_cache_end( x264_t *h )
854 for( j=0; j < ( i ? 1 : h->param.i_frame_reference ); j++ )
855 x264_free( h->mb.mvr[i][j] );
856 if( h->param.b_cabac )
858 x264_free( h->mb.chroma_pred_mode );
859 x264_free( h->mb.mvd[0] );
860 x264_free( h->mb.mvd[1] );
862 x264_free( h->mb.intra4x4_pred_mode );
863 x264_free( h->mb.non_zero_count );
864 x264_free( h->mb.skipbp );
865 x264_free( h->mb.cbp );
866 x264_free( h->mb.qp );
867 x264_free( h->mb.type );
869 void x264_macroblock_slice_init( x264_t *h )
873 h->mb.mv[0] = h->fdec->mv[0];
874 h->mb.mv[1] = h->fdec->mv[1];
875 h->mb.ref[0] = h->fdec->ref[0];
876 h->mb.ref[1] = h->fdec->ref[1];
878 h->fdec->i_ref[0] = h->i_ref0;
879 h->fdec->i_ref[1] = h->i_ref1;
880 for( i = 0; i < h->i_ref0; i++ )
881 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
882 if( h->sh.i_type == SLICE_TYPE_B )
884 for( i = 0; i < h->i_ref1; i++ )
885 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
890 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
892 const int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
893 const int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
897 int i_top_type = -1; /* gcc warn */
903 h->mb.i_mb_x = i_mb_x;
904 h->mb.i_mb_y = i_mb_y;
905 h->mb.i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
906 h->mb.i_b8_xy = i_mb_8x8;
907 h->mb.i_b4_xy = i_mb_4x4;
908 h->mb.i_neighbour = 0;
910 /* load picture pointers */
911 for( i = 0; i < 3; i++ )
913 const int w = (i == 0 ? 16 : 8);
914 const int i_stride = h->fdec->i_stride[i];
917 h->mb.pic.i_stride[i] = i_stride;
919 h->mb.pic.p_fenc[i] = &h->fenc->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
921 h->mb.pic.p_fdec[i] = &h->fdec->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
923 for( j = 0; j < h->i_ref0; j++ )
925 h->mb.pic.p_fref[0][j][i==0 ? 0:i+3] = &h->fref0[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
926 h->mb.pic.p_fref[0][j][i+1] = &h->fref0[j]->filtered[i+1][ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
928 for( j = 0; j < h->i_ref1; j++ )
930 h->mb.pic.p_fref[1][j][i==0 ? 0:i+3] = &h->fref1[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
931 h->mb.pic.p_fref[1][j][i+1] = &h->fref1[j]->filtered[i+1][ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
938 i_top_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
939 i_top_type= h->mb.type[i_top_xy];
941 h->mb.i_neighbour |= MB_TOP;
944 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
945 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
946 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
947 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
949 /* load non_zero_count */
950 h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
951 h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
952 h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
953 h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
955 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
956 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
958 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
959 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
964 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
965 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
966 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
967 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
969 /* load non_zero_count */
970 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
971 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
972 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
973 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
974 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
975 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
976 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
977 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
983 i_left_xy = h->mb.i_mb_xy - 1;
984 i_left_type= h->mb.type[i_left_xy];
986 h->mb.i_neighbour |= MB_LEFT;
989 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
990 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
991 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
992 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
994 /* load non_zero_count */
995 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
996 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
997 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
998 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1000 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1001 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1003 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1004 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1008 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1009 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1010 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1011 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1013 /* load non_zero_count */
1014 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1015 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1016 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1017 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1018 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1019 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1020 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1021 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1024 if( i_mb_y > 0 && i_mb_x < h->sps->i_mb_width - 1 )
1026 h->mb.i_neighbour |= MB_TOPRIGHT;
1029 /* load ref/mv/mvd */
1030 if( h->sh.i_type != SLICE_TYPE_I )
1032 const int s8x8 = h->mb.i_b8_stride;
1033 const int s4x4 = h->mb.i_b4_stride;
1035 int i_top_left_xy = -1;
1036 int i_top_right_xy = -1;
1040 if( h->mb.i_mb_y > 0 && h->mb.i_mb_x > 0 )
1042 i_top_left_xy = i_top_xy - 1;
1044 if( h->mb.i_mb_y > 0 && h->mb.i_mb_x < h->sps->i_mb_width - 1 )
1046 i_top_right_xy = i_top_xy + 1;
1049 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1052 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1053 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1054 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1057 if( i_top_left_xy >= 0 )
1059 const int i8 = x264_scan8[0] - 1 - 1*8;
1060 const int ir = i_mb_8x8 - s8x8 - 1;
1061 const int iv = i_mb_4x4 - s4x4 - 1;
1062 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1063 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1064 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1068 const int i8 = x264_scan8[0] - 1 - 1*8;
1069 h->mb.cache.ref[i_list][i8] = -2;
1070 h->mb.cache.mv[i_list][i8][0] = 0;
1071 h->mb.cache.mv[i_list][i8][1] = 0;
1076 const int i8 = x264_scan8[0] - 8;
1077 const int ir = i_mb_8x8 - s8x8;
1078 const int iv = i_mb_4x4 - s4x4;
1080 h->mb.cache.ref[i_list][i8+0] =
1081 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1082 h->mb.cache.ref[i_list][i8+2] =
1083 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1085 for( i = 0; i < 4; i++ )
1087 h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
1088 h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
1093 const int i8 = x264_scan8[0] - 8;
1094 for( i = 0; i < 4; i++ )
1096 h->mb.cache.ref[i_list][i8+i] = -2;
1097 h->mb.cache.mv[i_list][i8+i][0] =
1098 h->mb.cache.mv[i_list][i8+i][1] = 0;
1102 if( i_top_right_xy >= 0 )
1104 const int i8 = x264_scan8[0] + 4 - 1*8;
1105 const int ir = i_mb_8x8 - s8x8 + 2;
1106 const int iv = i_mb_4x4 - s4x4 + 4;
1108 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1109 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1110 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1114 const int i8 = x264_scan8[0] + 4 - 1*8;
1115 h->mb.cache.ref[i_list][i8] = -2;
1116 h->mb.cache.mv[i_list][i8][0] = 0;
1117 h->mb.cache.mv[i_list][i8][1] = 0;
1120 if( i_left_xy >= 0 )
1122 const int i8 = x264_scan8[0] - 1;
1123 const int ir = i_mb_8x8 - 1;
1124 const int iv = i_mb_4x4 - 1;
1126 h->mb.cache.ref[i_list][i8+0*8] =
1127 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1128 h->mb.cache.ref[i_list][i8+2*8] =
1129 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1131 for( i = 0; i < 4; i++ )
1133 h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
1134 h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
1139 const int i8 = x264_scan8[0] - 1;
1140 for( i = 0; i < 4; i++ )
1142 h->mb.cache.ref[i_list][i8+i*8] = -2;
1143 h->mb.cache.mv[i_list][i8+i*8][0] =
1144 h->mb.cache.mv[i_list][i8+i*8][1] = 0;
1148 if( h->param.b_cabac )
1152 const int i8 = x264_scan8[0] - 8;
1153 const int iv = i_mb_4x4 - s4x4;
1154 for( i = 0; i < 4; i++ )
1156 h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
1157 h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
1162 const int i8 = x264_scan8[0] - 8;
1163 for( i = 0; i < 4; i++ )
1165 h->mb.cache.mvd[i_list][i8+i][0] =
1166 h->mb.cache.mvd[i_list][i8+i][1] = 0;
1170 if( i_left_xy >= 0 )
1172 const int i8 = x264_scan8[0] - 1;
1173 const int iv = i_mb_4x4 - 1;
1174 for( i = 0; i < 4; i++ )
1176 h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
1177 h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
1182 const int i8 = x264_scan8[0] - 1;
1183 for( i = 0; i < 4; i++ )
1185 h->mb.cache.mvd[i_list][i8+i*8][0] =
1186 h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
1193 if( h->param.b_cabac )
1195 if( h->sh.i_type == SLICE_TYPE_B )
1197 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1198 if( i_left_xy >= 0 )
1200 h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
1201 h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
1205 h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
1206 h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
1209 else if( h->mb.i_mb_xy == 0 && h->sh.i_type == SLICE_TYPE_P )
1211 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1217 void x264_macroblock_cache_save( x264_t *h )
1219 const int i_mb_xy = h->mb.i_mb_xy;
1220 const int i_mb_type = h->mb.i_type;
1221 const int s8x8 = h->mb.i_b8_stride;
1222 const int s4x4 = h->mb.i_b4_stride;
1223 const int i_mb_4x4 = h->mb.i_b4_xy;
1224 const int i_mb_8x8 = h->mb.i_b8_xy;
1228 if( IS_SKIP( h->mb.i_type ) )
1229 h->mb.qp[i_mb_xy] = h->mb.i_last_qp;
1231 h->mb.i_last_dqp = h->mb.qp[i_mb_xy] - h->mb.i_last_qp;
1232 h->mb.i_last_qp = h->mb.qp[i_mb_xy];
1235 if( i_mb_type == I_4x4 )
1237 h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1238 h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
1239 h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
1240 h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
1241 h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
1242 h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
1243 h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
1247 h->mb.intra4x4_pred_mode[i_mb_xy][0] =
1248 h->mb.intra4x4_pred_mode[i_mb_xy][1] =
1249 h->mb.intra4x4_pred_mode[i_mb_xy][2] =
1250 h->mb.intra4x4_pred_mode[i_mb_xy][3] =
1251 h->mb.intra4x4_pred_mode[i_mb_xy][4] =
1252 h->mb.intra4x4_pred_mode[i_mb_xy][5] =
1253 h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
1256 if( i_mb_type == I_PCM )
1258 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1259 for( i = 0; i < 16 + 2*4; i++ )
1261 h->mb.non_zero_count[i_mb_xy][i] = 16;
1266 /* save non zero count */
1267 for( i = 0; i < 16 + 2*4; i++ )
1269 h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
1273 if( !IS_INTRA( i_mb_type ) )
1276 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1280 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
1281 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
1282 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
1283 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
1285 for( y = 0; y < 4; y++ )
1287 for( x = 0; x < 4; x++ )
1289 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = h->mb.cache.mv[i_list][x264_scan8[0]+x+8*y][0];
1290 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = h->mb.cache.mv[i_list][x264_scan8[0]+x+8*y][1];
1298 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1302 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
1303 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
1304 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
1305 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
1307 for( y = 0; y < 4; y++ )
1309 for( x = 0; x < 4; x++ )
1311 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1312 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1318 if( h->param.b_cabac )
1320 if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1321 h->mb.chroma_pred_mode[i_mb_xy] = h->mb.i_chroma_pred_mode;
1323 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1325 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1328 for( i_list = 0; i_list < 2; i_list++ )
1330 const int s4x4 = 4 * h->mb.i_mb_stride;
1332 for( y = 0; y < 4; y++ )
1334 for( x = 0; x < 4; x++ )
1336 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = h->mb.cache.mvd[i_list][x264_scan8[0]+x+8*y][0];
1337 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = h->mb.cache.mvd[i_list][x264_scan8[0]+x+8*y][1];
1345 for( i_list = 0; i_list < 2; i_list++ )
1347 const int s4x4 = 4 * h->mb.i_mb_stride;
1349 for( y = 0; y < 4; y++ )
1351 for( x = 0; x < 4; x++ )
1353 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1354 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1359 if( h->sh.i_type == SLICE_TYPE_B )
1361 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1362 h->mb.skipbp[i_mb_xy] = 0xf;
1363 else if( i_mb_type == B_8x8 )
1366 for( i = 0; i < 4; i++ )
1367 skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1368 h->mb.skipbp[i_mb_xy] = skipbp;
1371 h->mb.skipbp[i_mb_xy] = 0;
1376 void x264_macroblock_bipred_init( x264_t *h )
1379 for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1381 int poc0 = h->fref0[i_ref0]->i_poc;
1382 for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1384 int dist_scale_factor;
1385 int poc1 = h->fref1[i_ref1]->i_poc;
1386 int td = x264_clip3( poc1 - poc0, -128, 127 );
1387 if( td == 0 /* || pic0 is a long-term ref */ )
1388 dist_scale_factor = 256;
1391 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1392 int tx = (16384 + (abs(td) >> 1)) / td;
1393 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1395 h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1397 dist_scale_factor >>= 2;
1398 if( h->param.analyse.b_weighted_bipred
1399 && dist_scale_factor >= -64
1400 && dist_scale_factor <= 128 )
1401 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1403 h->mb.bipred_weight[i_ref0][i_ref1] = 32;