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 ) ) )
157 x264_mb_predict_mv_16x16( h, 0, 0, mv );
160 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
162 int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
163 int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
164 const int type_col = h->fref1[0]->mb_type[h->mb.i_mb_xy];
165 const int partition_col = h->fref1[0]->mb_partition[h->mb.i_mb_xy];
167 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
169 h->mb.i_partition = partition_col;
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 /* Don't do any checks other than the ones we have to, based
180 * on the size of the colocated partitions.
181 * Depends on the enum order: D_8x8, D_16x8, D_8x16, D_16x16 */
182 int max_i8 = (D_16x16 - partition_col) + 1;
183 int step = (partition_col == D_16x8) + 1;
184 int width = 4 >> ((D_16x16 - partition_col)&1);
185 int height = 4 >> ((D_16x16 - partition_col)>>1);
187 for( int i8 = 0; i8 < max_i8; i8 += step )
191 int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
192 int i_ref1_ref = h->fref1[0]->ref[0][i_part_8x8];
193 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);
197 int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
198 int16_t *mv_col = h->fref1[0]->mv[0][i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
199 int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
200 int l0y = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
201 if( h->param.i_threads > 1 && (l0y > h->mb.mv_max_spel[1] || l0y-mv_col[1] > h->mb.mv_max_spel[1]) )
203 x264_macroblock_cache_ref( h, 2*x8, 2*y8, width, height, 0, i_ref );
204 x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 0, pack16to32_mask(l0x, l0y) );
205 x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_col[1]) );
209 /* the collocated ref isn't in the current list0 */
210 /* FIXME: we might still be able to use direct_8x8 on some partitions */
211 /* FIXME: with B-pyramid + extensive ref list reordering
212 * (not currently used), we would also have to check
213 * l1mv1 like in spatial mode */
221 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
224 ALIGNED_ARRAY_8( int16_t, mv,[2],[2] );
225 const int8_t *l1ref0 = &h->fref1[0]->ref[0][h->mb.i_b8_xy];
226 const int8_t *l1ref1 = &h->fref1[0]->ref[1][h->mb.i_b8_xy];
227 const int16_t (*l1mv[2])[2] = { (const int16_t (*)[2]) &h->fref1[0]->mv[0][h->mb.i_b4_xy],
228 (const int16_t (*)[2]) &h->fref1[0]->mv[1][h->mb.i_b4_xy] };
229 const int type_col = h->fref1[0]->mb_type[h->mb.i_mb_xy];
230 const int partition_col = h->fref1[0]->mb_partition[h->mb.i_mb_xy];
232 h->mb.i_partition = partition_col;
234 for( int i_list = 0; i_list < 2; i_list++ )
236 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
237 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
238 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
239 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
240 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
241 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
244 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
245 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
248 int i_ref = X264_MIN3( (unsigned)i_refa, (unsigned)i_refb, (unsigned)i_refc );
252 M32( mv[i_list] ) = 0;
256 /* Same as x264_mb_predict_mv_16x16, but simplified to eliminate cases
257 * not relevant to spatial direct. */
258 int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
261 x264_median_mv( mv[i_list], mv_a, mv_b, mv_c );
264 if( i_refa == i_ref )
265 CP32( mv[i_list], mv_a );
266 else if( i_refb == i_ref )
267 CP32( mv[i_list], mv_b );
269 CP32( mv[i_list], mv_c );
273 x264_macroblock_cache_ref( h, 0, 0, 4, 4, i_list, i_ref );
274 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, i_list, mv[i_list] );
278 if( (M16( ref ) & 0x8080) == 0x8080 ) /* if( ref[0] < 0 && ref[1] < 0 ) */
280 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
281 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
285 if( h->param.i_threads > 1
286 && ( mv[0][1] > h->mb.mv_max_spel[1]
287 || mv[1][1] > h->mb.mv_max_spel[1] ) )
290 fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
291 mv[0][0], mv[0][1], mv[1][0], mv[1][1],
292 h->mb.mv_max_spel[1]);
297 if( !M64( mv ) || IS_INTRA( type_col ) || (ref[0]&&ref[1]) )
300 /* Don't do any checks other than the ones we have to, based
301 * on the size of the colocated partitions.
302 * Depends on the enum order: D_8x8, D_16x8, D_8x16, D_16x16 */
303 int max_i8 = (D_16x16 - partition_col) + 1;
304 int step = (partition_col == D_16x8) + 1;
305 int width = 4 >> ((D_16x16 - partition_col)&1);
306 int height = 4 >> ((D_16x16 - partition_col)>>1);
309 for( int i8 = 0; i8 < max_i8; i8 += step )
312 const int y8 = i8>>1;
313 const int o8 = x8 + y8 * h->mb.i_b8_stride;
314 const int o4 = 3*(x8 + y8 * h->mb.i_b4_stride);
316 if( l1ref0[o8] == 0 )
318 else if( l1ref0[o8] < 0 && l1ref1[o8] == 0 )
323 if( abs( l1mv[idx][o4][0] ) <= 1 && abs( l1mv[idx][o4][1] ) <= 1 )
325 if( ref[0] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 0, 0 );
326 if( ref[1] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 1, 0 );
333 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
336 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
338 else if( h->sh.b_direct_spatial_mv_pred )
339 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
341 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
343 if( b_changed != NULL && b_available )
347 changed = M32( h->mb.cache.direct_mv[0][0] ) ^ M32( h->mb.cache.mv[0][x264_scan8[0]] );
348 changed |= M32( h->mb.cache.direct_mv[1][0] ) ^ M32( h->mb.cache.mv[1][x264_scan8[0]] );
349 changed |= h->mb.cache.direct_ref[0][0] ^ h->mb.cache.ref[0][x264_scan8[0]];
350 changed |= h->mb.cache.direct_ref[1][0] ^ h->mb.cache.ref[1][x264_scan8[0]];
351 if( !changed && h->mb.i_partition != D_16x16 )
353 changed |= M32( h->mb.cache.direct_mv[0][3] ) ^ M32( h->mb.cache.mv[0][x264_scan8[12]] );
354 changed |= M32( h->mb.cache.direct_mv[1][3] ) ^ M32( h->mb.cache.mv[1][x264_scan8[12]] );
355 changed |= h->mb.cache.direct_ref[0][3] ^ h->mb.cache.ref[0][x264_scan8[12]];
356 changed |= h->mb.cache.direct_ref[1][3] ^ h->mb.cache.ref[1][x264_scan8[12]];
358 if( !changed && h->mb.i_partition == D_8x8 )
360 changed |= M32( h->mb.cache.direct_mv[0][1] ) ^ M32( h->mb.cache.mv[0][x264_scan8[4]] );
361 changed |= M32( h->mb.cache.direct_mv[1][1] ) ^ M32( h->mb.cache.mv[1][x264_scan8[4]] );
362 changed |= M32( h->mb.cache.direct_mv[0][2] ) ^ M32( h->mb.cache.mv[0][x264_scan8[8]] );
363 changed |= M32( h->mb.cache.direct_mv[1][2] ) ^ M32( h->mb.cache.mv[1][x264_scan8[8]] );
364 changed |= h->mb.cache.direct_ref[0][1] ^ h->mb.cache.ref[0][x264_scan8[4]];
365 changed |= h->mb.cache.direct_ref[1][1] ^ h->mb.cache.ref[1][x264_scan8[4]];
366 changed |= h->mb.cache.direct_ref[0][2] ^ h->mb.cache.ref[0][x264_scan8[8]];
367 changed |= h->mb.cache.direct_ref[1][2] ^ h->mb.cache.ref[1][x264_scan8[8]];
369 *b_changed = changed;
376 for( int l = 0; l < 2; l++ )
378 CP32( h->mb.cache.direct_mv[l][0], h->mb.cache.mv[l][x264_scan8[ 0]] );
379 CP32( h->mb.cache.direct_mv[l][1], h->mb.cache.mv[l][x264_scan8[ 4]] );
380 CP32( h->mb.cache.direct_mv[l][2], h->mb.cache.mv[l][x264_scan8[ 8]] );
381 CP32( h->mb.cache.direct_mv[l][3], h->mb.cache.mv[l][x264_scan8[12]] );
382 h->mb.cache.direct_ref[l][0] = h->mb.cache.ref[l][x264_scan8[ 0]];
383 h->mb.cache.direct_ref[l][1] = h->mb.cache.ref[l][x264_scan8[ 4]];
384 h->mb.cache.direct_ref[l][2] = h->mb.cache.ref[l][x264_scan8[ 8]];
385 h->mb.cache.direct_ref[l][3] = h->mb.cache.ref[l][x264_scan8[12]];
386 h->mb.cache.direct_partition = h->mb.i_partition;
392 /* This just improves encoder performance, it's not part of the spec */
393 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int16_t mvc[9][2], int *i_mvc )
395 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
398 #define SET_MVP(mvp)\
400 CP32( mvc[i], mvp ); \
405 if( h->sh.i_type == SLICE_TYPE_B
406 && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
408 SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
411 if( i_ref == 0 && h->frames.b_have_lowres )
413 int16_t (*lowres_mv)[2] = i_list ? h->fenc->lowres_mvs[1][h->fref1[0]->i_frame-h->fenc->i_frame-1]
414 : h->fenc->lowres_mvs[0][h->fenc->i_frame-h->fref0[0]->i_frame-1];
415 if( lowres_mv[0][0] != 0x7fff )
417 M32( mvc[i] ) = (M32( lowres_mv[h->mb.i_mb_xy] )*2)&0xfffeffff;
422 /* spatial predictors */
423 if( h->mb.i_neighbour_frame & MB_LEFT )
425 SET_MVP( mvr[h->mb.i_mb_left_xy] );
427 if( h->mb.i_neighbour_frame & MB_TOP )
429 SET_MVP( mvr[h->mb.i_mb_top_xy] );
431 if( h->mb.i_neighbour_frame & MB_TOPLEFT )
432 SET_MVP( mvr[h->mb.i_mb_topleft_xy] );
433 if( h->mb.i_neighbour_frame & MB_TOPRIGHT )
434 SET_MVP( mvr[h->mb.i_mb_topright_xy] );
438 /* temporal predictors */
439 if( h->fref0[0]->i_ref[0] > 0 )
441 x264_frame_t *l0 = h->fref0[0];
442 x264_frame_t **fref = i_list ? h->fref1 : h->fref0;
443 int field = h->mb.i_mb_y&1;
444 int curpoc = h->fdec->i_poc + field*h->sh.i_delta_poc_bottom;
445 int refpoc = fref[i_ref>>h->sh.b_mbaff]->i_poc;
446 if( h->sh.b_mbaff && field^(i_ref&1) )
447 refpoc += h->sh.i_delta_poc_bottom;
449 #define SET_TMVP(dx, dy) { \
450 int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
451 int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
452 int ref_col = l0->ref[0][i_b8]; \
455 int scale = (curpoc - refpoc) * l0->inv_ref_poc[h->mb.b_interlaced&field][ref_col];\
456 mvc[i][0] = (l0->mv[0][i_b4][0]*scale + 128) >> 8;\
457 mvc[i][1] = (l0->mv[0][i_b4][1]*scale + 128) >> 8;\
463 if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
465 if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
473 /* Set up a lookup table for delta pocs to reduce an IDIV to an IMUL */
474 static void setup_inverse_delta_pocs( x264_t *h )
476 for( int field = 0; field <= h->sh.b_mbaff; field++ )
478 int curpoc = h->fdec->i_poc + field*h->sh.i_delta_poc_bottom;
479 for( int i = 0; i < (h->i_ref0<<h->sh.b_mbaff); i++ )
481 int refpoc = h->fref0[i>>h->sh.b_mbaff]->i_poc;
482 if( h->sh.b_mbaff && field^(i&1) )
483 refpoc += h->sh.i_delta_poc_bottom;
484 int delta = curpoc - refpoc;
486 h->fdec->inv_ref_poc[field][i] = (256 + delta/2) / delta;
491 static NOINLINE void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
493 int i8 = x264_scan8[0]+x+8*y;
494 int i_ref = h->mb.cache.ref[0][i8];
495 int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
496 int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
498 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
499 h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
500 mvx, mvy, 4*width, 4*height, &h->sh.weight[i_ref][0] );
502 // chroma is offset if MCing from a field of opposite parity
503 if( h->mb.b_interlaced & i_ref )
504 mvy += (h->mb.i_mb_y & 1)*4 - 2;
506 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
507 h->mb.pic.p_fref[0][i_ref][4], h->mb.pic.i_stride[1],
508 mvx, mvy, 2*width, 2*height );
510 if( h->sh.weight[i_ref][1].weightfn )
511 h->sh.weight[i_ref][1].weightfn[width>>1]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
512 &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
513 &h->sh.weight[i_ref][1], height*2 );
515 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
516 h->mb.pic.p_fref[0][i_ref][5], h->mb.pic.i_stride[2],
517 mvx, mvy, 2*width, 2*height );
519 if( h->sh.weight[i_ref][2].weightfn )
520 h->sh.weight[i_ref][2].weightfn[width>>1]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
521 &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
522 &h->sh.weight[i_ref][2],height*2 );
525 static NOINLINE void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
527 int i8 = x264_scan8[0]+x+8*y;
528 int i_ref = h->mb.cache.ref[1][i8];
529 int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
530 int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
532 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
533 h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
534 mvx, mvy, 4*width, 4*height, weight_none );
536 if( h->mb.b_interlaced & i_ref )
537 mvy += (h->mb.i_mb_y & 1)*4 - 2;
539 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
540 h->mb.pic.p_fref[1][i_ref][4], h->mb.pic.i_stride[1],
541 mvx, mvy, 2*width, 2*height );
543 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
544 h->mb.pic.p_fref[1][i_ref][5], h->mb.pic.i_stride[2],
545 mvx, mvy, 2*width, 2*height );
548 static NOINLINE void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
550 int i8 = x264_scan8[0]+x+8*y;
551 int i_ref0 = h->mb.cache.ref[0][i8];
552 int i_ref1 = h->mb.cache.ref[1][i8];
553 int weight = h->mb.bipred_weight[i_ref0][i_ref1];
554 int mvx0 = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
555 int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
556 int mvy0 = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
557 int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
558 int i_mode = x264_size2pixel[height][width];
559 int i_stride0 = 16, i_stride1 = 16;
560 ALIGNED_ARRAY_16( uint8_t, tmp0,[16*16] );
561 ALIGNED_ARRAY_16( uint8_t, tmp1,[16*16] );
562 uint8_t *src0, *src1;
564 src0 = h->mc.get_ref( tmp0, &i_stride0, h->mb.pic.p_fref[0][i_ref0], h->mb.pic.i_stride[0],
565 mvx0, mvy0, 4*width, 4*height, weight_none );
566 src1 = h->mc.get_ref( tmp1, &i_stride1, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
567 mvx1, mvy1, 4*width, 4*height, weight_none );
568 h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
569 src0, i_stride0, src1, i_stride1, weight );
571 if( h->mb.b_interlaced & i_ref0 )
572 mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
573 if( h->mb.b_interlaced & i_ref1 )
574 mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
576 h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][4], h->mb.pic.i_stride[1],
577 mvx0, mvy0, 2*width, 2*height );
578 h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][4], h->mb.pic.i_stride[1],
579 mvx1, mvy1, 2*width, 2*height );
580 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 );
581 h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][5], h->mb.pic.i_stride[2],
582 mvx0, mvy0, 2*width, 2*height );
583 h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][5], h->mb.pic.i_stride[2],
584 mvx1, mvy1, 2*width, 2*height );
585 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 );
588 void x264_mb_mc_8x8( x264_t *h, int i8 )
593 if( h->sh.i_type == SLICE_TYPE_P )
595 switch( h->mb.i_sub_partition[i8] )
598 x264_mb_mc_0xywh( h, x, y, 2, 2 );
601 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
602 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
605 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
606 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
609 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
610 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
611 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
612 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
618 int scan8 = x264_scan8[0] + x + 8*y;
620 if( h->mb.cache.ref[0][scan8] >= 0 )
621 if( h->mb.cache.ref[1][scan8] >= 0 )
622 x264_mb_mc_01xywh( h, x, y, 2, 2 );
624 x264_mb_mc_0xywh( h, x, y, 2, 2 );
626 x264_mb_mc_1xywh( h, x, y, 2, 2 );
630 void x264_mb_mc( x264_t *h )
632 if( h->mb.i_partition == D_8x8 )
634 for( int i = 0; i < 4; i++ )
635 x264_mb_mc_8x8( h, i );
639 int ref0a = h->mb.cache.ref[0][x264_scan8[ 0]];
640 int ref0b = h->mb.cache.ref[0][x264_scan8[12]];
641 int ref1a = h->mb.cache.ref[1][x264_scan8[ 0]];
642 int ref1b = h->mb.cache.ref[1][x264_scan8[12]];
644 if( h->mb.i_partition == D_16x16 )
647 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
648 else x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
649 else x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
651 else if( h->mb.i_partition == D_16x8 )
654 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
655 else x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
656 else x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
659 if( ref1b >= 0 ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
660 else x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
661 else x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
663 else if( h->mb.i_partition == D_8x16 )
666 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
667 else x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
668 else x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
671 if( ref1b >= 0 ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
672 else x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
673 else x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
678 int x264_macroblock_cache_allocate( x264_t *h )
680 int i_mb_count = h->mb.i_mb_count;
682 h->mb.i_mb_stride = h->sps->i_mb_width;
683 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
684 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
686 h->mb.b_interlaced = h->param.b_interlaced;
688 CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
689 CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
690 CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
691 CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
692 CHECKED_MALLOC( h->mb.slice_table, i_mb_count * sizeof(uint16_t) );
693 memset( h->mb.slice_table, -1, i_mb_count * sizeof(uint16_t) );
695 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
696 CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
699 CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
701 if( h->param.b_cabac )
703 CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
704 CHECKED_MALLOC( h->mb.mvd[0], i_mb_count * sizeof( **h->mb.mvd ) );
705 CHECKED_MALLOC( h->mb.mvd[1], i_mb_count * sizeof( **h->mb.mvd ) );
708 for( int i = 0; i < 2; i++ )
710 int i_refs = X264_MIN(16, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << h->param.b_interlaced;
711 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
712 i_refs = X264_MIN(16, i_refs + 2); //smart weights add two duplicate frames
713 else if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_BLIND )
714 i_refs = X264_MIN(16, i_refs + 1); //blind weights add one duplicate frame
716 for( int j = 0; j < i_refs; j++ )
717 CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
720 if( h->param.analyse.i_weighted_pred )
722 int i_padv = PADV << h->param.b_interlaced;
723 #define ALIGN(x,a) (((x)+((a)-1))&~((a)-1))
724 int align = h->param.cpu&X264_CPU_CACHELINE_64 ? 64 : h->param.cpu&X264_CPU_CACHELINE_32 ? 32 : 16;
725 int i_stride, luma_plane_size = 0;
728 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_FAKE )
730 // only need buffer for lookahead
731 if( !h->param.i_sync_lookahead || h == h->thread[h->param.i_threads] )
733 // Fake analysis only works on lowres
734 i_stride = ALIGN( h->sps->i_mb_width*8 + 2*PADH, align );
735 luma_plane_size = i_stride * (h->sps->i_mb_height*8+2*i_padv);
736 // Only need 1 buffer for analysis
744 i_stride = ALIGN( h->sps->i_mb_width*16 + 2*PADH, align );
745 luma_plane_size = i_stride * (h->sps->i_mb_height*16+2*i_padv);
747 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
748 //SMART can weight one ref and one offset -1
751 //blind only has one weighted copy (offset -1)
755 for( int i = 0; i < numweightbuf; i++ )
756 CHECKED_MALLOC( h->mb.p_weight_buf[i], luma_plane_size );
763 void x264_macroblock_cache_free( x264_t *h )
765 for( int i = 0; i < 2; i++ )
766 for( int j = 0; j < 32; j++ )
767 x264_free( h->mb.mvr[i][j] );
768 for( int i = 0; i < 16; i++ )
769 x264_free( h->mb.p_weight_buf[i] );
771 if( h->param.b_cabac )
773 x264_free( h->mb.chroma_pred_mode );
774 x264_free( h->mb.mvd[0] );
775 x264_free( h->mb.mvd[1] );
777 x264_free( h->mb.slice_table );
778 x264_free( h->mb.intra4x4_pred_mode );
779 x264_free( h->mb.non_zero_count );
780 x264_free( h->mb.mb_transform_size );
781 x264_free( h->mb.skipbp );
782 x264_free( h->mb.cbp );
783 x264_free( h->mb.qp );
786 int x264_macroblock_thread_allocate( x264_t *h, int b_lookahead )
789 for( int i = 0; i <= h->param.b_interlaced; i++ )
790 for( int j = 0; j < 3; j++ )
792 /* shouldn't really be initialized, just silences a valgrind false-positive in predict_8x8_filter_mmx */
793 CHECKED_MALLOCZERO( h->intra_border_backup[i][j], (h->sps->i_mb_width*16+32)>>!!j );
794 h->intra_border_backup[i][j] += 8;
797 /* Allocate scratch buffer */
798 int scratch_size = 0;
801 int buf_hpel = (h->thread[0]->fdec->i_width[0]+48) * sizeof(int16_t);
802 int buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int);
803 int me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range);
804 int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) *
805 ((me_range*2+18) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t));
806 int buf_nnz = !h->param.b_cabac * h->pps->b_transform_8x8_mode * (h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t));
807 scratch_size = X264_MAX4( buf_hpel, buf_ssim, buf_tesa, buf_nnz );
809 int buf_mbtree = h->param.rc.b_mb_tree * ((h->sps->i_mb_width+3)&~3) * sizeof(int);
810 scratch_size = X264_MAX( scratch_size, buf_mbtree );
811 CHECKED_MALLOC( h->scratch_buffer, scratch_size );
817 void x264_macroblock_thread_free( x264_t *h, int b_lookahead )
820 for( int i = 0; i <= h->param.b_interlaced; i++ )
821 for( int j = 0; j < 3; j++ )
822 x264_free( h->intra_border_backup[i][j] - 8 );
823 x264_free( h->scratch_buffer );
826 void x264_macroblock_slice_init( x264_t *h )
828 h->mb.mv[0] = h->fdec->mv[0];
829 h->mb.mv[1] = h->fdec->mv[1];
830 h->mb.ref[0] = h->fdec->ref[0];
831 h->mb.ref[1] = h->fdec->ref[1];
832 h->mb.type = h->fdec->mb_type;
833 h->mb.partition = h->fdec->mb_partition;
835 h->fdec->i_ref[0] = h->i_ref0;
836 h->fdec->i_ref[1] = h->i_ref1;
837 for( int i = 0; i < h->i_ref0; i++ )
838 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
839 if( h->sh.i_type == SLICE_TYPE_B )
841 for( int i = 0; i < h->i_ref1; i++ )
842 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
844 map_col_to_list0(-1) = -1;
845 map_col_to_list0(-2) = -2;
846 for( int i = 0; i < h->fref1[0]->i_ref[0]; i++ )
848 int poc = h->fref1[0]->ref_poc[0][i];
849 map_col_to_list0(i) = -2;
850 for( int j = 0; j < h->i_ref0; j++ )
851 if( h->fref0[j]->i_poc == poc )
853 map_col_to_list0(i) = j;
858 if( h->sh.i_type == SLICE_TYPE_P )
859 memset( h->mb.cache.skip, 0, sizeof( h->mb.cache.skip ) );
861 /* init with not available (for top right idx=7,15) */
862 memset( h->mb.cache.ref, -2, sizeof( h->mb.cache.ref ) );
864 setup_inverse_delta_pocs( h );
866 h->mb.i_neighbour4[6] =
867 h->mb.i_neighbour4[9] =
868 h->mb.i_neighbour4[12] =
869 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
870 h->mb.i_neighbour4[3] =
871 h->mb.i_neighbour4[7] =
872 h->mb.i_neighbour4[11] =
873 h->mb.i_neighbour4[13] =
874 h->mb.i_neighbour4[15] =
875 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
878 void x264_macroblock_thread_init( x264_t *h )
880 h->mb.i_me_method = h->param.analyse.i_me_method;
881 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
882 if( h->sh.i_type == SLICE_TYPE_B && (h->mb.i_subpel_refine == 6 || h->mb.i_subpel_refine == 8) )
883 h->mb.i_subpel_refine--;
884 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
885 && h->mb.i_subpel_refine >= 5;
886 h->mb.b_dct_decimate = h->sh.i_type == SLICE_TYPE_B ||
887 (h->param.analyse.b_dct_decimate && h->sh.i_type != SLICE_TYPE_I);
900 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
901 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
902 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
903 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
904 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
905 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
908 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
910 int stride_y = fenc->i_stride[0];
911 int stride_uv = fenc->i_stride[1];
912 int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
913 int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
914 h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
915 fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
918 static NOINLINE void copy_column8( uint8_t *dst, uint8_t *src )
920 // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
921 for( int i = -4; i < 4; i++ )
922 dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
925 static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int mb_x, int mb_y, int i )
927 const int w = (i == 0 ? 16 : 8);
928 const int i_stride = h->fdec->i_stride[!!i];
929 const int i_stride2 = i_stride << h->mb.b_interlaced;
930 const int i_pix_offset = h->mb.b_interlaced
931 ? w * (mb_x + (mb_y&~1) * i_stride) + (mb_y&1) * i_stride
932 : w * (mb_x + mb_y * i_stride);
933 const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
934 const uint8_t *intra_fdec = &h->intra_border_backup[mb_y & h->sh.b_mbaff][i][mb_x*16>>!!i];
935 int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
936 x264_frame_t **fref[2] = { h->fref0, h->fref1 };
937 if( h->mb.b_interlaced )
938 ref_pix_offset[1] += (1-2*(mb_y&1)) * i_stride;
939 h->mb.pic.i_stride[i] = i_stride2;
940 h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
941 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
942 h->mb.pic.p_fenc_plane[i], i_stride2, w );
943 memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
944 if( h->mb.b_interlaced )
945 for( int j = 0; j < w; j++ )
946 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
947 for( int j = 0; j < h->mb.pic.i_fref[0]; j++ )
949 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]];
952 for( int k = 1; k < 4; k++ )
953 h->mb.pic.p_fref[0][j][k] = &fref[0][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
954 if( h->sh.weight[j][0].weightfn )
955 h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> h->mb.b_interlaced][ref_pix_offset[j&1]];
957 h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
960 if( h->sh.i_type == SLICE_TYPE_B )
961 for( int j = 0; j < h->mb.pic.i_fref[1]; j++ )
963 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]];
965 for( int k = 1; k < 4; k++ )
966 h->mb.pic.p_fref[1][j][k] = &fref[1][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
970 static void inline x264_macroblock_cache_load_neighbours( x264_t *h, int mb_x, int mb_y )
972 int top = (mb_y - (1 << h->mb.b_interlaced)) * h->mb.i_mb_stride + mb_x;
976 h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
977 h->mb.i_b8_xy = 2*(mb_y * h->mb.i_b8_stride + mb_x);
978 h->mb.i_b4_xy = 4*(mb_y * h->mb.i_b4_stride + mb_x);
979 h->mb.i_neighbour = 0;
980 h->mb.i_neighbour_intra = 0;
981 h->mb.i_neighbour_frame = 0;
982 h->mb.i_mb_top_xy = -1;
983 h->mb.i_mb_left_xy = -1;
984 h->mb.i_mb_topleft_xy = -1;
985 h->mb.i_mb_topright_xy = -1;
986 h->mb.i_mb_type_top = -1;
987 h->mb.i_mb_type_left = -1;
988 h->mb.i_mb_type_topleft = -1;
989 h->mb.i_mb_type_topright = -1;
993 h->mb.i_neighbour_frame |= MB_LEFT;
994 h->mb.i_mb_left_xy = h->mb.i_mb_xy - 1;
995 h->mb.i_mb_type_left = h->mb.type[h->mb.i_mb_left_xy];
996 if( h->mb.i_mb_xy > h->sh.i_first_mb )
998 h->mb.i_neighbour |= MB_LEFT;
1000 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_left ) )
1001 h->mb.i_neighbour_intra |= MB_LEFT;
1005 /* We can't predict from the previous threadslice since it hasn't been encoded yet. */
1006 if( (h->i_threadslice_start >> h->mb.b_interlaced) != (mb_y >> h->mb.b_interlaced) )
1010 h->mb.i_neighbour_frame |= MB_TOP;
1011 h->mb.i_mb_top_xy = top;
1012 h->mb.i_mb_type_top = h->mb.type[h->mb.i_mb_top_xy];
1013 if( top >= h->sh.i_first_mb )
1015 h->mb.i_neighbour |= MB_TOP;
1017 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_top ) )
1018 h->mb.i_neighbour_intra |= MB_TOP;
1020 /* We only need to prefetch the top blocks because the left was just written
1021 * to as part of the previous cache_save. Since most target CPUs use write-allocate
1022 * caches, left blocks are near-guaranteed to be in L1 cache. Top--not so much. */
1023 x264_prefetch( &h->mb.cbp[top] );
1024 x264_prefetch( h->mb.intra4x4_pred_mode[top] );
1025 x264_prefetch( &h->mb.non_zero_count[top][12] );
1026 /* These aren't always allocated, but prefetching an invalid address can't hurt. */
1027 x264_prefetch( &h->mb.mb_transform_size[top] );
1028 x264_prefetch( &h->mb.skipbp[top] );
1032 if( mb_x > 0 && top - 1 >= 0 )
1034 h->mb.i_neighbour_frame |= MB_TOPLEFT;
1035 h->mb.i_mb_topleft_xy = top - 1;
1036 h->mb.i_mb_type_topright = h->mb.type[h->mb.i_mb_topleft_xy];
1037 if( top - 1 >= h->sh.i_first_mb )
1039 h->mb.i_neighbour |= MB_TOPLEFT;
1041 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
1042 h->mb.i_neighbour_intra |= MB_TOPLEFT;
1046 if( mb_x < h->sps->i_mb_width - 1 && top + 1 >= 0 )
1048 h->mb.i_neighbour_frame |= MB_TOPRIGHT;
1049 h->mb.i_mb_topright_xy = top + 1;
1050 h->mb.i_mb_type_topleft = h->mb.type[h->mb.i_mb_topright_xy];
1051 if( top + 1 >= h->sh.i_first_mb )
1053 h->mb.i_neighbour |= MB_TOPRIGHT;
1055 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
1056 h->mb.i_neighbour_intra |= MB_TOPRIGHT;
1062 void x264_macroblock_cache_load( x264_t *h, int mb_x, int mb_y )
1064 x264_macroblock_cache_load_neighbours( h, mb_x, mb_y );
1066 int left = h->mb.i_mb_left_xy;
1067 int top = h->mb.i_mb_top_xy;
1068 int top_y = mb_y - (1 << h->mb.b_interlaced);
1069 int top_8x8 = (2*top_y+1) * h->mb.i_b8_stride + 2*mb_x;
1070 int top_4x4 = (4*top_y+3) * h->mb.i_b4_stride + 4*mb_x;
1072 /* GCC pessimizes direct loads from heap-allocated arrays due to aliasing. */
1073 /* By only dereferencing them once, we avoid this issue. */
1074 int8_t (*i4x4)[8] = h->mb.intra4x4_pred_mode;
1075 uint8_t (*nnz)[24] = h->mb.non_zero_count;
1076 int16_t *cbp = h->mb.cbp;
1079 if( h->mb.i_neighbour & MB_TOP )
1081 h->mb.cache.i_cbp_top = cbp[top];
1083 CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &i4x4[top][0] );
1085 /* load non_zero_count */
1086 CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &nnz[top][12] );
1087 /* shift because x264_scan8[16] is misaligned */
1088 M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = M16( &nnz[top][18] ) << 8;
1089 M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = M16( &nnz[top][22] ) << 8;
1093 h->mb.cache.i_cbp_top = -1;
1096 M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
1098 /* load non_zero_count */
1099 M32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8] ) = 0x80808080U;
1100 M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = 0x80808080U;
1101 M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = 0x80808080U;
1104 if( h->mb.i_neighbour & MB_LEFT )
1106 h->mb.cache.i_cbp_left = cbp[left];
1109 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = i4x4[left][4];
1110 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = i4x4[left][5];
1111 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = i4x4[left][6];
1112 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = i4x4[left][3];
1114 /* load non_zero_count */
1115 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = nnz[left][3];
1116 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = nnz[left][7];
1117 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = nnz[left][11];
1118 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[left][15];
1120 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = nnz[left][16+1];
1121 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = nnz[left][16+3];
1123 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = nnz[left][16+4+1];
1124 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = nnz[left][16+4+3];
1126 /* Finish the prefetching */
1127 if( h->sh.i_type != SLICE_TYPE_I )
1128 for( int l = 0; l < (h->sh.i_type == SLICE_TYPE_B) + 1; l++ )
1130 x264_prefetch( &h->mb.mv[l][top_4x4-1] );
1131 /* Top right being not in the same cacheline as top left will happen
1132 * once every 4 MBs, so one extra prefetch is worthwhile */
1133 x264_prefetch( &h->mb.mv[l][top_4x4+4] );
1134 x264_prefetch( &h->mb.ref[l][top_8x8-1] );
1135 x264_prefetch( &h->mb.mvd[l][top] );
1140 h->mb.cache.i_cbp_left = -1;
1142 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1143 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1144 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1145 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1147 /* load non_zero_count */
1148 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1149 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1150 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1151 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1152 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1153 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1154 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1155 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1158 if( h->pps->b_transform_8x8_mode )
1160 h->mb.cache.i_neighbour_transform_size =
1161 ( (h->mb.i_neighbour & MB_LEFT) && h->mb.mb_transform_size[left] )
1162 + ( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top] );
1167 h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1168 h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1169 h->mb.cache.i_neighbour_interlaced =
1170 !!(h->mb.i_neighbour & MB_LEFT)
1171 + !!(h->mb.i_neighbour & MB_TOP);
1174 if( !h->mb.b_interlaced )
1176 copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
1177 copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
1178 copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
1179 copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
1182 /* load picture pointers */
1183 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 0 );
1184 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1 );
1185 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 2 );
1187 if( h->fdec->integral )
1189 int offset = 16 * (mb_x + mb_y * h->fdec->i_stride[0]);
1190 for( int i = 0; i < h->mb.pic.i_fref[0]; i++ )
1191 h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[offset];
1192 for( int i = 0; i < h->mb.pic.i_fref[1]; i++ )
1193 h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[offset];
1196 x264_prefetch_fenc( h, h->fenc, mb_x, mb_y );
1198 /* load ref/mv/mvd */
1199 if( h->sh.i_type != SLICE_TYPE_I )
1201 int s8x8 = h->mb.i_b8_stride;
1202 int s4x4 = h->mb.i_b4_stride;
1204 for( int l = 0; l < (h->sh.i_type == SLICE_TYPE_B) + 1; l++ )
1206 int16_t (*mv)[2] = h->mb.mv[l];
1207 int8_t *ref = h->mb.ref[l];
1209 int i8 = x264_scan8[0] - 1 - 1*8;
1210 if( h->mb.i_neighbour & MB_TOPLEFT )
1212 h->mb.cache.ref[l][i8] = ref[top_8x8 - 1];
1213 CP32( h->mb.cache.mv[l][i8], mv[top_4x4 - 1] );
1217 h->mb.cache.ref[l][i8] = -2;
1218 M32( h->mb.cache.mv[l][i8] ) = 0;
1221 i8 = x264_scan8[0] - 8;
1222 if( h->mb.i_neighbour & MB_TOP )
1224 h->mb.cache.ref[l][i8+0] =
1225 h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
1226 h->mb.cache.ref[l][i8+2] =
1227 h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
1228 CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
1232 M128( h->mb.cache.mv[l][i8] ) = M128_ZERO;
1233 M32( &h->mb.cache.ref[l][i8] ) = (uint8_t)(-2) * 0x01010101U;
1236 i8 = x264_scan8[0] + 4 - 1*8;
1237 if( h->mb.i_neighbour & MB_TOPRIGHT )
1239 h->mb.cache.ref[l][i8] = ref[top_8x8 + 2];
1240 CP32( h->mb.cache.mv[l][i8], mv[top_4x4 + 4] );
1243 h->mb.cache.ref[l][i8] = -2;
1245 i8 = x264_scan8[0] - 1;
1246 if( h->mb.i_neighbour & MB_LEFT )
1248 const int ir = h->mb.i_b8_xy - 1;
1249 const int iv = h->mb.i_b4_xy - 1;
1250 h->mb.cache.ref[l][i8+0*8] =
1251 h->mb.cache.ref[l][i8+1*8] = ref[ir + 0*s8x8];
1252 h->mb.cache.ref[l][i8+2*8] =
1253 h->mb.cache.ref[l][i8+3*8] = ref[ir + 1*s8x8];
1255 CP32( h->mb.cache.mv[l][i8+0*8], mv[iv + 0*s4x4] );
1256 CP32( h->mb.cache.mv[l][i8+1*8], mv[iv + 1*s4x4] );
1257 CP32( h->mb.cache.mv[l][i8+2*8], mv[iv + 2*s4x4] );
1258 CP32( h->mb.cache.mv[l][i8+3*8], mv[iv + 3*s4x4] );
1262 for( int i = 0; i < 4; i++ )
1264 h->mb.cache.ref[l][i8+i*8] = -2;
1265 M32( h->mb.cache.mv[l][i8+i*8] ) = 0;
1269 if( h->param.b_cabac )
1271 uint8_t (*mvd)[8][2] = h->mb.mvd[l];
1272 if( h->mb.i_neighbour & MB_TOP )
1273 CP64( h->mb.cache.mvd[l][x264_scan8[0] - 8], mvd[top][0] );
1275 M64( h->mb.cache.mvd[l][x264_scan8[0] - 8] ) = 0;
1277 if( h->mb.i_neighbour & MB_LEFT )
1279 CP16( h->mb.cache.mvd[l][x264_scan8[0 ] - 1], mvd[left][4] );
1280 CP16( h->mb.cache.mvd[l][x264_scan8[2 ] - 1], mvd[left][5] );
1281 CP16( h->mb.cache.mvd[l][x264_scan8[8 ] - 1], mvd[left][6] );
1282 CP16( h->mb.cache.mvd[l][x264_scan8[10] - 1], mvd[left][3] );
1285 for( int i = 0; i < 4; i++ )
1286 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+i*8] ) = 0;
1291 if( h->sh.i_type == SLICE_TYPE_B )
1293 h->mb.bipred_weight = h->mb.bipred_weight_buf[h->mb.b_interlaced&(mb_y&1)];
1294 h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[h->mb.b_interlaced&(mb_y&1)];
1295 if( h->param.b_cabac )
1298 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1299 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left] : 0;
1300 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
1301 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
1302 skipbp = (h->mb.i_neighbour & MB_TOP) ? h->mb.skipbp[top] : 0;
1303 h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
1304 h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
1308 if( h->sh.i_type == SLICE_TYPE_P )
1309 x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1312 h->mb.i_neighbour4[0] =
1313 h->mb.i_neighbour8[0] = (h->mb.i_neighbour_intra & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1314 | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOPRIGHT : 0);
1315 h->mb.i_neighbour4[4] =
1316 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour_intra & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1317 h->mb.i_neighbour4[2] =
1318 h->mb.i_neighbour4[8] =
1319 h->mb.i_neighbour4[10] =
1320 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour_intra & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1321 h->mb.i_neighbour4[5] =
1322 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour_intra & MB_TOPRIGHT)
1323 | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1326 static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int i )
1329 int i_stride = h->fdec->i_stride[!!i];
1330 int i_stride2 = i_stride << h->mb.b_interlaced;
1331 int i_pix_offset = h->mb.b_interlaced
1332 ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1333 : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1334 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( &h->fdec->plane[i][i_pix_offset], i_stride2,
1335 h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1338 void x264_macroblock_cache_save( x264_t *h )
1340 const int i_mb_xy = h->mb.i_mb_xy;
1341 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1342 const int s8x8 = h->mb.i_b8_stride;
1343 const int s4x4 = h->mb.i_b4_stride;
1344 const int i_mb_4x4 = h->mb.i_b4_xy;
1345 const int i_mb_8x8 = h->mb.i_b8_xy;
1347 /* GCC pessimizes direct stores to heap-allocated arrays due to aliasing. */
1348 /* By only dereferencing them once, we avoid this issue. */
1349 int8_t *i4x4 = h->mb.intra4x4_pred_mode[i_mb_xy];
1350 uint8_t *nnz = h->mb.non_zero_count[i_mb_xy];
1352 x264_macroblock_store_pic( h, 0 );
1353 x264_macroblock_store_pic( h, 1 );
1354 x264_macroblock_store_pic( h, 2 );
1356 x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1358 h->mb.type[i_mb_xy] = i_mb_type;
1359 h->mb.slice_table[i_mb_xy] = h->sh.i_first_mb;
1360 h->mb.partition[i_mb_xy] = IS_INTRA( i_mb_type ) ? D_16x16 : h->mb.i_partition;
1361 h->mb.i_mb_prev_xy = i_mb_xy;
1364 if( i_mb_type == I_4x4 )
1366 CP32( &i4x4[0], &h->mb.cache.intra4x4_pred_mode[x264_scan8[10]] );
1367 M32( &i4x4[4] ) = pack8to32( h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
1368 h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
1369 h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
1371 else if( !h->param.b_constrained_intra || IS_INTRA(i_mb_type) )
1372 M64( i4x4 ) = I_PRED_4x4_DC * 0x0101010101010101ULL;
1374 M64( i4x4 ) = (uint8_t)(-1) * 0x0101010101010101ULL;
1377 if( i_mb_type == I_PCM )
1379 h->mb.qp[i_mb_xy] = 0;
1380 h->mb.i_last_dqp = 0;
1381 h->mb.i_cbp_chroma = 2;
1382 h->mb.i_cbp_luma = 0xf;
1383 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1384 h->mb.b_transform_8x8 = 0;
1385 memset( nnz, 16, sizeof( *h->mb.non_zero_count ) );
1389 /* save non zero count */
1390 CP32( &nnz[0*4], &h->mb.cache.non_zero_count[x264_scan8[0]+0*8] );
1391 CP32( &nnz[1*4], &h->mb.cache.non_zero_count[x264_scan8[0]+1*8] );
1392 CP32( &nnz[2*4], &h->mb.cache.non_zero_count[x264_scan8[0]+2*8] );
1393 CP32( &nnz[3*4], &h->mb.cache.non_zero_count[x264_scan8[0]+3*8] );
1394 M16( &nnz[16+0*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+0*2]-1] ) >> 8;
1395 M16( &nnz[16+1*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+1*2]-1] ) >> 8;
1396 M16( &nnz[16+2*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+2*2]-1] ) >> 8;
1397 M16( &nnz[16+3*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+3*2]-1] ) >> 8;
1399 if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1400 h->mb.i_qp = h->mb.i_last_qp;
1401 h->mb.qp[i_mb_xy] = h->mb.i_qp;
1402 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1403 h->mb.i_last_qp = h->mb.i_qp;
1406 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1407 h->mb.b_transform_8x8 = 0;
1408 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1410 if( h->sh.i_type != SLICE_TYPE_I )
1412 int16_t (*mv0)[2] = &h->mb.mv[0][i_mb_4x4];
1413 int16_t (*mv1)[2] = &h->mb.mv[1][i_mb_4x4];
1414 int8_t *ref0 = &h->mb.ref[0][i_mb_8x8];
1415 int8_t *ref1 = &h->mb.ref[1][i_mb_8x8];
1416 if( !IS_INTRA( i_mb_type ) )
1418 ref0[0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
1419 ref0[1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
1420 ref0[0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
1421 ref0[1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
1422 CP128( &mv0[0*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*0] );
1423 CP128( &mv0[1*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*1] );
1424 CP128( &mv0[2*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*2] );
1425 CP128( &mv0[3*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*3] );
1426 if( h->sh.i_type == SLICE_TYPE_B )
1428 ref1[0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
1429 ref1[1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
1430 ref1[0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
1431 ref1[1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
1432 CP128( &mv1[0*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*0] );
1433 CP128( &mv1[1*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*1] );
1434 CP128( &mv1[2*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*2] );
1435 CP128( &mv1[3*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*3] );
1440 M16( &ref0[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1441 M16( &ref0[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1442 M128( &mv0[0*s4x4] ) = M128_ZERO;
1443 M128( &mv0[1*s4x4] ) = M128_ZERO;
1444 M128( &mv0[2*s4x4] ) = M128_ZERO;
1445 M128( &mv0[3*s4x4] ) = M128_ZERO;
1446 if( h->sh.i_type == SLICE_TYPE_B )
1448 M16( &ref1[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1449 M16( &ref1[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1450 M128( &mv1[0*s4x4] ) = M128_ZERO;
1451 M128( &mv1[1*s4x4] ) = M128_ZERO;
1452 M128( &mv1[2*s4x4] ) = M128_ZERO;
1453 M128( &mv1[3*s4x4] ) = M128_ZERO;
1458 if( h->param.b_cabac )
1460 uint8_t (*mvd0)[2] = h->mb.mvd[0][i_mb_xy];
1461 uint8_t (*mvd1)[2] = h->mb.mvd[1][i_mb_xy];
1462 if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
1463 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1465 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1467 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1469 CP64( mvd0[0], h->mb.cache.mvd[0][x264_scan8[10]] );
1470 CP16( mvd0[4], h->mb.cache.mvd[0][x264_scan8[5 ]] );
1471 CP16( mvd0[5], h->mb.cache.mvd[0][x264_scan8[7 ]] );
1472 CP16( mvd0[6], h->mb.cache.mvd[0][x264_scan8[13]] );
1473 if( h->sh.i_type == SLICE_TYPE_B )
1475 CP64( mvd1[0], h->mb.cache.mvd[1][x264_scan8[10]] );
1476 CP16( mvd1[4], h->mb.cache.mvd[1][x264_scan8[5 ]] );
1477 CP16( mvd1[5], h->mb.cache.mvd[1][x264_scan8[7 ]] );
1478 CP16( mvd1[6], h->mb.cache.mvd[1][x264_scan8[13]] );
1483 M128( mvd0[0] ) = M128_ZERO;
1484 if( h->sh.i_type == SLICE_TYPE_B )
1485 M128( mvd1[0] ) = M128_ZERO;
1488 if( h->sh.i_type == SLICE_TYPE_B )
1490 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1491 h->mb.skipbp[i_mb_xy] = 0xf;
1492 else if( i_mb_type == B_8x8 )
1494 int skipbp = ( h->mb.i_sub_partition[0] == D_DIRECT_8x8 ) << 0;
1495 skipbp |= ( h->mb.i_sub_partition[1] == D_DIRECT_8x8 ) << 1;
1496 skipbp |= ( h->mb.i_sub_partition[2] == D_DIRECT_8x8 ) << 2;
1497 skipbp |= ( h->mb.i_sub_partition[3] == D_DIRECT_8x8 ) << 3;
1498 h->mb.skipbp[i_mb_xy] = skipbp;
1501 h->mb.skipbp[i_mb_xy] = 0;
1507 void x264_macroblock_bipred_init( x264_t *h )
1509 for( int field = 0; field <= h->sh.b_mbaff; field++ )
1510 for( int i_ref0 = 0; i_ref0 < (h->i_ref0<<h->sh.b_mbaff); i_ref0++ )
1512 int poc0 = h->fref0[i_ref0>>h->sh.b_mbaff]->i_poc;
1513 if( h->sh.b_mbaff && field^(i_ref0&1) )
1514 poc0 += h->sh.i_delta_poc_bottom;
1515 for( int i_ref1 = 0; i_ref1 < (h->i_ref1<<h->sh.b_mbaff); i_ref1++ )
1517 int dist_scale_factor;
1518 int poc1 = h->fref1[i_ref1>>h->sh.b_mbaff]->i_poc;
1519 if( h->sh.b_mbaff && field^(i_ref1&1) )
1520 poc1 += h->sh.i_delta_poc_bottom;
1521 int cur_poc = h->fdec->i_poc + field*h->sh.i_delta_poc_bottom;
1522 int td = x264_clip3( poc1 - poc0, -128, 127 );
1523 if( td == 0 /* || pic0 is a long-term ref */ )
1524 dist_scale_factor = 256;
1527 int tb = x264_clip3( cur_poc - poc0, -128, 127 );
1528 int tx = (16384 + (abs(td) >> 1)) / td;
1529 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1532 h->mb.dist_scale_factor_buf[field][i_ref0][i_ref1] = dist_scale_factor;
1534 dist_scale_factor >>= 2;
1535 if( h->param.analyse.b_weighted_bipred
1536 && dist_scale_factor >= -64
1537 && dist_scale_factor <= 128 )
1539 h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 64 - dist_scale_factor;
1540 // ssse3 implementation of biweight doesn't support the extrema.
1541 // if we ever generate them, we'll have to drop that optimization.
1542 assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
1545 h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 32;