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];
41 if( (idx&3) >= 2 + (i_width&1) || i_refc == -2 )
43 i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
44 mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
47 if( h->mb.i_partition == D_16x8 )
66 else if( h->mb.i_partition == D_8x16 )
86 if( i_refa == i_ref ) i_count++;
87 if( i_refb == i_ref ) i_count++;
88 if( i_refc == i_ref ) i_count++;
93 x264_median_mv( mvp, mv_a, mv_b, mv_c );
95 else if( i_count == 1 )
99 else if( i_refb == i_ref )
104 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
110 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int16_t mvp[2] )
112 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
113 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
114 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
115 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
116 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
117 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
123 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
124 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
127 if( i_refa == i_ref ) i_count++;
128 if( i_refb == i_ref ) i_count++;
129 if( i_refc == i_ref ) i_count++;
134 x264_median_mv( mvp, mv_a, mv_b, mv_c );
136 else if( i_count == 1 )
138 if( i_refa == i_ref )
140 else if( i_refb == i_ref )
145 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
152 void x264_mb_predict_mv_pskip( x264_t *h, int16_t mv[2] )
154 int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
155 int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
156 int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
157 int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
159 if( i_refa == -2 || i_refb == -2 ||
160 !( i_refa | M32( mv_a ) ) ||
161 !( i_refb | M32( mv_b ) ) )
167 x264_mb_predict_mv_16x16( h, 0, 0, mv );
171 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
173 int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
174 int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
176 const int type_col = h->fref1[0]->mb_type[h->mb.i_mb_xy];
178 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
180 if( IS_INTRA( type_col ) )
182 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
183 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0 );
184 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0 );
188 for( i8 = 0; i8 < 4; i8++ )
192 const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
193 const int i_ref1_ref = h->fref1[0]->ref[0][i_part_8x8];
194 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);
198 const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
199 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
200 const int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
201 const int l0y = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
202 if( h->param.i_threads > 1 && (l0y > h->mb.mv_max_spel[1] || l0y-mv_col[1] > h->mb.mv_max_spel[1]) )
204 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
205 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, pack16to32_mask(l0x, l0y) );
206 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_col[1]) );
210 /* the collocated ref isn't in the current list0 */
211 /* FIXME: we might still be able to use direct_8x8 on some partitions */
212 /* FIXME: with B-pyramid + extensive ref list reordering
213 * (not currently used), we would also have to check
214 * l1mv1 like in spatial mode */
222 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
225 ALIGNED_ARRAY_8( int16_t, mv,[2],[2] );
228 const int8_t *l1ref0 = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
229 const int8_t *l1ref1 = &h->fref1[0]->ref[1][ h->mb.i_b8_xy ];
230 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
231 const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[1][ h->mb.i_b4_xy ];
232 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
234 for( i_list=0; i_list<2; i_list++ )
236 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
237 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
238 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
240 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
242 ref[i_list] = i_refa;
243 if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
244 ref[i_list] = i_refb;
245 if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
246 ref[i_list] = i_refc;
247 if( ref[i_list] < 0 )
251 if( ref[0] < 0 && ref[1] < 0 )
253 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
254 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
255 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0 );
256 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0 );
261 x264_mb_predict_mv_16x16( h, 0, ref[0], mv[0] );
266 x264_mb_predict_mv_16x16( h, 1, ref[1], mv[1] );
270 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
271 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
272 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, mv[0] );
273 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, mv[1] );
275 if( h->param.i_threads > 1
276 && ( mv[0][1] > h->mb.mv_max_spel[1]
277 || mv[1][1] > h->mb.mv_max_spel[1] ) )
280 fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
281 mv[0][0], mv[0][1], mv[1][0], mv[1][1],
282 h->mb.mv_max_spel[1]);
287 if( IS_INTRA( type_col ) || (ref[0]&&ref[1]) )
291 for( i8=0; i8<4; i8++ )
295 const int o8 = x8 + y8 * h->mb.i_b8_stride;
296 const int o4 = 3*(x8 + y8 * h->mb.i_b4_stride);
297 if( l1ref0[o8] == 0 )
299 if( abs( l1mv0[o4][0] ) <= 1 && abs( l1mv0[o4][1] ) <= 1 )
301 if( ref[0] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0 );
302 if( ref[1] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0 );
305 else if( l1ref0[o8] < 0 && l1ref1[o8] == 0 )
307 if( abs( l1mv1[o4][0] ) <= 1 && abs( l1mv1[o4][1] ) <= 1 )
309 if( ref[0] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0 );
310 if( ref[1] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0 );
318 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
321 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
323 else if( h->sh.b_direct_spatial_mv_pred )
324 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
326 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
328 if( b_changed != NULL && b_available )
330 int type_col = h->fref1[0]->mb_type[h->mb.i_mb_xy];
333 if( IS_INTRA( type_col ) || type_col == P_SKIP )
335 changed |= M32( h->mb.cache.direct_mv[0][0] ) ^ M32( h->mb.cache.mv[0][X264_SCAN8_0] );
336 changed |= M32( h->mb.cache.direct_mv[1][0] ) ^ M32( h->mb.cache.mv[1][X264_SCAN8_0] );
337 changed |= h->mb.cache.direct_ref[0][0] ^ h->mb.cache.ref[0][X264_SCAN8_0];
338 changed |= h->mb.cache.direct_ref[1][0] ^ h->mb.cache.ref[1][X264_SCAN8_0];
343 for( l = 0; l < 2; l++ )
345 changed |= M32( h->mb.cache.direct_mv[l][0] ) ^ M32( h->mb.cache.mv[l][x264_scan8[ 0]] );
347 changed |= M32( h->mb.cache.direct_mv[l][1] ) ^ M32( h->mb.cache.mv[l][x264_scan8[ 4]] );
348 changed |= M32( h->mb.cache.direct_mv[l][2] ) ^ M32( h->mb.cache.mv[l][x264_scan8[ 8]] );
349 changed |= M32( h->mb.cache.direct_mv[l][3] ) ^ M32( h->mb.cache.mv[l][x264_scan8[12]] );
351 changed |= h->mb.cache.direct_ref[l][0] ^ h->mb.cache.ref[l][x264_scan8[ 0]];
352 changed |= h->mb.cache.direct_ref[l][1] ^ h->mb.cache.ref[l][x264_scan8[ 4]];
353 changed |= h->mb.cache.direct_ref[l][2] ^ h->mb.cache.ref[l][x264_scan8[ 8]];
354 changed |= h->mb.cache.direct_ref[l][3] ^ h->mb.cache.ref[l][x264_scan8[12]];
357 *b_changed = changed;
366 for( l = 0; l < 2; l++ )
368 CP32( h->mb.cache.direct_mv[l][0], h->mb.cache.mv[l][x264_scan8[ 0]] );
369 CP32( h->mb.cache.direct_mv[l][1], h->mb.cache.mv[l][x264_scan8[ 4]] );
370 CP32( h->mb.cache.direct_mv[l][2], h->mb.cache.mv[l][x264_scan8[ 8]] );
371 CP32( h->mb.cache.direct_mv[l][3], h->mb.cache.mv[l][x264_scan8[12]] );
372 h->mb.cache.direct_ref[l][0] = h->mb.cache.ref[l][x264_scan8[ 0]];
373 h->mb.cache.direct_ref[l][1] = h->mb.cache.ref[l][x264_scan8[ 4]];
374 h->mb.cache.direct_ref[l][2] = h->mb.cache.ref[l][x264_scan8[ 8]];
375 h->mb.cache.direct_ref[l][3] = h->mb.cache.ref[l][x264_scan8[12]];
382 /* This just improves encoder performance, it's not part of the spec */
383 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int16_t mvc[9][2], int *i_mvc )
385 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
388 #define SET_MVP(mvp) { \
389 CP32( mvc[i], mvp ); \
394 if( h->sh.i_type == SLICE_TYPE_B
395 && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
397 SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
400 if( i_ref == 0 && h->frames.b_have_lowres )
402 int16_t (*lowres_mv)[2] = i_list ? h->fenc->lowres_mvs[1][h->fref1[0]->i_frame-h->fenc->i_frame-1]
403 : h->fenc->lowres_mvs[0][h->fenc->i_frame-h->fref0[0]->i_frame-1];
404 if( lowres_mv[0][0] != 0x7fff )
406 M32( mvc[i] ) = (M32( lowres_mv[h->mb.i_mb_xy] )*2)&0xfffeffff;
411 /* spatial predictors */
412 if( h->mb.i_neighbour & MB_LEFT )
414 int i_mb_l = h->mb.i_mb_xy - 1;
415 /* skip MBs didn't go through the whole search process, so mvr is undefined */
416 if( !IS_SKIP( h->mb.type[i_mb_l] ) )
417 SET_MVP( mvr[i_mb_l] );
419 if( h->mb.i_neighbour & MB_TOP )
421 int i_mb_t = h->mb.i_mb_top_xy;
422 if( !IS_SKIP( h->mb.type[i_mb_t] ) )
423 SET_MVP( mvr[i_mb_t] );
425 if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
426 SET_MVP( mvr[i_mb_t-1] );
427 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
428 SET_MVP( mvr[i_mb_t+1] );
432 /* temporal predictors */
433 /* FIXME temporal scaling w/ interlace */
434 if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
436 x264_frame_t *l0 = h->fref0[0];
438 #define SET_TMVP(dx, dy) { \
439 int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
440 int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
441 int ref_col = l0->ref[0][i_b8]; \
444 int scale = (h->fdec->i_poc - h->fdec->ref_poc[0][i_ref]) * l0->inv_ref_poc[ref_col];\
445 mvc[i][0] = (l0->mv[0][i_b4][0]*scale + 128) >> 8;\
446 mvc[i][1] = (l0->mv[0][i_b4][1]*scale + 128) >> 8;\
452 if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
454 if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
462 /* Set up a lookup table for delta pocs to reduce an IDIV to an IMUL */
463 static void setup_inverse_delta_pocs( x264_t *h )
466 for( i = 0; i < h->i_ref0; i++ )
468 int delta = h->fdec->i_poc - h->fref0[i]->i_poc;
469 h->fdec->inv_ref_poc[i] = (256 + delta/2) / delta;
473 static NOINLINE void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
475 const int i8 = x264_scan8[0]+x+8*y;
476 const int i_ref = h->mb.cache.ref[0][i8];
477 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;
478 int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
480 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
481 h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
482 mvx, mvy, 4*width, 4*height, &h->sh.weight[i_ref][0] );
484 // chroma is offset if MCing from a field of opposite parity
485 if( h->mb.b_interlaced & i_ref )
486 mvy += (h->mb.i_mb_y & 1)*4 - 2;
488 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
489 h->mb.pic.p_fref[0][i_ref][4], h->mb.pic.i_stride[1],
490 mvx, mvy, 2*width, 2*height );
492 if( h->sh.weight[i_ref][1].weightfn )
493 h->sh.weight[i_ref][1].weightfn[width>>1]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
494 &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
495 &h->sh.weight[i_ref][1], height*2 );
497 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
498 h->mb.pic.p_fref[0][i_ref][5], h->mb.pic.i_stride[2],
499 mvx, mvy, 2*width, 2*height );
501 if( h->sh.weight[i_ref][2].weightfn )
502 h->sh.weight[i_ref][2].weightfn[width>>1]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
503 &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
504 &h->sh.weight[i_ref][2],height*2 );
507 static NOINLINE void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
509 const int i8 = x264_scan8[0]+x+8*y;
510 const int i_ref = h->mb.cache.ref[1][i8];
511 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;
512 int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
514 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
515 h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
516 mvx, mvy, 4*width, 4*height, weight_none );
518 if( h->mb.b_interlaced & i_ref )
519 mvy += (h->mb.i_mb_y & 1)*4 - 2;
521 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
522 h->mb.pic.p_fref[1][i_ref][4], h->mb.pic.i_stride[1],
523 mvx, mvy, 2*width, 2*height );
525 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
526 h->mb.pic.p_fref[1][i_ref][5], h->mb.pic.i_stride[2],
527 mvx, mvy, 2*width, 2*height );
530 static NOINLINE void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
532 const int i8 = x264_scan8[0]+x+8*y;
533 const int i_ref0 = h->mb.cache.ref[0][i8];
534 const int i_ref1 = h->mb.cache.ref[1][i8];
535 const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
536 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;
537 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;
538 int mvy0 = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
539 int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
540 int i_mode = x264_size2pixel[height][width];
541 int i_stride0 = 16, i_stride1 = 16;
542 ALIGNED_ARRAY_16( uint8_t, tmp0,[16*16] );
543 ALIGNED_ARRAY_16( uint8_t, tmp1,[16*16] );
544 uint8_t *src0, *src1;
546 src0 = h->mc.get_ref( tmp0, &i_stride0, h->mb.pic.p_fref[0][i_ref0], h->mb.pic.i_stride[0],
547 mvx0, mvy0, 4*width, 4*height, weight_none );
548 src1 = h->mc.get_ref( tmp1, &i_stride1, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
549 mvx1, mvy1, 4*width, 4*height, weight_none );
550 h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
551 src0, i_stride0, src1, i_stride1, weight );
553 if( h->mb.b_interlaced & i_ref0 )
554 mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
555 if( h->mb.b_interlaced & i_ref1 )
556 mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
558 h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][4], h->mb.pic.i_stride[1],
559 mvx0, mvy0, 2*width, 2*height );
560 h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][4], h->mb.pic.i_stride[1],
561 mvx1, mvy1, 2*width, 2*height );
562 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 );
563 h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][5], h->mb.pic.i_stride[2],
564 mvx0, mvy0, 2*width, 2*height );
565 h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][5], h->mb.pic.i_stride[2],
566 mvx1, mvy1, 2*width, 2*height );
567 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 );
570 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
572 const int i8 = x264_scan8[0] + x + 8*y;
574 if( h->mb.cache.ref[0][i8] >= 0 )
575 if( h->mb.cache.ref[1][i8] >= 0 )
576 x264_mb_mc_01xywh( h, x, y, 2, 2 );
578 x264_mb_mc_0xywh( h, x, y, 2, 2 );
580 x264_mb_mc_1xywh( h, x, y, 2, 2 );
583 void x264_mb_mc_8x8( x264_t *h, int i8 )
585 const int x = 2*(i8&1);
586 const int y = 2*(i8>>1);
587 switch( h->mb.i_sub_partition[i8] )
590 x264_mb_mc_0xywh( h, x, y, 2, 2 );
593 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
594 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
597 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
598 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
601 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
602 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
603 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
604 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
607 x264_mb_mc_1xywh( h, x, y, 2, 2 );
610 x264_mb_mc_01xywh( h, x, y, 2, 2 );
613 x264_mb_mc_direct8x8( h, x, y );
618 void x264_mb_mc( x264_t *h )
620 if( h->mb.i_type == P_L0 )
622 if( h->mb.i_partition == D_16x16 )
624 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
626 else if( h->mb.i_partition == D_16x8 )
628 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
629 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
631 else if( h->mb.i_partition == D_8x16 )
633 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
634 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
637 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
640 for( i = 0; i < 4; i++ )
641 x264_mb_mc_8x8( h, i );
643 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
645 x264_mb_mc_direct8x8( h, 0, 0 );
646 x264_mb_mc_direct8x8( h, 2, 0 );
647 x264_mb_mc_direct8x8( h, 0, 2 );
648 x264_mb_mc_direct8x8( h, 2, 2 );
652 const uint8_t *b_list0 = x264_mb_type_list_table[h->mb.i_type][0];
653 const uint8_t *b_list1 = x264_mb_type_list_table[h->mb.i_type][1];
655 if( h->mb.i_partition == D_16x16 )
657 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
658 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
659 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
661 else if( h->mb.i_partition == D_16x8 )
663 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
664 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
665 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
667 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
668 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
669 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
671 else if( h->mb.i_partition == D_8x16 )
673 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
674 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
675 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
677 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
678 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
679 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
684 int x264_macroblock_cache_init( x264_t *h )
687 int i_mb_count = h->mb.i_mb_count;
689 h->mb.i_mb_stride = h->sps->i_mb_width;
690 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
691 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
693 h->mb.b_interlaced = h->param.b_interlaced;
695 CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
696 CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
697 CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
698 CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
700 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
701 CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
704 CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
706 if( h->param.b_cabac )
708 CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
709 CHECKED_MALLOC( h->mb.mvd[0], 2*16 * i_mb_count * sizeof(int16_t) );
710 CHECKED_MALLOC( h->mb.mvd[1], 2*16 * i_mb_count * sizeof(int16_t) );
715 int i_refs = X264_MIN(16, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << h->param.b_interlaced;
716 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
717 i_refs = X264_MIN(16, i_refs + 2); //smart weights add two duplicate frames
718 else if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_BLIND )
719 i_refs = X264_MIN(16, i_refs + 1); //blind weights add one duplicate frame
721 for( j=0; j < i_refs; j++ )
722 CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
725 if( h->param.analyse.i_weighted_pred )
727 int i_padv = PADV << h->param.b_interlaced;
728 #define ALIGN(x,a) (((x)+((a)-1))&~((a)-1))
729 int align = h->param.cpu&X264_CPU_CACHELINE_64 ? 64 : h->param.cpu&X264_CPU_CACHELINE_32 ? 32 : 16;
730 int i_stride, luma_plane_size;
733 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_FAKE )
735 // only need buffer for lookahead
736 if( !h->param.i_sync_lookahead || h == h->thread[h->param.i_threads] )
738 // Fake analysis only works on lowres
739 i_stride = ALIGN( h->sps->i_mb_width*8 + 2*PADH, align );
740 luma_plane_size = i_stride * (h->sps->i_mb_height*8+2*i_padv);
741 // Only need 1 buffer for analysis
749 i_stride = ALIGN( h->sps->i_mb_width*16 + 2*PADH, align );
750 luma_plane_size = i_stride * (h->sps->i_mb_height*16+2*i_padv);
752 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
753 //SMART can weight one ref and one offset -1
756 //blind only has one weighted copy (offset -1)
760 for( i = 0; i < numweightbuf; i++ )
761 CHECKED_MALLOC( h->mb.p_weight_buf[i], luma_plane_size );
765 for( i=0; i<=h->param.b_interlaced; i++ )
768 /* shouldn't really be initialized, just silences a valgrind false-positive in predict_8x8_filter_mmx */
769 CHECKED_MALLOCZERO( h->mb.intra_border_backup[i][j], (h->sps->i_mb_width*16+32)>>!!j );
770 h->mb.intra_border_backup[i][j] += 8;
773 /* init with not available (for top right idx=7,15) */
774 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
775 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
780 void x264_macroblock_cache_end( x264_t *h )
783 for( i=0; i<=h->param.b_interlaced; i++ )
785 x264_free( h->mb.intra_border_backup[i][j] - 8 );
787 for( j=0; j<32; j++ )
788 x264_free( h->mb.mvr[i][j] );
789 for( i=0; i<16; i++ )
790 x264_free( h->mb.p_weight_buf[i] );
792 if( h->param.b_cabac )
794 x264_free( h->mb.chroma_pred_mode );
795 x264_free( h->mb.mvd[0] );
796 x264_free( h->mb.mvd[1] );
798 x264_free( h->mb.intra4x4_pred_mode );
799 x264_free( h->mb.non_zero_count );
800 x264_free( h->mb.mb_transform_size );
801 x264_free( h->mb.skipbp );
802 x264_free( h->mb.cbp );
803 x264_free( h->mb.qp );
805 void x264_macroblock_slice_init( x264_t *h )
809 h->mb.mv[0] = h->fdec->mv[0];
810 h->mb.mv[1] = h->fdec->mv[1];
811 h->mb.ref[0] = h->fdec->ref[0];
812 h->mb.ref[1] = h->fdec->ref[1];
813 h->mb.type = h->fdec->mb_type;
815 h->fdec->i_ref[0] = h->i_ref0;
816 h->fdec->i_ref[1] = h->i_ref1;
817 for( i = 0; i < h->i_ref0; i++ )
818 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
819 if( h->sh.i_type == SLICE_TYPE_B )
821 for( i = 0; i < h->i_ref1; i++ )
822 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
824 map_col_to_list0(-1) = -1;
825 map_col_to_list0(-2) = -2;
826 for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
828 int poc = h->fref1[0]->ref_poc[0][i];
829 map_col_to_list0(i) = -2;
830 for( j = 0; j < h->i_ref0; j++ )
831 if( h->fref0[j]->i_poc == poc )
833 map_col_to_list0(i) = j;
838 if( h->sh.i_type == SLICE_TYPE_P )
839 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
841 setup_inverse_delta_pocs( h );
843 h->mb.i_neighbour4[6] =
844 h->mb.i_neighbour4[9] =
845 h->mb.i_neighbour4[12] =
846 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
847 h->mb.i_neighbour4[3] =
848 h->mb.i_neighbour4[7] =
849 h->mb.i_neighbour4[11] =
850 h->mb.i_neighbour4[13] =
851 h->mb.i_neighbour4[15] =
852 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
855 void x264_macroblock_thread_init( x264_t *h )
867 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
868 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
869 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
870 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
871 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
872 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
875 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
877 int stride_y = fenc->i_stride[0];
878 int stride_uv = fenc->i_stride[1];
879 int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
880 int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
881 h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
882 fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
885 static NOINLINE void copy_column8( uint8_t *dst, uint8_t *src )
887 // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
889 for( i = -4; i < 4; i++ )
890 dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
893 static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int i_mb_x, int i_mb_y, int i)
895 const int w = (i == 0 ? 16 : 8);
896 const int i_stride = h->fdec->i_stride[!!i];
897 const int i_stride2 = i_stride << h->mb.b_interlaced;
898 const int i_pix_offset = h->mb.b_interlaced
899 ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride
900 : w * (i_mb_x + i_mb_y * i_stride);
901 const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
902 const uint8_t *intra_fdec = h->param.b_sliced_threads ? plane_fdec-i_stride2 :
903 &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
904 int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
905 x264_frame_t **fref[2] = { h->fref0, h->fref1 };
907 if( h->mb.b_interlaced )
908 ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
909 h->mb.pic.i_stride[i] = i_stride2;
910 h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
911 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
912 h->mb.pic.p_fenc_plane[i], i_stride2, w );
914 memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
916 memset( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], 0, w*3/2+1 );
917 if( h->mb.b_interlaced || h->mb.b_reencode_mb )
918 for( j = 0; j < w; j++ )
919 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
920 for( j = 0; j < h->mb.pic.i_fref[0]; j++ )
922 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]];
925 for( k = 1; k < 4; k++ )
926 h->mb.pic.p_fref[0][j][k] = &fref[0][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
927 if( h->sh.weight[j][0].weightfn )
928 h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> h->mb.b_interlaced][ref_pix_offset[j&1]];
930 h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
933 if( h->sh.i_type == SLICE_TYPE_B )
934 for( j = 0; j < h->mb.pic.i_fref[1]; j++ )
936 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]];
938 for( k = 1; k < 4; k++ )
939 h->mb.pic.p_fref[1][j][k] = &fref[1][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
943 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
945 int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
946 int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
947 int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
948 int i_top_y = i_mb_y - (1 << h->mb.b_interlaced);
949 int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x;
950 int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x;
951 int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x;
953 int i_top_type = -1; /* gcc warn */
959 h->mb.i_mb_x = i_mb_x;
960 h->mb.i_mb_y = i_mb_y;
961 h->mb.i_mb_xy = i_mb_xy;
962 h->mb.i_b8_xy = i_mb_8x8;
963 h->mb.i_b4_xy = i_mb_4x4;
964 h->mb.i_mb_top_xy = i_top_xy;
965 h->mb.i_neighbour = 0;
966 h->mb.i_neighbour_intra = 0;
969 if( i_top_xy >= h->sh.i_first_mb )
971 h->mb.i_mb_type_top =
972 i_top_type = h->mb.type[i_top_xy];
973 h->mb.cache.i_cbp_top = h->mb.cbp[i_top_xy];
975 h->mb.i_neighbour |= MB_TOP;
977 if( !h->param.b_constrained_intra || IS_INTRA( i_top_type ) )
978 h->mb.i_neighbour_intra |= MB_TOP;
981 CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &h->mb.intra4x4_pred_mode[i_top_xy][0] );
983 /* load non_zero_count */
984 CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &h->mb.non_zero_count[i_top_xy][12] );
985 /* shift because x264_scan8[16] is misaligned */
986 M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = M16( &h->mb.non_zero_count[i_top_xy][18] ) << 8;
987 M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = M16( &h->mb.non_zero_count[i_top_xy][22] ) << 8;
991 h->mb.i_mb_type_top = -1;
992 h->mb.cache.i_cbp_top = -1;
995 M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
997 /* load non_zero_count */
998 M32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8] ) = 0x80808080U;
999 M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = 0x80808080U;
1000 M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = 0x80808080U;
1003 if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1005 i_left_xy = i_mb_xy - 1;
1006 h->mb.i_mb_type_left =
1007 i_left_type = h->mb.type[i_left_xy];
1008 h->mb.cache.i_cbp_left = h->mb.cbp[h->mb.i_mb_xy - 1];
1010 h->mb.i_neighbour |= MB_LEFT;
1012 if( !h->param.b_constrained_intra || IS_INTRA( i_left_type ) )
1013 h->mb.i_neighbour_intra |= MB_LEFT;
1016 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1017 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1018 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1019 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1021 /* load non_zero_count */
1022 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][3];
1023 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1024 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][11];
1025 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1027 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1028 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1030 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1031 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1035 h->mb.i_mb_type_left = -1;
1036 h->mb.cache.i_cbp_left = -1;
1038 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1039 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1040 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1041 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1043 /* load non_zero_count */
1044 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1045 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1046 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1047 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1048 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1049 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1050 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1051 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1054 if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1056 h->mb.i_neighbour |= MB_TOPRIGHT;
1057 h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1058 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
1059 h->mb.i_neighbour_intra |= MB_TOPRIGHT;
1062 h->mb.i_mb_type_topright = -1;
1063 if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1065 h->mb.i_neighbour |= MB_TOPLEFT;
1066 h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1067 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
1068 h->mb.i_neighbour_intra |= MB_TOPLEFT;
1071 h->mb.i_mb_type_topleft = -1;
1073 if( h->pps->b_transform_8x8_mode )
1075 h->mb.cache.i_neighbour_transform_size =
1076 ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1077 + ( i_top_type >= 0 && h->mb.mb_transform_size[i_top_xy] );
1082 h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1083 h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1084 h->mb.cache.i_neighbour_interlaced =
1085 !!(h->mb.i_neighbour & MB_LEFT)
1086 + !!(h->mb.i_neighbour & MB_TOP);
1089 if( !h->mb.b_interlaced && !h->mb.b_reencode_mb )
1091 copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
1092 copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
1093 copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
1094 copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
1097 /* load picture pointers */
1098 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 0 );
1099 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 1 );
1100 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 2 );
1102 if( h->fdec->integral )
1104 assert( !h->mb.b_interlaced );
1105 for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
1106 h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1107 for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
1108 h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1111 x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
1113 /* load ref/mv/mvd */
1114 if( h->sh.i_type != SLICE_TYPE_I )
1116 const int s8x8 = h->mb.i_b8_stride;
1117 const int s4x4 = h->mb.i_b4_stride;
1121 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1124 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1125 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1126 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1129 if( h->mb.i_neighbour & MB_TOPLEFT )
1131 const int i8 = x264_scan8[0] - 1 - 1*8;
1132 const int ir = i_top_8x8 - 1;
1133 const int iv = i_top_4x4 - 1;
1134 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1135 CP32( h->mb.cache.mv[i_list][i8], h->mb.mv[i_list][iv] );
1139 const int i8 = x264_scan8[0] - 1 - 1*8;
1140 h->mb.cache.ref[i_list][i8] = -2;
1141 M32( h->mb.cache.mv[i_list][i8] ) = 0;
1144 if( h->mb.i_neighbour & MB_TOP )
1146 const int i8 = x264_scan8[0] - 8;
1147 const int ir = i_top_8x8;
1148 const int iv = i_top_4x4;
1149 h->mb.cache.ref[i_list][i8+0] =
1150 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1151 h->mb.cache.ref[i_list][i8+2] =
1152 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1153 CP64( h->mb.cache.mv[i_list][i8+0], h->mb.mv[i_list][iv+0] );
1154 CP64( h->mb.cache.mv[i_list][i8+2], h->mb.mv[i_list][iv+2] );
1158 const int i8 = x264_scan8[0] - 8;
1159 M64( h->mb.cache.mv[i_list][i8+0] ) = 0;
1160 M64( h->mb.cache.mv[i_list][i8+2] ) = 0;
1161 M32( &h->mb.cache.ref[i_list][i8] ) = (uint8_t)(-2) * 0x01010101U;
1164 if( h->mb.i_neighbour & MB_TOPRIGHT )
1166 const int i8 = x264_scan8[0] + 4 - 1*8;
1167 const int ir = i_top_8x8 + 2;
1168 const int iv = i_top_4x4 + 4;
1169 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1170 CP32( h->mb.cache.mv[i_list][i8], h->mb.mv[i_list][iv] );
1174 const int i8 = x264_scan8[0] + 4 - 1*8;
1175 h->mb.cache.ref[i_list][i8] = -2;
1176 M32( h->mb.cache.mv[i_list][i8] ) = 0;
1179 if( h->mb.i_neighbour & MB_LEFT )
1181 const int i8 = x264_scan8[0] - 1;
1182 const int ir = i_mb_8x8 - 1;
1183 const int iv = i_mb_4x4 - 1;
1184 h->mb.cache.ref[i_list][i8+0*8] =
1185 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1186 h->mb.cache.ref[i_list][i8+2*8] =
1187 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1189 CP32( h->mb.cache.mv[i_list][i8+0*8], h->mb.mv[i_list][iv + 0*s4x4] );
1190 CP32( h->mb.cache.mv[i_list][i8+1*8], h->mb.mv[i_list][iv + 1*s4x4] );
1191 CP32( h->mb.cache.mv[i_list][i8+2*8], h->mb.mv[i_list][iv + 2*s4x4] );
1192 CP32( h->mb.cache.mv[i_list][i8+3*8], h->mb.mv[i_list][iv + 3*s4x4] );
1196 const int i8 = x264_scan8[0] - 1;
1197 for( i = 0; i < 4; i++ )
1199 h->mb.cache.ref[i_list][i8+i*8] = -2;
1200 M32( h->mb.cache.mv[i_list][i8+i*8] ) = 0;
1204 if( h->param.b_cabac )
1206 if( i_top_type >= 0 )
1208 const int i8 = x264_scan8[0] - 8;
1209 const int iv = i_top_4x4;
1210 CP64( h->mb.cache.mvd[i_list][i8+0], h->mb.mvd[i_list][iv+0] );
1211 CP64( h->mb.cache.mvd[i_list][i8+2], h->mb.mvd[i_list][iv+2] );
1215 const int i8 = x264_scan8[0] - 8;
1216 M64( h->mb.cache.mvd[i_list][i8+0] ) = 0;
1217 M64( h->mb.cache.mvd[i_list][i8+2] ) = 0;
1220 if( i_left_type >= 0 )
1222 const int i8 = x264_scan8[0] - 1;
1223 const int iv = i_mb_4x4 - 1;
1224 CP32( h->mb.cache.mvd[i_list][i8+0*8], h->mb.mvd[i_list][iv + 0*s4x4] );
1225 CP32( h->mb.cache.mvd[i_list][i8+1*8], h->mb.mvd[i_list][iv + 1*s4x4] );
1226 CP32( h->mb.cache.mvd[i_list][i8+2*8], h->mb.mvd[i_list][iv + 2*s4x4] );
1227 CP32( h->mb.cache.mvd[i_list][i8+3*8], h->mb.mvd[i_list][iv + 3*s4x4] );
1231 const int i8 = x264_scan8[0] - 1;
1232 for( i = 0; i < 4; i++ )
1233 M32( h->mb.cache.mvd[i_list][i8+i*8] ) = 0;
1239 if( h->sh.i_type == SLICE_TYPE_B )
1241 h->mb.bipred_weight = h->mb.bipred_weight_buf[h->mb.b_interlaced&(i_mb_y&1)];
1242 h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[h->mb.b_interlaced&(i_mb_y&1)];
1243 if( h->param.b_cabac )
1246 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1247 skipbp = i_left_type >= 0 ? h->mb.skipbp[i_left_xy] : 0;
1248 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
1249 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
1250 skipbp = i_top_type >= 0 ? h->mb.skipbp[i_top_xy] : 0;
1251 h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
1252 h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
1256 if( h->sh.i_type == SLICE_TYPE_P )
1257 x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1260 h->mb.i_neighbour4[0] =
1261 h->mb.i_neighbour8[0] = (h->mb.i_neighbour_intra & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1262 | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOPRIGHT : 0);
1263 h->mb.i_neighbour4[4] =
1264 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour_intra & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1265 h->mb.i_neighbour4[2] =
1266 h->mb.i_neighbour4[8] =
1267 h->mb.i_neighbour4[10] =
1268 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour_intra & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1269 h->mb.i_neighbour4[5] =
1270 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour_intra & MB_TOPRIGHT)
1271 | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1274 static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int i )
1277 int i_stride = h->fdec->i_stride[!!i];
1278 int i_stride2 = i_stride << h->mb.b_interlaced;
1279 int i_pix_offset = h->mb.b_interlaced
1280 ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1281 : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1282 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1283 &h->fdec->plane[i][i_pix_offset], i_stride2,
1284 h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1287 void x264_macroblock_cache_save( x264_t *h )
1289 const int i_mb_xy = h->mb.i_mb_xy;
1290 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1291 const int s8x8 = h->mb.i_b8_stride;
1292 const int s4x4 = h->mb.i_b4_stride;
1293 const int i_mb_4x4 = h->mb.i_b4_xy;
1294 const int i_mb_8x8 = h->mb.i_b8_xy;
1296 /* GCC pessimizes direct stores to heap-allocated 8-bit arrays due to aliasing.*/
1297 /* By only dereferencing them once, we avoid this issue. */
1298 int8_t *intra4x4_pred_mode = h->mb.intra4x4_pred_mode[i_mb_xy];
1299 uint8_t *non_zero_count = h->mb.non_zero_count[i_mb_xy];
1303 x264_macroblock_store_pic( h, 0 );
1304 x264_macroblock_store_pic( h, 1 );
1305 x264_macroblock_store_pic( h, 2 );
1307 x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1309 h->mb.type[i_mb_xy] = i_mb_type;
1310 h->mb.i_mb_prev_xy = i_mb_xy;
1313 if( i_mb_type == I_4x4 )
1315 CP32( &intra4x4_pred_mode[0], &h->mb.cache.intra4x4_pred_mode[x264_scan8[10]] );
1316 M32( &intra4x4_pred_mode[4] ) = pack8to32(h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
1317 h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
1318 h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
1320 else if( !h->param.b_constrained_intra || IS_INTRA(i_mb_type) )
1321 M64( intra4x4_pred_mode ) = I_PRED_4x4_DC * 0x0101010101010101ULL;
1323 M64( intra4x4_pred_mode ) = (uint8_t)(-1) * 0x0101010101010101ULL;
1326 if( i_mb_type == I_PCM )
1328 h->mb.qp[i_mb_xy] = 0;
1329 h->mb.i_last_dqp = 0;
1330 h->mb.i_cbp_chroma = 2;
1331 h->mb.i_cbp_luma = 0xf;
1332 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1333 h->mb.b_transform_8x8 = 0;
1334 memset( non_zero_count, 16, 24 );
1338 /* save non zero count */
1339 CP32( &non_zero_count[0*4], &h->mb.cache.non_zero_count[x264_scan8[0]+0*8] );
1340 CP32( &non_zero_count[1*4], &h->mb.cache.non_zero_count[x264_scan8[0]+1*8] );
1341 CP32( &non_zero_count[2*4], &h->mb.cache.non_zero_count[x264_scan8[0]+2*8] );
1342 CP32( &non_zero_count[3*4], &h->mb.cache.non_zero_count[x264_scan8[0]+3*8] );
1343 M16( &non_zero_count[16+0*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+0*2]-1] ) >> 8;
1344 M16( &non_zero_count[16+1*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+1*2]-1] ) >> 8;
1345 M16( &non_zero_count[16+2*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+2*2]-1] ) >> 8;
1346 M16( &non_zero_count[16+3*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+3*2]-1] ) >> 8;
1348 if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1349 h->mb.i_qp = h->mb.i_last_qp;
1350 h->mb.qp[i_mb_xy] = h->mb.i_qp;
1351 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1352 h->mb.i_last_qp = h->mb.i_qp;
1355 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1356 h->mb.b_transform_8x8 = 0;
1357 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1359 if( h->sh.i_type != SLICE_TYPE_I )
1361 if( !IS_INTRA( i_mb_type ) )
1363 h->mb.ref[0][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
1364 h->mb.ref[0][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
1365 h->mb.ref[0][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
1366 h->mb.ref[0][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
1367 for( y = 0; y < 4; y++ )
1369 CP64( h->mb.mv[0][i_mb_4x4+y*s4x4+0], h->mb.cache.mv[0][x264_scan8[0]+8*y+0] );
1370 CP64( h->mb.mv[0][i_mb_4x4+y*s4x4+2], h->mb.cache.mv[0][x264_scan8[0]+8*y+2] );
1372 if( h->sh.i_type == SLICE_TYPE_B )
1374 h->mb.ref[1][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
1375 h->mb.ref[1][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
1376 h->mb.ref[1][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
1377 h->mb.ref[1][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
1378 for( y = 0; y < 4; y++ )
1380 CP64( h->mb.mv[1][i_mb_4x4+y*s4x4+0], h->mb.cache.mv[1][x264_scan8[0]+8*y+0] );
1381 CP64( h->mb.mv[1][i_mb_4x4+y*s4x4+2], h->mb.cache.mv[1][x264_scan8[0]+8*y+2] );
1388 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1390 M16( &h->mb.ref[i_list][i_mb_8x8+0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1391 M16( &h->mb.ref[i_list][i_mb_8x8+1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1392 for( y = 0; y < 4; y++ )
1394 M64( h->mb.mv[i_list][i_mb_4x4+y*s4x4+0] ) = 0;
1395 M64( h->mb.mv[i_list][i_mb_4x4+y*s4x4+2] ) = 0;
1401 if( h->param.b_cabac )
1403 if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
1404 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1406 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1408 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1410 for( y = 0; y < 4; y++ )
1412 CP64( h->mb.mvd[0][i_mb_4x4+y*s4x4+0], h->mb.cache.mvd[0][x264_scan8[0]+8*y+0] );
1413 CP64( h->mb.mvd[0][i_mb_4x4+y*s4x4+2], h->mb.cache.mvd[0][x264_scan8[0]+8*y+2] );
1415 if( h->sh.i_type == SLICE_TYPE_B )
1416 for( y = 0; y < 4; y++ )
1418 CP64( h->mb.mvd[1][i_mb_4x4+y*s4x4+0], h->mb.cache.mvd[1][x264_scan8[0]+8*y+0] );
1419 CP64( h->mb.mvd[1][i_mb_4x4+y*s4x4+2], h->mb.cache.mvd[1][x264_scan8[0]+8*y+2] );
1424 for( y = 0; y < 4; y++ )
1426 M64( h->mb.mvd[0][i_mb_4x4+y*s4x4+0] ) = 0;
1427 M64( h->mb.mvd[0][i_mb_4x4+y*s4x4+2] ) = 0;
1429 if( h->sh.i_type == SLICE_TYPE_B )
1430 for( y = 0; y < 4; y++ )
1432 M64( h->mb.mvd[1][i_mb_4x4+y*s4x4+0] ) = 0;
1433 M64( h->mb.mvd[1][i_mb_4x4+y*s4x4+2] ) = 0;
1437 if( h->sh.i_type == SLICE_TYPE_B )
1439 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1440 h->mb.skipbp[i_mb_xy] = 0xf;
1441 else if( i_mb_type == B_8x8 )
1443 int skipbp = ( h->mb.i_sub_partition[0] == D_DIRECT_8x8 ) << 0;
1444 skipbp |= ( h->mb.i_sub_partition[1] == D_DIRECT_8x8 ) << 1;
1445 skipbp |= ( h->mb.i_sub_partition[2] == D_DIRECT_8x8 ) << 2;
1446 skipbp |= ( h->mb.i_sub_partition[3] == D_DIRECT_8x8 ) << 3;
1447 h->mb.skipbp[i_mb_xy] = skipbp;
1450 h->mb.skipbp[i_mb_xy] = 0;
1456 void x264_macroblock_bipred_init( x264_t *h )
1458 int i_ref0, i_ref1, field;
1459 for( field = 0; field <= h->sh.b_mbaff; field++ )
1460 for( i_ref0 = 0; i_ref0 < (h->i_ref0<<h->sh.b_mbaff); i_ref0++ )
1462 int poc0 = h->fref0[i_ref0>>h->sh.b_mbaff]->i_poc;
1463 if( h->sh.b_mbaff && field^(i_ref0&1) )
1464 poc0 += h->sh.i_delta_poc_bottom;
1465 for( i_ref1 = 0; i_ref1 < (h->i_ref1<<h->sh.b_mbaff); i_ref1++ )
1467 int dist_scale_factor;
1468 int poc1 = h->fref1[i_ref1>>h->sh.b_mbaff]->i_poc;
1469 if( h->sh.b_mbaff && field^(i_ref1&1) )
1470 poc1 += h->sh.i_delta_poc_bottom;
1471 int cur_poc = h->fdec->i_poc + field*h->sh.i_delta_poc_bottom;
1472 int td = x264_clip3( poc1 - poc0, -128, 127 );
1473 if( td == 0 /* || pic0 is a long-term ref */ )
1474 dist_scale_factor = 256;
1477 int tb = x264_clip3( cur_poc - poc0, -128, 127 );
1478 int tx = (16384 + (abs(td) >> 1)) / td;
1479 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1482 h->mb.dist_scale_factor_buf[field][i_ref0][i_ref1] = dist_scale_factor;
1484 dist_scale_factor >>= 2;
1485 if( h->param.analyse.b_weighted_bipred
1486 && dist_scale_factor >= -64
1487 && dist_scale_factor <= 128 )
1489 h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 64 - dist_scale_factor;
1490 // ssse3 implementation of biweight doesn't support the extrema.
1491 // if we ever generate them, we'll have to drop that optimization.
1492 assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
1495 h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 32;