- const int i8 = x264_scan8[idx];
- const int i_ref= h->mb.cache.ref[i_list][i8];
- int i_refa = h->mb.cache.ref[i_list][i8 - 1];
- int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
- int i_refb = h->mb.cache.ref[i_list][i8 - 8];
- int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
- int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
- int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
-
- int i_count = 0;
-
- if( (idx&3) >= 2 + (i_width&1) || i_refc == -2 )
- {
- i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
- mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
- }
-
- if( h->mb.i_partition == D_16x8 )
- {
- if( idx == 0 )
- {
- if( i_refb == i_ref )
- {
- *(uint32_t*)mvp = *(uint32_t*)mv_b;
- return;
- }
- }
- else
- {
- if( i_refa == i_ref )
- {
- *(uint32_t*)mvp = *(uint32_t*)mv_a;
- return;
- }
- }
- }
- else if( h->mb.i_partition == D_8x16 )
- {
- if( idx == 0 )
- {
- if( i_refa == i_ref )
- {
- *(uint32_t*)mvp = *(uint32_t*)mv_a;
- return;
- }
- }
- else
- {
- if( i_refc == i_ref )
- {
- *(uint32_t*)mvp = *(uint32_t*)mv_c;
- return;
- }
- }
- }
-
- if( i_refa == i_ref ) i_count++;
- if( i_refb == i_ref ) i_count++;
- if( i_refc == i_ref ) i_count++;
-
- if( i_count > 1 )
- {
-median:
- x264_median_mv( mvp, mv_a, mv_b, mv_c );
- }
- else if( i_count == 1 )
- {
- if( i_refa == i_ref )
- *(uint32_t*)mvp = *(uint32_t*)mv_a;
- else if( i_refb == i_ref )
- *(uint32_t*)mvp = *(uint32_t*)mv_b;
- else
- *(uint32_t*)mvp = *(uint32_t*)mv_c;
- }
- else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
- *(uint32_t*)mvp = *(uint32_t*)mv_a;
- else
- goto median;
-}
-
-void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int16_t mvp[2] )
-{
- int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
- int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
- int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
- int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
- int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
- int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
-
- int i_count = 0;
-
- if( i_refc == -2 )
- {
- i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
- mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
- }
-
- if( i_refa == i_ref ) i_count++;
- if( i_refb == i_ref ) i_count++;
- if( i_refc == i_ref ) i_count++;
-
- if( i_count > 1 )
- {
-median:
- x264_median_mv( mvp, mv_a, mv_b, mv_c );
- }
- else if( i_count == 1 )
- {
- if( i_refa == i_ref )
- *(uint32_t*)mvp = *(uint32_t*)mv_a;
- else if( i_refb == i_ref )
- *(uint32_t*)mvp = *(uint32_t*)mv_b;
- else
- *(uint32_t*)mvp = *(uint32_t*)mv_c;
- }
- else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
- *(uint32_t*)mvp = *(uint32_t*)mv_a;
- else
- goto median;
-}
-
-
-void x264_mb_predict_mv_pskip( x264_t *h, int16_t mv[2] )
-{
- int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
- int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
- int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
- int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
-
- if( i_refa == -2 || i_refb == -2 ||
- !( i_refa | *(uint32_t*)mv_a ) ||
- !( i_refb | *(uint32_t*)mv_b ) )
- {
- *(uint32_t*)mv = 0;
- }
- else
- {
- x264_mb_predict_mv_16x16( h, 0, 0, mv );
- }
-}
-
-static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
-{
- int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
- int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
- int i8;
- const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
-
- x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
-
- if( IS_INTRA( type_col ) )
- {
- x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
- x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0 );
- x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0 );
- return 1;
- }
-
- for( i8 = 0; i8 < 4; i8++ )
- {
- const int x8 = i8%2;
- const int y8 = i8/2;
- const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
- const int i_ref = h->mb.map_col_to_list0[ h->fref1[0]->ref[0][ i_part_8x8 ] ];
-
- if( i_ref >= 0 )
- {
- const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
- const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
- const int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
- const int l0y = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
- if( h->param.i_threads > 1 && (l0y > h->mb.mv_max_spel[1] || l0y-mv_col[1] > h->mb.mv_max_spel[1]) )
- return 0;
- x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
- x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, pack16to32_mask(l0x, l0y) );
- x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_col[1]) );
- }
- else
- {
- /* the collocated ref isn't in the current list0 */
- /* FIXME: we might still be able to use direct_8x8 on some partitions */
- /* FIXME: with B-pyramid + extensive ref list reordering
- * (not currently used), we would also have to check
- * l1mv1 like in spatial mode */
- return 0;
- }
- }
-
- return 1;
-}
-
-static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
-{
- int ref[2];
- ALIGNED_8( int16_t mv[2][2] );
- int i_list;
- int i8;
- const int8_t *l1ref0 = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
- const int8_t *l1ref1 = &h->fref1[0]->ref[1][ h->mb.i_b8_xy ];
- const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
- const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[1][ h->mb.i_b4_xy ];
- const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
-
- for( i_list=0; i_list<2; i_list++ )
- {
- int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
- int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
- int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
- if( i_refc == -2 )
- i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
-
- ref[i_list] = i_refa;
- if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
- ref[i_list] = i_refb;
- if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
- ref[i_list] = i_refc;
- if( ref[i_list] < 0 )
- ref[i_list] = -1;
- }
-
- if( ref[0] < 0 && ref[1] < 0 )
- {
- x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
- x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
- x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0 );
- x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0 );
- return 1;
- }
-
- if( ref[0] >= 0 )
- x264_mb_predict_mv_16x16( h, 0, ref[0], mv[0] );
- else
- *(uint32_t*)mv[0] = 0;
- if( ref[1] >= 0 )
- x264_mb_predict_mv_16x16( h, 1, ref[1], mv[1] );
- else
- *(uint32_t*)mv[1] = 0;
-
- x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
- x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
- x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, mv[0] );
- x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, mv[1] );
-
- if( h->param.i_threads > 1
- && ( mv[0][1] > h->mb.mv_max_spel[1]
- || mv[1][1] > h->mb.mv_max_spel[1] ) )
- {
-#if 0
- fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
- mv[0][0], mv[0][1], mv[1][0], mv[1][1],
- h->mb.mv_max_spel[1]);
-#endif
- return 0;
- }
-
- if( IS_INTRA( type_col ) || (ref[0]&&ref[1]) )
- return 1;
-
- /* col_zero_flag */
- for( i8=0; i8<4; i8++ )
- {
- const int x8 = i8%2;
- const int y8 = i8/2;
- const int o8 = x8 + y8 * h->mb.i_b8_stride;
- const int o4 = 3*(x8 + y8 * h->mb.i_b4_stride);
- if( l1ref0[o8] == 0 )
- {
- if( abs( l1mv0[o4][0] ) <= 1 && abs( l1mv0[o4][1] ) <= 1 )
- {
- if( ref[0] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0 );
- if( ref[1] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0 );
- }
- }
- else if( l1ref0[o8] < 0 && l1ref1[o8] == 0 )
- {
- if( abs( l1mv1[o4][0] ) <= 1 && abs( l1mv1[o4][1] ) <= 1 )
- {
- if( ref[0] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0 );
- if( ref[1] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0 );
- }
- }
- }
-
- return 1;
-}
-
-int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
-{
- int b_available;
- if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
- return 0;
- else if( h->sh.b_direct_spatial_mv_pred )
- b_available = x264_mb_predict_mv_direct16x16_spatial( h );
- else
- b_available = x264_mb_predict_mv_direct16x16_temporal( h );
-
- if( b_changed != NULL && b_available )
- {
- int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
- if( IS_INTRA(type_col) || type_col == P_SKIP )
- {
- *b_changed = h->mb.cache.direct_ref[0][0] != h->mb.cache.ref[0][X264_SCAN8_0]
- || h->mb.cache.direct_ref[1][0] != h->mb.cache.ref[1][X264_SCAN8_0]
- || *(uint32_t*)h->mb.cache.direct_mv[0][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[0][X264_SCAN8_0]
- || *(uint32_t*)h->mb.cache.direct_mv[1][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[1][X264_SCAN8_0];
- }
- else
- {
- int i, l;
- *b_changed = 0;
- for( l = 0; l < 2; l++ )
- for( i = 0; i < 4; i++ )
- *b_changed |= h->mb.cache.direct_ref[l][i] != h->mb.cache.ref[l][x264_scan8[i*4]];
- *b_changed = *b_changed || memcmp(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
- }
- if( !*b_changed )
- return b_available;
- }
-
- /* cache ref & mv */
- if( b_available )
- {
- int i, l;
- for( l = 0; l < 2; l++ )
- for( i = 0; i < 4; i++ )
- h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
- h->mc.memcpy_aligned(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
- }
-
- return b_available;
-}
-
-void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
-{
- const int x = 2*(idx%2);
- const int y = 2*(idx/2);
- x264_macroblock_cache_ref( h, x, y, 2, 2, 0, h->mb.cache.direct_ref[0][idx] );
- x264_macroblock_cache_ref( h, x, y, 2, 2, 1, h->mb.cache.direct_ref[1][idx] );
- *(uint64_t*)h->mb.cache.mv[0][x264_scan8[idx*4]] =
- *(uint64_t*)h->mb.cache.direct_mv[0][x264_scan8[idx*4]];
- *(uint64_t*)h->mb.cache.mv[0][x264_scan8[idx*4]+8] =
- *(uint64_t*)h->mb.cache.direct_mv[0][x264_scan8[idx*4]+8];
- *(uint64_t*)h->mb.cache.mv[1][x264_scan8[idx*4]] =
- *(uint64_t*)h->mb.cache.direct_mv[1][x264_scan8[idx*4]];
- *(uint64_t*)h->mb.cache.mv[1][x264_scan8[idx*4]+8] =
- *(uint64_t*)h->mb.cache.direct_mv[1][x264_scan8[idx*4]+8];
-}
-
-/* This just improves encoder performance, it's not part of the spec */
-void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int16_t mvc[9][2], int *i_mvc )
-{
- int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
- int i = 0;
-
-#define SET_MVP(mvp) { \
- *(uint32_t*)mvc[i] = *(uint32_t*)mvp; \
- i++; \
- }
-
- /* b_direct */
- if( h->sh.i_type == SLICE_TYPE_B
- && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
- {
- SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
- }
-
- if( i_ref == 0 && h->frames.b_have_lowres )
- {
- int16_t (*lowres_mv)[2] = i_list ? h->fenc->lowres_mvs[1][h->fref1[0]->i_frame-h->fenc->i_frame-1]
- : h->fenc->lowres_mvs[0][h->fenc->i_frame-h->fref0[0]->i_frame-1];
- if( lowres_mv[0][0] != 0x7fff ) *(uint32_t*)mvc[i++] = (*(uint32_t*)lowres_mv[h->mb.i_mb_xy]*2)&0xfffeffff;
- }
-
- /* spatial predictors */
- if( h->mb.i_neighbour & MB_LEFT )
- {
- int i_mb_l = h->mb.i_mb_xy - 1;
- /* skip MBs didn't go through the whole search process, so mvr is undefined */
- if( !IS_SKIP( h->mb.type[i_mb_l] ) )
- SET_MVP( mvr[i_mb_l] );
- }
- if( h->mb.i_neighbour & MB_TOP )
- {
- int i_mb_t = h->mb.i_mb_top_xy;
- if( !IS_SKIP( h->mb.type[i_mb_t] ) )
- SET_MVP( mvr[i_mb_t] );
-
- if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
- SET_MVP( mvr[i_mb_t-1] );
- if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
- SET_MVP( mvr[i_mb_t+1] );
- }
-#undef SET_MVP
-
- /* temporal predictors */
- /* FIXME temporal scaling w/ interlace */
- if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
- {
- x264_frame_t *l0 = h->fref0[0];
-
-#define SET_TMVP(dx, dy) { \
- int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
- int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
- int ref_col = l0->ref[0][i_b8]; \
- if( ref_col >= 0 ) \
- { \
- int scale = (h->fdec->i_poc - h->fdec->ref_poc[0][i_ref]) * l0->inv_ref_poc[ref_col];\
- mvc[i][0] = (l0->mv[0][i_b4][0]*scale + 128) >> 8;\
- mvc[i][1] = (l0->mv[0][i_b4][1]*scale + 128) >> 8;\
- i++; \
- } \
- }
-
- SET_TMVP(0,0);
- if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
- SET_TMVP(1,0);
- if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
- SET_TMVP(0,1);
-#undef SET_TMVP
- }
-
- *i_mvc = i;
-}
-
-/* Set up a lookup table for delta pocs to reduce an IDIV to an IMUL */
-static void setup_inverse_delta_pocs( x264_t *h )
-{
- int i;
- for( i = 0; i < h->i_ref0; i++ )
- {
- int delta = h->fdec->i_poc - h->fref0[i]->i_poc;
- h->fdec->inv_ref_poc[i] = (256 + delta/2) / delta;
- }
-}
-
-static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
-{
- const int i8 = x264_scan8[0]+x+8*y;
- const int i_ref = h->mb.cache.ref[0][i8];
- const int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
- int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );