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 *****************************************************************************/
29 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
31 const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
32 const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
33 const int m = X264_MIN( x264_mb_pred_mode4x4_fix(ma),
34 x264_mb_pred_mode4x4_fix(mb) );
42 int x264_mb_predict_non_zero_code( x264_t *h, int idx )
44 const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1];
45 const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8];
51 i_ret = ( i_ret + 1 ) >> 1;
56 int x264_mb_transform_8x8_allowed( x264_t *h )
58 if( IS_SKIP( h->mb.i_type ) )
60 if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
63 for( i = 0; i < 4; i++ )
64 if( !IS_SUB8x8(h->mb.i_sub_partition[i])
65 || ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 && !h->sps->b_direct8x8_inference ) )
70 if( h->mb.i_type == B_DIRECT && !h->sps->b_direct8x8_inference )
76 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
78 const int i8 = x264_scan8[idx];
79 const int i_ref= h->mb.cache.ref[i_list][i8];
80 int i_refa = h->mb.cache.ref[i_list][i8 - 1];
81 int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
82 int i_refb = h->mb.cache.ref[i_list][i8 - 8];
83 int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
84 int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
85 int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
89 if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
91 i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
92 mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
95 if( h->mb.i_partition == D_16x8 )
97 if( idx == 0 && i_refb == i_ref )
103 else if( idx != 0 && i_refa == i_ref )
110 else if( h->mb.i_partition == D_8x16 )
112 if( idx == 0 && i_refa == i_ref )
118 else if( idx != 0 && i_refc == i_ref )
127 if( i_refa == i_ref ) i_count++;
128 if( i_refb == i_ref ) i_count++;
129 if( i_refc == i_ref ) i_count++;
133 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
134 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
136 else if( i_count == 1 )
138 if( i_refa == i_ref )
143 else if( i_refb == i_ref )
154 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
161 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
162 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
166 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int mvp[2] )
168 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
169 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
170 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
171 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
172 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
173 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
179 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
180 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
184 if( i_refa == i_ref ) i_count++;
185 if( i_refb == i_ref ) i_count++;
186 if( i_refc == i_ref ) i_count++;
190 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
191 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
193 else if( i_count == 1 )
195 if( i_refa == i_ref )
200 else if( i_refb == i_ref )
211 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
218 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
219 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
224 void x264_mb_predict_mv_pskip( x264_t *h, int mv[2] )
226 int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
227 int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
228 int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
229 int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
231 if( i_refa == -2 || i_refb == -2 ||
232 ( i_refa == 0 && mv_a[0] == 0 && mv_a[1] == 0 ) ||
233 ( i_refb == 0 && mv_b[0] == 0 && mv_b[1] == 0 ) )
239 x264_mb_predict_mv_16x16( h, 0, 0, mv );
243 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
245 int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
246 int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
249 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
251 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
253 if( IS_INTRA( type_col ) )
255 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
256 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0, 0 );
257 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0, 0 );
260 b8x8 = h->sps->b_direct8x8_inference ||
261 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
263 for( i8 = 0; i8 < 4; i8++ )
267 const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
268 const int i_ref = h->mb.map_col_to_list0[ h->fref1[0]->ref[0][ i_part_8x8 ] ];
272 const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
274 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
278 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
280 mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
281 mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
282 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, mv_l0[0], mv_l0[1] );
283 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
287 for( i4 = 0; i4 < 4; i4++ )
289 const int x4 = i4%2 + 2*x8;
290 const int y4 = i4/2 + 2*y8;
291 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * h->mb.i_b4_stride ];
293 mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
294 mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
295 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, mv_l0[0], mv_l0[1] );
296 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
302 /* the collocated ref isn't in the current list0 */
303 /* FIXME: we might still be able to use direct_8x8 on some partitions */
304 /* FIXME: with B-pyramid + extensive ref list reordering
305 * (not currently used), we would also have to check
306 * l1mv1 like in spatial mode */
311 if( h->param.i_threads > 1 )
313 int di = b8x8 ? 4 : 1;
314 for( i4=0; i4<16; i4+=di )
316 if( h->mb.cache.mv[0][x264_scan8[i4]][1] > h->mb.mv_max_spel[1]
317 || h->mb.cache.mv[1][x264_scan8[i4]][1] > h->mb.mv_max_spel[1] )
320 fprintf(stderr, "direct_temporal: (%d,%d) (%d,%d) > %d \n",
321 h->mb.cache.mv[0][x264_scan8[i4]][0],
322 h->mb.cache.mv[0][x264_scan8[i4]][1],
323 h->mb.cache.mv[1][x264_scan8[i4]][0],
324 h->mb.cache.mv[1][x264_scan8[i4]][1],
325 h->mb.mv_max_spel[1]);
335 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
342 const int8_t *l1ref0 = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
343 const int8_t *l1ref1 = &h->fref1[0]->ref[1][ h->mb.i_b8_xy ];
344 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
345 const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[1][ h->mb.i_b4_xy ];
346 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
348 for( i_list=0; i_list<2; i_list++ )
350 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
351 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
352 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
354 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
356 ref[i_list] = i_refa;
357 if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
358 ref[i_list] = i_refb;
359 if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
360 ref[i_list] = i_refc;
361 if( ref[i_list] < 0 )
365 if( ref[0] < 0 && ref[1] < 0 )
376 for( i_list=0; i_list<2; i_list++ )
378 if( ref[i_list] >= 0 )
379 x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
381 mv[i_list][0] = mv[i_list][1] = 0;
385 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
386 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
387 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, mv[0][0], mv[0][1] );
388 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, mv[1][0], mv[1][1] );
390 if( IS_INTRA( type_col ) )
393 if( h->param.i_threads > 1
394 && ( mv[0][1] > h->mb.mv_max_spel[1]
395 || mv[1][1] > h->mb.mv_max_spel[1] ) )
398 fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
399 mv[0][0], mv[0][1], mv[1][0], mv[1][1],
400 h->mb.mv_max_spel[1]);
405 b8x8 = h->sps->b_direct8x8_inference ||
406 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
409 for( i8=0; i8<4; i8++ )
413 const int o8 = x8 + y8 * h->mb.i_b8_stride;
414 if( l1ref0[o8] == 0 || ( l1ref0[o8] < 0 && l1ref1[o8] == 0 ) )
416 const int16_t (*l1mv)[2] = (l1ref0[o8] == 0) ? l1mv0 : l1mv1;
419 const int16_t *mvcol = l1mv[3*x8 + 3*y8 * h->mb.i_b4_stride];
420 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
423 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0, 0 );
425 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0, 0 );
430 for( i4=0; i4<4; i4++ )
432 const int x4 = i4%2 + 2*x8;
433 const int y4 = i4/2 + 2*y8;
434 const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
435 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
438 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0, 0 );
440 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0, 0 );
450 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
453 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
455 else if( h->sh.b_direct_spatial_mv_pred )
456 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
458 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
460 if( b_changed != NULL && b_available )
462 int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
463 if( IS_INTRA(type_col) || type_col == P_SKIP )
465 *b_changed = h->mb.cache.direct_ref[0][0] != h->mb.cache.ref[0][X264_SCAN8_0]
466 || h->mb.cache.direct_ref[1][0] != h->mb.cache.ref[1][X264_SCAN8_0]
467 || *(uint32_t*)h->mb.cache.direct_mv[0][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[0][X264_SCAN8_0]
468 || *(uint32_t*)h->mb.cache.direct_mv[1][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[1][X264_SCAN8_0];
474 for( l = 0; l < 2; l++ )
475 for( i = 0; i < 4; i++ )
476 *b_changed |= h->mb.cache.direct_ref[l][i] != h->mb.cache.ref[l][x264_scan8[i*4]];
477 *b_changed = *b_changed || memcmp(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
487 for( l = 0; l < 2; l++ )
488 for( i = 0; i < 4; i++ )
489 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
490 memcpy(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
496 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
498 const int x = 2*(idx%2);
499 const int y = 2*(idx/2);
501 x264_macroblock_cache_ref( h, x, y, 2, 2, 0, h->mb.cache.direct_ref[0][idx] );
502 x264_macroblock_cache_ref( h, x, y, 2, 2, 1, h->mb.cache.direct_ref[1][idx] );
503 for( l = 0; l < 2; l++ )
505 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]] =
506 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]];
507 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]+8] =
508 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]+8];
512 /* This just improves encoder performance, it's not part of the spec */
513 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[8][2], int *i_mvc )
515 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
518 #define SET_MVP(mvp) { \
519 mvc[i][0] = mvp[0]; \
520 mvc[i][1] = mvp[1]; \
525 if( h->sh.i_type == SLICE_TYPE_B
526 && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
528 SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
531 /* spatial predictors */
532 if( h->mb.i_neighbour & MB_LEFT )
534 int i_mb_l = h->mb.i_mb_xy - 1;
535 /* skip MBs didn't go through the whole search process, so mvr is undefined */
536 if( !IS_SKIP( h->mb.type[i_mb_l] ) )
537 SET_MVP( mvr[i_mb_l] );
539 if( h->mb.i_neighbour & MB_TOP )
541 int i_mb_t = h->mb.i_mb_top_xy;
542 if( !IS_SKIP( h->mb.type[i_mb_t] ) )
543 SET_MVP( mvr[i_mb_t] );
545 if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
546 SET_MVP( mvr[i_mb_t-1] );
547 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
548 SET_MVP( mvr[i_mb_t+1] );
552 /* temporal predictors */
553 /* FIXME temporal scaling w/ interlace */
554 if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
556 x264_frame_t *l0 = h->fref0[0];
557 int ref_col_cur, ref_col_prev = -1;
560 #define SET_TMVP(dx, dy) { \
561 int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
562 int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
563 ref_col_cur = l0->ref[0][i_b8]; \
564 if( ref_col_cur >= 0 ) \
566 /* TODO: calc once per frame and tablize? */\
567 if( ref_col_cur != ref_col_prev ) \
568 scale = 256 * (h->fenc->i_poc - h->fref0[i_ref]->i_poc) \
569 / (l0->i_poc - l0->ref_poc[0][ref_col_cur]); \
570 mvc[i][0] = l0->mv[0][i_b4][0] * scale / 256; \
571 mvc[i][1] = l0->mv[0][i_b4][1] * scale / 256; \
573 ref_col_prev = ref_col_cur; \
578 if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
580 if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
588 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
590 const int i8 = x264_scan8[0]+x+8*y;
591 const int i_ref = h->mb.cache.ref[0][i8];
592 const int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
593 int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
595 h->mc.mc_luma( h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
596 &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
597 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
599 // chroma is offset if MCing from a field of opposite parity
600 if( h->mb.b_interlaced & i_ref )
601 mvy += (h->mb.i_mb_y & 1)*4 - 2;
603 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],
604 &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
605 mvx, mvy, 2*width, 2*height );
607 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],
608 &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
609 mvx, mvy, 2*width, 2*height );
611 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
613 const int i8 = x264_scan8[0]+x+8*y;
614 const int i_ref = h->mb.cache.ref[1][i8];
615 const int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
616 int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
618 h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
619 &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
620 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
622 if( h->mb.b_interlaced & i_ref )
623 mvy += (h->mb.i_mb_y & 1)*4 - 2;
625 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],
626 &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
627 mvx, mvy, 2*width, 2*height );
629 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],
630 &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
631 mvx, mvy, 2*width, 2*height );
634 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
636 const int i8 = x264_scan8[0]+x+8*y;
638 const int i_ref1 = h->mb.cache.ref[1][i8];
639 const int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
640 int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
641 DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
642 int i_mode = x264_size2pixel[height][width];
644 x264_mb_mc_0xywh( h, x, y, width, height );
646 h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
647 tmp, 16, mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
649 if( h->mb.b_interlaced & i_ref1 )
650 mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
652 if( h->param.analyse.b_weighted_bipred )
654 const int i_ref0 = h->mb.cache.ref[0][i8];
655 const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
657 h->mc.avg_weight[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16, weight );
659 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],
660 tmp, 16, mvx1, mvy1, 2*width, 2*height );
661 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
663 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],
664 tmp, 16, mvx1, mvy1, 2*width, 2*height );
665 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
669 h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16 );
671 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],
672 tmp, 16, mvx1, mvy1, 2*width, 2*height );
673 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
675 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],
676 tmp, 16, mvx1, mvy1, 2*width, 2*height );
677 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
681 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
683 const int i8 = x264_scan8[0] + x + 8*y;
685 /* FIXME: optimize based on current block size, not global settings? */
686 if( h->sps->b_direct8x8_inference )
688 if( h->mb.cache.ref[0][i8] >= 0 )
689 if( h->mb.cache.ref[1][i8] >= 0 )
690 x264_mb_mc_01xywh( h, x, y, 2, 2 );
692 x264_mb_mc_0xywh( h, x, y, 2, 2 );
694 x264_mb_mc_1xywh( h, x, y, 2, 2 );
698 if( h->mb.cache.ref[0][i8] >= 0 )
700 if( h->mb.cache.ref[1][i8] >= 0 )
702 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
703 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
704 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
705 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
709 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
710 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
711 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
712 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
717 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
718 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
719 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
720 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
725 void x264_mb_mc_8x8( x264_t *h, int i8 )
727 const int x = 2*(i8&1);
728 const int y = 2*(i8>>1);
729 switch( h->mb.i_sub_partition[i8] )
732 x264_mb_mc_0xywh( h, x, y, 2, 2 );
735 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
736 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
739 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
740 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
743 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
744 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
745 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
746 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
749 x264_mb_mc_1xywh( h, x, y, 2, 2 );
752 x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
753 x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
756 x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
757 x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
760 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
761 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
762 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
763 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
766 x264_mb_mc_01xywh( h, x, y, 2, 2 );
769 x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
770 x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
773 x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
774 x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
777 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
778 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
779 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
780 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
783 x264_mb_mc_direct8x8( h, x, y );
788 void x264_mb_mc( x264_t *h )
790 if( h->mb.i_type == P_L0 )
792 if( h->mb.i_partition == D_16x16 )
794 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
796 else if( h->mb.i_partition == D_16x8 )
798 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
799 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
801 else if( h->mb.i_partition == D_8x16 )
803 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
804 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
807 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
810 for( i = 0; i < 4; i++ )
811 x264_mb_mc_8x8( h, i );
813 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
815 x264_mb_mc_direct8x8( h, 0, 0 );
816 x264_mb_mc_direct8x8( h, 2, 0 );
817 x264_mb_mc_direct8x8( h, 0, 2 );
818 x264_mb_mc_direct8x8( h, 2, 2 );
827 /* init ref list utilisations */
828 for( i = 0; i < 2; i++ )
830 b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
831 b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
833 if( h->mb.i_partition == D_16x16 )
835 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
836 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
837 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
839 else if( h->mb.i_partition == D_16x8 )
841 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
842 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
843 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
845 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
846 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
847 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
849 else if( h->mb.i_partition == D_8x16 )
851 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
852 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
853 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
855 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
856 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
857 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
862 int x264_macroblock_cache_init( x264_t *h )
865 int i_mb_count = h->mb.i_mb_count;
867 h->mb.i_mb_stride = h->sps->i_mb_width;
868 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
869 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
871 h->mb.b_interlaced = h->param.b_interlaced;
873 CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
874 CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
875 CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
876 CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
878 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
879 CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 7 * sizeof(int8_t) );
882 CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
883 CHECKED_MALLOC( h->mb.nnz_backup, h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t) );
885 if( h->param.b_cabac )
887 CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
888 CHECKED_MALLOC( h->mb.mvd[0], 2*16 * i_mb_count * sizeof(int16_t) );
889 CHECKED_MALLOC( h->mb.mvd[1], 2*16 * i_mb_count * sizeof(int16_t) );
894 int i_refs = X264_MIN(16, (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid) << h->param.b_interlaced;
895 for( j=0; j < i_refs; j++ )
896 CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
899 for( i=0; i<=h->param.b_interlaced; i++ )
902 CHECKED_MALLOC( h->mb.intra_border_backup[i][j], h->fdec->i_stride[j] );
903 h->mb.intra_border_backup[i][j] += 8;
906 /* init with not available (for top right idx=7,15) */
907 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
908 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
913 void x264_macroblock_cache_end( x264_t *h )
916 for( i=0; i<=h->param.b_interlaced; i++ )
918 x264_free( h->mb.intra_border_backup[i][j] - 8 );
921 int i_refs = i ? 1 + h->param.b_bframe_pyramid : h->param.i_frame_reference;
922 for( j=0; j < i_refs; j++ )
923 x264_free( h->mb.mvr[i][j] );
925 if( h->param.b_cabac )
927 x264_free( h->mb.chroma_pred_mode );
928 x264_free( h->mb.mvd[0] );
929 x264_free( h->mb.mvd[1] );
931 x264_free( h->mb.intra4x4_pred_mode );
932 x264_free( h->mb.non_zero_count );
933 x264_free( h->mb.nnz_backup );
934 x264_free( h->mb.mb_transform_size );
935 x264_free( h->mb.skipbp );
936 x264_free( h->mb.cbp );
937 x264_free( h->mb.qp );
939 void x264_macroblock_slice_init( x264_t *h )
943 h->mb.mv[0] = h->fdec->mv[0];
944 h->mb.mv[1] = h->fdec->mv[1];
945 h->mb.ref[0] = h->fdec->ref[0];
946 h->mb.ref[1] = h->fdec->ref[1];
947 h->mb.type = h->fdec->mb_type;
949 h->fdec->i_ref[0] = h->i_ref0;
950 h->fdec->i_ref[1] = h->i_ref1;
951 for( i = 0; i < h->i_ref0; i++ )
952 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
953 if( h->sh.i_type == SLICE_TYPE_B )
955 for( i = 0; i < h->i_ref1; i++ )
956 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
958 h->mb.map_col_to_list0[-1] = -1;
959 h->mb.map_col_to_list0[-2] = -2;
960 for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
962 int poc = h->fref1[0]->ref_poc[0][i];
963 h->mb.map_col_to_list0[i] = -2;
964 for( j = 0; j < h->i_ref0; j++ )
965 if( h->fref0[j]->i_poc == poc )
967 h->mb.map_col_to_list0[i] = j;
972 if( h->sh.i_type == SLICE_TYPE_P )
973 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
976 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
978 int stride_y = fenc->i_stride[0];
979 int stride_uv = fenc->i_stride[1];
980 int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
981 int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
982 h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
983 fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
986 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
988 int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
989 int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
990 int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
991 int i_top_y = i_mb_y - (1 << h->mb.b_interlaced);
992 int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x;
993 int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x;
994 int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x;
996 int i_top_type = -1; /* gcc warn */
1001 assert( h->mb.i_b8_stride == 2*h->mb.i_mb_stride );
1002 assert( h->mb.i_b4_stride == 4*h->mb.i_mb_stride );
1005 h->mb.i_mb_x = i_mb_x;
1006 h->mb.i_mb_y = i_mb_y;
1007 h->mb.i_mb_xy = i_mb_xy;
1008 h->mb.i_b8_xy = i_mb_8x8;
1009 h->mb.i_b4_xy = i_mb_4x4;
1010 h->mb.i_mb_top_xy = i_top_xy;
1011 h->mb.i_neighbour = 0;
1014 if( i_top_xy >= h->sh.i_first_mb )
1016 h->mb.i_mb_type_top =
1017 i_top_type= h->mb.type[i_top_xy];
1019 h->mb.i_neighbour |= MB_TOP;
1022 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
1023 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
1024 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
1025 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
1027 /* load non_zero_count */
1028 h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
1029 h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
1030 h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
1031 h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
1033 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
1034 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
1036 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
1037 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
1041 h->mb.i_mb_type_top = -1;
1044 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
1045 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
1046 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
1047 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
1049 /* load non_zero_count */
1050 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
1051 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
1052 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
1053 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
1054 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
1055 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1056 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1057 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1061 if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1063 i_left_xy = i_mb_xy - 1;
1064 h->mb.i_mb_type_left =
1065 i_left_type = h->mb.type[i_left_xy];
1067 h->mb.i_neighbour |= MB_LEFT;
1070 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1071 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1072 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1073 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1075 /* load non_zero_count */
1076 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
1077 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1078 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
1079 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1081 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1082 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1084 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1085 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1089 h->mb.i_mb_type_left = -1;
1091 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1092 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1093 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1094 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1096 /* load non_zero_count */
1097 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1098 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1099 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1100 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1101 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1102 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1103 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1104 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1107 if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1109 h->mb.i_neighbour |= MB_TOPRIGHT;
1110 h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1113 h->mb.i_mb_type_topright = -1;
1114 if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1116 h->mb.i_neighbour |= MB_TOPLEFT;
1117 h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1120 h->mb.i_mb_type_topleft = -1;
1122 if( h->pps->b_transform_8x8_mode )
1124 h->mb.cache.i_neighbour_transform_size =
1125 ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1126 + ( i_top_type >= 0 && h->mb.mb_transform_size[i_top_xy] );
1131 h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1132 h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1133 h->mb.cache.i_neighbour_interlaced =
1134 !!(h->mb.i_neighbour & MB_LEFT)
1135 + !!(h->mb.i_neighbour & MB_TOP);
1148 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
1149 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
1150 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
1151 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
1152 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
1153 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
1155 /* load picture pointers */
1156 for( i = 0; i < 3; i++ )
1158 const int w = (i == 0 ? 16 : 8);
1159 const int i_stride = h->fdec->i_stride[i];
1160 const int i_stride2 = i_stride << h->mb.b_interlaced;
1161 const int i_pix_offset = h->mb.b_interlaced
1162 ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride
1163 : w * (i_mb_x + i_mb_y * i_stride);
1164 int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
1165 const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
1166 const uint8_t *intra_fdec = &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
1167 x264_frame_t **fref[2] = { h->fref0, h->fref1 };
1170 if( h->mb.b_interlaced )
1171 ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
1173 h->mb.pic.i_stride[i] = i_stride2;
1175 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
1176 &h->fenc->plane[i][i_pix_offset], i_stride2, w );
1177 memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
1178 for( j = 0; j < w; j++ )
1179 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
1181 for( l=0; l<2; l++ )
1183 for( j=0; j<h->mb.pic.i_fref[l]; j++ )
1185 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]];
1187 for( k = 1; k < 4; k++ )
1188 h->mb.pic.p_fref[l][j][k] = &fref[l][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
1193 if( h->fdec->integral )
1195 assert( !h->mb.b_interlaced );
1196 for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
1197 h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1198 for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
1199 h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1202 x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
1204 /* load ref/mv/mvd */
1205 if( h->sh.i_type != SLICE_TYPE_I )
1207 const int s8x8 = h->mb.i_b8_stride;
1208 const int s4x4 = h->mb.i_b4_stride;
1212 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1215 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1216 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1217 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1220 if( h->mb.i_neighbour & MB_TOPLEFT )
1222 const int i8 = x264_scan8[0] - 1 - 1*8;
1223 const int ir = i_top_8x8 - 1;
1224 const int iv = i_top_4x4 - 1;
1225 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1226 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1227 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1231 const int i8 = x264_scan8[0] - 1 - 1*8;
1232 h->mb.cache.ref[i_list][i8] = -2;
1233 h->mb.cache.mv[i_list][i8][0] = 0;
1234 h->mb.cache.mv[i_list][i8][1] = 0;
1237 if( h->mb.i_neighbour & MB_TOP )
1239 const int i8 = x264_scan8[0] - 8;
1240 const int ir = i_top_8x8;
1241 const int iv = i_top_4x4;
1242 h->mb.cache.ref[i_list][i8+0] =
1243 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1244 h->mb.cache.ref[i_list][i8+2] =
1245 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1247 for( i = 0; i < 4; i++ )
1249 h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
1250 h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
1255 const int i8 = x264_scan8[0] - 8;
1256 for( i = 0; i < 4; i++ )
1258 h->mb.cache.ref[i_list][i8+i] = -2;
1259 h->mb.cache.mv[i_list][i8+i][0] =
1260 h->mb.cache.mv[i_list][i8+i][1] = 0;
1264 if( h->mb.i_neighbour & MB_TOPRIGHT )
1266 const int i8 = x264_scan8[0] + 4 - 1*8;
1267 const int ir = i_top_8x8 + 2;
1268 const int iv = i_top_4x4 + 4;
1269 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1270 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1271 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1275 const int i8 = x264_scan8[0] + 4 - 1*8;
1276 h->mb.cache.ref[i_list][i8] = -2;
1277 h->mb.cache.mv[i_list][i8][0] = 0;
1278 h->mb.cache.mv[i_list][i8][1] = 0;
1281 if( h->mb.i_neighbour & MB_LEFT )
1283 const int i8 = x264_scan8[0] - 1;
1284 const int ir = i_mb_8x8 - 1;
1285 const int iv = i_mb_4x4 - 1;
1286 h->mb.cache.ref[i_list][i8+0*8] =
1287 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1288 h->mb.cache.ref[i_list][i8+2*8] =
1289 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1291 for( i = 0; i < 4; i++ )
1293 h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
1294 h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
1299 const int i8 = x264_scan8[0] - 1;
1300 for( i = 0; i < 4; i++ )
1302 h->mb.cache.ref[i_list][i8+i*8] = -2;
1303 h->mb.cache.mv[i_list][i8+i*8][0] =
1304 h->mb.cache.mv[i_list][i8+i*8][1] = 0;
1308 if( h->param.b_cabac )
1310 if( i_top_type >= 0 )
1312 const int i8 = x264_scan8[0] - 8;
1313 const int iv = i_top_4x4;
1314 for( i = 0; i < 4; i++ )
1316 h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
1317 h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
1322 const int i8 = x264_scan8[0] - 8;
1323 for( i = 0; i < 4; i++ )
1325 h->mb.cache.mvd[i_list][i8+i][0] =
1326 h->mb.cache.mvd[i_list][i8+i][1] = 0;
1330 if( i_left_type >= 0 )
1332 const int i8 = x264_scan8[0] - 1;
1333 const int iv = i_mb_4x4 - 1;
1334 for( i = 0; i < 4; i++ )
1336 h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
1337 h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
1342 const int i8 = x264_scan8[0] - 1;
1343 for( i = 0; i < 4; i++ )
1345 h->mb.cache.mvd[i_list][i8+i*8][0] =
1346 h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
1353 if( h->sh.i_type == SLICE_TYPE_B && h->param.b_cabac )
1355 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1356 if( i_left_type >= 0 )
1358 h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
1359 h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
1361 if( i_top_type >= 0 )
1363 h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
1364 h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
1368 if( h->sh.i_type == SLICE_TYPE_P )
1369 x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1372 h->mb.i_neighbour4[0] =
1373 h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1374 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0);
1375 h->mb.i_neighbour4[4] =
1376 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1377 h->mb.i_neighbour4[2] =
1378 h->mb.i_neighbour4[8] =
1379 h->mb.i_neighbour4[10] =
1380 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1381 h->mb.i_neighbour4[3] =
1382 h->mb.i_neighbour4[7] =
1383 h->mb.i_neighbour4[11] =
1384 h->mb.i_neighbour4[13] =
1385 h->mb.i_neighbour4[15] =
1386 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
1387 h->mb.i_neighbour4[5] =
1388 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour & MB_TOPRIGHT)
1389 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1390 h->mb.i_neighbour4[6] =
1391 h->mb.i_neighbour4[9] =
1392 h->mb.i_neighbour4[12] =
1393 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
1396 void x264_macroblock_cache_save( x264_t *h )
1398 const int i_mb_xy = h->mb.i_mb_xy;
1399 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1400 const int s8x8 = h->mb.i_b8_stride;
1401 const int s4x4 = h->mb.i_b4_stride;
1402 const int i_mb_4x4 = h->mb.i_b4_xy;
1403 const int i_mb_8x8 = h->mb.i_b8_xy;
1407 for( i = 0; i < 3; i++ )
1410 int i_stride = h->fdec->i_stride[i];
1411 int i_stride2 = i_stride << h->mb.b_interlaced;
1412 int i_pix_offset = h->mb.b_interlaced
1413 ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1414 : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1415 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1416 &h->fdec->plane[i][i_pix_offset], i_stride2,
1417 h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1420 x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1422 h->mb.type[i_mb_xy] = i_mb_type;
1424 if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1425 h->mb.i_qp = h->mb.i_last_qp;
1426 h->mb.qp[i_mb_xy] = h->mb.i_qp;
1428 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1429 h->mb.i_last_qp = h->mb.i_qp;
1430 h->mb.i_mb_prev_xy = h->mb.i_mb_xy;
1433 if( i_mb_type == I_4x4 )
1435 h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1436 h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
1437 h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
1438 h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
1439 h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
1440 h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
1441 h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
1445 h->mb.intra4x4_pred_mode[i_mb_xy][0] =
1446 h->mb.intra4x4_pred_mode[i_mb_xy][1] =
1447 h->mb.intra4x4_pred_mode[i_mb_xy][2] =
1448 h->mb.intra4x4_pred_mode[i_mb_xy][3] =
1449 h->mb.intra4x4_pred_mode[i_mb_xy][4] =
1450 h->mb.intra4x4_pred_mode[i_mb_xy][5] =
1451 h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
1454 if( i_mb_type == I_PCM )
1456 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1457 for( i = 0; i < 16 + 2*4; i++ )
1459 h->mb.non_zero_count[i_mb_xy][i] = 16;
1464 /* save non zero count */
1465 for( i = 0; i < 16 + 2*4; i++ )
1467 h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
1471 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1472 h->mb.b_transform_8x8 = 0;
1473 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1475 if( !IS_INTRA( i_mb_type ) )
1478 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1482 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
1483 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
1484 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
1485 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
1487 for( y = 0; y < 4; y++ )
1489 for( x = 0; x < 4; x++ )
1491 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];
1492 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];
1500 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1504 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
1505 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
1506 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
1507 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
1509 for( y = 0; y < 4; y++ )
1511 for( x = 0; x < 4; x++ )
1513 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1514 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1520 if( h->param.b_cabac )
1522 if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1523 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1525 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1527 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1530 for( i_list = 0; i_list < 2; i_list++ )
1532 const int s4x4 = 4 * h->mb.i_mb_stride;
1534 for( y = 0; y < 4; y++ )
1536 for( x = 0; x < 4; x++ )
1538 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];
1539 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];
1547 for( i_list = 0; i_list < 2; i_list++ )
1549 const int s4x4 = 4 * h->mb.i_mb_stride;
1551 for( y = 0; y < 4; y++ )
1553 for( x = 0; x < 4; x++ )
1555 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1556 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1561 if( h->sh.i_type == SLICE_TYPE_B )
1563 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1564 h->mb.skipbp[i_mb_xy] = 0xf;
1565 else if( i_mb_type == B_8x8 )
1568 for( i = 0; i < 4; i++ )
1569 skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1570 h->mb.skipbp[i_mb_xy] = skipbp;
1573 h->mb.skipbp[i_mb_xy] = 0;
1578 void x264_macroblock_bipred_init( x264_t *h )
1581 for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1583 int poc0 = h->fref0[i_ref0]->i_poc;
1584 for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1586 int dist_scale_factor;
1587 int poc1 = h->fref1[i_ref1]->i_poc;
1588 int td = x264_clip3( poc1 - poc0, -128, 127 );
1589 if( td == 0 /* || pic0 is a long-term ref */ )
1590 dist_scale_factor = 256;
1593 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1594 int tx = (16384 + (abs(td) >> 1)) / td;
1595 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1597 h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1599 dist_scale_factor >>= 2;
1600 if( h->param.analyse.b_weighted_bipred
1601 && dist_scale_factor >= -64
1602 && dist_scale_factor <= 128 )
1603 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1605 h->mb.bipred_weight[i_ref0][i_ref1] = 32;
1610 for( i_ref0 = 2*h->i_ref0-1; i_ref0 >= 0; i_ref0-- )
1611 for( i_ref1 = 2*h->i_ref1-1; i_ref1 >= 0; i_ref1-- )
1612 h->mb.bipred_weight[i_ref0][i_ref1] = h->mb.bipred_weight[i_ref0>>1][i_ref1>>1];