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 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
31 const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
32 const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
33 const int m = X264_MIN( x264_mb_pred_mode4x4_fix(ma),
34 x264_mb_pred_mode4x4_fix(mb) );
42 int x264_mb_predict_non_zero_code( x264_t *h, int idx )
44 const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1];
45 const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8];
51 i_ret = ( i_ret + 1 ) >> 1;
56 int x264_mb_transform_8x8_allowed( x264_t *h )
58 if( IS_SKIP( h->mb.i_type ) )
60 if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
63 for( i = 0; i < 4; i++ )
64 if( !IS_SUB8x8(h->mb.i_sub_partition[i])
65 || ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 && !h->sps->b_direct8x8_inference ) )
70 if( h->mb.i_type == B_DIRECT && !h->sps->b_direct8x8_inference )
76 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
78 const int i8 = x264_scan8[idx];
79 const int i_ref= h->mb.cache.ref[i_list][i8];
80 int i_refa = h->mb.cache.ref[i_list][i8 - 1];
81 int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
82 int i_refb = h->mb.cache.ref[i_list][i8 - 8];
83 int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
84 int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
85 int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
89 if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
91 i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
92 mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
95 if( h->mb.i_partition == D_16x8 )
97 if( idx == 0 && i_refb == i_ref )
103 else if( idx != 0 && i_refa == i_ref )
110 else if( h->mb.i_partition == D_8x16 )
112 if( idx == 0 && i_refa == i_ref )
118 else if( idx != 0 && i_refc == i_ref )
127 if( i_refa == i_ref ) i_count++;
128 if( i_refb == i_ref ) i_count++;
129 if( i_refc == i_ref ) i_count++;
133 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
134 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
136 else if( i_count == 1 )
138 if( i_refa == i_ref )
143 else if( i_refb == i_ref )
154 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
161 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
162 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
166 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int mvp[2] )
168 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
169 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
170 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
171 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
172 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
173 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
179 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
180 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
184 if( i_refa == i_ref ) i_count++;
185 if( i_refb == i_ref ) i_count++;
186 if( i_refc == i_ref ) i_count++;
190 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
191 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
193 else if( i_count == 1 )
195 if( i_refa == i_ref )
200 else if( i_refb == i_ref )
211 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
218 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
219 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
224 void x264_mb_predict_mv_pskip( x264_t *h, int mv[2] )
226 int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
227 int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
228 int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
229 int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
231 if( i_refa == -2 || i_refb == -2 ||
232 ( i_refa == 0 && mv_a[0] == 0 && mv_a[1] == 0 ) ||
233 ( i_refb == 0 && mv_b[0] == 0 && mv_b[1] == 0 ) )
239 x264_mb_predict_mv_16x16( h, 0, 0, mv );
243 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
245 int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
246 int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
249 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
251 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
253 if( IS_INTRA( type_col ) )
255 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
256 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0, 0 );
257 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0, 0 );
260 b8x8 = h->sps->b_direct8x8_inference ||
261 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
263 for( i8 = 0; i8 < 4; i8++ )
267 const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
268 const int i_ref = h->mb.map_col_to_list0[ h->fref1[0]->ref[0][ i_part_8x8 ] ];
272 const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
274 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
278 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
280 mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
281 mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
282 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, mv_l0[0], mv_l0[1] );
283 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
287 for( i4 = 0; i4 < 4; i4++ )
289 const int x4 = i4%2 + 2*x8;
290 const int y4 = i4/2 + 2*y8;
291 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * h->mb.i_b4_stride ];
293 mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
294 mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
295 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, mv_l0[0], mv_l0[1] );
296 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
302 /* the colocated ref isn't in the current list0 */
303 /* FIXME: we might still be able to use direct_8x8 on some partitions */
304 /* FIXME: with B-pyramid + extensive ref list reordering
305 * (not currently used), we would also have to check
306 * l1mv1 like in spatial mode */
314 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
321 const int8_t *l1ref0 = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
322 const int8_t *l1ref1 = &h->fref1[0]->ref[1][ h->mb.i_b8_xy ];
323 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
324 const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[1][ h->mb.i_b4_xy ];
325 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
327 for( i_list=0; i_list<2; i_list++ )
329 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
330 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
331 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
333 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
335 ref[i_list] = i_refa;
336 if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
337 ref[i_list] = i_refb;
338 if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
339 ref[i_list] = i_refc;
340 if( ref[i_list] < 0 )
344 if( ref[0] < 0 && ref[1] < 0 )
355 for( i_list=0; i_list<2; i_list++ )
357 if( ref[i_list] >= 0 )
358 x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
360 mv[i_list][0] = mv[i_list][1] = 0;
364 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
365 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
366 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, mv[0][0], mv[0][1] );
367 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, mv[1][0], mv[1][1] );
369 if( IS_INTRA( type_col ) )
371 b8x8 = h->sps->b_direct8x8_inference ||
372 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
375 for( i8=0; i8<4; i8++ )
379 const int o8 = x8 + y8 * h->mb.i_b8_stride;
380 if( l1ref0[o8] == 0 || ( l1ref0[o8] < 0 && l1ref1[o8] == 0 ) )
382 const int16_t (*l1mv)[2] = (l1ref0[o8] == 0) ? l1mv0 : l1mv1;
385 const int16_t *mvcol = l1mv[3*x8 + 3*y8 * h->mb.i_b4_stride];
386 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
389 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0, 0 );
391 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0, 0 );
396 for( i4=0; i4<4; i4++ )
398 const int x4 = i4%2 + 2*x8;
399 const int y4 = i4/2 + 2*y8;
400 const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
401 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
404 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0, 0 );
406 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0, 0 );
416 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
419 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
421 else if( h->sh.b_direct_spatial_mv_pred )
422 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
424 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
426 if( b_changed != NULL && b_available )
428 int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
429 if( IS_INTRA(type_col) || type_col == P_SKIP )
431 *b_changed = h->mb.cache.direct_ref[0][0] != h->mb.cache.ref[0][X264_SCAN8_0]
432 || h->mb.cache.direct_ref[1][0] != h->mb.cache.ref[1][X264_SCAN8_0]
433 || *(uint32_t*)h->mb.cache.direct_mv[0][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[0][X264_SCAN8_0]
434 || *(uint32_t*)h->mb.cache.direct_mv[1][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[1][X264_SCAN8_0];
440 for( l = 0; l < 2; l++ )
441 for( i = 0; i < 4; i++ )
442 *b_changed |= h->mb.cache.direct_ref[l][i] != h->mb.cache.ref[l][x264_scan8[i*4]];
443 *b_changed = *b_changed || memcmp(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
453 for( l = 0; l < 2; l++ )
454 for( i = 0; i < 4; i++ )
455 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
456 memcpy(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
462 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
464 const int x = 2*(idx%2);
465 const int y = 2*(idx/2);
467 x264_macroblock_cache_ref( h, x, y, 2, 2, 0, h->mb.cache.direct_ref[0][idx] );
468 x264_macroblock_cache_ref( h, x, y, 2, 2, 1, h->mb.cache.direct_ref[1][idx] );
469 for( l = 0; l < 2; l++ )
471 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]] =
472 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]];
473 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]+8] =
474 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]+8];
478 /* This just improves encoder performance, it's not part of the spec */
479 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[8][2], int *i_mvc )
481 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
484 #define SET_MVP(mvp) { \
485 mvc[i][0] = mvp[0]; \
486 mvc[i][1] = mvp[1]; \
491 if( h->sh.i_type == SLICE_TYPE_B
492 && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
494 SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
497 /* spatial predictors */
498 if( h->mb.i_neighbour & MB_LEFT )
500 int i_mb_l = h->mb.i_mb_xy - 1;
501 /* skip MBs didn't go through the whole search process, so mvr is undefined */
502 if( !IS_SKIP( h->mb.type[i_mb_l] ) )
503 SET_MVP( mvr[i_mb_l] );
505 if( h->mb.i_neighbour & MB_TOP )
507 int i_mb_t = h->mb.i_mb_xy - h->mb.i_mb_stride;
508 if( !IS_SKIP( h->mb.type[i_mb_t] ) )
509 SET_MVP( mvr[i_mb_t] );
511 if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
512 SET_MVP( mvr[i_mb_t-1] );
513 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
514 SET_MVP( mvr[i_mb_t+1] );
518 /* temporal predictors */
519 if( h->fref0[0]->i_ref[0] > 0 )
521 x264_frame_t *l0 = h->fref0[0];
522 int ref_col_cur, ref_col_prev = -1;
525 #define SET_TMVP(dx, dy) { \
526 int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
527 int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
528 ref_col_cur = l0->ref[0][i_b8]; \
529 if( ref_col_cur >= 0 ) \
531 /* TODO: calc once per frame and tablize? */\
532 if( ref_col_cur != ref_col_prev ) \
533 scale = 256 * (h->fenc->i_poc - h->fref0[i_ref]->i_poc) \
534 / (l0->i_poc - l0->ref_poc[0][ref_col_cur]); \
535 mvc[i][0] = l0->mv[0][i_b4][0] * scale / 256; \
536 mvc[i][1] = l0->mv[0][i_b4][1] * scale / 256; \
538 ref_col_prev = ref_col_cur; \
543 if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
545 if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
553 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
555 const int i8 = x264_scan8[0]+x+8*y;
556 const int i_ref = h->mb.cache.ref[0][i8];
557 const int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
558 const int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
560 h->mc.mc_luma( h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
561 &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
562 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
564 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],
565 &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
566 mvx, mvy, 2*width, 2*height );
568 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],
569 &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
570 mvx, mvy, 2*width, 2*height );
572 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
574 const int i8 = x264_scan8[0]+x+8*y;
575 const int i_ref = h->mb.cache.ref[1][i8];
576 const int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
577 const int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
579 h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
580 &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
581 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
583 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],
584 &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
585 mvx, mvy, 2*width, 2*height );
587 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],
588 &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
589 mvx, mvy, 2*width, 2*height );
592 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
594 const int i8 = x264_scan8[0]+x+8*y;
596 const int i_ref1 = h->mb.cache.ref[1][i8];
597 const int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
598 const int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
599 DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
600 int i_mode = x264_size2pixel[height][width];
602 x264_mb_mc_0xywh( h, x, y, width, height );
604 h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
605 tmp, 16, mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
607 if( h->param.analyse.b_weighted_bipred )
609 const int i_ref0 = h->mb.cache.ref[0][i8];
610 const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
612 h->mc.avg_weight[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16, weight );
614 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],
615 tmp, 16, mvx1, mvy1, 2*width, 2*height );
616 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
618 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],
619 tmp, 16, mvx1, mvy1, 2*width, 2*height );
620 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
624 h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16 );
626 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],
627 tmp, 16, mvx1, mvy1, 2*width, 2*height );
628 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
630 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],
631 tmp, 16, mvx1, mvy1, 2*width, 2*height );
632 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
636 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
638 const int i8 = x264_scan8[0] + x + 8*y;
640 /* FIXME: optimize based on current block size, not global settings? */
641 if( h->sps->b_direct8x8_inference )
643 if( h->mb.cache.ref[0][i8] >= 0 )
644 if( h->mb.cache.ref[1][i8] >= 0 )
645 x264_mb_mc_01xywh( h, x, y, 2, 2 );
647 x264_mb_mc_0xywh( h, x, y, 2, 2 );
649 x264_mb_mc_1xywh( h, x, y, 2, 2 );
653 if( h->mb.cache.ref[0][i8] >= 0 )
655 if( h->mb.cache.ref[1][i8] >= 0 )
657 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
658 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
659 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
660 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
664 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
665 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
666 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
667 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
672 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
673 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
674 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
675 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
680 void x264_mb_mc_8x8( x264_t *h, int i8 )
682 const int x = 2*(i8&1);
683 const int y = 2*(i8>>1);
684 switch( h->mb.i_sub_partition[i8] )
687 x264_mb_mc_0xywh( h, x, y, 2, 2 );
690 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
691 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
694 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
695 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
698 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
699 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
700 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
701 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
704 x264_mb_mc_1xywh( h, x, y, 2, 2 );
707 x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
708 x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
711 x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
712 x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
715 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
716 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
717 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
718 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
721 x264_mb_mc_01xywh( h, x, y, 2, 2 );
724 x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
725 x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
728 x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
729 x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
732 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
733 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
734 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
735 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
738 x264_mb_mc_direct8x8( h, x, y );
743 void x264_mb_mc( x264_t *h )
745 if( h->mb.i_type == P_L0 )
747 if( h->mb.i_partition == D_16x16 )
749 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
751 else if( h->mb.i_partition == D_16x8 )
753 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
754 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
756 else if( h->mb.i_partition == D_8x16 )
758 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
759 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
762 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
765 for( i = 0; i < 4; i++ )
766 x264_mb_mc_8x8( h, i );
768 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
770 x264_mb_mc_direct8x8( h, 0, 0 );
771 x264_mb_mc_direct8x8( h, 2, 0 );
772 x264_mb_mc_direct8x8( h, 0, 2 );
773 x264_mb_mc_direct8x8( h, 2, 2 );
782 /* init ref list utilisations */
783 for( i = 0; i < 2; i++ )
785 b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
786 b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
788 if( h->mb.i_partition == D_16x16 )
790 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
791 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
792 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
794 else if( h->mb.i_partition == D_16x8 )
796 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
797 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
798 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
800 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
801 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
802 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
804 else if( h->mb.i_partition == D_8x16 )
806 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
807 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
808 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
810 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
811 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
812 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
817 int x264_macroblock_cache_init( x264_t *h )
820 int i_mb_count = h->mb.i_mb_count;
822 h->mb.i_mb_stride = h->sps->i_mb_width;
823 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
824 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
826 CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
827 CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
828 CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
829 CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
831 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
832 CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 7 * sizeof(int8_t) );
835 CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
837 if( h->param.b_cabac )
839 CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
840 CHECKED_MALLOC( h->mb.mvd[0], 2*16 * i_mb_count * sizeof(int16_t) );
841 CHECKED_MALLOC( h->mb.mvd[1], 2*16 * i_mb_count * sizeof(int16_t) );
846 int i_refs = (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid;
847 for( j=0; j < i_refs && j < 16; j++ )
848 CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
851 /* init with not avaiable (for top right idx=7,15) */
852 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
853 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
858 void x264_macroblock_cache_end( x264_t *h )
863 int i_refs = i ? 1 + h->param.b_bframe_pyramid : h->param.i_frame_reference;
864 for( j=0; j < i_refs; j++ )
865 x264_free( h->mb.mvr[i][j] );
867 if( h->param.b_cabac )
869 x264_free( h->mb.chroma_pred_mode );
870 x264_free( h->mb.mvd[0] );
871 x264_free( h->mb.mvd[1] );
873 x264_free( h->mb.intra4x4_pred_mode );
874 x264_free( h->mb.non_zero_count );
875 x264_free( h->mb.mb_transform_size );
876 x264_free( h->mb.skipbp );
877 x264_free( h->mb.cbp );
878 x264_free( h->mb.qp );
880 void x264_macroblock_slice_init( x264_t *h )
884 h->mb.mv[0] = h->fdec->mv[0];
885 h->mb.mv[1] = h->fdec->mv[1];
886 h->mb.ref[0] = h->fdec->ref[0];
887 h->mb.ref[1] = h->fdec->ref[1];
888 h->mb.type = h->fdec->mb_type;
890 h->fdec->i_ref[0] = h->i_ref0;
891 h->fdec->i_ref[1] = h->i_ref1;
892 for( i = 0; i < h->i_ref0; i++ )
893 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
894 if( h->sh.i_type == SLICE_TYPE_B )
896 for( i = 0; i < h->i_ref1; i++ )
897 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
899 h->mb.map_col_to_list0[-1] = -1;
900 h->mb.map_col_to_list0[-2] = -2;
901 for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
903 int poc = h->fref1[0]->ref_poc[0][i];
904 h->mb.map_col_to_list0[i] = -2;
905 for( j = 0; j < h->i_ref0; j++ )
906 if( h->fref0[j]->i_poc == poc )
908 h->mb.map_col_to_list0[i] = j;
913 if( h->sh.i_type == SLICE_TYPE_P )
914 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
918 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
920 const int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
921 const int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
923 int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
924 int i_top_xy = i_mb_xy - h->mb.i_mb_stride;
926 int i_top_type = -1; /* gcc warn */
932 h->mb.i_mb_x = i_mb_x;
933 h->mb.i_mb_y = i_mb_y;
934 h->mb.i_mb_xy = i_mb_xy;
935 h->mb.i_b8_xy = i_mb_8x8;
936 h->mb.i_b4_xy = i_mb_4x4;
937 h->mb.i_neighbour = 0;
949 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
950 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
951 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
952 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
953 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
954 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
956 /* load picture pointers */
957 for( i = 0; i < 3; i++ )
959 const int w = (i == 0 ? 16 : 8);
960 const int i_stride = h->fdec->i_stride[i];
961 const uint8_t *plane_fdec = &h->fdec->plane[i][ w * (i_mb_x + i_mb_y * i_stride) ];
964 h->mb.pic.i_stride[i] = i_stride;
966 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
967 &h->fenc->plane[i][ w * (i_mb_x + i_mb_y * i_stride) ], i_stride, w );
968 memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], &plane_fdec[-1-i_stride], w*3/2+1 );
969 for( j = 0; j < w; j++ )
970 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride];
972 for( j = 0; j < h->i_ref0; j++ )
974 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 )];
975 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] )];
977 for( j = 0; j < h->i_ref1; j++ )
979 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 )];
980 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] )];
984 if( h->fdec->integral )
986 for( i = 0; i < h->i_ref0; i++ )
987 h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
988 for( i = 0; i < h->i_ref1; i++ )
989 h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
993 if( i_mb_xy >= h->sh.i_first_mb + h->mb.i_mb_stride )
995 h->mb.i_mb_type_top =
996 i_top_type= h->mb.type[i_top_xy];
998 h->mb.i_neighbour |= MB_TOP;
1001 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
1002 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
1003 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
1004 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
1006 /* load non_zero_count */
1007 h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
1008 h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
1009 h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
1010 h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
1012 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
1013 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
1015 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
1016 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
1020 h->mb.i_mb_type_top = -1;
1023 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
1024 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
1025 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
1026 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
1028 /* load non_zero_count */
1029 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
1030 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
1031 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
1032 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
1033 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
1034 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1035 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1036 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1040 if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1042 i_left_xy = i_mb_xy - 1;
1043 h->mb.i_mb_type_left =
1044 i_left_type = h->mb.type[i_left_xy];
1046 h->mb.i_neighbour |= MB_LEFT;
1049 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1050 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1051 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1052 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1054 /* load non_zero_count */
1055 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
1056 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1057 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
1058 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1060 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1061 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1063 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1064 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1068 h->mb.i_mb_type_left = -1;
1070 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1071 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1072 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1073 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1075 /* load non_zero_count */
1076 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1077 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1078 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1079 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1080 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1081 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1082 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1083 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1086 if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1088 h->mb.i_neighbour |= MB_TOPRIGHT;
1089 h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1092 h->mb.i_mb_type_topright = -1;
1093 if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1095 h->mb.i_neighbour |= MB_TOPLEFT;
1096 h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1099 h->mb.i_mb_type_topleft = -1;
1101 if( h->param.analyse.b_transform_8x8 )
1103 h->mb.cache.i_neighbour_transform_size =
1104 ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1105 + ( i_top_type >= 0 && h->mb.mb_transform_size[i_top_xy] );
1108 /* load ref/mv/mvd */
1109 if( h->sh.i_type != SLICE_TYPE_I )
1111 const int s8x8 = h->mb.i_b8_stride;
1112 const int s4x4 = h->mb.i_b4_stride;
1116 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1119 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1120 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1121 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1124 if( h->mb.i_neighbour & MB_TOPLEFT )
1126 const int i8 = x264_scan8[0] - 1 - 1*8;
1127 const int ir = i_mb_8x8 - s8x8 - 1;
1128 const int iv = i_mb_4x4 - s4x4 - 1;
1129 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1130 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1131 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1135 const int i8 = x264_scan8[0] - 1 - 1*8;
1136 h->mb.cache.ref[i_list][i8] = -2;
1137 h->mb.cache.mv[i_list][i8][0] = 0;
1138 h->mb.cache.mv[i_list][i8][1] = 0;
1141 if( h->mb.i_neighbour & MB_TOP )
1143 const int i8 = x264_scan8[0] - 8;
1144 const int ir = i_mb_8x8 - s8x8;
1145 const int iv = i_mb_4x4 - s4x4;
1147 h->mb.cache.ref[i_list][i8+0] =
1148 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1149 h->mb.cache.ref[i_list][i8+2] =
1150 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1152 for( i = 0; i < 4; i++ )
1154 h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
1155 h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
1160 const int i8 = x264_scan8[0] - 8;
1161 for( i = 0; i < 4; i++ )
1163 h->mb.cache.ref[i_list][i8+i] = -2;
1164 h->mb.cache.mv[i_list][i8+i][0] =
1165 h->mb.cache.mv[i_list][i8+i][1] = 0;
1169 if( h->mb.i_neighbour & MB_TOPRIGHT )
1171 const int i8 = x264_scan8[0] + 4 - 1*8;
1172 const int ir = i_mb_8x8 - s8x8 + 2;
1173 const int iv = i_mb_4x4 - s4x4 + 4;
1175 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1176 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1177 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1181 const int i8 = x264_scan8[0] + 4 - 1*8;
1182 h->mb.cache.ref[i_list][i8] = -2;
1183 h->mb.cache.mv[i_list][i8][0] = 0;
1184 h->mb.cache.mv[i_list][i8][1] = 0;
1187 if( h->mb.i_neighbour & MB_LEFT )
1189 const int i8 = x264_scan8[0] - 1;
1190 const int ir = i_mb_8x8 - 1;
1191 const int iv = i_mb_4x4 - 1;
1193 h->mb.cache.ref[i_list][i8+0*8] =
1194 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1195 h->mb.cache.ref[i_list][i8+2*8] =
1196 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1198 for( i = 0; i < 4; i++ )
1200 h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
1201 h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
1206 const int i8 = x264_scan8[0] - 1;
1207 for( i = 0; i < 4; i++ )
1209 h->mb.cache.ref[i_list][i8+i*8] = -2;
1210 h->mb.cache.mv[i_list][i8+i*8][0] =
1211 h->mb.cache.mv[i_list][i8+i*8][1] = 0;
1215 if( h->param.b_cabac )
1217 if( i_top_type >= 0 )
1219 const int i8 = x264_scan8[0] - 8;
1220 const int iv = i_mb_4x4 - s4x4;
1221 for( i = 0; i < 4; i++ )
1223 h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
1224 h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
1229 const int i8 = x264_scan8[0] - 8;
1230 for( i = 0; i < 4; i++ )
1232 h->mb.cache.mvd[i_list][i8+i][0] =
1233 h->mb.cache.mvd[i_list][i8+i][1] = 0;
1237 if( i_left_type >= 0 )
1239 const int i8 = x264_scan8[0] - 1;
1240 const int iv = i_mb_4x4 - 1;
1241 for( i = 0; i < 4; i++ )
1243 h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
1244 h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
1249 const int i8 = x264_scan8[0] - 1;
1250 for( i = 0; i < 4; i++ )
1252 h->mb.cache.mvd[i_list][i8+i*8][0] =
1253 h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
1260 if( h->sh.i_type == SLICE_TYPE_B && h->param.b_cabac )
1262 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1263 if( i_left_type >= 0 )
1265 h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
1266 h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
1268 if( i_top_type >= 0 )
1270 h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
1271 h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
1276 h->mb.i_neighbour4[0] =
1277 h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1278 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0);
1279 h->mb.i_neighbour4[4] =
1280 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1281 h->mb.i_neighbour4[2] =
1282 h->mb.i_neighbour4[8] =
1283 h->mb.i_neighbour4[10] =
1284 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1285 h->mb.i_neighbour4[3] =
1286 h->mb.i_neighbour4[7] =
1287 h->mb.i_neighbour4[11] =
1288 h->mb.i_neighbour4[13] =
1289 h->mb.i_neighbour4[15] =
1290 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
1291 h->mb.i_neighbour4[5] =
1292 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour & MB_TOPRIGHT)
1293 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1294 h->mb.i_neighbour4[6] =
1295 h->mb.i_neighbour4[9] =
1296 h->mb.i_neighbour4[12] =
1297 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
1300 void x264_macroblock_cache_save( x264_t *h )
1302 const int i_mb_xy = h->mb.i_mb_xy;
1303 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1304 const int s8x8 = h->mb.i_b8_stride;
1305 const int s4x4 = h->mb.i_b4_stride;
1306 const int i_mb_4x4 = h->mb.i_b4_xy;
1307 const int i_mb_8x8 = h->mb.i_b8_xy;
1311 for( i = 0; i < 3; i++ )
1314 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1315 &h->fdec->plane[i][ w * (h->mb.i_mb_x + h->mb.i_mb_y * h->fdec->i_stride[i]) ],
1316 h->fdec->i_stride[i],
1317 h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1320 h->mb.type[i_mb_xy] = i_mb_type;
1322 if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1323 h->mb.i_qp = h->mb.i_last_qp;
1324 h->mb.qp[i_mb_xy] = h->mb.i_qp;
1326 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1327 h->mb.i_last_qp = h->mb.i_qp;
1330 if( i_mb_type == I_4x4 )
1332 h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1333 h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
1334 h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
1335 h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
1336 h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
1337 h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
1338 h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
1342 h->mb.intra4x4_pred_mode[i_mb_xy][0] =
1343 h->mb.intra4x4_pred_mode[i_mb_xy][1] =
1344 h->mb.intra4x4_pred_mode[i_mb_xy][2] =
1345 h->mb.intra4x4_pred_mode[i_mb_xy][3] =
1346 h->mb.intra4x4_pred_mode[i_mb_xy][4] =
1347 h->mb.intra4x4_pred_mode[i_mb_xy][5] =
1348 h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
1351 if( i_mb_type == I_PCM )
1353 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1354 for( i = 0; i < 16 + 2*4; i++ )
1356 h->mb.non_zero_count[i_mb_xy][i] = 16;
1361 /* save non zero count */
1362 for( i = 0; i < 16 + 2*4; i++ )
1364 h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
1368 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1369 h->mb.b_transform_8x8 = 0;
1370 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1372 if( !IS_INTRA( i_mb_type ) )
1375 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1379 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
1380 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
1381 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
1382 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
1384 for( y = 0; y < 4; y++ )
1386 for( x = 0; x < 4; x++ )
1388 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];
1389 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];
1397 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1401 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
1402 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
1403 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
1404 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
1406 for( y = 0; y < 4; y++ )
1408 for( x = 0; x < 4; x++ )
1410 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1411 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1417 if( h->param.b_cabac )
1419 if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1420 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1422 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1424 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1427 for( i_list = 0; i_list < 2; i_list++ )
1429 const int s4x4 = 4 * h->mb.i_mb_stride;
1431 for( y = 0; y < 4; y++ )
1433 for( x = 0; x < 4; x++ )
1435 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];
1436 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];
1444 for( i_list = 0; i_list < 2; i_list++ )
1446 const int s4x4 = 4 * h->mb.i_mb_stride;
1448 for( y = 0; y < 4; y++ )
1450 for( x = 0; x < 4; x++ )
1452 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1453 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1458 if( h->sh.i_type == SLICE_TYPE_B )
1460 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1461 h->mb.skipbp[i_mb_xy] = 0xf;
1462 else if( i_mb_type == B_8x8 )
1465 for( i = 0; i < 4; i++ )
1466 skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1467 h->mb.skipbp[i_mb_xy] = skipbp;
1470 h->mb.skipbp[i_mb_xy] = 0;
1475 void x264_macroblock_bipred_init( x264_t *h )
1478 for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1480 int poc0 = h->fref0[i_ref0]->i_poc;
1481 for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1483 int dist_scale_factor;
1484 int poc1 = h->fref1[i_ref1]->i_poc;
1485 int td = x264_clip3( poc1 - poc0, -128, 127 );
1486 if( td == 0 /* || pic0 is a long-term ref */ )
1487 dist_scale_factor = 256;
1490 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1491 int tx = (16384 + (abs(td) >> 1)) / td;
1492 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1494 h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1496 dist_scale_factor >>= 2;
1497 if( h->param.analyse.b_weighted_bipred
1498 && dist_scale_factor >= -64
1499 && dist_scale_factor <= 128 )
1500 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1502 h->mb.bipred_weight[i_ref0][i_ref1] = 32;