1 /*****************************************************************************
2 * macroblock.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003 Laurent Aimar
5 * $Id: macroblock.c,v 1.1 2004/06/03 19:27:06 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
26 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
28 const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
29 const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
30 const int m = X264_MIN( x264_mb_pred_mode4x4_fix(ma),
31 x264_mb_pred_mode4x4_fix(mb) );
39 int x264_mb_predict_non_zero_code( x264_t *h, int idx )
41 const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1];
42 const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8];
48 i_ret = ( i_ret + 1 ) >> 1;
53 int x264_mb_transform_8x8_allowed( x264_t *h )
55 if( IS_SKIP( h->mb.i_type ) )
57 if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
60 for( i = 0; i < 4; i++ )
61 if( !IS_SUB8x8(h->mb.i_sub_partition[i])
62 || ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 && !h->sps->b_direct8x8_inference ) )
67 if( h->mb.i_type == B_DIRECT && !h->sps->b_direct8x8_inference )
73 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
75 const int i8 = x264_scan8[idx];
76 const int i_ref= h->mb.cache.ref[i_list][i8];
77 int i_refa = h->mb.cache.ref[i_list][i8 - 1];
78 int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
79 int i_refb = h->mb.cache.ref[i_list][i8 - 8];
80 int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
81 int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
82 int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
86 if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
88 i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
89 mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
92 if( h->mb.i_partition == D_16x8 )
94 if( idx == 0 && i_refb == i_ref )
100 else if( idx != 0 && i_refa == i_ref )
107 else if( h->mb.i_partition == D_8x16 )
109 if( idx == 0 && i_refa == i_ref )
115 else if( idx != 0 && i_refc == i_ref )
124 if( i_refa == i_ref ) i_count++;
125 if( i_refb == i_ref ) i_count++;
126 if( i_refc == i_ref ) i_count++;
130 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
131 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
133 else if( i_count == 1 )
135 if( i_refa == i_ref )
140 else if( i_refb == i_ref )
151 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
158 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
159 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
163 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int mvp[2] )
165 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
166 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
167 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
168 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
169 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
170 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
176 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
177 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
181 if( i_refa == i_ref ) i_count++;
182 if( i_refb == i_ref ) i_count++;
183 if( i_refc == i_ref ) i_count++;
187 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
188 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
190 else if( i_count == 1 )
192 if( i_refa == i_ref )
197 else if( i_refb == i_ref )
208 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
215 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
216 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
221 void x264_mb_predict_mv_pskip( x264_t *h, int mv[2] )
223 int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
224 int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
225 int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
226 int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
228 if( i_refa == -2 || i_refb == -2 ||
229 ( i_refa == 0 && mv_a[0] == 0 && mv_a[1] == 0 ) ||
230 ( i_refb == 0 && mv_b[0] == 0 && mv_b[1] == 0 ) )
236 x264_mb_predict_mv_16x16( h, 0, 0, mv );
240 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
242 int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
243 int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
246 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
248 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
250 if( IS_INTRA( type_col ) )
252 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
253 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0, 0 );
254 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0, 0 );
257 b8x8 = h->sps->b_direct8x8_inference ||
258 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
260 for( i8 = 0; i8 < 4; i8++ )
264 const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
265 const int i_ref = h->mb.map_col_to_list0[ h->fref1[0]->ref[0][ i_part_8x8 ] ];
269 const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
271 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
275 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
277 mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
278 mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
279 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, mv_l0[0], mv_l0[1] );
280 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
284 for( i4 = 0; i4 < 4; i4++ )
286 const int x4 = i4%2 + 2*x8;
287 const int y4 = i4/2 + 2*y8;
288 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * h->mb.i_b4_stride ];
290 mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
291 mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
292 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, mv_l0[0], mv_l0[1] );
293 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
299 /* the collocated ref isn't in the current list0 */
300 /* FIXME: we might still be able to use direct_8x8 on some partitions */
301 /* FIXME: with B-pyramid + extensive ref list reordering
302 * (not currently used), we would also have to check
303 * l1mv1 like in spatial mode */
308 if( h->param.i_threads > 1 )
310 int di = b8x8 ? 4 : 1;
311 for( i4=0; i4<16; i4+=di )
313 if( h->mb.cache.mv[0][x264_scan8[i4]][1] > h->mb.mv_max_spel[1]
314 || h->mb.cache.mv[1][x264_scan8[i4]][1] > h->mb.mv_max_spel[1] )
317 fprintf(stderr, "direct_temporal: (%d,%d) (%d,%d) > %d \n",
318 h->mb.cache.mv[0][x264_scan8[i4]][0],
319 h->mb.cache.mv[0][x264_scan8[i4]][1],
320 h->mb.cache.mv[1][x264_scan8[i4]][0],
321 h->mb.cache.mv[1][x264_scan8[i4]][1],
322 h->mb.mv_max_spel[1]);
332 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
339 const int8_t *l1ref0 = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
340 const int8_t *l1ref1 = &h->fref1[0]->ref[1][ h->mb.i_b8_xy ];
341 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
342 const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[1][ h->mb.i_b4_xy ];
343 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
345 for( i_list=0; i_list<2; i_list++ )
347 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
348 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
349 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
351 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
353 ref[i_list] = i_refa;
354 if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
355 ref[i_list] = i_refb;
356 if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
357 ref[i_list] = i_refc;
358 if( ref[i_list] < 0 )
362 if( ref[0] < 0 && ref[1] < 0 )
373 for( i_list=0; i_list<2; i_list++ )
375 if( ref[i_list] >= 0 )
376 x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
378 mv[i_list][0] = mv[i_list][1] = 0;
382 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
383 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
384 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, mv[0][0], mv[0][1] );
385 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, mv[1][0], mv[1][1] );
387 if( IS_INTRA( type_col ) )
390 if( h->param.i_threads > 1
391 && ( mv[0][1] > h->mb.mv_max_spel[1]
392 || mv[1][1] > h->mb.mv_max_spel[1] ) )
395 fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
396 mv[0][0], mv[0][1], mv[1][0], mv[1][1],
397 h->mb.mv_max_spel[1]);
402 b8x8 = h->sps->b_direct8x8_inference ||
403 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
406 for( i8=0; i8<4; i8++ )
410 const int o8 = x8 + y8 * h->mb.i_b8_stride;
411 if( l1ref0[o8] == 0 || ( l1ref0[o8] < 0 && l1ref1[o8] == 0 ) )
413 const int16_t (*l1mv)[2] = (l1ref0[o8] == 0) ? l1mv0 : l1mv1;
416 const int16_t *mvcol = l1mv[3*x8 + 3*y8 * h->mb.i_b4_stride];
417 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
420 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0, 0 );
422 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0, 0 );
427 for( i4=0; i4<4; i4++ )
429 const int x4 = i4%2 + 2*x8;
430 const int y4 = i4/2 + 2*y8;
431 const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
432 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
435 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0, 0 );
437 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0, 0 );
447 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
450 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
452 else if( h->sh.b_direct_spatial_mv_pred )
453 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
455 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
457 if( b_changed != NULL && b_available )
459 int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
460 if( IS_INTRA(type_col) || type_col == P_SKIP )
462 *b_changed = h->mb.cache.direct_ref[0][0] != h->mb.cache.ref[0][X264_SCAN8_0]
463 || h->mb.cache.direct_ref[1][0] != h->mb.cache.ref[1][X264_SCAN8_0]
464 || *(uint32_t*)h->mb.cache.direct_mv[0][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[0][X264_SCAN8_0]
465 || *(uint32_t*)h->mb.cache.direct_mv[1][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[1][X264_SCAN8_0];
471 for( l = 0; l < 2; l++ )
472 for( i = 0; i < 4; i++ )
473 *b_changed |= h->mb.cache.direct_ref[l][i] != h->mb.cache.ref[l][x264_scan8[i*4]];
474 *b_changed = *b_changed || memcmp(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
484 for( l = 0; l < 2; l++ )
485 for( i = 0; i < 4; i++ )
486 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
487 memcpy(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
493 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
495 const int x = 2*(idx%2);
496 const int y = 2*(idx/2);
498 x264_macroblock_cache_ref( h, x, y, 2, 2, 0, h->mb.cache.direct_ref[0][idx] );
499 x264_macroblock_cache_ref( h, x, y, 2, 2, 1, h->mb.cache.direct_ref[1][idx] );
500 for( l = 0; l < 2; l++ )
502 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]] =
503 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]];
504 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]+8] =
505 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]+8];
509 /* This just improves encoder performance, it's not part of the spec */
510 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[8][2], int *i_mvc )
512 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
515 #define SET_MVP(mvp) { \
516 mvc[i][0] = mvp[0]; \
517 mvc[i][1] = mvp[1]; \
522 if( h->sh.i_type == SLICE_TYPE_B
523 && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
525 SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
528 /* spatial predictors */
529 if( h->mb.i_neighbour & MB_LEFT )
531 int i_mb_l = h->mb.i_mb_xy - 1;
532 /* skip MBs didn't go through the whole search process, so mvr is undefined */
533 if( !IS_SKIP( h->mb.type[i_mb_l] ) )
534 SET_MVP( mvr[i_mb_l] );
536 if( h->mb.i_neighbour & MB_TOP )
538 int i_mb_t = h->mb.i_mb_top_xy;
539 if( !IS_SKIP( h->mb.type[i_mb_t] ) )
540 SET_MVP( mvr[i_mb_t] );
542 if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
543 SET_MVP( mvr[i_mb_t-1] );
544 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
545 SET_MVP( mvr[i_mb_t+1] );
549 /* temporal predictors */
550 /* FIXME temporal scaling w/ interlace */
551 if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
553 x264_frame_t *l0 = h->fref0[0];
554 int ref_col_cur, ref_col_prev = -1;
557 #define SET_TMVP(dx, dy) { \
558 int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
559 int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
560 ref_col_cur = l0->ref[0][i_b8]; \
561 if( ref_col_cur >= 0 ) \
563 /* TODO: calc once per frame and tablize? */\
564 if( ref_col_cur != ref_col_prev ) \
565 scale = 256 * (h->fenc->i_poc - h->fref0[i_ref]->i_poc) \
566 / (l0->i_poc - l0->ref_poc[0][ref_col_cur]); \
567 mvc[i][0] = l0->mv[0][i_b4][0] * scale / 256; \
568 mvc[i][1] = l0->mv[0][i_b4][1] * scale / 256; \
570 ref_col_prev = ref_col_cur; \
575 if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
577 if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
585 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
587 const int i8 = x264_scan8[0]+x+8*y;
588 const int i_ref = h->mb.cache.ref[0][i8];
589 const int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
590 int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
592 h->mc.mc_luma( h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
593 &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
594 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
596 // chroma is offset if MCing from a field of opposite parity
597 if( h->mb.b_interlaced & i_ref )
598 mvy += (h->mb.i_mb_y & 1)*4 - 2;
600 h->mc.mc_chroma( &h->mb.pic.p_fref[0][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
601 &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
602 mvx, mvy, 2*width, 2*height );
604 h->mc.mc_chroma( &h->mb.pic.p_fref[0][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
605 &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
606 mvx, mvy, 2*width, 2*height );
608 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
610 const int i8 = x264_scan8[0]+x+8*y;
611 const int i_ref = h->mb.cache.ref[1][i8];
612 const int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
613 int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
615 h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
616 &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
617 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
619 if( h->mb.b_interlaced & i_ref )
620 mvy += (h->mb.i_mb_y & 1)*4 - 2;
622 h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
623 &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
624 mvx, mvy, 2*width, 2*height );
626 h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
627 &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
628 mvx, mvy, 2*width, 2*height );
631 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
633 const int i8 = x264_scan8[0]+x+8*y;
635 const int i_ref1 = h->mb.cache.ref[1][i8];
636 const int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
637 int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
638 DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
639 int i_mode = x264_size2pixel[height][width];
641 x264_mb_mc_0xywh( h, x, y, width, height );
643 h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
644 tmp, 16, mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
646 if( h->mb.b_interlaced & i_ref1 )
647 mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
649 if( h->param.analyse.b_weighted_bipred )
651 const int i_ref0 = h->mb.cache.ref[0][i8];
652 const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
654 h->mc.avg_weight[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16, weight );
656 h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
657 tmp, 16, mvx1, mvy1, 2*width, 2*height );
658 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
660 h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
661 tmp, 16, mvx1, mvy1, 2*width, 2*height );
662 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
666 h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16 );
668 h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
669 tmp, 16, mvx1, mvy1, 2*width, 2*height );
670 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
672 h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
673 tmp, 16, mvx1, mvy1, 2*width, 2*height );
674 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
678 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
680 const int i8 = x264_scan8[0] + x + 8*y;
682 /* FIXME: optimize based on current block size, not global settings? */
683 if( h->sps->b_direct8x8_inference )
685 if( h->mb.cache.ref[0][i8] >= 0 )
686 if( h->mb.cache.ref[1][i8] >= 0 )
687 x264_mb_mc_01xywh( h, x, y, 2, 2 );
689 x264_mb_mc_0xywh( h, x, y, 2, 2 );
691 x264_mb_mc_1xywh( h, x, y, 2, 2 );
695 if( h->mb.cache.ref[0][i8] >= 0 )
697 if( h->mb.cache.ref[1][i8] >= 0 )
699 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
700 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
701 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
702 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
706 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
707 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
708 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
709 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
714 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
715 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
716 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
717 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
722 void x264_mb_mc_8x8( x264_t *h, int i8 )
724 const int x = 2*(i8&1);
725 const int y = 2*(i8>>1);
726 switch( h->mb.i_sub_partition[i8] )
729 x264_mb_mc_0xywh( h, x, y, 2, 2 );
732 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
733 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
736 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
737 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
740 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
741 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
742 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
743 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
746 x264_mb_mc_1xywh( h, x, y, 2, 2 );
749 x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
750 x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
753 x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
754 x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
757 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
758 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
759 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
760 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
763 x264_mb_mc_01xywh( h, x, y, 2, 2 );
766 x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
767 x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
770 x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
771 x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
774 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
775 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
776 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
777 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
780 x264_mb_mc_direct8x8( h, x, y );
785 void x264_mb_mc( x264_t *h )
787 if( h->mb.i_type == P_L0 )
789 if( h->mb.i_partition == D_16x16 )
791 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
793 else if( h->mb.i_partition == D_16x8 )
795 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
796 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
798 else if( h->mb.i_partition == D_8x16 )
800 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
801 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
804 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
807 for( i = 0; i < 4; i++ )
808 x264_mb_mc_8x8( h, i );
810 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
812 x264_mb_mc_direct8x8( h, 0, 0 );
813 x264_mb_mc_direct8x8( h, 2, 0 );
814 x264_mb_mc_direct8x8( h, 0, 2 );
815 x264_mb_mc_direct8x8( h, 2, 2 );
824 /* init ref list utilisations */
825 for( i = 0; i < 2; i++ )
827 b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
828 b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
830 if( h->mb.i_partition == D_16x16 )
832 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
833 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
834 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
836 else if( h->mb.i_partition == D_16x8 )
838 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
839 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
840 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
842 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
843 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
844 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
846 else if( h->mb.i_partition == D_8x16 )
848 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
849 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
850 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
852 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
853 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
854 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
859 int x264_macroblock_cache_init( x264_t *h )
862 int i_mb_count = h->mb.i_mb_count;
864 h->mb.i_mb_stride = h->sps->i_mb_width;
865 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
866 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
868 h->mb.b_interlaced = h->param.b_interlaced;
870 CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
871 CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
872 CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
873 CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
875 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
876 CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 7 * sizeof(int8_t) );
879 CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
880 CHECKED_MALLOC( h->mb.nnz_backup, h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t) );
882 if( h->param.b_cabac )
884 CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
885 CHECKED_MALLOC( h->mb.mvd[0], 2*16 * i_mb_count * sizeof(int16_t) );
886 CHECKED_MALLOC( h->mb.mvd[1], 2*16 * i_mb_count * sizeof(int16_t) );
891 int i_refs = X264_MIN(16, (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid) << h->param.b_interlaced;
892 for( j=0; j < i_refs; j++ )
893 CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
896 for( i=0; i<=h->param.b_interlaced; i++ )
899 CHECKED_MALLOC( h->mb.intra_border_backup[i][j], h->fdec->i_stride[j] );
900 h->mb.intra_border_backup[i][j] += 8;
903 /* init with not available (for top right idx=7,15) */
904 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
905 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
910 void x264_macroblock_cache_end( x264_t *h )
913 for( i=0; i<=h->param.b_interlaced; i++ )
915 x264_free( h->mb.intra_border_backup[i][j] - 8 );
918 int i_refs = i ? 1 + h->param.b_bframe_pyramid : h->param.i_frame_reference;
919 for( j=0; j < i_refs; j++ )
920 x264_free( h->mb.mvr[i][j] );
922 if( h->param.b_cabac )
924 x264_free( h->mb.chroma_pred_mode );
925 x264_free( h->mb.mvd[0] );
926 x264_free( h->mb.mvd[1] );
928 x264_free( h->mb.intra4x4_pred_mode );
929 x264_free( h->mb.non_zero_count );
930 x264_free( h->mb.nnz_backup );
931 x264_free( h->mb.mb_transform_size );
932 x264_free( h->mb.skipbp );
933 x264_free( h->mb.cbp );
934 x264_free( h->mb.qp );
936 void x264_macroblock_slice_init( x264_t *h )
940 h->mb.mv[0] = h->fdec->mv[0];
941 h->mb.mv[1] = h->fdec->mv[1];
942 h->mb.ref[0] = h->fdec->ref[0];
943 h->mb.ref[1] = h->fdec->ref[1];
944 h->mb.type = h->fdec->mb_type;
946 h->fdec->i_ref[0] = h->i_ref0;
947 h->fdec->i_ref[1] = h->i_ref1;
948 for( i = 0; i < h->i_ref0; i++ )
949 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
950 if( h->sh.i_type == SLICE_TYPE_B )
952 for( i = 0; i < h->i_ref1; i++ )
953 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
955 h->mb.map_col_to_list0[-1] = -1;
956 h->mb.map_col_to_list0[-2] = -2;
957 for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
959 int poc = h->fref1[0]->ref_poc[0][i];
960 h->mb.map_col_to_list0[i] = -2;
961 for( j = 0; j < h->i_ref0; j++ )
962 if( h->fref0[j]->i_poc == poc )
964 h->mb.map_col_to_list0[i] = j;
969 if( h->sh.i_type == SLICE_TYPE_P )
970 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
973 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
975 int stride_y = fenc->i_stride[0];
976 int stride_uv = fenc->i_stride[1];
977 int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
978 int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
979 h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
980 fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
983 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
985 int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
986 int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
987 int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
988 int i_top_y = i_mb_y - (1 << h->mb.b_interlaced);
989 int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x;
990 int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x;
991 int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x;
993 int i_top_type = -1; /* gcc warn */
998 assert( h->mb.i_b8_stride == 2*h->mb.i_mb_stride );
999 assert( h->mb.i_b4_stride == 4*h->mb.i_mb_stride );
1002 h->mb.i_mb_x = i_mb_x;
1003 h->mb.i_mb_y = i_mb_y;
1004 h->mb.i_mb_xy = i_mb_xy;
1005 h->mb.i_b8_xy = i_mb_8x8;
1006 h->mb.i_b4_xy = i_mb_4x4;
1007 h->mb.i_mb_top_xy = i_top_xy;
1008 h->mb.i_neighbour = 0;
1011 if( i_top_xy >= h->sh.i_first_mb )
1013 h->mb.i_mb_type_top =
1014 i_top_type= h->mb.type[i_top_xy];
1016 h->mb.i_neighbour |= MB_TOP;
1019 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
1020 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
1021 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
1022 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
1024 /* load non_zero_count */
1025 h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
1026 h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
1027 h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
1028 h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
1030 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
1031 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
1033 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
1034 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
1038 h->mb.i_mb_type_top = -1;
1041 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
1042 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
1043 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
1044 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
1046 /* load non_zero_count */
1047 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
1048 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
1049 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
1050 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
1051 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
1052 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1053 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1054 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1058 if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1060 i_left_xy = i_mb_xy - 1;
1061 h->mb.i_mb_type_left =
1062 i_left_type = h->mb.type[i_left_xy];
1064 h->mb.i_neighbour |= MB_LEFT;
1067 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1068 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1069 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1070 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1072 /* load non_zero_count */
1073 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
1074 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1075 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
1076 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1078 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1079 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1081 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1082 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1086 h->mb.i_mb_type_left = -1;
1088 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1089 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1090 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1091 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1093 /* load non_zero_count */
1094 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1095 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1096 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1097 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1098 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1099 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1100 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1101 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1104 if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1106 h->mb.i_neighbour |= MB_TOPRIGHT;
1107 h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1110 h->mb.i_mb_type_topright = -1;
1111 if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1113 h->mb.i_neighbour |= MB_TOPLEFT;
1114 h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1117 h->mb.i_mb_type_topleft = -1;
1119 if( h->pps->b_transform_8x8_mode )
1121 h->mb.cache.i_neighbour_transform_size =
1122 ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1123 + ( i_top_type >= 0 && h->mb.mb_transform_size[i_top_xy] );
1128 h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1129 h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1130 h->mb.cache.i_neighbour_interlaced =
1131 !!(h->mb.i_neighbour & MB_LEFT)
1132 + !!(h->mb.i_neighbour & MB_TOP);
1145 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
1146 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
1147 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
1148 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
1149 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
1150 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
1152 /* load picture pointers */
1153 for( i = 0; i < 3; i++ )
1155 const int w = (i == 0 ? 16 : 8);
1156 const int i_stride = h->fdec->i_stride[i];
1157 const int i_stride2 = i_stride << h->mb.b_interlaced;
1158 const int i_pix_offset = h->mb.b_interlaced
1159 ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride
1160 : w * (i_mb_x + i_mb_y * i_stride);
1161 int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
1162 const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
1163 const uint8_t *intra_fdec = &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
1164 x264_frame_t **fref[2] = { h->fref0, h->fref1 };
1167 if( h->mb.b_interlaced )
1168 ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
1170 h->mb.pic.i_stride[i] = i_stride2;
1172 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
1173 &h->fenc->plane[i][i_pix_offset], i_stride2, w );
1174 memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
1175 for( j = 0; j < w; j++ )
1176 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
1178 for( l=0; l<2; l++ )
1180 for( j=0; j<h->mb.pic.i_fref[l]; j++ )
1182 h->mb.pic.p_fref[l][j][i==0 ? 0:i+3] = &fref[l][j >> h->mb.b_interlaced]->plane[i][ref_pix_offset[j&1]];
1184 for( k = 1; k < 4; k++ )
1185 h->mb.pic.p_fref[l][j][k] = &fref[l][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
1190 if( h->fdec->integral )
1192 assert( !h->mb.b_interlaced );
1193 for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
1194 h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1195 for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
1196 h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1199 x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
1201 /* load ref/mv/mvd */
1202 if( h->sh.i_type != SLICE_TYPE_I )
1204 const int s8x8 = h->mb.i_b8_stride;
1205 const int s4x4 = h->mb.i_b4_stride;
1209 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1212 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1213 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1214 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1217 if( h->mb.i_neighbour & MB_TOPLEFT )
1219 const int i8 = x264_scan8[0] - 1 - 1*8;
1220 const int ir = i_top_8x8 - 1;
1221 const int iv = i_top_4x4 - 1;
1222 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1223 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1224 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1228 const int i8 = x264_scan8[0] - 1 - 1*8;
1229 h->mb.cache.ref[i_list][i8] = -2;
1230 h->mb.cache.mv[i_list][i8][0] = 0;
1231 h->mb.cache.mv[i_list][i8][1] = 0;
1234 if( h->mb.i_neighbour & MB_TOP )
1236 const int i8 = x264_scan8[0] - 8;
1237 const int ir = i_top_8x8;
1238 const int iv = i_top_4x4;
1239 h->mb.cache.ref[i_list][i8+0] =
1240 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1241 h->mb.cache.ref[i_list][i8+2] =
1242 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1244 for( i = 0; i < 4; i++ )
1246 h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
1247 h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
1252 const int i8 = x264_scan8[0] - 8;
1253 for( i = 0; i < 4; i++ )
1255 h->mb.cache.ref[i_list][i8+i] = -2;
1256 h->mb.cache.mv[i_list][i8+i][0] =
1257 h->mb.cache.mv[i_list][i8+i][1] = 0;
1261 if( h->mb.i_neighbour & MB_TOPRIGHT )
1263 const int i8 = x264_scan8[0] + 4 - 1*8;
1264 const int ir = i_top_8x8 + 2;
1265 const int iv = i_top_4x4 + 4;
1266 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1267 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1268 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1272 const int i8 = x264_scan8[0] + 4 - 1*8;
1273 h->mb.cache.ref[i_list][i8] = -2;
1274 h->mb.cache.mv[i_list][i8][0] = 0;
1275 h->mb.cache.mv[i_list][i8][1] = 0;
1278 if( h->mb.i_neighbour & MB_LEFT )
1280 const int i8 = x264_scan8[0] - 1;
1281 const int ir = i_mb_8x8 - 1;
1282 const int iv = i_mb_4x4 - 1;
1283 h->mb.cache.ref[i_list][i8+0*8] =
1284 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1285 h->mb.cache.ref[i_list][i8+2*8] =
1286 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1288 for( i = 0; i < 4; i++ )
1290 h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
1291 h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
1296 const int i8 = x264_scan8[0] - 1;
1297 for( i = 0; i < 4; i++ )
1299 h->mb.cache.ref[i_list][i8+i*8] = -2;
1300 h->mb.cache.mv[i_list][i8+i*8][0] =
1301 h->mb.cache.mv[i_list][i8+i*8][1] = 0;
1305 if( h->param.b_cabac )
1307 if( i_top_type >= 0 )
1309 const int i8 = x264_scan8[0] - 8;
1310 const int iv = i_top_4x4;
1311 for( i = 0; i < 4; i++ )
1313 h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
1314 h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
1319 const int i8 = x264_scan8[0] - 8;
1320 for( i = 0; i < 4; i++ )
1322 h->mb.cache.mvd[i_list][i8+i][0] =
1323 h->mb.cache.mvd[i_list][i8+i][1] = 0;
1327 if( i_left_type >= 0 )
1329 const int i8 = x264_scan8[0] - 1;
1330 const int iv = i_mb_4x4 - 1;
1331 for( i = 0; i < 4; i++ )
1333 h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
1334 h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
1339 const int i8 = x264_scan8[0] - 1;
1340 for( i = 0; i < 4; i++ )
1342 h->mb.cache.mvd[i_list][i8+i*8][0] =
1343 h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
1350 if( h->sh.i_type == SLICE_TYPE_B && h->param.b_cabac )
1352 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1353 if( i_left_type >= 0 )
1355 h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
1356 h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
1358 if( i_top_type >= 0 )
1360 h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
1361 h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
1365 if( h->sh.i_type == SLICE_TYPE_P )
1366 x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1369 h->mb.i_neighbour4[0] =
1370 h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1371 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0);
1372 h->mb.i_neighbour4[4] =
1373 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1374 h->mb.i_neighbour4[2] =
1375 h->mb.i_neighbour4[8] =
1376 h->mb.i_neighbour4[10] =
1377 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1378 h->mb.i_neighbour4[3] =
1379 h->mb.i_neighbour4[7] =
1380 h->mb.i_neighbour4[11] =
1381 h->mb.i_neighbour4[13] =
1382 h->mb.i_neighbour4[15] =
1383 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
1384 h->mb.i_neighbour4[5] =
1385 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour & MB_TOPRIGHT)
1386 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1387 h->mb.i_neighbour4[6] =
1388 h->mb.i_neighbour4[9] =
1389 h->mb.i_neighbour4[12] =
1390 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
1393 void x264_macroblock_cache_save( x264_t *h )
1395 const int i_mb_xy = h->mb.i_mb_xy;
1396 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1397 const int s8x8 = h->mb.i_b8_stride;
1398 const int s4x4 = h->mb.i_b4_stride;
1399 const int i_mb_4x4 = h->mb.i_b4_xy;
1400 const int i_mb_8x8 = h->mb.i_b8_xy;
1404 for( i = 0; i < 3; i++ )
1407 int i_stride = h->fdec->i_stride[i];
1408 int i_stride2 = i_stride << h->mb.b_interlaced;
1409 int i_pix_offset = h->mb.b_interlaced
1410 ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1411 : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1412 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1413 &h->fdec->plane[i][i_pix_offset], i_stride2,
1414 h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1417 x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1419 h->mb.type[i_mb_xy] = i_mb_type;
1421 if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1422 h->mb.i_qp = h->mb.i_last_qp;
1423 h->mb.qp[i_mb_xy] = h->mb.i_qp;
1425 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1426 h->mb.i_last_qp = h->mb.i_qp;
1427 h->mb.i_mb_prev_xy = h->mb.i_mb_xy;
1430 if( i_mb_type == I_4x4 )
1432 h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1433 h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
1434 h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
1435 h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
1436 h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
1437 h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
1438 h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
1442 h->mb.intra4x4_pred_mode[i_mb_xy][0] =
1443 h->mb.intra4x4_pred_mode[i_mb_xy][1] =
1444 h->mb.intra4x4_pred_mode[i_mb_xy][2] =
1445 h->mb.intra4x4_pred_mode[i_mb_xy][3] =
1446 h->mb.intra4x4_pred_mode[i_mb_xy][4] =
1447 h->mb.intra4x4_pred_mode[i_mb_xy][5] =
1448 h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
1451 if( i_mb_type == I_PCM )
1453 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1454 for( i = 0; i < 16 + 2*4; i++ )
1456 h->mb.non_zero_count[i_mb_xy][i] = 16;
1461 /* save non zero count */
1462 for( i = 0; i < 16 + 2*4; i++ )
1464 h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
1468 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1469 h->mb.b_transform_8x8 = 0;
1470 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1472 if( !IS_INTRA( i_mb_type ) )
1475 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1479 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
1480 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
1481 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
1482 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
1484 for( y = 0; y < 4; y++ )
1486 for( x = 0; x < 4; x++ )
1488 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = h->mb.cache.mv[i_list][x264_scan8[0]+x+8*y][0];
1489 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = h->mb.cache.mv[i_list][x264_scan8[0]+x+8*y][1];
1497 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1501 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
1502 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
1503 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
1504 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
1506 for( y = 0; y < 4; y++ )
1508 for( x = 0; x < 4; x++ )
1510 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1511 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1517 if( h->param.b_cabac )
1519 if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1520 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1522 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1524 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1527 for( i_list = 0; i_list < 2; i_list++ )
1529 const int s4x4 = 4 * h->mb.i_mb_stride;
1531 for( y = 0; y < 4; y++ )
1533 for( x = 0; x < 4; x++ )
1535 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = h->mb.cache.mvd[i_list][x264_scan8[0]+x+8*y][0];
1536 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = h->mb.cache.mvd[i_list][x264_scan8[0]+x+8*y][1];
1544 for( i_list = 0; i_list < 2; i_list++ )
1546 const int s4x4 = 4 * h->mb.i_mb_stride;
1548 for( y = 0; y < 4; y++ )
1550 for( x = 0; x < 4; x++ )
1552 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1553 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1558 if( h->sh.i_type == SLICE_TYPE_B )
1560 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1561 h->mb.skipbp[i_mb_xy] = 0xf;
1562 else if( i_mb_type == B_8x8 )
1565 for( i = 0; i < 4; i++ )
1566 skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1567 h->mb.skipbp[i_mb_xy] = skipbp;
1570 h->mb.skipbp[i_mb_xy] = 0;
1575 void x264_macroblock_bipred_init( x264_t *h )
1578 for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1580 int poc0 = h->fref0[i_ref0]->i_poc;
1581 for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1583 int dist_scale_factor;
1584 int poc1 = h->fref1[i_ref1]->i_poc;
1585 int td = x264_clip3( poc1 - poc0, -128, 127 );
1586 if( td == 0 /* || pic0 is a long-term ref */ )
1587 dist_scale_factor = 256;
1590 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1591 int tx = (16384 + (abs(td) >> 1)) / td;
1592 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1594 h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1596 dist_scale_factor >>= 2;
1597 if( h->param.analyse.b_weighted_bipred
1598 && dist_scale_factor >= -64
1599 && dist_scale_factor <= 128 )
1600 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1602 h->mb.bipred_weight[i_ref0][i_ref1] = 32;
1607 for( i_ref0 = 2*h->i_ref0-1; i_ref0 >= 0; i_ref0-- )
1608 for( i_ref1 = 2*h->i_ref1-1; i_ref1 >= 0; i_ref1-- )
1609 h->mb.bipred_weight[i_ref0][i_ref1] = h->mb.bipred_weight[i_ref0>>1][i_ref1>>1];