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 if( IS_INTRA( h->fref1[0]->mb_type[ h->mb.i_mb_xy ] ) )
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 );
352 /* FIXME: optimize per block size */
353 for( i = 0; i < 4; i++ )
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 ] ];
362 const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
365 x264_macroblock_cache_ref( h, x8, y8, 2, 2, 0, i_ref );
367 for( y4 = y8; y4 < y8+2; y4++ )
368 for( x4 = x8; x4 < x8+2; x4++ )
370 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * 4 * h->mb.i_mb_stride ];
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] );
380 /* the colocated ref isn't in the current list0 */
381 /* FIXME: we might still be able to use direct_8x8 on some partitions */
389 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
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 ];
398 for( i_list=0; i_list<2; i_list++ )
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];
404 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
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 )
415 if( ref[0] < 0 && ref[1] < 0 )
426 for( i_list=0; i_list<2; i_list++ )
428 if( ref[i_list] >= 0 )
429 x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
431 mv[i_list][0] = mv[i_list][1] = 0;
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] );
441 for( i8=0; i8<4; i8++ )
445 if( l1ref[ x8 + y8 * h->mb.i_b8_stride ] == 0 )
447 for( i4=0; i4<4; i4++ )
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 )
455 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0, 0 );
457 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0, 0 );
466 int x264_mb_predict_mv_direct16x16( x264_t *h )
469 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
471 else if( h->sh.b_direct_spatial_mv_pred )
472 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
474 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
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));
489 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
491 const int x = 2*(idx%2);
492 const int y = 2*(idx/2);
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++ )
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];
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 )
508 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
512 if( h->sh.i_type == SLICE_TYPE_B )
514 if( h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
516 /* FIXME: use direct_mv to be clearer? */
517 int16_t *mvp = h->mb.cache.mv[i_list][x264_scan8[12]];
525 if( h->mb.i_mb_x > 0 )
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] ) )
531 mvc[i][0] = mvr[i_mb_l][0];
532 mvc[i][1] = mvr[i_mb_l][1];
536 if( h->mb.i_mb_y > 0 )
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] ) )
541 mvc[i][0] = mvr[i_mb_t][0];
542 mvc[i][1] = mvr[i_mb_t][1];
546 if( h->mb.i_mb_x > 0 && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
548 mvc[i][0] = mvr[i_mb_t - 1][0];
549 mvc[i][1] = mvr[i_mb_t - 1][1];
552 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
554 mvc[i][0] = mvr[i_mb_t + 1][0];
555 mvc[i][1] = mvr[i_mb_t + 1][1];
562 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
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] );
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 );
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 );
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 );
581 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
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] );
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 );
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 );
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 );
601 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
603 const int i8 = x264_scan8[0]+x+8*y;
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];
611 x264_mb_mc_0xywh( h, x, y, width, height );
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 );
616 if( h->param.analyse.b_weighted_bipred )
618 const int i_ref0 = h->mb.cache.ref[0][i8];
619 const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
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 );
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 );
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 );
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 );
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 );
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 );
645 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
647 const int i8 = x264_scan8[0] + x + 8*y;
649 /* FIXME: optimize based on current block size, not global settings? */
650 if( h->sps->b_direct8x8_inference )
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 );
656 x264_mb_mc_0xywh( h, x, y, 2, 2 );
658 x264_mb_mc_1xywh( h, x, y, 2, 2 );
662 if( h->mb.cache.ref[0][i8] >= 0 )
664 if( h->mb.cache.ref[1][i8] >= 0 )
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 );
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 );
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 );
689 void x264_mb_mc( x264_t *h )
691 if( h->mb.i_type == P_L0 )
693 if( h->mb.i_partition == D_16x16 )
695 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
697 else if( h->mb.i_partition == D_16x8 )
699 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
700 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
702 else if( h->mb.i_partition == D_8x16 )
704 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
705 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
708 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
711 for( i = 0; i < 4; i++ )
713 const int x = 2*(i%2);
714 const int y = 2*(i/2);
715 switch( h->mb.i_sub_partition[i] )
718 x264_mb_mc_0xywh( h, x, y, 2, 2 );
721 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
722 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
725 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
726 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
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 );
735 x264_mb_mc_1xywh( h, x, y, 2, 2 );
738 x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
739 x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
742 x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
743 x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
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 );
752 x264_mb_mc_01xywh( h, x, y, 2, 2 );
755 x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
756 x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
759 x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
760 x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
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 );
769 x264_mb_mc_direct8x8( h, x, y );
774 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
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 );
788 /* init ref list utilisations */
789 for( i = 0; i < 2; i++ )
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];
794 if( h->mb.i_partition == D_16x16 )
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 );
800 else if( h->mb.i_partition == D_16x8 )
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 );
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 );
810 else if( h->mb.i_partition == D_8x16 )
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 );
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 );
823 void x264_macroblock_cache_init( x264_t *h )
826 int i_mb_count = h->mb.i_mb_count;
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;
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) );
836 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
837 h->mb.intra4x4_pred_mode = x264_malloc( i_mb_count * 7 * sizeof( int8_t ) );
840 h->mb.non_zero_count = x264_malloc( i_mb_count * 24 * sizeof( uint8_t ) );
842 if( h->param.b_cabac )
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 ) );
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 ) );
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 ) );
860 void x264_macroblock_cache_end( x264_t *h )
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] );
869 if( h->param.b_cabac )
871 x264_free( h->mb.chroma_pred_mode );
872 x264_free( h->mb.mvd[0] );
873 x264_free( h->mb.mvd[1] );
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 );
881 void x264_macroblock_slice_init( x264_t *h )
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;
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 )
897 for( i = 0; i < h->i_ref1; i++ )
898 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
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++ )
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 )
909 h->mb.map_col_to_list0[i] = j;
917 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
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);
924 int i_top_type = -1; /* gcc warn */
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;
937 /* load picture pointers */
938 for( i = 0; i < 3; i++ )
940 const int w = (i == 0 ? 16 : 8);
941 const int i_stride = h->fdec->i_stride[i];
942 const int i_xy = i_mb_x + i_mb_y * i_stride;
945 h->mb.pic.i_stride[i] = i_stride;
947 h->mb.pic.p_fenc[i] = &h->fenc->plane[i][w*i_xy];
948 h->mb.pic.p_fdec[i] = &h->fdec->plane[i][w*i_xy];
950 for( j = 0; j < h->i_ref0; j++ )
952 h->mb.pic.p_fref[0][j][i==0 ? 0:i+3] = &h->fref0[j]->plane[i][w*i_xy];
953 h->mb.pic.p_fref[0][j][i+1] = &h->fref0[j]->filtered[i+1][16*i_xy];
955 for( j = 0; j < h->i_ref1; j++ )
957 h->mb.pic.p_fref[1][j][i==0 ? 0:i+3] = &h->fref1[j]->plane[i][w*i_xy];
958 h->mb.pic.p_fref[1][j][i+1] = &h->fref1[j]->filtered[i+1][16*i_xy];
965 i_top_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
966 i_top_type= h->mb.type[i_top_xy];
968 h->mb.i_neighbour |= MB_TOP;
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];
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];
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];
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];
991 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
992 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
993 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
994 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
996 /* load non_zero_count */
997 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
998 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
999 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
1000 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
1001 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
1002 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1003 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1004 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1010 i_left_xy = h->mb.i_mb_xy - 1;
1011 i_left_type= h->mb.type[i_left_xy];
1013 h->mb.i_neighbour |= MB_LEFT;
1016 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1017 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1018 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1019 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1021 /* load non_zero_count */
1022 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
1023 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1024 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
1025 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1027 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1028 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1030 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1031 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1035 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1036 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1037 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1038 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1040 /* load non_zero_count */
1041 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1042 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1043 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1044 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1045 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1046 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1047 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1048 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1051 if( i_mb_y > 0 && i_mb_x < h->sps->i_mb_width - 1 )
1053 h->mb.i_neighbour |= MB_TOPRIGHT;
1056 /* load ref/mv/mvd */
1057 if( h->sh.i_type != SLICE_TYPE_I )
1059 const int s8x8 = h->mb.i_b8_stride;
1060 const int s4x4 = h->mb.i_b4_stride;
1062 int i_top_left_xy = -1;
1063 int i_top_right_xy = -1;
1067 if( h->mb.i_mb_y > 0 && h->mb.i_mb_x > 0 )
1069 i_top_left_xy = i_top_xy - 1;
1071 if( h->mb.i_mb_y > 0 && h->mb.i_mb_x < h->sps->i_mb_width - 1 )
1073 i_top_right_xy = i_top_xy + 1;
1076 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1079 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1080 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1081 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1084 if( i_top_left_xy >= 0 )
1086 const int i8 = x264_scan8[0] - 1 - 1*8;
1087 const int ir = i_mb_8x8 - s8x8 - 1;
1088 const int iv = i_mb_4x4 - s4x4 - 1;
1089 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1090 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1091 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1095 const int i8 = x264_scan8[0] - 1 - 1*8;
1096 h->mb.cache.ref[i_list][i8] = -2;
1097 h->mb.cache.mv[i_list][i8][0] = 0;
1098 h->mb.cache.mv[i_list][i8][1] = 0;
1103 const int i8 = x264_scan8[0] - 8;
1104 const int ir = i_mb_8x8 - s8x8;
1105 const int iv = i_mb_4x4 - s4x4;
1107 h->mb.cache.ref[i_list][i8+0] =
1108 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1109 h->mb.cache.ref[i_list][i8+2] =
1110 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1112 for( i = 0; i < 4; i++ )
1114 h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
1115 h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
1120 const int i8 = x264_scan8[0] - 8;
1121 for( i = 0; i < 4; i++ )
1123 h->mb.cache.ref[i_list][i8+i] = -2;
1124 h->mb.cache.mv[i_list][i8+i][0] =
1125 h->mb.cache.mv[i_list][i8+i][1] = 0;
1129 if( i_top_right_xy >= 0 )
1131 const int i8 = x264_scan8[0] + 4 - 1*8;
1132 const int ir = i_mb_8x8 - s8x8 + 2;
1133 const int iv = i_mb_4x4 - s4x4 + 4;
1135 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1136 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1137 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1141 const int i8 = x264_scan8[0] + 4 - 1*8;
1142 h->mb.cache.ref[i_list][i8] = -2;
1143 h->mb.cache.mv[i_list][i8][0] = 0;
1144 h->mb.cache.mv[i_list][i8][1] = 0;
1147 if( i_left_xy >= 0 )
1149 const int i8 = x264_scan8[0] - 1;
1150 const int ir = i_mb_8x8 - 1;
1151 const int iv = i_mb_4x4 - 1;
1153 h->mb.cache.ref[i_list][i8+0*8] =
1154 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1155 h->mb.cache.ref[i_list][i8+2*8] =
1156 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1158 for( i = 0; i < 4; i++ )
1160 h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
1161 h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
1166 const int i8 = x264_scan8[0] - 1;
1167 for( i = 0; i < 4; i++ )
1169 h->mb.cache.ref[i_list][i8+i*8] = -2;
1170 h->mb.cache.mv[i_list][i8+i*8][0] =
1171 h->mb.cache.mv[i_list][i8+i*8][1] = 0;
1175 if( h->param.b_cabac )
1179 const int i8 = x264_scan8[0] - 8;
1180 const int iv = i_mb_4x4 - s4x4;
1181 for( i = 0; i < 4; i++ )
1183 h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
1184 h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
1189 const int i8 = x264_scan8[0] - 8;
1190 for( i = 0; i < 4; i++ )
1192 h->mb.cache.mvd[i_list][i8+i][0] =
1193 h->mb.cache.mvd[i_list][i8+i][1] = 0;
1197 if( i_left_xy >= 0 )
1199 const int i8 = x264_scan8[0] - 1;
1200 const int iv = i_mb_4x4 - 1;
1201 for( i = 0; i < 4; i++ )
1203 h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
1204 h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
1209 const int i8 = x264_scan8[0] - 1;
1210 for( i = 0; i < 4; i++ )
1212 h->mb.cache.mvd[i_list][i8+i*8][0] =
1213 h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
1220 if( h->param.b_cabac )
1222 if( h->sh.i_type == SLICE_TYPE_B )
1224 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1225 if( i_left_xy >= 0 )
1227 h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
1228 h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
1232 h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
1233 h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
1236 else if( h->mb.i_mb_xy == 0 && h->sh.i_type == SLICE_TYPE_P )
1238 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1244 void x264_macroblock_cache_save( x264_t *h )
1246 const int i_mb_xy = h->mb.i_mb_xy;
1247 const int i_mb_type = h->mb.i_type;
1248 const int s8x8 = h->mb.i_b8_stride;
1249 const int s4x4 = h->mb.i_b4_stride;
1250 const int i_mb_4x4 = h->mb.i_b4_xy;
1251 const int i_mb_8x8 = h->mb.i_b8_xy;
1255 if( IS_SKIP( h->mb.i_type ) )
1256 h->mb.qp[i_mb_xy] = h->mb.i_last_qp;
1258 h->mb.i_last_dqp = h->mb.qp[i_mb_xy] - h->mb.i_last_qp;
1259 h->mb.i_last_qp = h->mb.qp[i_mb_xy];
1262 if( i_mb_type == I_4x4 )
1264 h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1265 h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
1266 h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
1267 h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
1268 h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
1269 h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
1270 h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
1274 h->mb.intra4x4_pred_mode[i_mb_xy][0] =
1275 h->mb.intra4x4_pred_mode[i_mb_xy][1] =
1276 h->mb.intra4x4_pred_mode[i_mb_xy][2] =
1277 h->mb.intra4x4_pred_mode[i_mb_xy][3] =
1278 h->mb.intra4x4_pred_mode[i_mb_xy][4] =
1279 h->mb.intra4x4_pred_mode[i_mb_xy][5] =
1280 h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
1283 if( i_mb_type == I_PCM )
1285 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1286 for( i = 0; i < 16 + 2*4; i++ )
1288 h->mb.non_zero_count[i_mb_xy][i] = 16;
1293 /* save non zero count */
1294 for( i = 0; i < 16 + 2*4; i++ )
1296 h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
1300 if( !IS_INTRA( i_mb_type ) )
1303 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1307 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
1308 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
1309 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
1310 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
1312 for( y = 0; y < 4; y++ )
1314 for( x = 0; x < 4; x++ )
1316 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = h->mb.cache.mv[i_list][x264_scan8[0]+x+8*y][0];
1317 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = h->mb.cache.mv[i_list][x264_scan8[0]+x+8*y][1];
1325 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1329 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
1330 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
1331 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
1332 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
1334 for( y = 0; y < 4; y++ )
1336 for( x = 0; x < 4; x++ )
1338 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1339 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1345 if( h->param.b_cabac )
1347 if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1348 h->mb.chroma_pred_mode[i_mb_xy] = h->mb.i_chroma_pred_mode;
1350 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1352 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1355 for( i_list = 0; i_list < 2; i_list++ )
1357 const int s4x4 = 4 * h->mb.i_mb_stride;
1359 for( y = 0; y < 4; y++ )
1361 for( x = 0; x < 4; x++ )
1363 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = h->mb.cache.mvd[i_list][x264_scan8[0]+x+8*y][0];
1364 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = h->mb.cache.mvd[i_list][x264_scan8[0]+x+8*y][1];
1372 for( i_list = 0; i_list < 2; i_list++ )
1374 const int s4x4 = 4 * h->mb.i_mb_stride;
1376 for( y = 0; y < 4; y++ )
1378 for( x = 0; x < 4; x++ )
1380 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1381 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1386 if( h->sh.i_type == SLICE_TYPE_B )
1388 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1389 h->mb.skipbp[i_mb_xy] = 0xf;
1390 else if( i_mb_type == B_8x8 )
1393 for( i = 0; i < 4; i++ )
1394 skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1395 h->mb.skipbp[i_mb_xy] = skipbp;
1398 h->mb.skipbp[i_mb_xy] = 0;
1403 void x264_macroblock_bipred_init( x264_t *h )
1406 for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1408 int poc0 = h->fref0[i_ref0]->i_poc;
1409 for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1411 int dist_scale_factor;
1412 int poc1 = h->fref1[i_ref1]->i_poc;
1413 int td = x264_clip3( poc1 - poc0, -128, 127 );
1414 if( td == 0 /* || pic0 is a long-term ref */ )
1415 dist_scale_factor = 256;
1418 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1419 int tx = (16384 + (abs(td) >> 1)) / td;
1420 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1422 h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1424 dist_scale_factor >>= 2;
1425 if( h->param.analyse.b_weighted_bipred
1426 && dist_scale_factor >= -64
1427 && dist_scale_factor <= 128 )
1428 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1430 h->mb.bipred_weight[i_ref0][i_ref1] = 32;