1 /*****************************************************************************
2 * macroblock.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
8 * Fiona Glaser <fiona@x264.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
23 *****************************************************************************/
26 #include "encoder/me.h"
28 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int16_t mvp[2] )
30 const int i8 = x264_scan8[idx];
31 const int i_ref= h->mb.cache.ref[i_list][i8];
32 int i_refa = h->mb.cache.ref[i_list][i8 - 1];
33 int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
34 int i_refb = h->mb.cache.ref[i_list][i8 - 8];
35 int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
36 int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width];
37 int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
39 if( (idx&3) >= 2 + (i_width&1) || i_refc == -2 )
41 i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
42 mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
45 if( h->mb.i_partition == D_16x8 )
64 else if( h->mb.i_partition == D_8x16 )
84 int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
89 x264_median_mv( mvp, mv_a, mv_b, mv_c );
91 else if( i_count == 1 )
95 else if( i_refb == i_ref )
100 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
106 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int16_t mvp[2] )
108 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
109 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
110 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
111 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
112 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
113 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
116 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
117 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
120 int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
125 x264_median_mv( mvp, mv_a, mv_b, mv_c );
127 else if( i_count == 1 )
129 if( i_refa == i_ref )
131 else if( i_refb == i_ref )
136 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
143 void x264_mb_predict_mv_pskip( x264_t *h, int16_t mv[2] )
145 int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
146 int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
147 int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
148 int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
150 if( i_refa == -2 || i_refb == -2 ||
151 !( i_refa | M32( mv_a ) ) ||
152 !( i_refb | M32( mv_b ) ) )
158 x264_mb_predict_mv_16x16( h, 0, 0, mv );
162 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
164 int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
165 int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
167 const int type_col = h->fref1[0]->mb_type[h->mb.i_mb_xy];
169 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
171 if( IS_INTRA( type_col ) )
173 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
174 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0 );
175 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0 );
179 for( i8 = 0; i8 < 4; i8++ )
183 const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
184 const int i_ref1_ref = h->fref1[0]->ref[0][i_part_8x8];
185 const int i_ref = (map_col_to_list0(i_ref1_ref>>h->sh.b_mbaff) << h->sh.b_mbaff) + (i_ref1_ref&h->sh.b_mbaff);
189 const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
190 const int16_t *mv_col = h->fref1[0]->mv[0][i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
191 const int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
192 const int l0y = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
193 if( h->param.i_threads > 1 && (l0y > h->mb.mv_max_spel[1] || l0y-mv_col[1] > h->mb.mv_max_spel[1]) )
195 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
196 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, pack16to32_mask(l0x, l0y) );
197 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_col[1]) );
201 /* the collocated ref isn't in the current list0 */
202 /* FIXME: we might still be able to use direct_8x8 on some partitions */
203 /* FIXME: with B-pyramid + extensive ref list reordering
204 * (not currently used), we would also have to check
205 * l1mv1 like in spatial mode */
213 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
216 ALIGNED_ARRAY_8( int16_t, mv,[2],[2] );
217 int i_list, i8, i_ref;
218 const int8_t *l1ref0 = &h->fref1[0]->ref[0][h->mb.i_b8_xy];
219 const int8_t *l1ref1 = &h->fref1[0]->ref[1][h->mb.i_b8_xy];
220 const int16_t (*l1mv[2])[2] = { (const int16_t (*)[2]) &h->fref1[0]->mv[0][h->mb.i_b4_xy],
221 (const int16_t (*)[2]) &h->fref1[0]->mv[1][h->mb.i_b4_xy] };
222 const int type_col = h->fref1[0]->mb_type[h->mb.i_mb_xy];
224 for( i_list = 0; i_list < 2; i_list++ )
226 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
227 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
228 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
229 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
230 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
231 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
234 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
235 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
238 i_ref = X264_MIN3( (unsigned)i_refa, (unsigned)i_refb, (unsigned)i_refc );
242 M32( mv[i_list] ) = 0;
246 /* Same as x264_mb_predict_mv_16x16, but simplified to eliminate cases
247 * not relevant to spatial direct. */
248 int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
251 x264_median_mv( mv[i_list], mv_a, mv_b, mv_c );
254 if( i_refa == i_ref )
255 CP32( mv[i_list], mv_a );
256 else if( i_refb == i_ref )
257 CP32( mv[i_list], mv_b );
259 CP32( mv[i_list], mv_c );
263 x264_macroblock_cache_ref( h, 0, 0, 4, 4, i_list, i_ref );
264 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, i_list, mv[i_list] );
268 if( (M16( ref ) & 0x8080) == 0x8080 ) /* if( ref[0] < 0 && ref[1] < 0 ) */
270 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
271 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
275 if( !M64( mv ) || IS_INTRA( type_col ) || (ref[0]&&ref[1]) )
278 if( h->param.i_threads > 1
279 && ( mv[0][1] > h->mb.mv_max_spel[1]
280 || mv[1][1] > h->mb.mv_max_spel[1] ) )
283 fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
284 mv[0][0], mv[0][1], mv[1][0], mv[1][1],
285 h->mb.mv_max_spel[1]);
291 for( i8 = 0; i8 < 4; i8++ )
294 const int y8 = i8>>1;
295 const int o8 = x8 + y8 * h->mb.i_b8_stride;
296 const int o4 = 3*(x8 + y8 * h->mb.i_b4_stride);
298 if( l1ref0[o8] == 0 )
300 else if( l1ref0[o8] < 0 && l1ref1[o8] == 0 )
305 if( abs( l1mv[idx][o4][0] ) <= 1 && abs( l1mv[idx][o4][1] ) <= 1 )
307 if( ref[0] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0 );
308 if( ref[1] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0 );
315 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
318 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
320 else if( h->sh.b_direct_spatial_mv_pred )
321 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
323 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
325 if( b_changed != NULL && b_available )
327 int type_col = h->fref1[0]->mb_type[h->mb.i_mb_xy];
330 if( IS_INTRA( type_col ) || type_col == P_SKIP )
332 changed |= M32( h->mb.cache.direct_mv[0][0] ) ^ M32( h->mb.cache.mv[0][X264_SCAN8_0] );
333 changed |= M32( h->mb.cache.direct_mv[1][0] ) ^ M32( h->mb.cache.mv[1][X264_SCAN8_0] );
334 changed |= h->mb.cache.direct_ref[0][0] ^ h->mb.cache.ref[0][X264_SCAN8_0];
335 changed |= h->mb.cache.direct_ref[1][0] ^ h->mb.cache.ref[1][X264_SCAN8_0];
340 for( l = 0; l < 2; l++ )
342 changed |= M32( h->mb.cache.direct_mv[l][0] ) ^ M32( h->mb.cache.mv[l][x264_scan8[ 0]] );
344 changed |= M32( h->mb.cache.direct_mv[l][1] ) ^ M32( h->mb.cache.mv[l][x264_scan8[ 4]] );
345 changed |= M32( h->mb.cache.direct_mv[l][2] ) ^ M32( h->mb.cache.mv[l][x264_scan8[ 8]] );
346 changed |= M32( h->mb.cache.direct_mv[l][3] ) ^ M32( h->mb.cache.mv[l][x264_scan8[12]] );
348 changed |= h->mb.cache.direct_ref[l][0] ^ h->mb.cache.ref[l][x264_scan8[ 0]];
349 changed |= h->mb.cache.direct_ref[l][1] ^ h->mb.cache.ref[l][x264_scan8[ 4]];
350 changed |= h->mb.cache.direct_ref[l][2] ^ h->mb.cache.ref[l][x264_scan8[ 8]];
351 changed |= h->mb.cache.direct_ref[l][3] ^ h->mb.cache.ref[l][x264_scan8[12]];
354 *b_changed = changed;
363 for( l = 0; l < 2; l++ )
365 CP32( h->mb.cache.direct_mv[l][0], h->mb.cache.mv[l][x264_scan8[ 0]] );
366 CP32( h->mb.cache.direct_mv[l][1], h->mb.cache.mv[l][x264_scan8[ 4]] );
367 CP32( h->mb.cache.direct_mv[l][2], h->mb.cache.mv[l][x264_scan8[ 8]] );
368 CP32( h->mb.cache.direct_mv[l][3], h->mb.cache.mv[l][x264_scan8[12]] );
369 h->mb.cache.direct_ref[l][0] = h->mb.cache.ref[l][x264_scan8[ 0]];
370 h->mb.cache.direct_ref[l][1] = h->mb.cache.ref[l][x264_scan8[ 4]];
371 h->mb.cache.direct_ref[l][2] = h->mb.cache.ref[l][x264_scan8[ 8]];
372 h->mb.cache.direct_ref[l][3] = h->mb.cache.ref[l][x264_scan8[12]];
379 /* This just improves encoder performance, it's not part of the spec */
380 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int16_t mvc[9][2], int *i_mvc )
382 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
385 #define SET_MVP(mvp) { \
386 CP32( mvc[i], mvp ); \
391 if( h->sh.i_type == SLICE_TYPE_B
392 && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
394 SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
397 if( i_ref == 0 && h->frames.b_have_lowres )
399 int16_t (*lowres_mv)[2] = i_list ? h->fenc->lowres_mvs[1][h->fref1[0]->i_frame-h->fenc->i_frame-1]
400 : h->fenc->lowres_mvs[0][h->fenc->i_frame-h->fref0[0]->i_frame-1];
401 if( lowres_mv[0][0] != 0x7fff )
403 M32( mvc[i] ) = (M32( lowres_mv[h->mb.i_mb_xy] )*2)&0xfffeffff;
408 /* spatial predictors */
409 if( h->mb.i_neighbour & MB_LEFT )
411 int i_mb_l = h->mb.i_mb_xy - 1;
412 /* skip MBs didn't go through the whole search process, so mvr is undefined */
413 if( !IS_SKIP( h->mb.type[i_mb_l] ) )
414 SET_MVP( mvr[i_mb_l] );
416 if( h->mb.i_neighbour & MB_TOP )
418 int i_mb_t = h->mb.i_mb_top_xy;
419 if( !IS_SKIP( h->mb.type[i_mb_t] ) )
420 SET_MVP( mvr[i_mb_t] );
422 if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
423 SET_MVP( mvr[i_mb_t-1] );
424 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
425 SET_MVP( mvr[i_mb_t+1] );
429 /* temporal predictors */
430 /* FIXME temporal scaling w/ interlace */
431 if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
433 x264_frame_t *l0 = h->fref0[0];
435 #define SET_TMVP(dx, dy) { \
436 int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
437 int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
438 int ref_col = l0->ref[0][i_b8]; \
441 int scale = (h->fdec->i_poc - h->fdec->ref_poc[0][i_ref]) * l0->inv_ref_poc[ref_col];\
442 mvc[i][0] = (l0->mv[0][i_b4][0]*scale + 128) >> 8;\
443 mvc[i][1] = (l0->mv[0][i_b4][1]*scale + 128) >> 8;\
449 if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
451 if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
459 /* Set up a lookup table for delta pocs to reduce an IDIV to an IMUL */
460 static void setup_inverse_delta_pocs( x264_t *h )
463 for( i = 0; i < h->i_ref0; i++ )
465 int delta = h->fdec->i_poc - h->fref0[i]->i_poc;
466 h->fdec->inv_ref_poc[i] = (256 + delta/2) / delta;
470 static NOINLINE void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
472 const int i8 = x264_scan8[0]+x+8*y;
473 const int i_ref = h->mb.cache.ref[0][i8];
474 const int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
475 int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
477 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
478 h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
479 mvx, mvy, 4*width, 4*height, &h->sh.weight[i_ref][0] );
481 // chroma is offset if MCing from a field of opposite parity
482 if( h->mb.b_interlaced & i_ref )
483 mvy += (h->mb.i_mb_y & 1)*4 - 2;
485 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
486 h->mb.pic.p_fref[0][i_ref][4], h->mb.pic.i_stride[1],
487 mvx, mvy, 2*width, 2*height );
489 if( h->sh.weight[i_ref][1].weightfn )
490 h->sh.weight[i_ref][1].weightfn[width>>1]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
491 &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
492 &h->sh.weight[i_ref][1], height*2 );
494 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
495 h->mb.pic.p_fref[0][i_ref][5], h->mb.pic.i_stride[2],
496 mvx, mvy, 2*width, 2*height );
498 if( h->sh.weight[i_ref][2].weightfn )
499 h->sh.weight[i_ref][2].weightfn[width>>1]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
500 &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
501 &h->sh.weight[i_ref][2],height*2 );
504 static NOINLINE void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
506 const int i8 = x264_scan8[0]+x+8*y;
507 const int i_ref = h->mb.cache.ref[1][i8];
508 const int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
509 int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
511 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
512 h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
513 mvx, mvy, 4*width, 4*height, weight_none );
515 if( h->mb.b_interlaced & i_ref )
516 mvy += (h->mb.i_mb_y & 1)*4 - 2;
518 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
519 h->mb.pic.p_fref[1][i_ref][4], h->mb.pic.i_stride[1],
520 mvx, mvy, 2*width, 2*height );
522 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
523 h->mb.pic.p_fref[1][i_ref][5], h->mb.pic.i_stride[2],
524 mvx, mvy, 2*width, 2*height );
527 static NOINLINE void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
529 const int i8 = x264_scan8[0]+x+8*y;
530 const int i_ref0 = h->mb.cache.ref[0][i8];
531 const int i_ref1 = h->mb.cache.ref[1][i8];
532 const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
533 const int mvx0 = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
534 const int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
535 int mvy0 = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
536 int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
537 int i_mode = x264_size2pixel[height][width];
538 int i_stride0 = 16, i_stride1 = 16;
539 ALIGNED_ARRAY_16( uint8_t, tmp0,[16*16] );
540 ALIGNED_ARRAY_16( uint8_t, tmp1,[16*16] );
541 uint8_t *src0, *src1;
543 src0 = h->mc.get_ref( tmp0, &i_stride0, h->mb.pic.p_fref[0][i_ref0], h->mb.pic.i_stride[0],
544 mvx0, mvy0, 4*width, 4*height, weight_none );
545 src1 = h->mc.get_ref( tmp1, &i_stride1, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
546 mvx1, mvy1, 4*width, 4*height, weight_none );
547 h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
548 src0, i_stride0, src1, i_stride1, weight );
550 if( h->mb.b_interlaced & i_ref0 )
551 mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
552 if( h->mb.b_interlaced & i_ref1 )
553 mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
555 h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][4], h->mb.pic.i_stride[1],
556 mvx0, mvy0, 2*width, 2*height );
557 h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][4], h->mb.pic.i_stride[1],
558 mvx1, mvy1, 2*width, 2*height );
559 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp0, 16, tmp1, 16, weight );
560 h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][5], h->mb.pic.i_stride[2],
561 mvx0, mvy0, 2*width, 2*height );
562 h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][5], h->mb.pic.i_stride[2],
563 mvx1, mvy1, 2*width, 2*height );
564 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp0, 16, tmp1, 16, weight );
567 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
569 const int i8 = x264_scan8[0] + x + 8*y;
571 if( h->mb.cache.ref[0][i8] >= 0 )
572 if( h->mb.cache.ref[1][i8] >= 0 )
573 x264_mb_mc_01xywh( h, x, y, 2, 2 );
575 x264_mb_mc_0xywh( h, x, y, 2, 2 );
577 x264_mb_mc_1xywh( h, x, y, 2, 2 );
580 void x264_mb_mc_8x8( x264_t *h, int i8 )
582 const int x = 2*(i8&1);
583 const int y = 2*(i8>>1);
584 switch( h->mb.i_sub_partition[i8] )
587 x264_mb_mc_0xywh( h, x, y, 2, 2 );
590 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
591 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
594 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
595 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
598 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
599 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
600 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
601 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
604 x264_mb_mc_1xywh( h, x, y, 2, 2 );
607 x264_mb_mc_01xywh( h, x, y, 2, 2 );
610 x264_mb_mc_direct8x8( h, x, y );
615 void x264_mb_mc( x264_t *h )
617 if( h->mb.i_type == P_L0 )
619 if( h->mb.i_partition == D_16x16 )
621 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
623 else if( h->mb.i_partition == D_16x8 )
625 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
626 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
628 else if( h->mb.i_partition == D_8x16 )
630 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
631 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
634 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
637 for( i = 0; i < 4; i++ )
638 x264_mb_mc_8x8( h, i );
640 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
642 x264_mb_mc_direct8x8( h, 0, 0 );
643 x264_mb_mc_direct8x8( h, 2, 0 );
644 x264_mb_mc_direct8x8( h, 0, 2 );
645 x264_mb_mc_direct8x8( h, 2, 2 );
649 const uint8_t *b_list0 = x264_mb_type_list_table[h->mb.i_type][0];
650 const uint8_t *b_list1 = x264_mb_type_list_table[h->mb.i_type][1];
652 if( h->mb.i_partition == D_16x16 )
654 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
655 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
656 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
658 else if( h->mb.i_partition == D_16x8 )
660 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
661 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
662 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
664 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
665 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
666 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
668 else if( h->mb.i_partition == D_8x16 )
670 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
671 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
672 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
674 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
675 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
676 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
681 int x264_macroblock_cache_init( x264_t *h )
684 int i_mb_count = h->mb.i_mb_count;
686 h->mb.i_mb_stride = h->sps->i_mb_width;
687 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
688 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
690 h->mb.b_interlaced = h->param.b_interlaced;
692 CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
693 CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
694 CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
695 CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
697 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
698 CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
701 CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
703 if( h->param.b_cabac )
705 CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
706 CHECKED_MALLOC( h->mb.mvd[0], 2*16 * i_mb_count * sizeof(int16_t) );
707 CHECKED_MALLOC( h->mb.mvd[1], 2*16 * i_mb_count * sizeof(int16_t) );
712 int i_refs = X264_MIN(16, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << h->param.b_interlaced;
713 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
714 i_refs = X264_MIN(16, i_refs + 2); //smart weights add two duplicate frames
715 else if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_BLIND )
716 i_refs = X264_MIN(16, i_refs + 1); //blind weights add one duplicate frame
718 for( j=0; j < i_refs; j++ )
719 CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
722 if( h->param.analyse.i_weighted_pred )
724 int i_padv = PADV << h->param.b_interlaced;
725 #define ALIGN(x,a) (((x)+((a)-1))&~((a)-1))
726 int align = h->param.cpu&X264_CPU_CACHELINE_64 ? 64 : h->param.cpu&X264_CPU_CACHELINE_32 ? 32 : 16;
727 int i_stride, luma_plane_size;
730 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_FAKE )
732 // only need buffer for lookahead
733 if( !h->param.i_sync_lookahead || h == h->thread[h->param.i_threads] )
735 // Fake analysis only works on lowres
736 i_stride = ALIGN( h->sps->i_mb_width*8 + 2*PADH, align );
737 luma_plane_size = i_stride * (h->sps->i_mb_height*8+2*i_padv);
738 // Only need 1 buffer for analysis
746 i_stride = ALIGN( h->sps->i_mb_width*16 + 2*PADH, align );
747 luma_plane_size = i_stride * (h->sps->i_mb_height*16+2*i_padv);
749 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
750 //SMART can weight one ref and one offset -1
753 //blind only has one weighted copy (offset -1)
757 for( i = 0; i < numweightbuf; i++ )
758 CHECKED_MALLOC( h->mb.p_weight_buf[i], luma_plane_size );
762 for( i=0; i<=h->param.b_interlaced; i++ )
765 /* shouldn't really be initialized, just silences a valgrind false-positive in predict_8x8_filter_mmx */
766 CHECKED_MALLOCZERO( h->mb.intra_border_backup[i][j], (h->sps->i_mb_width*16+32)>>!!j );
767 h->mb.intra_border_backup[i][j] += 8;
770 /* init with not available (for top right idx=7,15) */
771 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
772 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
777 void x264_macroblock_cache_end( x264_t *h )
780 for( i=0; i<=h->param.b_interlaced; i++ )
782 x264_free( h->mb.intra_border_backup[i][j] - 8 );
784 for( j=0; j<32; j++ )
785 x264_free( h->mb.mvr[i][j] );
786 for( i=0; i<16; i++ )
787 x264_free( h->mb.p_weight_buf[i] );
789 if( h->param.b_cabac )
791 x264_free( h->mb.chroma_pred_mode );
792 x264_free( h->mb.mvd[0] );
793 x264_free( h->mb.mvd[1] );
795 x264_free( h->mb.intra4x4_pred_mode );
796 x264_free( h->mb.non_zero_count );
797 x264_free( h->mb.mb_transform_size );
798 x264_free( h->mb.skipbp );
799 x264_free( h->mb.cbp );
800 x264_free( h->mb.qp );
802 void x264_macroblock_slice_init( x264_t *h )
806 h->mb.mv[0] = h->fdec->mv[0];
807 h->mb.mv[1] = h->fdec->mv[1];
808 h->mb.ref[0] = h->fdec->ref[0];
809 h->mb.ref[1] = h->fdec->ref[1];
810 h->mb.type = h->fdec->mb_type;
812 h->fdec->i_ref[0] = h->i_ref0;
813 h->fdec->i_ref[1] = h->i_ref1;
814 for( i = 0; i < h->i_ref0; i++ )
815 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
816 if( h->sh.i_type == SLICE_TYPE_B )
818 for( i = 0; i < h->i_ref1; i++ )
819 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
821 map_col_to_list0(-1) = -1;
822 map_col_to_list0(-2) = -2;
823 for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
825 int poc = h->fref1[0]->ref_poc[0][i];
826 map_col_to_list0(i) = -2;
827 for( j = 0; j < h->i_ref0; j++ )
828 if( h->fref0[j]->i_poc == poc )
830 map_col_to_list0(i) = j;
835 if( h->sh.i_type == SLICE_TYPE_P )
836 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
838 setup_inverse_delta_pocs( h );
840 h->mb.i_neighbour4[6] =
841 h->mb.i_neighbour4[9] =
842 h->mb.i_neighbour4[12] =
843 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
844 h->mb.i_neighbour4[3] =
845 h->mb.i_neighbour4[7] =
846 h->mb.i_neighbour4[11] =
847 h->mb.i_neighbour4[13] =
848 h->mb.i_neighbour4[15] =
849 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
852 void x264_macroblock_thread_init( x264_t *h )
864 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
865 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
866 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
867 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
868 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
869 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
872 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
874 int stride_y = fenc->i_stride[0];
875 int stride_uv = fenc->i_stride[1];
876 int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
877 int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
878 h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
879 fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
882 static NOINLINE void copy_column8( uint8_t *dst, uint8_t *src )
884 // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
886 for( i = -4; i < 4; i++ )
887 dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
890 static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int i_mb_x, int i_mb_y, int i)
892 const int w = (i == 0 ? 16 : 8);
893 const int i_stride = h->fdec->i_stride[!!i];
894 const int i_stride2 = i_stride << h->mb.b_interlaced;
895 const int i_pix_offset = h->mb.b_interlaced
896 ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride
897 : w * (i_mb_x + i_mb_y * i_stride);
898 const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
899 const uint8_t *intra_fdec = h->param.b_sliced_threads ? plane_fdec-i_stride2 :
900 &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
901 int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
902 x264_frame_t **fref[2] = { h->fref0, h->fref1 };
904 if( h->mb.b_interlaced )
905 ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
906 h->mb.pic.i_stride[i] = i_stride2;
907 h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
908 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
909 h->mb.pic.p_fenc_plane[i], i_stride2, w );
911 memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
913 memset( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], 0, w*3/2+1 );
914 if( h->mb.b_interlaced || h->mb.b_reencode_mb )
915 for( j = 0; j < w; j++ )
916 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
917 for( j = 0; j < h->mb.pic.i_fref[0]; j++ )
919 h->mb.pic.p_fref[0][j][i==0 ? 0:i+3] = &fref[0][j >> h->mb.b_interlaced]->plane[i][ref_pix_offset[j&1]];
922 for( k = 1; k < 4; k++ )
923 h->mb.pic.p_fref[0][j][k] = &fref[0][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
924 if( h->sh.weight[j][0].weightfn )
925 h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> h->mb.b_interlaced][ref_pix_offset[j&1]];
927 h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
930 if( h->sh.i_type == SLICE_TYPE_B )
931 for( j = 0; j < h->mb.pic.i_fref[1]; j++ )
933 h->mb.pic.p_fref[1][j][i==0 ? 0:i+3] = &fref[1][j >> h->mb.b_interlaced]->plane[i][ref_pix_offset[j&1]];
935 for( k = 1; k < 4; k++ )
936 h->mb.pic.p_fref[1][j][k] = &fref[1][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
940 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
942 int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
943 int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
944 int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
945 int i_top_y = i_mb_y - (1 << h->mb.b_interlaced);
946 int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x;
947 int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x;
948 int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x;
950 int i_top_type = -1; /* gcc warn */
956 h->mb.i_mb_x = i_mb_x;
957 h->mb.i_mb_y = i_mb_y;
958 h->mb.i_mb_xy = i_mb_xy;
959 h->mb.i_b8_xy = i_mb_8x8;
960 h->mb.i_b4_xy = i_mb_4x4;
961 h->mb.i_mb_top_xy = i_top_xy;
962 h->mb.i_neighbour = 0;
963 h->mb.i_neighbour_intra = 0;
966 if( i_top_xy >= h->sh.i_first_mb )
968 h->mb.i_mb_type_top =
969 i_top_type = h->mb.type[i_top_xy];
970 h->mb.cache.i_cbp_top = h->mb.cbp[i_top_xy];
972 h->mb.i_neighbour |= MB_TOP;
974 if( !h->param.b_constrained_intra || IS_INTRA( i_top_type ) )
975 h->mb.i_neighbour_intra |= MB_TOP;
978 CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &h->mb.intra4x4_pred_mode[i_top_xy][0] );
980 /* load non_zero_count */
981 CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &h->mb.non_zero_count[i_top_xy][12] );
982 /* shift because x264_scan8[16] is misaligned */
983 M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = M16( &h->mb.non_zero_count[i_top_xy][18] ) << 8;
984 M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = M16( &h->mb.non_zero_count[i_top_xy][22] ) << 8;
988 h->mb.i_mb_type_top = -1;
989 h->mb.cache.i_cbp_top = -1;
992 M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
994 /* load non_zero_count */
995 M32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8] ) = 0x80808080U;
996 M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = 0x80808080U;
997 M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = 0x80808080U;
1000 if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1002 i_left_xy = i_mb_xy - 1;
1003 h->mb.i_mb_type_left =
1004 i_left_type = h->mb.type[i_left_xy];
1005 h->mb.cache.i_cbp_left = h->mb.cbp[h->mb.i_mb_xy - 1];
1007 h->mb.i_neighbour |= MB_LEFT;
1009 if( !h->param.b_constrained_intra || IS_INTRA( i_left_type ) )
1010 h->mb.i_neighbour_intra |= MB_LEFT;
1013 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1014 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1015 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1016 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1018 /* load non_zero_count */
1019 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][3];
1020 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1021 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][11];
1022 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1024 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1025 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1027 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1028 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1032 h->mb.i_mb_type_left = -1;
1033 h->mb.cache.i_cbp_left = -1;
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_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1053 h->mb.i_neighbour |= MB_TOPRIGHT;
1054 h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1055 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
1056 h->mb.i_neighbour_intra |= MB_TOPRIGHT;
1059 h->mb.i_mb_type_topright = -1;
1060 if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1062 h->mb.i_neighbour |= MB_TOPLEFT;
1063 h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1064 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
1065 h->mb.i_neighbour_intra |= MB_TOPLEFT;
1068 h->mb.i_mb_type_topleft = -1;
1070 if( h->pps->b_transform_8x8_mode )
1072 h->mb.cache.i_neighbour_transform_size =
1073 ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1074 + ( i_top_type >= 0 && h->mb.mb_transform_size[i_top_xy] );
1079 h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1080 h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1081 h->mb.cache.i_neighbour_interlaced =
1082 !!(h->mb.i_neighbour & MB_LEFT)
1083 + !!(h->mb.i_neighbour & MB_TOP);
1086 if( !h->mb.b_interlaced && !h->mb.b_reencode_mb )
1088 copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
1089 copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
1090 copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
1091 copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
1094 /* load picture pointers */
1095 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 0 );
1096 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 1 );
1097 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 2 );
1099 if( h->fdec->integral )
1101 assert( !h->mb.b_interlaced );
1102 for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
1103 h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1104 for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
1105 h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1108 x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
1110 /* load ref/mv/mvd */
1111 if( h->sh.i_type != SLICE_TYPE_I )
1113 const int s8x8 = h->mb.i_b8_stride;
1114 const int s4x4 = h->mb.i_b4_stride;
1118 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1121 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1122 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1123 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1126 if( h->mb.i_neighbour & MB_TOPLEFT )
1128 const int i8 = x264_scan8[0] - 1 - 1*8;
1129 const int ir = i_top_8x8 - 1;
1130 const int iv = i_top_4x4 - 1;
1131 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1132 CP32( h->mb.cache.mv[i_list][i8], h->mb.mv[i_list][iv] );
1136 const int i8 = x264_scan8[0] - 1 - 1*8;
1137 h->mb.cache.ref[i_list][i8] = -2;
1138 M32( h->mb.cache.mv[i_list][i8] ) = 0;
1141 if( h->mb.i_neighbour & MB_TOP )
1143 const int i8 = x264_scan8[0] - 8;
1144 const int ir = i_top_8x8;
1145 const int iv = i_top_4x4;
1146 h->mb.cache.ref[i_list][i8+0] =
1147 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1148 h->mb.cache.ref[i_list][i8+2] =
1149 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1150 CP64( h->mb.cache.mv[i_list][i8+0], h->mb.mv[i_list][iv+0] );
1151 CP64( h->mb.cache.mv[i_list][i8+2], h->mb.mv[i_list][iv+2] );
1155 const int i8 = x264_scan8[0] - 8;
1156 M64( h->mb.cache.mv[i_list][i8+0] ) = 0;
1157 M64( h->mb.cache.mv[i_list][i8+2] ) = 0;
1158 M32( &h->mb.cache.ref[i_list][i8] ) = (uint8_t)(-2) * 0x01010101U;
1161 if( h->mb.i_neighbour & MB_TOPRIGHT )
1163 const int i8 = x264_scan8[0] + 4 - 1*8;
1164 const int ir = i_top_8x8 + 2;
1165 const int iv = i_top_4x4 + 4;
1166 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1167 CP32( h->mb.cache.mv[i_list][i8], h->mb.mv[i_list][iv] );
1171 const int i8 = x264_scan8[0] + 4 - 1*8;
1172 h->mb.cache.ref[i_list][i8] = -2;
1173 M32( h->mb.cache.mv[i_list][i8] ) = 0;
1176 if( h->mb.i_neighbour & MB_LEFT )
1178 const int i8 = x264_scan8[0] - 1;
1179 const int ir = i_mb_8x8 - 1;
1180 const int iv = i_mb_4x4 - 1;
1181 h->mb.cache.ref[i_list][i8+0*8] =
1182 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1183 h->mb.cache.ref[i_list][i8+2*8] =
1184 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1186 CP32( h->mb.cache.mv[i_list][i8+0*8], h->mb.mv[i_list][iv + 0*s4x4] );
1187 CP32( h->mb.cache.mv[i_list][i8+1*8], h->mb.mv[i_list][iv + 1*s4x4] );
1188 CP32( h->mb.cache.mv[i_list][i8+2*8], h->mb.mv[i_list][iv + 2*s4x4] );
1189 CP32( h->mb.cache.mv[i_list][i8+3*8], h->mb.mv[i_list][iv + 3*s4x4] );
1193 const int i8 = x264_scan8[0] - 1;
1194 for( i = 0; i < 4; i++ )
1196 h->mb.cache.ref[i_list][i8+i*8] = -2;
1197 M32( h->mb.cache.mv[i_list][i8+i*8] ) = 0;
1201 if( h->param.b_cabac )
1203 if( i_top_type >= 0 )
1205 const int i8 = x264_scan8[0] - 8;
1206 const int iv = i_top_4x4;
1207 CP64( h->mb.cache.mvd[i_list][i8+0], h->mb.mvd[i_list][iv+0] );
1208 CP64( h->mb.cache.mvd[i_list][i8+2], h->mb.mvd[i_list][iv+2] );
1212 const int i8 = x264_scan8[0] - 8;
1213 M64( h->mb.cache.mvd[i_list][i8+0] ) = 0;
1214 M64( h->mb.cache.mvd[i_list][i8+2] ) = 0;
1217 if( i_left_type >= 0 )
1219 const int i8 = x264_scan8[0] - 1;
1220 const int iv = i_mb_4x4 - 1;
1221 CP32( h->mb.cache.mvd[i_list][i8+0*8], h->mb.mvd[i_list][iv + 0*s4x4] );
1222 CP32( h->mb.cache.mvd[i_list][i8+1*8], h->mb.mvd[i_list][iv + 1*s4x4] );
1223 CP32( h->mb.cache.mvd[i_list][i8+2*8], h->mb.mvd[i_list][iv + 2*s4x4] );
1224 CP32( h->mb.cache.mvd[i_list][i8+3*8], h->mb.mvd[i_list][iv + 3*s4x4] );
1228 const int i8 = x264_scan8[0] - 1;
1229 for( i = 0; i < 4; i++ )
1230 M32( h->mb.cache.mvd[i_list][i8+i*8] ) = 0;
1236 if( h->sh.i_type == SLICE_TYPE_B )
1238 h->mb.bipred_weight = h->mb.bipred_weight_buf[h->mb.b_interlaced&(i_mb_y&1)];
1239 h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[h->mb.b_interlaced&(i_mb_y&1)];
1240 if( h->param.b_cabac )
1243 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1244 skipbp = i_left_type >= 0 ? h->mb.skipbp[i_left_xy] : 0;
1245 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
1246 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
1247 skipbp = i_top_type >= 0 ? h->mb.skipbp[i_top_xy] : 0;
1248 h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
1249 h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
1253 if( h->sh.i_type == SLICE_TYPE_P )
1254 x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1257 h->mb.i_neighbour4[0] =
1258 h->mb.i_neighbour8[0] = (h->mb.i_neighbour_intra & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1259 | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOPRIGHT : 0);
1260 h->mb.i_neighbour4[4] =
1261 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour_intra & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1262 h->mb.i_neighbour4[2] =
1263 h->mb.i_neighbour4[8] =
1264 h->mb.i_neighbour4[10] =
1265 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour_intra & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1266 h->mb.i_neighbour4[5] =
1267 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour_intra & MB_TOPRIGHT)
1268 | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1271 static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int i )
1274 int i_stride = h->fdec->i_stride[!!i];
1275 int i_stride2 = i_stride << h->mb.b_interlaced;
1276 int i_pix_offset = h->mb.b_interlaced
1277 ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1278 : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1279 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1280 &h->fdec->plane[i][i_pix_offset], i_stride2,
1281 h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1284 void x264_macroblock_cache_save( x264_t *h )
1286 const int i_mb_xy = h->mb.i_mb_xy;
1287 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1288 const int s8x8 = h->mb.i_b8_stride;
1289 const int s4x4 = h->mb.i_b4_stride;
1290 const int i_mb_4x4 = h->mb.i_b4_xy;
1291 const int i_mb_8x8 = h->mb.i_b8_xy;
1293 /* GCC pessimizes direct stores to heap-allocated 8-bit arrays due to aliasing.*/
1294 /* By only dereferencing them once, we avoid this issue. */
1295 int8_t *intra4x4_pred_mode = h->mb.intra4x4_pred_mode[i_mb_xy];
1296 uint8_t *non_zero_count = h->mb.non_zero_count[i_mb_xy];
1300 x264_macroblock_store_pic( h, 0 );
1301 x264_macroblock_store_pic( h, 1 );
1302 x264_macroblock_store_pic( h, 2 );
1304 x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1306 h->mb.type[i_mb_xy] = i_mb_type;
1307 h->mb.i_mb_prev_xy = i_mb_xy;
1310 if( i_mb_type == I_4x4 )
1312 CP32( &intra4x4_pred_mode[0], &h->mb.cache.intra4x4_pred_mode[x264_scan8[10]] );
1313 M32( &intra4x4_pred_mode[4] ) = pack8to32(h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
1314 h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
1315 h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
1317 else if( !h->param.b_constrained_intra || IS_INTRA(i_mb_type) )
1318 M64( intra4x4_pred_mode ) = I_PRED_4x4_DC * 0x0101010101010101ULL;
1320 M64( intra4x4_pred_mode ) = (uint8_t)(-1) * 0x0101010101010101ULL;
1323 if( i_mb_type == I_PCM )
1325 h->mb.qp[i_mb_xy] = 0;
1326 h->mb.i_last_dqp = 0;
1327 h->mb.i_cbp_chroma = 2;
1328 h->mb.i_cbp_luma = 0xf;
1329 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1330 h->mb.b_transform_8x8 = 0;
1331 memset( non_zero_count, 16, 24 );
1335 /* save non zero count */
1336 CP32( &non_zero_count[0*4], &h->mb.cache.non_zero_count[x264_scan8[0]+0*8] );
1337 CP32( &non_zero_count[1*4], &h->mb.cache.non_zero_count[x264_scan8[0]+1*8] );
1338 CP32( &non_zero_count[2*4], &h->mb.cache.non_zero_count[x264_scan8[0]+2*8] );
1339 CP32( &non_zero_count[3*4], &h->mb.cache.non_zero_count[x264_scan8[0]+3*8] );
1340 M16( &non_zero_count[16+0*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+0*2]-1] ) >> 8;
1341 M16( &non_zero_count[16+1*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+1*2]-1] ) >> 8;
1342 M16( &non_zero_count[16+2*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+2*2]-1] ) >> 8;
1343 M16( &non_zero_count[16+3*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+3*2]-1] ) >> 8;
1345 if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1346 h->mb.i_qp = h->mb.i_last_qp;
1347 h->mb.qp[i_mb_xy] = h->mb.i_qp;
1348 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1349 h->mb.i_last_qp = h->mb.i_qp;
1352 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1353 h->mb.b_transform_8x8 = 0;
1354 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1356 if( h->sh.i_type != SLICE_TYPE_I )
1358 if( !IS_INTRA( i_mb_type ) )
1360 h->mb.ref[0][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
1361 h->mb.ref[0][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
1362 h->mb.ref[0][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
1363 h->mb.ref[0][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
1364 for( y = 0; y < 4; y++ )
1366 CP64( h->mb.mv[0][i_mb_4x4+y*s4x4+0], h->mb.cache.mv[0][x264_scan8[0]+8*y+0] );
1367 CP64( h->mb.mv[0][i_mb_4x4+y*s4x4+2], h->mb.cache.mv[0][x264_scan8[0]+8*y+2] );
1369 if( h->sh.i_type == SLICE_TYPE_B )
1371 h->mb.ref[1][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
1372 h->mb.ref[1][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
1373 h->mb.ref[1][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
1374 h->mb.ref[1][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
1375 for( y = 0; y < 4; y++ )
1377 CP64( h->mb.mv[1][i_mb_4x4+y*s4x4+0], h->mb.cache.mv[1][x264_scan8[0]+8*y+0] );
1378 CP64( h->mb.mv[1][i_mb_4x4+y*s4x4+2], h->mb.cache.mv[1][x264_scan8[0]+8*y+2] );
1385 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1387 M16( &h->mb.ref[i_list][i_mb_8x8+0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1388 M16( &h->mb.ref[i_list][i_mb_8x8+1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1389 for( y = 0; y < 4; y++ )
1391 M64( h->mb.mv[i_list][i_mb_4x4+y*s4x4+0] ) = 0;
1392 M64( h->mb.mv[i_list][i_mb_4x4+y*s4x4+2] ) = 0;
1398 if( h->param.b_cabac )
1400 if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
1401 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1403 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1405 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1407 for( y = 0; y < 4; y++ )
1409 CP64( h->mb.mvd[0][i_mb_4x4+y*s4x4+0], h->mb.cache.mvd[0][x264_scan8[0]+8*y+0] );
1410 CP64( h->mb.mvd[0][i_mb_4x4+y*s4x4+2], h->mb.cache.mvd[0][x264_scan8[0]+8*y+2] );
1412 if( h->sh.i_type == SLICE_TYPE_B )
1413 for( y = 0; y < 4; y++ )
1415 CP64( h->mb.mvd[1][i_mb_4x4+y*s4x4+0], h->mb.cache.mvd[1][x264_scan8[0]+8*y+0] );
1416 CP64( h->mb.mvd[1][i_mb_4x4+y*s4x4+2], h->mb.cache.mvd[1][x264_scan8[0]+8*y+2] );
1421 for( y = 0; y < 4; y++ )
1423 M64( h->mb.mvd[0][i_mb_4x4+y*s4x4+0] ) = 0;
1424 M64( h->mb.mvd[0][i_mb_4x4+y*s4x4+2] ) = 0;
1426 if( h->sh.i_type == SLICE_TYPE_B )
1427 for( y = 0; y < 4; y++ )
1429 M64( h->mb.mvd[1][i_mb_4x4+y*s4x4+0] ) = 0;
1430 M64( h->mb.mvd[1][i_mb_4x4+y*s4x4+2] ) = 0;
1434 if( h->sh.i_type == SLICE_TYPE_B )
1436 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1437 h->mb.skipbp[i_mb_xy] = 0xf;
1438 else if( i_mb_type == B_8x8 )
1440 int skipbp = ( h->mb.i_sub_partition[0] == D_DIRECT_8x8 ) << 0;
1441 skipbp |= ( h->mb.i_sub_partition[1] == D_DIRECT_8x8 ) << 1;
1442 skipbp |= ( h->mb.i_sub_partition[2] == D_DIRECT_8x8 ) << 2;
1443 skipbp |= ( h->mb.i_sub_partition[3] == D_DIRECT_8x8 ) << 3;
1444 h->mb.skipbp[i_mb_xy] = skipbp;
1447 h->mb.skipbp[i_mb_xy] = 0;
1453 void x264_macroblock_bipred_init( x264_t *h )
1455 int i_ref0, i_ref1, field;
1456 for( field = 0; field <= h->sh.b_mbaff; field++ )
1457 for( i_ref0 = 0; i_ref0 < (h->i_ref0<<h->sh.b_mbaff); i_ref0++ )
1459 int poc0 = h->fref0[i_ref0>>h->sh.b_mbaff]->i_poc;
1460 if( h->sh.b_mbaff && field^(i_ref0&1) )
1461 poc0 += h->sh.i_delta_poc_bottom;
1462 for( i_ref1 = 0; i_ref1 < (h->i_ref1<<h->sh.b_mbaff); i_ref1++ )
1464 int dist_scale_factor;
1465 int poc1 = h->fref1[i_ref1>>h->sh.b_mbaff]->i_poc;
1466 if( h->sh.b_mbaff && field^(i_ref1&1) )
1467 poc1 += h->sh.i_delta_poc_bottom;
1468 int cur_poc = h->fdec->i_poc + field*h->sh.i_delta_poc_bottom;
1469 int td = x264_clip3( poc1 - poc0, -128, 127 );
1470 if( td == 0 /* || pic0 is a long-term ref */ )
1471 dist_scale_factor = 256;
1474 int tb = x264_clip3( cur_poc - poc0, -128, 127 );
1475 int tx = (16384 + (abs(td) >> 1)) / td;
1476 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1479 h->mb.dist_scale_factor_buf[field][i_ref0][i_ref1] = dist_scale_factor;
1481 dist_scale_factor >>= 2;
1482 if( h->param.analyse.b_weighted_bipred
1483 && dist_scale_factor >= -64
1484 && dist_scale_factor <= 128 )
1486 h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 64 - dist_scale_factor;
1487 // ssse3 implementation of biweight doesn't support the extrema.
1488 // if we ever generate them, we'll have to drop that optimization.
1489 assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
1492 h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 32;