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 *****************************************************************************/
26 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
28 const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
29 const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
30 const int m = X264_MIN( x264_mb_pred_mode4x4_fix(ma),
31 x264_mb_pred_mode4x4_fix(mb) );
39 int x264_mb_predict_non_zero_code( x264_t *h, int idx )
41 const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1];
42 const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8];
48 i_ret = ( i_ret + 1 ) >> 1;
53 int x264_mb_transform_8x8_allowed( x264_t *h )
55 // intra and skip are disallowed
56 // large partitions are allowed
57 // direct and 8x8 are conditional
58 static const uint8_t partition_tab[X264_MBTYPE_MAX] = {
59 0,0,0,0,1,2,0,2,1,1,1,1,1,1,1,1,1,2,0,
63 if( !h->pps->b_transform_8x8_mode )
65 p = partition_tab[h->mb.i_type];
68 else if( h->mb.i_type == B_DIRECT )
69 return h->sps->b_direct8x8_inference;
70 else if( h->mb.i_type == P_8x8 )
72 if( !(h->param.analyse.inter & X264_ANALYSE_PSUB8x8) )
75 if( h->mb.i_sub_partition[i] != D_L0_8x8 )
81 // x264 currently doesn't use sub-8x8 B partitions, so don't check for them
82 if( h->sps->b_direct8x8_inference )
85 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
91 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
93 const int i8 = x264_scan8[idx];
94 const int i_ref= h->mb.cache.ref[i_list][i8];
95 int i_refa = h->mb.cache.ref[i_list][i8 - 1];
96 int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
97 int i_refb = h->mb.cache.ref[i_list][i8 - 8];
98 int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
99 int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
100 int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
104 if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
106 i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
107 mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
110 if( h->mb.i_partition == D_16x8 )
112 if( idx == 0 && i_refb == i_ref )
118 else if( idx != 0 && i_refa == i_ref )
125 else if( h->mb.i_partition == D_8x16 )
127 if( idx == 0 && i_refa == i_ref )
133 else if( idx != 0 && i_refc == i_ref )
142 if( i_refa == i_ref ) i_count++;
143 if( i_refb == i_ref ) i_count++;
144 if( i_refc == i_ref ) i_count++;
148 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
149 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
151 else if( i_count == 1 )
153 if( i_refa == i_ref )
158 else if( i_refb == i_ref )
169 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
176 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
177 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
181 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int mvp[2] )
183 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
184 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
185 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
186 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
187 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
188 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
194 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
195 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
199 if( i_refa == i_ref ) i_count++;
200 if( i_refb == i_ref ) i_count++;
201 if( i_refc == i_ref ) i_count++;
205 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
206 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
208 else if( i_count == 1 )
210 if( i_refa == i_ref )
215 else if( i_refb == i_ref )
226 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
233 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
234 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
239 void x264_mb_predict_mv_pskip( x264_t *h, int mv[2] )
241 int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
242 int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
243 int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
244 int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
246 if( i_refa == -2 || i_refb == -2 ||
247 ( i_refa == 0 && mv_a[0] == 0 && mv_a[1] == 0 ) ||
248 ( i_refb == 0 && mv_b[0] == 0 && mv_b[1] == 0 ) )
254 x264_mb_predict_mv_16x16( h, 0, 0, mv );
258 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
260 int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
261 int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
264 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
266 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
268 if( IS_INTRA( type_col ) )
270 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
271 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0, 0 );
272 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0, 0 );
275 b8x8 = h->sps->b_direct8x8_inference ||
276 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
278 for( i8 = 0; i8 < 4; i8++ )
282 const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
283 const int i_ref = h->mb.map_col_to_list0[ h->fref1[0]->ref[0][ i_part_8x8 ] ];
287 const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
289 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
293 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
295 mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
296 mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
297 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, mv_l0[0], mv_l0[1] );
298 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
302 for( i4 = 0; i4 < 4; i4++ )
304 const int x4 = i4%2 + 2*x8;
305 const int y4 = i4/2 + 2*y8;
306 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * h->mb.i_b4_stride ];
308 mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
309 mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
310 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, mv_l0[0], mv_l0[1] );
311 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
317 /* the collocated ref isn't in the current list0 */
318 /* FIXME: we might still be able to use direct_8x8 on some partitions */
319 /* FIXME: with B-pyramid + extensive ref list reordering
320 * (not currently used), we would also have to check
321 * l1mv1 like in spatial mode */
326 if( h->param.i_threads > 1 )
328 int di = b8x8 ? 4 : 1;
329 for( i4=0; i4<16; i4+=di )
331 if( h->mb.cache.mv[0][x264_scan8[i4]][1] > h->mb.mv_max_spel[1]
332 || h->mb.cache.mv[1][x264_scan8[i4]][1] > h->mb.mv_max_spel[1] )
335 fprintf(stderr, "direct_temporal: (%d,%d) (%d,%d) > %d \n",
336 h->mb.cache.mv[0][x264_scan8[i4]][0],
337 h->mb.cache.mv[0][x264_scan8[i4]][1],
338 h->mb.cache.mv[1][x264_scan8[i4]][0],
339 h->mb.cache.mv[1][x264_scan8[i4]][1],
340 h->mb.mv_max_spel[1]);
350 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
357 const int8_t *l1ref0 = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
358 const int8_t *l1ref1 = &h->fref1[0]->ref[1][ h->mb.i_b8_xy ];
359 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
360 const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[1][ h->mb.i_b4_xy ];
361 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
363 for( i_list=0; i_list<2; i_list++ )
365 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
366 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
367 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
369 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
371 ref[i_list] = i_refa;
372 if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
373 ref[i_list] = i_refb;
374 if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
375 ref[i_list] = i_refc;
376 if( ref[i_list] < 0 )
380 if( ref[0] < 0 && ref[1] < 0 )
391 for( i_list=0; i_list<2; i_list++ )
393 if( ref[i_list] >= 0 )
394 x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
396 mv[i_list][0] = mv[i_list][1] = 0;
400 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
401 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
402 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, mv[0][0], mv[0][1] );
403 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, mv[1][0], mv[1][1] );
405 if( IS_INTRA( type_col ) )
408 if( h->param.i_threads > 1
409 && ( mv[0][1] > h->mb.mv_max_spel[1]
410 || mv[1][1] > h->mb.mv_max_spel[1] ) )
413 fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
414 mv[0][0], mv[0][1], mv[1][0], mv[1][1],
415 h->mb.mv_max_spel[1]);
420 b8x8 = h->sps->b_direct8x8_inference ||
421 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
424 for( i8=0; i8<4; i8++ )
428 const int o8 = x8 + y8 * h->mb.i_b8_stride;
429 if( l1ref0[o8] == 0 || ( l1ref0[o8] < 0 && l1ref1[o8] == 0 ) )
431 const int16_t (*l1mv)[2] = (l1ref0[o8] == 0) ? l1mv0 : l1mv1;
434 const int16_t *mvcol = l1mv[3*x8 + 3*y8 * h->mb.i_b4_stride];
435 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
438 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0, 0 );
440 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0, 0 );
445 for( i4=0; i4<4; i4++ )
447 const int x4 = i4%2 + 2*x8;
448 const int y4 = i4/2 + 2*y8;
449 const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
450 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
453 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0, 0 );
455 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0, 0 );
465 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
468 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
470 else if( h->sh.b_direct_spatial_mv_pred )
471 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
473 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
475 if( b_changed != NULL && b_available )
477 int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
478 if( IS_INTRA(type_col) || type_col == P_SKIP )
480 *b_changed = h->mb.cache.direct_ref[0][0] != h->mb.cache.ref[0][X264_SCAN8_0]
481 || h->mb.cache.direct_ref[1][0] != h->mb.cache.ref[1][X264_SCAN8_0]
482 || *(uint32_t*)h->mb.cache.direct_mv[0][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[0][X264_SCAN8_0]
483 || *(uint32_t*)h->mb.cache.direct_mv[1][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[1][X264_SCAN8_0];
489 for( l = 0; l < 2; l++ )
490 for( i = 0; i < 4; i++ )
491 *b_changed |= h->mb.cache.direct_ref[l][i] != h->mb.cache.ref[l][x264_scan8[i*4]];
492 *b_changed = *b_changed || memcmp(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
502 for( l = 0; l < 2; l++ )
503 for( i = 0; i < 4; i++ )
504 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
505 memcpy(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
511 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
513 const int x = 2*(idx%2);
514 const int y = 2*(idx/2);
516 x264_macroblock_cache_ref( h, x, y, 2, 2, 0, h->mb.cache.direct_ref[0][idx] );
517 x264_macroblock_cache_ref( h, x, y, 2, 2, 1, h->mb.cache.direct_ref[1][idx] );
518 for( l = 0; l < 2; l++ )
520 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]] =
521 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]];
522 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]+8] =
523 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]+8];
527 #define FIXED_SCALE 256
529 /* This just improves encoder performance, it's not part of the spec */
530 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[8][2], int *i_mvc )
532 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
535 #define SET_MVP(mvp) { \
536 mvc[i][0] = mvp[0]; \
537 mvc[i][1] = mvp[1]; \
542 if( h->sh.i_type == SLICE_TYPE_B
543 && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
545 SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
548 /* spatial predictors */
549 if( h->mb.i_neighbour & MB_LEFT )
551 int i_mb_l = h->mb.i_mb_xy - 1;
552 /* skip MBs didn't go through the whole search process, so mvr is undefined */
553 if( !IS_SKIP( h->mb.type[i_mb_l] ) )
554 SET_MVP( mvr[i_mb_l] );
556 if( h->mb.i_neighbour & MB_TOP )
558 int i_mb_t = h->mb.i_mb_top_xy;
559 if( !IS_SKIP( h->mb.type[i_mb_t] ) )
560 SET_MVP( mvr[i_mb_t] );
562 if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
563 SET_MVP( mvr[i_mb_t-1] );
564 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
565 SET_MVP( mvr[i_mb_t+1] );
569 /* temporal predictors */
570 /* FIXME temporal scaling w/ interlace */
571 if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
573 x264_frame_t *l0 = h->fref0[0];
575 #define SET_TMVP(dx, dy) { \
576 int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
577 int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
578 int ref_col = l0->ref[0][i_b8]; \
581 int scale = (h->fdec->i_poc - h->fdec->ref_poc[0][i_ref]) * l0->inv_ref_poc[ref_col];\
582 mvc[i][0] = l0->mv[0][i_b4][0] * scale / FIXED_SCALE; \
583 mvc[i][1] = l0->mv[0][i_b4][1] * scale / FIXED_SCALE; \
589 if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
591 if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
599 /* Set up a lookup table for delta pocs to reduce an IDIV to an IMUL */
600 static void setup_inverse_delta_pocs( x264_t *h )
603 for( i = 0; i < h->i_ref0; i++ )
605 int delta = h->fdec->i_poc - h->fref0[i]->i_poc;
606 h->fdec->inv_ref_poc[i] = (FIXED_SCALE + delta/2) / delta;
610 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
612 const int i8 = x264_scan8[0]+x+8*y;
613 const int i_ref = h->mb.cache.ref[0][i8];
614 const int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
615 int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
617 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
618 h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
619 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
621 // chroma is offset if MCing from a field of opposite parity
622 if( h->mb.b_interlaced & i_ref )
623 mvy += (h->mb.i_mb_y & 1)*4 - 2;
625 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
626 &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],
627 mvx, mvy, 2*width, 2*height );
629 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
630 &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],
631 mvx, mvy, 2*width, 2*height );
633 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
635 const int i8 = x264_scan8[0]+x+8*y;
636 const int i_ref = h->mb.cache.ref[1][i8];
637 const int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
638 int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
640 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
641 h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
642 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
644 if( h->mb.b_interlaced & i_ref )
645 mvy += (h->mb.i_mb_y & 1)*4 - 2;
647 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
648 &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],
649 mvx, mvy, 2*width, 2*height );
651 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
652 &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],
653 mvx, mvy, 2*width, 2*height );
656 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
658 const int i8 = x264_scan8[0]+x+8*y;
660 const int i_ref1 = h->mb.cache.ref[1][i8];
661 const int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
662 int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
663 DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
664 int i_mode = x264_size2pixel[height][width];
666 x264_mb_mc_0xywh( h, x, y, width, height );
668 h->mc.mc_luma( tmp, 16, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
669 mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
671 if( h->mb.b_interlaced & i_ref1 )
672 mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
674 if( h->param.analyse.b_weighted_bipred )
676 const int i_ref0 = h->mb.cache.ref[0][i8];
677 const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
679 h->mc.avg_weight[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16, weight );
681 h->mc.mc_chroma( tmp, 16, &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],
682 mvx1, mvy1, 2*width, 2*height );
683 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
685 h->mc.mc_chroma( tmp, 16, &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],
686 mvx1, mvy1, 2*width, 2*height );
687 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
691 h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16 );
693 h->mc.mc_chroma( tmp, 16, &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],
694 mvx1, mvy1, 2*width, 2*height );
695 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
697 h->mc.mc_chroma( tmp, 16, &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],
698 mvx1, mvy1, 2*width, 2*height );
699 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
703 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
705 const int i8 = x264_scan8[0] + x + 8*y;
707 /* FIXME: optimize based on current block size, not global settings? */
708 if( h->sps->b_direct8x8_inference )
710 if( h->mb.cache.ref[0][i8] >= 0 )
711 if( h->mb.cache.ref[1][i8] >= 0 )
712 x264_mb_mc_01xywh( h, x, y, 2, 2 );
714 x264_mb_mc_0xywh( h, x, y, 2, 2 );
716 x264_mb_mc_1xywh( h, x, y, 2, 2 );
720 if( h->mb.cache.ref[0][i8] >= 0 )
722 if( h->mb.cache.ref[1][i8] >= 0 )
724 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
725 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
726 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
727 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
731 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
732 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
733 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
734 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
739 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
740 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
741 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
742 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
747 void x264_mb_mc_8x8( x264_t *h, int i8 )
749 const int x = 2*(i8&1);
750 const int y = 2*(i8>>1);
751 switch( h->mb.i_sub_partition[i8] )
754 x264_mb_mc_0xywh( h, x, y, 2, 2 );
757 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
758 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
761 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
762 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
765 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
766 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
767 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
768 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
771 x264_mb_mc_1xywh( h, x, y, 2, 2 );
774 x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
775 x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
778 x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
779 x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
782 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
783 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
784 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
785 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
788 x264_mb_mc_01xywh( h, x, y, 2, 2 );
791 x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
792 x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
795 x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
796 x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
799 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
800 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
801 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
802 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
805 x264_mb_mc_direct8x8( h, x, y );
810 void x264_mb_mc( x264_t *h )
812 if( h->mb.i_type == P_L0 )
814 if( h->mb.i_partition == D_16x16 )
816 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
818 else if( h->mb.i_partition == D_16x8 )
820 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
821 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
823 else if( h->mb.i_partition == D_8x16 )
825 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
826 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
829 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
832 for( i = 0; i < 4; i++ )
833 x264_mb_mc_8x8( h, i );
835 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
837 x264_mb_mc_direct8x8( h, 0, 0 );
838 x264_mb_mc_direct8x8( h, 2, 0 );
839 x264_mb_mc_direct8x8( h, 0, 2 );
840 x264_mb_mc_direct8x8( h, 2, 2 );
849 /* init ref list utilisations */
850 for( i = 0; i < 2; i++ )
852 b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
853 b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
855 if( h->mb.i_partition == D_16x16 )
857 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
858 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
859 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
861 else if( h->mb.i_partition == D_16x8 )
863 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
864 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
865 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
867 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
868 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
869 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
871 else if( h->mb.i_partition == D_8x16 )
873 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
874 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
875 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
877 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
878 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
879 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
884 int x264_macroblock_cache_init( x264_t *h )
887 int i_mb_count = h->mb.i_mb_count;
889 h->mb.i_mb_stride = h->sps->i_mb_width;
890 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
891 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
893 h->mb.b_interlaced = h->param.b_interlaced;
895 CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
896 CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
897 CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
898 CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
900 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
901 CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 7 * sizeof(int8_t) );
904 CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
905 CHECKED_MALLOC( h->mb.nnz_backup, h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t) );
907 if( h->param.b_cabac )
909 CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
910 CHECKED_MALLOC( h->mb.mvd[0], 2*16 * i_mb_count * sizeof(int16_t) );
911 CHECKED_MALLOC( h->mb.mvd[1], 2*16 * i_mb_count * sizeof(int16_t) );
916 int i_refs = X264_MIN(16, (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid) << h->param.b_interlaced;
917 for( j=0; j < i_refs; j++ )
918 CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
921 for( i=0; i<=h->param.b_interlaced; i++ )
924 CHECKED_MALLOC( h->mb.intra_border_backup[i][j], h->fdec->i_stride[j] );
925 h->mb.intra_border_backup[i][j] += 8;
928 /* init with not available (for top right idx=7,15) */
929 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
930 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
935 void x264_macroblock_cache_end( x264_t *h )
938 for( i=0; i<=h->param.b_interlaced; i++ )
940 x264_free( h->mb.intra_border_backup[i][j] - 8 );
942 for( j=0; j<32; j++ )
943 x264_free( h->mb.mvr[i][j] );
944 if( h->param.b_cabac )
946 x264_free( h->mb.chroma_pred_mode );
947 x264_free( h->mb.mvd[0] );
948 x264_free( h->mb.mvd[1] );
950 x264_free( h->mb.intra4x4_pred_mode );
951 x264_free( h->mb.non_zero_count );
952 x264_free( h->mb.nnz_backup );
953 x264_free( h->mb.mb_transform_size );
954 x264_free( h->mb.skipbp );
955 x264_free( h->mb.cbp );
956 x264_free( h->mb.qp );
958 void x264_macroblock_slice_init( x264_t *h )
962 h->mb.mv[0] = h->fdec->mv[0];
963 h->mb.mv[1] = h->fdec->mv[1];
964 h->mb.ref[0] = h->fdec->ref[0];
965 h->mb.ref[1] = h->fdec->ref[1];
966 h->mb.type = h->fdec->mb_type;
968 h->fdec->i_ref[0] = h->i_ref0;
969 h->fdec->i_ref[1] = h->i_ref1;
970 for( i = 0; i < h->i_ref0; i++ )
971 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
972 if( h->sh.i_type == SLICE_TYPE_B )
974 for( i = 0; i < h->i_ref1; i++ )
975 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
977 h->mb.map_col_to_list0[-1] = -1;
978 h->mb.map_col_to_list0[-2] = -2;
979 for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
981 int poc = h->fref1[0]->ref_poc[0][i];
982 h->mb.map_col_to_list0[i] = -2;
983 for( j = 0; j < h->i_ref0; j++ )
984 if( h->fref0[j]->i_poc == poc )
986 h->mb.map_col_to_list0[i] = j;
991 if( h->sh.i_type == SLICE_TYPE_P )
992 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
994 setup_inverse_delta_pocs( h );
997 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
999 int stride_y = fenc->i_stride[0];
1000 int stride_uv = fenc->i_stride[1];
1001 int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
1002 int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
1003 h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
1004 fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
1007 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
1009 int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
1010 int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
1011 int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
1012 int i_top_y = i_mb_y - (1 << h->mb.b_interlaced);
1013 int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x;
1014 int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x;
1015 int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x;
1017 int i_top_type = -1; /* gcc warn */
1018 int i_left_type= -1;
1022 assert( h->mb.i_b8_stride == 2*h->mb.i_mb_stride );
1023 assert( h->mb.i_b4_stride == 4*h->mb.i_mb_stride );
1026 h->mb.i_mb_x = i_mb_x;
1027 h->mb.i_mb_y = i_mb_y;
1028 h->mb.i_mb_xy = i_mb_xy;
1029 h->mb.i_b8_xy = i_mb_8x8;
1030 h->mb.i_b4_xy = i_mb_4x4;
1031 h->mb.i_mb_top_xy = i_top_xy;
1032 h->mb.i_neighbour = 0;
1035 if( i_top_xy >= h->sh.i_first_mb )
1037 h->mb.i_mb_type_top =
1038 i_top_type= h->mb.type[i_top_xy];
1040 h->mb.i_neighbour |= MB_TOP;
1043 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
1044 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
1045 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
1046 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
1048 /* load non_zero_count */
1049 h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
1050 h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
1051 h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
1052 h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
1054 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
1055 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
1057 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
1058 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
1062 h->mb.i_mb_type_top = -1;
1065 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
1066 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
1067 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
1068 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
1070 /* load non_zero_count */
1071 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
1072 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
1073 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
1074 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
1075 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
1076 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1077 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1078 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1082 if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1084 i_left_xy = i_mb_xy - 1;
1085 h->mb.i_mb_type_left =
1086 i_left_type = h->mb.type[i_left_xy];
1088 h->mb.i_neighbour |= MB_LEFT;
1091 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1092 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1093 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1094 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1096 /* load non_zero_count */
1097 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
1098 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1099 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
1100 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1102 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1103 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1105 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1106 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1110 h->mb.i_mb_type_left = -1;
1112 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1113 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1114 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1115 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1117 /* load non_zero_count */
1118 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1119 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1120 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1121 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1122 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1123 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1124 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1125 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1128 if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1130 h->mb.i_neighbour |= MB_TOPRIGHT;
1131 h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1134 h->mb.i_mb_type_topright = -1;
1135 if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1137 h->mb.i_neighbour |= MB_TOPLEFT;
1138 h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1141 h->mb.i_mb_type_topleft = -1;
1143 if( h->pps->b_transform_8x8_mode )
1145 h->mb.cache.i_neighbour_transform_size =
1146 ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1147 + ( i_top_type >= 0 && h->mb.mb_transform_size[i_top_xy] );
1152 h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1153 h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1154 h->mb.cache.i_neighbour_interlaced =
1155 !!(h->mb.i_neighbour & MB_LEFT)
1156 + !!(h->mb.i_neighbour & MB_TOP);
1169 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
1170 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
1171 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
1172 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
1173 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
1174 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
1176 /* load picture pointers */
1177 for( i = 0; i < 3; i++ )
1179 const int w = (i == 0 ? 16 : 8);
1180 const int i_stride = h->fdec->i_stride[i];
1181 const int i_stride2 = i_stride << h->mb.b_interlaced;
1182 const int i_pix_offset = h->mb.b_interlaced
1183 ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride
1184 : w * (i_mb_x + i_mb_y * i_stride);
1185 int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
1186 const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
1187 const uint8_t *intra_fdec = &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
1188 x264_frame_t **fref[2] = { h->fref0, h->fref1 };
1191 if( h->mb.b_interlaced )
1192 ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
1194 h->mb.pic.i_stride[i] = i_stride2;
1196 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
1197 &h->fenc->plane[i][i_pix_offset], i_stride2, w );
1198 memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
1199 for( j = 0; j < w; j++ )
1200 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
1202 for( l=0; l<2; l++ )
1204 for( j=0; j<h->mb.pic.i_fref[l]; j++ )
1206 h->mb.pic.p_fref[l][j][i==0 ? 0:i+3] = &fref[l][j >> h->mb.b_interlaced]->plane[i][ref_pix_offset[j&1]];
1208 for( k = 1; k < 4; k++ )
1209 h->mb.pic.p_fref[l][j][k] = &fref[l][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
1214 if( h->fdec->integral )
1216 assert( !h->mb.b_interlaced );
1217 for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
1218 h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1219 for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
1220 h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1223 x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
1225 /* load ref/mv/mvd */
1226 if( h->sh.i_type != SLICE_TYPE_I )
1228 const int s8x8 = h->mb.i_b8_stride;
1229 const int s4x4 = h->mb.i_b4_stride;
1233 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1236 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1237 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1238 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1241 if( h->mb.i_neighbour & MB_TOPLEFT )
1243 const int i8 = x264_scan8[0] - 1 - 1*8;
1244 const int ir = i_top_8x8 - 1;
1245 const int iv = i_top_4x4 - 1;
1246 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1247 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1248 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1252 const int i8 = x264_scan8[0] - 1 - 1*8;
1253 h->mb.cache.ref[i_list][i8] = -2;
1254 h->mb.cache.mv[i_list][i8][0] = 0;
1255 h->mb.cache.mv[i_list][i8][1] = 0;
1258 if( h->mb.i_neighbour & MB_TOP )
1260 const int i8 = x264_scan8[0] - 8;
1261 const int ir = i_top_8x8;
1262 const int iv = i_top_4x4;
1263 h->mb.cache.ref[i_list][i8+0] =
1264 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1265 h->mb.cache.ref[i_list][i8+2] =
1266 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1268 for( i = 0; i < 4; i++ )
1270 h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
1271 h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
1276 const int i8 = x264_scan8[0] - 8;
1277 for( i = 0; i < 4; i++ )
1279 h->mb.cache.ref[i_list][i8+i] = -2;
1280 h->mb.cache.mv[i_list][i8+i][0] =
1281 h->mb.cache.mv[i_list][i8+i][1] = 0;
1285 if( h->mb.i_neighbour & MB_TOPRIGHT )
1287 const int i8 = x264_scan8[0] + 4 - 1*8;
1288 const int ir = i_top_8x8 + 2;
1289 const int iv = i_top_4x4 + 4;
1290 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1291 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1292 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1296 const int i8 = x264_scan8[0] + 4 - 1*8;
1297 h->mb.cache.ref[i_list][i8] = -2;
1298 h->mb.cache.mv[i_list][i8][0] = 0;
1299 h->mb.cache.mv[i_list][i8][1] = 0;
1302 if( h->mb.i_neighbour & MB_LEFT )
1304 const int i8 = x264_scan8[0] - 1;
1305 const int ir = i_mb_8x8 - 1;
1306 const int iv = i_mb_4x4 - 1;
1307 h->mb.cache.ref[i_list][i8+0*8] =
1308 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1309 h->mb.cache.ref[i_list][i8+2*8] =
1310 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1312 for( i = 0; i < 4; i++ )
1314 h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
1315 h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
1320 const int i8 = x264_scan8[0] - 1;
1321 for( i = 0; i < 4; i++ )
1323 h->mb.cache.ref[i_list][i8+i*8] = -2;
1324 h->mb.cache.mv[i_list][i8+i*8][0] =
1325 h->mb.cache.mv[i_list][i8+i*8][1] = 0;
1329 if( h->param.b_cabac )
1331 if( i_top_type >= 0 )
1333 const int i8 = x264_scan8[0] - 8;
1334 const int iv = i_top_4x4;
1335 for( i = 0; i < 4; i++ )
1337 h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
1338 h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
1343 const int i8 = x264_scan8[0] - 8;
1344 for( i = 0; i < 4; i++ )
1346 h->mb.cache.mvd[i_list][i8+i][0] =
1347 h->mb.cache.mvd[i_list][i8+i][1] = 0;
1351 if( i_left_type >= 0 )
1353 const int i8 = x264_scan8[0] - 1;
1354 const int iv = i_mb_4x4 - 1;
1355 for( i = 0; i < 4; i++ )
1357 h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
1358 h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
1363 const int i8 = x264_scan8[0] - 1;
1364 for( i = 0; i < 4; i++ )
1366 h->mb.cache.mvd[i_list][i8+i*8][0] =
1367 h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
1374 if( h->sh.i_type == SLICE_TYPE_B && h->param.b_cabac )
1376 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1377 if( i_left_type >= 0 )
1379 h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
1380 h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
1382 if( i_top_type >= 0 )
1384 h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
1385 h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
1389 if( h->sh.i_type == SLICE_TYPE_P )
1390 x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1393 h->mb.i_neighbour4[0] =
1394 h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1395 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0);
1396 h->mb.i_neighbour4[4] =
1397 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1398 h->mb.i_neighbour4[2] =
1399 h->mb.i_neighbour4[8] =
1400 h->mb.i_neighbour4[10] =
1401 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1402 h->mb.i_neighbour4[3] =
1403 h->mb.i_neighbour4[7] =
1404 h->mb.i_neighbour4[11] =
1405 h->mb.i_neighbour4[13] =
1406 h->mb.i_neighbour4[15] =
1407 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
1408 h->mb.i_neighbour4[5] =
1409 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour & MB_TOPRIGHT)
1410 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1411 h->mb.i_neighbour4[6] =
1412 h->mb.i_neighbour4[9] =
1413 h->mb.i_neighbour4[12] =
1414 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
1417 void x264_macroblock_cache_save( x264_t *h )
1419 const int i_mb_xy = h->mb.i_mb_xy;
1420 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1421 const int s8x8 = h->mb.i_b8_stride;
1422 const int s4x4 = h->mb.i_b4_stride;
1423 const int i_mb_4x4 = h->mb.i_b4_xy;
1424 const int i_mb_8x8 = h->mb.i_b8_xy;
1428 for( i = 0; i < 3; i++ )
1431 int i_stride = h->fdec->i_stride[i];
1432 int i_stride2 = i_stride << h->mb.b_interlaced;
1433 int i_pix_offset = h->mb.b_interlaced
1434 ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1435 : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1436 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1437 &h->fdec->plane[i][i_pix_offset], i_stride2,
1438 h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1441 x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1443 h->mb.type[i_mb_xy] = i_mb_type;
1445 if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1446 h->mb.i_qp = h->mb.i_last_qp;
1447 h->mb.qp[i_mb_xy] = h->mb.i_qp;
1449 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1450 h->mb.i_last_qp = h->mb.i_qp;
1451 h->mb.i_mb_prev_xy = h->mb.i_mb_xy;
1454 if( i_mb_type == I_4x4 )
1456 h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1457 h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
1458 h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
1459 h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
1460 h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
1461 h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
1462 h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
1466 h->mb.intra4x4_pred_mode[i_mb_xy][0] =
1467 h->mb.intra4x4_pred_mode[i_mb_xy][1] =
1468 h->mb.intra4x4_pred_mode[i_mb_xy][2] =
1469 h->mb.intra4x4_pred_mode[i_mb_xy][3] =
1470 h->mb.intra4x4_pred_mode[i_mb_xy][4] =
1471 h->mb.intra4x4_pred_mode[i_mb_xy][5] =
1472 h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
1475 if( i_mb_type == I_PCM )
1477 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1478 for( i = 0; i < 16 + 2*4; i++ )
1480 h->mb.non_zero_count[i_mb_xy][i] = 16;
1485 /* save non zero count */
1486 for( i = 0; i < 16 + 2*4; i++ )
1488 h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
1492 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1493 h->mb.b_transform_8x8 = 0;
1494 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1496 if( !IS_INTRA( i_mb_type ) )
1499 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1503 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
1504 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
1505 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
1506 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
1508 for( y = 0; y < 4; y++ )
1510 for( x = 0; x < 4; x++ )
1512 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];
1513 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];
1521 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1525 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
1526 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
1527 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
1528 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
1530 for( y = 0; y < 4; y++ )
1532 for( x = 0; x < 4; x++ )
1534 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1535 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1541 if( h->param.b_cabac )
1543 if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1544 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1546 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1548 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1551 for( i_list = 0; i_list < 2; i_list++ )
1553 const int s4x4 = 4 * h->mb.i_mb_stride;
1555 for( y = 0; y < 4; y++ )
1557 for( x = 0; x < 4; x++ )
1559 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];
1560 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];
1568 for( i_list = 0; i_list < 2; i_list++ )
1570 const int s4x4 = 4 * h->mb.i_mb_stride;
1572 for( y = 0; y < 4; y++ )
1574 for( x = 0; x < 4; x++ )
1576 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1577 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1582 if( h->sh.i_type == SLICE_TYPE_B )
1584 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1585 h->mb.skipbp[i_mb_xy] = 0xf;
1586 else if( i_mb_type == B_8x8 )
1589 for( i = 0; i < 4; i++ )
1590 skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1591 h->mb.skipbp[i_mb_xy] = skipbp;
1594 h->mb.skipbp[i_mb_xy] = 0;
1599 void x264_macroblock_bipred_init( x264_t *h )
1602 for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1604 int poc0 = h->fref0[i_ref0]->i_poc;
1605 for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1607 int dist_scale_factor;
1608 int poc1 = h->fref1[i_ref1]->i_poc;
1609 int td = x264_clip3( poc1 - poc0, -128, 127 );
1610 if( td == 0 /* || pic0 is a long-term ref */ )
1611 dist_scale_factor = 256;
1614 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1615 int tx = (16384 + (abs(td) >> 1)) / td;
1616 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1618 h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1620 dist_scale_factor >>= 2;
1621 if( h->param.analyse.b_weighted_bipred
1622 && dist_scale_factor >= -64
1623 && dist_scale_factor <= 128 )
1624 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1626 h->mb.bipred_weight[i_ref0][i_ref1] = 32;
1631 for( i_ref0 = 2*h->i_ref0-1; i_ref0 >= 0; i_ref0-- )
1632 for( i_ref1 = 2*h->i_ref1-1; i_ref1 >= 0; i_ref1-- )
1633 h->mb.bipred_weight[i_ref0][i_ref1] = h->mb.bipred_weight[i_ref0>>1][i_ref1>>1];