1 /*****************************************************************************
2 * analyse.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003 Laurent Aimar
5 * $Id: analyse.c,v 1.1 2004/06/03 19:27:08 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 *****************************************************************************/
30 #include "common/common.h"
31 #include "common/macroblock.h"
32 #include "macroblock.h"
34 #include "ratecontrol.h"
47 int i_cost4x4[4]; /* cost per 8x8 partition */
48 x264_me_t me4x4[4][4];
51 int i_cost8x4[4]; /* cost per 8x8 partition */
52 x264_me_t me8x4[4][2];
55 int i_cost4x8[4]; /* cost per 8x8 partition */
56 x264_me_t me4x8[4][4];
66 } x264_mb_analysis_list_t;
70 /* conduct the analysis using this lamda and QP */
77 /* Luma part 16x16 and 4x4 modes stats */
82 int i_predict4x4[4][4];
88 /* II: Inter part P/B frame */
89 x264_mb_analysis_list_t l0;
90 x264_mb_analysis_list_t l1;
92 int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
93 int i_cost16x16direct;
95 int i_cost8x8direct[4];
99 int i_mb_partition16x8[2]; /* mb_partition_e */
100 int i_mb_partition8x16[2];
101 int i_mb_type16x8; /* mb_class_e */
104 int b_direct_available;
106 } x264_mb_analysis_t;
108 static const int i_qp0_cost_table[52] = {
109 1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
110 1, 1, 1, 1, /* 8-11 */
111 1, 1, 1, 1, 2, 2, 2, 2, /* 12-19 */
112 3, 3, 3, 4, 4, 4, 5, 6, /* 20-27 */
113 6, 7, 8, 9,10,11,13,14, /* 28-35 */
114 16,18,20,23,25,29,32,36, /* 36-43 */
115 40,45,51,57,64,72,81,91 /* 44-51 */
118 static const uint8_t block_idx_x[16] = {
119 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
121 static const uint8_t block_idx_y[16] = {
122 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
125 /* TODO: calculate CABAC costs */
126 static const int i_mb_b_cost_table[18] = {
127 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
129 static const int i_mb_b16x8_cost_table[16] = {
130 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
132 static const int i_sub_mb_b_cost_table[13] = {
133 7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
135 static const int i_sub_mb_p_cost_table[4] = {
139 /* initialize an array of lambda*nbits for all possible mvs */
140 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
142 static int16_t *p_cost_mv[52];
144 if( !p_cost_mv[a->i_qp] )
146 /* could be faster, but isn't called many times */
147 /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
149 p_cost_mv[a->i_qp] = x264_malloc( (4*4*h->param.analyse.i_mv_range + 1) * sizeof(int16_t) );
150 p_cost_mv[a->i_qp] += 2*4*h->param.analyse.i_mv_range;
151 for( i = 0; i <= 2*4*h->param.analyse.i_mv_range; i++ )
153 p_cost_mv[a->i_qp][-i] =
154 p_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i );
158 a->p_cost_mv = p_cost_mv[a->i_qp];
161 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
163 memset( a, 0, sizeof( x264_mb_analysis_t ) );
165 /* conduct the analysis using this lamda and QP */
167 a->i_lambda = i_qp0_cost_table[i_qp];
169 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
170 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
171 && h->mb.i_subpel_refine >= 5;
176 a->i_sad_i8x8 = COST_MAX;
178 /* II: Inter part P/B frame */
179 if( h->sh.i_type != SLICE_TYPE_I )
182 int i_fmv_range = h->param.analyse.i_mv_range - 16;
184 /* Calculate max allowed MV range */
185 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range )
186 h->mb.mv_min_fpel[0] = CLIP_FMV( -16*h->mb.i_mb_x - 8 );
187 h->mb.mv_max_fpel[0] = CLIP_FMV( 16*( h->sps->i_mb_width - h->mb.i_mb_x ) - 8 );
188 h->mb.mv_min[0] = 4*( h->mb.mv_min_fpel[0] - 16 );
189 h->mb.mv_max[0] = 4*( h->mb.mv_max_fpel[0] + 16 );
190 if( h->mb.i_mb_x == 0)
192 h->mb.mv_min_fpel[1] = CLIP_FMV( -16*h->mb.i_mb_y - 8 );
193 h->mb.mv_max_fpel[1] = CLIP_FMV( 16*( h->sps->i_mb_height - h->mb.i_mb_y ) - 8 );
194 h->mb.mv_min[1] = 4*( h->mb.mv_min_fpel[1] - 16 );
195 h->mb.mv_max[1] = 4*( h->mb.mv_max_fpel[1] + 16 );
200 a->l0.i_cost8x8 = COST_MAX;
202 for( i = 0; i < 4; i++ )
206 a->l0.i_cost4x8[i] = COST_MAX;
210 a->l0.i_cost8x16 = COST_MAX;
211 if( h->sh.i_type == SLICE_TYPE_B )
214 a->l1.i_cost8x8 = COST_MAX;
216 for( i = 0; i < 4; i++ )
221 a->i_cost8x8direct[i] = COST_MAX;
228 a->i_cost16x16direct =
231 a->i_cost8x16bi = COST_MAX;
242 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
244 if( ( i_neighbour & (MB_LEFT|MB_TOP) ) == (MB_LEFT|MB_TOP) )
246 /* top and left avaible */
247 *mode++ = I_PRED_16x16_V;
248 *mode++ = I_PRED_16x16_H;
249 *mode++ = I_PRED_16x16_DC;
250 *mode++ = I_PRED_16x16_P;
253 else if( ( i_neighbour & MB_LEFT ) )
256 *mode++ = I_PRED_16x16_DC_LEFT;
257 *mode++ = I_PRED_16x16_H;
260 else if( ( i_neighbour & MB_TOP ) )
263 *mode++ = I_PRED_16x16_DC_TOP;
264 *mode++ = I_PRED_16x16_V;
270 *mode = I_PRED_16x16_DC_128;
276 static void predict_8x8_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
278 if( ( i_neighbour & (MB_LEFT|MB_TOP) ) == (MB_LEFT|MB_TOP) )
280 /* top and left avaible */
281 *mode++ = I_PRED_CHROMA_V;
282 *mode++ = I_PRED_CHROMA_H;
283 *mode++ = I_PRED_CHROMA_DC;
284 *mode++ = I_PRED_CHROMA_P;
287 else if( ( i_neighbour & MB_LEFT ) )
290 *mode++ = I_PRED_CHROMA_DC_LEFT;
291 *mode++ = I_PRED_CHROMA_H;
294 else if( ( i_neighbour & MB_TOP ) )
297 *mode++ = I_PRED_CHROMA_DC_TOP;
298 *mode++ = I_PRED_CHROMA_V;
304 *mode = I_PRED_CHROMA_DC_128;
310 static void predict_4x4_mode_available( unsigned int i_neighbour, int idx, int *mode, int *pi_count )
313 static const unsigned int needmb[16] =
315 MB_LEFT|MB_TOP, MB_TOP,
317 MB_TOP, MB_TOP|MB_TOPRIGHT,
325 /* FIXME even when b_c == 0 there is some case where missing pixels
326 * are emulated and thus more mode are available TODO
327 * analysis and encode should be fixed too */
328 b_a = (needmb[idx]&i_neighbour&MB_LEFT) == (needmb[idx]&MB_LEFT);
329 b_b = (needmb[idx]&i_neighbour&MB_TOP) == (needmb[idx]&MB_TOP);
330 b_c = (needmb[idx]&i_neighbour&(MB_TOPRIGHT|MB_PRIVATE)) == (needmb[idx]&(MB_TOPRIGHT|MB_PRIVATE));
334 *mode++ = I_PRED_4x4_DC;
335 *mode++ = I_PRED_4x4_H;
336 *mode++ = I_PRED_4x4_V;
337 *mode++ = I_PRED_4x4_DDR;
338 *mode++ = I_PRED_4x4_VR;
339 *mode++ = I_PRED_4x4_HD;
340 *mode++ = I_PRED_4x4_HU;
346 *mode++ = I_PRED_4x4_DDL;
347 *mode++ = I_PRED_4x4_VL;
351 else if( b_a && !b_b )
353 *mode++ = I_PRED_4x4_DC_LEFT;
354 *mode++ = I_PRED_4x4_H;
355 *mode++ = I_PRED_4x4_HU;
358 else if( !b_a && b_b )
360 *mode++ = I_PRED_4x4_DC_TOP;
361 *mode++ = I_PRED_4x4_V;
366 *mode++ = I_PRED_4x4_DC_128;
371 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *res )
373 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
374 const int i_stride = h->mb.pic.i_stride[0];
375 uint8_t *p_src = h->mb.pic.p_fenc[0];
376 uint8_t *p_dst = h->mb.pic.p_fdec[0];
383 /*---------------- Try all mode and calculate their score ---------------*/
385 /* 16x16 prediction selection */
386 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
387 for( i = 0; i < i_max; i++ )
392 i_mode = predict_mode[i];
394 /* we do the prediction */
395 h->predict_16x16[i_mode]( p_dst, i_stride );
397 /* we calculate the diff and get the square sum of the diff */
398 i_sad = h->pixf.satd[PIXEL_16x16]( p_dst, i_stride, p_src, i_stride ) +
399 res->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
400 /* if i_score is lower it is better */
401 if( res->i_sad_i16x16 > i_sad )
403 res->i_predict16x16 = i_mode;
404 res->i_sad_i16x16 = i_sad;
407 /* cavlc mb type prefix */
408 if( h->sh.i_type == SLICE_TYPE_B )
409 res->i_sad_i16x16 += res->i_lambda * i_mb_b_cost_table[I_16x16];
411 /* 4x4 prediction selection */
412 if( flags & X264_ANALYSE_I4x4 )
415 for( idx = 0; idx < 16; idx++ )
423 i_pred_mode= x264_mb_predict_intra4x4_mode( h, idx );
424 x = block_idx_x[idx];
425 y = block_idx_y[idx];
427 p_src_by = p_src + 4 * x + 4 * y * i_stride;
428 p_dst_by = p_dst + 4 * x + 4 * y * i_stride;
431 predict_4x4_mode_available( h->mb.i_neighbour, idx, predict_mode, &i_max );
432 for( i = 0; i < i_max; i++ )
437 i_mode = predict_mode[i];
439 /* we do the prediction */
440 h->predict_4x4[i_mode]( p_dst_by, i_stride );
442 /* we calculate diff and get the square sum of the diff */
443 i_sad = h->pixf.satd[PIXEL_4x4]( p_dst_by, i_stride,
444 p_src_by, i_stride );
446 i_sad += res->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix[i_mode] ? 1 : 4);
448 /* if i_score is lower it is better */
451 res->i_predict4x4[x][y] = i_mode;
455 res->i_sad_i4x4 += i_best;
457 /* we need to encode this mb now (for next ones) */
458 h->predict_4x4[res->i_predict4x4[x][y]]( p_dst_by, i_stride );
459 x264_mb_encode_i4x4( h, idx, res->i_qp );
461 /* we need to store the 'fixed' version */
462 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] =
463 x264_mb_pred_mode4x4_fix[res->i_predict4x4[x][y]];
465 res->i_sad_i4x4 += res->i_lambda * 24; /* from JVT (SATD0) */
466 if( h->sh.i_type == SLICE_TYPE_B )
467 res->i_sad_i4x4 += res->i_lambda * i_mb_b_cost_table[I_4x4];
471 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *res )
478 uint8_t *p_dstc[2], *p_srcc[2];
481 if( res->i_sad_i8x8 < COST_MAX )
484 /* 8x8 prediction selection for chroma */
485 p_dstc[0] = h->mb.pic.p_fdec[1];
486 p_dstc[1] = h->mb.pic.p_fdec[2];
487 p_srcc[0] = h->mb.pic.p_fenc[1];
488 p_srcc[1] = h->mb.pic.p_fenc[2];
490 i_stride[0] = h->mb.pic.i_stride[1];
491 i_stride[1] = h->mb.pic.i_stride[2];
493 predict_8x8_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
494 res->i_sad_i8x8 = COST_MAX;
495 for( i = 0; i < i_max; i++ )
500 i_mode = predict_mode[i];
502 /* we do the prediction */
503 h->predict_8x8[i_mode]( p_dstc[0], i_stride[0] );
504 h->predict_8x8[i_mode]( p_dstc[1], i_stride[1] );
506 /* we calculate the cost */
507 i_sad = h->pixf.satd[PIXEL_8x8]( p_dstc[0], i_stride[0],
508 p_srcc[0], i_stride[0] ) +
509 h->pixf.satd[PIXEL_8x8]( p_dstc[1], i_stride[1],
510 p_srcc[1], i_stride[1] ) +
511 res->i_lambda * bs_size_ue( x264_mb_pred_mode8x8_fix[i_mode] );
513 /* if i_score is lower it is better */
514 if( res->i_sad_i8x8 > i_sad )
516 res->i_predict8x8 = i_mode;
517 res->i_sad_i8x8 = i_sad;
522 #define LOAD_FENC( m, src, xoff, yoff) \
523 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
524 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
525 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
526 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
527 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]];
528 #define LOAD_HPELS(m, src, xoff, yoff) \
529 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
530 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
531 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
532 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
533 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
534 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]];
536 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
540 int mvc[4][2], i_mvc;
541 int i_fullpel_thresh = INT_MAX;
542 int *p_fullpel_thresh = h->i_ref0>1 ? &i_fullpel_thresh : NULL;
544 /* 16x16 Search on all ref frame */
545 m.i_pixel = PIXEL_16x16;
546 m.p_cost_mv = a->p_cost_mv;
547 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
549 a->l0.me16x16.cost = INT_MAX;
550 for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
552 const int i_ref_cost = a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, i_ref );
553 i_fullpel_thresh -= i_ref_cost;
555 /* search with ref */
556 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, 0 );
557 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
558 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
559 x264_me_search_ref( h, &m, mvc, i_mvc, p_fullpel_thresh );
561 m.cost += i_ref_cost;
562 i_fullpel_thresh += i_ref_cost;
564 if( m.cost < a->l0.me16x16.cost )
570 /* save mv for predicting neighbors */
571 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
572 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
575 /* subtract ref cost, so we don't have to add it for the other P types */
576 a->l0.me16x16.cost -= a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, a->l0.i_ref );
578 /* Set global ref, needed for all others modes */
579 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
582 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
584 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.i_ref];
585 uint8_t **p_fenc = h->mb.pic.p_fenc;
586 int mvc[5][2], i_mvc;
589 /* XXX Needed for x264_mb_predict_mv */
590 h->mb.i_partition = D_8x8;
593 mvc[0][0] = a->l0.me16x16.mv[0];
594 mvc[0][1] = a->l0.me16x16.mv[1];
596 for( i = 0; i < 4; i++ )
598 x264_me_t *m = &a->l0.me8x8[i];
602 m->i_pixel = PIXEL_8x8;
603 m->p_cost_mv = a->p_cost_mv;
605 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
606 LOAD_HPELS( m, p_fref, 8*x8, 8*y8 );
608 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
609 x264_me_search( h, m, mvc, i_mvc );
611 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
613 mvc[i_mvc][0] = m->mv[0];
614 mvc[i_mvc][1] = m->mv[1];
618 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
621 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
622 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
625 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
627 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.i_ref];
628 uint8_t **p_fenc = h->mb.pic.p_fenc;
632 /* XXX Needed for x264_mb_predict_mv */
633 h->mb.i_partition = D_16x8;
635 for( i = 0; i < 2; i++ )
637 x264_me_t *m = &a->l0.me16x8[i];
639 m->i_pixel = PIXEL_16x8;
640 m->p_cost_mv = a->p_cost_mv;
642 LOAD_FENC( m, p_fenc, 0, 8*i );
643 LOAD_HPELS( m, p_fref, 0, 8*i );
645 mvc[0][0] = a->l0.me8x8[2*i].mv[0];
646 mvc[0][1] = a->l0.me8x8[2*i].mv[1];
647 mvc[1][0] = a->l0.me8x8[2*i+1].mv[0];
648 mvc[1][1] = a->l0.me8x8[2*i+1].mv[1];
650 x264_mb_predict_mv( h, 0, 8*i, 4, m->mvp );
651 x264_me_search( h, m, mvc, 2 );
653 x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, m->mv[0], m->mv[1] );
656 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
659 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
661 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.i_ref];
662 uint8_t **p_fenc = h->mb.pic.p_fenc;
666 /* XXX Needed for x264_mb_predict_mv */
667 h->mb.i_partition = D_8x16;
669 for( i = 0; i < 2; i++ )
671 x264_me_t *m = &a->l0.me8x16[i];
673 m->i_pixel = PIXEL_8x16;
674 m->p_cost_mv = a->p_cost_mv;
676 LOAD_FENC( m, p_fenc, 8*i, 0 );
677 LOAD_HPELS( m, p_fref, 8*i, 0 );
679 mvc[0][0] = a->l0.me8x8[i].mv[0];
680 mvc[0][1] = a->l0.me8x8[i].mv[1];
681 mvc[1][0] = a->l0.me8x8[i+2].mv[0];
682 mvc[1][1] = a->l0.me8x8[i+2].mv[1];
684 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
685 x264_me_search( h, m, mvc, 2 );
687 x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, m->mv[0], m->mv[1] );
690 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
693 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
695 uint8_t pix1[8*8], pix2[8*8];
696 const int i_stride = h->mb.pic.i_stride[1];
697 const int off = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
699 #define CHROMA4x4MC( width, height, me, x, y ) \
700 h->mc.mc_chroma( &p_fref[4][off+x+y*i_stride], i_stride, &pix1[x+y*8], 8, (me).mv[0], (me).mv[1], width, height ); \
701 h->mc.mc_chroma( &p_fref[5][off+x+y*i_stride], i_stride, &pix2[x+y*8], 8, (me).mv[0], (me).mv[1], width, height );
703 if( pixel == PIXEL_4x4 )
705 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
706 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 0,2 );
707 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 2,0 );
708 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
710 else if( pixel == PIXEL_8x4 )
712 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
713 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
717 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
718 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
721 return h->pixf.satd[PIXEL_4x4]( &h->mb.pic.p_fenc[1][off], i_stride, pix1, 8 )
722 + h->pixf.satd[PIXEL_4x4]( &h->mb.pic.p_fenc[2][off], i_stride, pix2, 8 );
725 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
727 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.i_ref];
728 uint8_t **p_fenc = h->mb.pic.p_fenc;
732 /* XXX Needed for x264_mb_predict_mv */
733 h->mb.i_partition = D_8x8;
735 for( i4x4 = 0; i4x4 < 4; i4x4++ )
737 const int idx = 4*i8x8 + i4x4;
738 const int x4 = block_idx_x[idx];
739 const int y4 = block_idx_y[idx];
740 const int i_mvc = (i4x4 == 0);
742 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
744 m->i_pixel = PIXEL_4x4;
745 m->p_cost_mv = a->p_cost_mv;
747 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
748 LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
750 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
751 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
753 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
756 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
757 a->l0.me4x4[i8x8][1].cost +
758 a->l0.me4x4[i8x8][2].cost +
759 a->l0.me4x4[i8x8][3].cost +
760 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
761 if( h->mb.b_chroma_me )
762 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
765 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
767 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.i_ref];
768 uint8_t **p_fenc = h->mb.pic.p_fenc;
772 /* XXX Needed for x264_mb_predict_mv */
773 h->mb.i_partition = D_8x8;
775 for( i8x4 = 0; i8x4 < 2; i8x4++ )
777 const int idx = 4*i8x8 + 2*i8x4;
778 const int x4 = block_idx_x[idx];
779 const int y4 = block_idx_y[idx];
780 const int i_mvc = (i8x4 == 0);
782 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
784 m->i_pixel = PIXEL_8x4;
785 m->p_cost_mv = a->p_cost_mv;
787 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
788 LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
790 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
791 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
793 x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
796 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
797 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
798 if( h->mb.b_chroma_me )
799 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
802 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
804 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.i_ref];
805 uint8_t **p_fenc = h->mb.pic.p_fenc;
809 /* XXX Needed for x264_mb_predict_mv */
810 h->mb.i_partition = D_8x8;
812 for( i4x8 = 0; i4x8 < 2; i4x8++ )
814 const int idx = 4*i8x8 + i4x8;
815 const int x4 = block_idx_x[idx];
816 const int y4 = block_idx_y[idx];
817 const int i_mvc = (i4x8 == 0);
819 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
821 m->i_pixel = PIXEL_4x8;
822 m->p_cost_mv = a->p_cost_mv;
824 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
825 LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
827 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
828 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
830 x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
833 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
834 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
835 if( h->mb.b_chroma_me )
836 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
839 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
841 /* Assumes that fdec still contains the results of
842 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
844 uint8_t **p_fenc = h->mb.pic.p_fenc;
845 uint8_t **p_fdec = h->mb.pic.p_fdec;
846 int i_stride= h->mb.pic.i_stride[0];
849 a->i_cost16x16direct = 0;
850 for( i = 0; i < 4; i++ )
854 const int off = 8 * x8 + 8 * i_stride * y8;
855 a->i_cost16x16direct +=
856 a->i_cost8x8direct[i] =
857 h->pixf.satd[PIXEL_8x8]( &p_fenc[0][off], i_stride, &p_fdec[0][off], i_stride );
860 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
863 a->i_cost16x16direct += a->i_lambda * i_mb_b_cost_table[B_DIRECT];
866 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
868 if( h->param.analyse.b_weighted_bipred ) \
869 h->pixf.avg_weight[size]( pix1, stride1, src2, stride2, \
870 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
872 h->pixf.avg[size]( pix1, stride1, src2, stride2 ); \
875 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
877 uint8_t pix1[16*16], pix2[16*16];
880 int src2_ref, pix1_ref;
884 int mvc[5][2], i_mvc;
885 int i_fullpel_thresh = INT_MAX;
886 int *p_fullpel_thresh = h->i_ref0>1 ? &i_fullpel_thresh : NULL;
888 /* 16x16 Search on all ref frame */
889 m.i_pixel = PIXEL_16x16;
890 m.p_cost_mv = a->p_cost_mv;
891 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
894 a->l0.me16x16.cost = INT_MAX;
895 for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
897 /* search with ref */
898 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, 0 );
899 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
900 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
901 x264_me_search_ref( h, &m, mvc, i_mvc, p_fullpel_thresh );
904 m.cost += a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, i_ref );
906 if( m.cost < a->l0.me16x16.cost )
912 /* save mv for predicting neighbors */
913 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
914 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
916 /* subtract ref cost, so we don't have to add it for the other MB types */
917 a->l0.me16x16.cost -= a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, a->l0.i_ref );
920 i_fullpel_thresh = INT_MAX;
921 p_fullpel_thresh = h->i_ref1>1 ? &i_fullpel_thresh : NULL;
922 a->l1.me16x16.cost = INT_MAX;
923 for( i_ref = 0; i_ref < h->i_ref1; i_ref++ )
925 /* search with ref */
926 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 0, 0 );
927 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
928 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
929 x264_me_search_ref( h, &m, mvc, i_mvc, p_fullpel_thresh );
932 m.cost += a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l1_active - 1, i_ref );
934 if( m.cost < a->l1.me16x16.cost )
940 /* save mv for predicting neighbors */
941 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
942 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
944 /* subtract ref cost, so we don't have to add it for the other MB types */
945 a->l1.me16x16.cost -= a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l1_active - 1, a->l1.i_ref );
947 /* Set global ref, needed for other modes? */
948 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
949 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
951 /* get cost of BI mode */
952 if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
954 /* l0 reference is halfpel, so get_ref on it will make it faster */
955 src2 = h->mc.get_ref( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
957 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
959 h->mc.mc_luma( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
961 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
963 src2_ref = a->l0.i_ref;
964 pix1_ref = a->l1.i_ref;
968 /* if l0 was qpel, we'll use get_ref on l1 instead */
969 h->mc.mc_luma( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
971 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
973 src2 = h->mc.get_ref( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
975 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
977 src2_ref = a->l1.i_ref;
978 pix1_ref = a->l0.i_ref;
981 if( h->param.analyse.b_weighted_bipred )
982 h->pixf.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2,
983 h->mb.bipred_weight[pix1_ref][src2_ref] );
985 h->pixf.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
987 a->i_cost16x16bi = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0], pix1, 16 )
988 + a->i_lambda * ( bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, a->l0.i_ref )
989 + bs_size_te( h->sh.i_num_ref_idx_l1_active - 1, a->l1.i_ref ) )
990 + a->l0.me16x16.cost_mv
991 + a->l1.me16x16.cost_mv;
994 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
995 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
996 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
999 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1000 if( x264_mb_partition_listX_table[0][part] ) \
1002 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1003 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
1007 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1008 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0, 0 ); \
1010 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
1012 if( x264_mb_partition_listX_table[1][part] ) \
1014 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1015 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
1019 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1020 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0, 0 ); \
1022 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
1025 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1029 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1031 x264_mb_load_mv_direct8x8( h, i );
1034 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0, 0 );
1035 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0, 0 );
1036 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1041 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1044 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1046 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1048 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1050 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1054 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1056 uint8_t **p_fref[2] =
1057 { h->mb.pic.p_fref[0][a->l0.i_ref],
1058 h->mb.pic.p_fref[1][a->l1.i_ref] };
1059 uint8_t pix[2][8*8];
1062 /* XXX Needed for x264_mb_predict_mv */
1063 h->mb.i_partition = D_8x8;
1067 for( i = 0; i < 4; i++ )
1072 int i_part_cost_bi = 0;
1074 for( l = 0; l < 2; l++ )
1076 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1077 x264_me_t *m = &lX->me8x8[i];
1079 m->i_pixel = PIXEL_8x8;
1080 m->p_cost_mv = a->p_cost_mv;
1082 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1083 LOAD_HPELS( m, p_fref[l], 8*x8, 8*y8 );
1085 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1086 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1088 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
1091 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1092 m->mv[0], m->mv[1], 8, 8 );
1093 i_part_cost_bi += m->cost_mv;
1094 /* FIXME: ref cost */
1097 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1098 i_part_cost_bi += h->pixf.satd[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], h->mb.pic.i_stride[0], pix[0], 8 )
1099 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1100 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1101 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1103 i_part_cost = a->l0.me8x8[i].cost;
1104 h->mb.i_sub_partition[i] = D_L0_8x8;
1105 if( a->l1.me8x8[i].cost < i_part_cost )
1107 i_part_cost = a->l1.me8x8[i].cost;
1108 h->mb.i_sub_partition[i] = D_L1_8x8;
1110 if( i_part_cost_bi < i_part_cost )
1112 i_part_cost = i_part_cost_bi;
1113 h->mb.i_sub_partition[i] = D_BI_8x8;
1115 if( a->i_cost8x8direct[i] < i_part_cost )
1117 i_part_cost = a->i_cost8x8direct[i];
1118 h->mb.i_sub_partition[i] = D_DIRECT_8x8;
1120 a->i_cost8x8bi += i_part_cost;
1122 /* XXX Needed for x264_mb_predict_mv */
1123 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1127 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1130 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1132 uint8_t **p_fref[2] =
1133 { h->mb.pic.p_fref[0][a->l0.i_ref],
1134 h->mb.pic.p_fref[1][a->l1.i_ref] };
1135 uint8_t pix[2][16*8];
1139 h->mb.i_partition = D_16x8;
1140 a->i_cost16x8bi = 0;
1142 for( i = 0; i < 2; i++ )
1145 int i_part_cost_bi = 0;
1147 /* TODO: check only the list(s) that were used in b8x8? */
1148 for( l = 0; l < 2; l++ )
1150 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1151 x264_me_t *m = &lX->me16x8[i];
1153 m->i_pixel = PIXEL_16x8;
1154 m->p_cost_mv = a->p_cost_mv;
1156 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1157 LOAD_HPELS( m, p_fref[l], 0, 8*i );
1159 mvc[0][0] = lX->me8x8[2*i].mv[0];
1160 mvc[0][1] = lX->me8x8[2*i].mv[1];
1161 mvc[1][0] = lX->me8x8[2*i+1].mv[0];
1162 mvc[1][1] = lX->me8x8[2*i+1].mv[1];
1164 x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp );
1165 x264_me_search( h, m, mvc, 2 );
1168 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 16,
1169 m->mv[0], m->mv[1], 16, 8 );
1170 /* FIXME: ref cost */
1171 i_part_cost_bi += m->cost_mv;
1174 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1175 i_part_cost_bi += h->pixf.satd[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], h->mb.pic.i_stride[0], pix[0], 16 );
1177 i_part_cost = a->l0.me16x8[i].cost;
1178 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1179 if( a->l1.me16x8[i].cost < i_part_cost )
1181 i_part_cost = a->l1.me16x8[i].cost;
1182 a->i_mb_partition16x8[i] = D_L1_8x8;
1184 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1186 i_part_cost = i_part_cost_bi;
1187 a->i_mb_partition16x8[i] = D_BI_8x8;
1189 a->i_cost16x8bi += i_part_cost;
1192 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1196 a->i_mb_type16x8 = B_L0_L0
1197 + (a->i_mb_partition16x8[0]>>2) * 3
1198 + (a->i_mb_partition16x8[1]>>2);
1199 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1201 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1203 uint8_t **p_fref[2] =
1204 { h->mb.pic.p_fref[0][a->l0.i_ref],
1205 h->mb.pic.p_fref[1][a->l1.i_ref] };
1206 uint8_t pix[2][8*16];
1210 h->mb.i_partition = D_8x16;
1211 a->i_cost8x16bi = 0;
1213 for( i = 0; i < 2; i++ )
1216 int i_part_cost_bi = 0;
1218 for( l = 0; l < 2; l++ )
1220 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1221 x264_me_t *m = &lX->me8x16[i];
1223 m->i_pixel = PIXEL_8x16;
1224 m->p_cost_mv = a->p_cost_mv;
1226 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1227 LOAD_HPELS( m, p_fref[l], 8*i, 0 );
1229 mvc[0][0] = lX->me8x8[i].mv[0];
1230 mvc[0][1] = lX->me8x8[i].mv[1];
1231 mvc[1][0] = lX->me8x8[i+2].mv[0];
1232 mvc[1][1] = lX->me8x8[i+2].mv[1];
1234 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1235 x264_me_search( h, m, mvc, 2 );
1238 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1239 m->mv[0], m->mv[1], 8, 16 );
1240 /* FIXME: ref cost */
1241 i_part_cost_bi += m->cost_mv;
1244 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1245 i_part_cost_bi += h->pixf.satd[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], h->mb.pic.i_stride[0], pix[0], 8 );
1247 i_part_cost = a->l0.me8x16[i].cost;
1248 a->i_mb_partition8x16[i] = D_L0_8x8;
1249 if( a->l1.me8x16[i].cost < i_part_cost )
1251 i_part_cost = a->l1.me8x16[i].cost;
1252 a->i_mb_partition8x16[i] = D_L1_8x8;
1254 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1256 i_part_cost = i_part_cost_bi;
1257 a->i_mb_partition8x16[i] = D_BI_8x8;
1259 a->i_cost8x16bi += i_part_cost;
1262 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1266 a->i_mb_type8x16 = B_L0_L0
1267 + (a->i_mb_partition8x16[0]>>2) * 3
1268 + (a->i_mb_partition8x16[1]>>2);
1269 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1272 /*****************************************************************************
1273 * x264_macroblock_analyse:
1274 *****************************************************************************/
1275 void x264_macroblock_analyse( x264_t *h )
1277 x264_mb_analysis_t analysis;
1280 h->mb.qp[h->mb.i_mb_xy] = x264_ratecontrol_qp(h);
1282 /* prevent QP from varying too fast. FIXME what's a sane limit? */
1283 h->mb.qp[h->mb.i_mb_xy] = x264_clip3( h->mb.qp[h->mb.i_mb_xy],
1284 h->mb.i_last_qp - 12, h->mb.i_last_qp + 12 );
1287 x264_mb_analyse_init( h, &analysis, h->mb.qp[h->mb.i_mb_xy] );
1289 /*--------------------------- Do the analysis ---------------------------*/
1290 if( h->sh.i_type == SLICE_TYPE_I )
1292 x264_mb_analyse_intra( h, &analysis );
1294 if( analysis.i_sad_i4x4 < analysis.i_sad_i16x16 )
1295 h->mb.i_type = I_4x4;
1297 h->mb.i_type = I_16x16;
1299 else if( h->sh.i_type == SLICE_TYPE_P )
1301 const unsigned int i_neighbour = h->mb.i_neighbour;
1305 int i_intra_cost, i_intra_type;
1307 /* Fast P_SKIP detection */
1308 if( ( (i_neighbour&MB_LEFT) && h->mb.type[h->mb.i_mb_xy - 1] == P_SKIP ) ||
1309 ( (i_neighbour&MB_TOP) && h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride] == P_SKIP ) ||
1310 ( ((i_neighbour&(MB_TOP|MB_LEFT)) == (MB_TOP|MB_LEFT) ) && h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride-1 ] == P_SKIP ) ||
1311 ( (i_neighbour&MB_TOPRIGHT) && h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride+1 ] == P_SKIP ) )
1313 b_skip = x264_macroblock_probe_pskip( h );
1318 h->mb.i_type = P_SKIP;
1319 h->mb.i_partition = D_16x16;
1323 const unsigned int flags = h->param.analyse.inter;
1327 x264_mb_analyse_load_costs( h, &analysis );
1329 x264_mb_analyse_inter_p16x16( h, &analysis );
1330 if( flags & X264_ANALYSE_PSUB16x16 )
1331 x264_mb_analyse_inter_p8x8( h, &analysis );
1333 /* Select best inter mode */
1335 i_partition = D_16x16;
1336 i_cost = analysis.l0.me16x16.cost;
1338 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
1339 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
1344 i_partition = D_8x8;
1345 h->mb.i_sub_partition[0] = D_L0_8x8;
1346 h->mb.i_sub_partition[1] = D_L0_8x8;
1347 h->mb.i_sub_partition[2] = D_L0_8x8;
1348 h->mb.i_sub_partition[3] = D_L0_8x8;
1350 i_cost = analysis.l0.i_cost8x8;
1353 if( flags & X264_ANALYSE_PSUB8x8 )
1355 for( i = 0; i < 4; i++ )
1357 x264_mb_analyse_inter_p4x4( h, &analysis, i );
1358 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
1362 h->mb.i_sub_partition[i] = D_L0_4x4;
1363 i_cost8x8 = analysis.l0.i_cost4x4[i];
1365 x264_mb_analyse_inter_p8x4( h, &analysis, i );
1366 if( analysis.l0.i_cost8x4[i] < analysis.l0.i_cost4x4[i] )
1368 h->mb.i_sub_partition[i] = D_L0_8x4;
1369 i_cost8x8 = analysis.l0.i_cost8x4[i];
1372 x264_mb_analyse_inter_p4x8( h, &analysis, i );
1373 if( analysis.l0.i_cost4x8[i] < analysis.l0.i_cost4x4[i] )
1375 h->mb.i_sub_partition[i] = D_L0_4x8;
1376 i_cost8x8 = analysis.l0.i_cost4x8[i];
1379 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
1384 /* Now do sub 16x8/8x16 */
1385 x264_mb_analyse_inter_p16x8( h, &analysis );
1386 if( analysis.l0.i_cost16x8 < i_cost )
1389 i_partition = D_16x8;
1390 i_cost = analysis.l0.i_cost16x8;
1393 x264_mb_analyse_inter_p8x16( h, &analysis );
1394 if( analysis.l0.i_cost8x16 < i_cost )
1397 i_partition = D_8x16;
1398 i_cost = analysis.l0.i_cost8x16;
1402 h->mb.i_type = i_type;
1403 h->mb.i_partition = i_partition;
1406 if( h->mb.i_partition == D_16x16 )
1408 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1409 i_cost = analysis.l0.me16x16.cost;
1411 else if( h->mb.i_partition == D_16x8 )
1413 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
1414 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
1415 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
1417 else if( h->mb.i_partition == D_8x16 )
1419 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
1420 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
1421 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
1423 else if( h->mb.i_partition == D_8x8 )
1427 for( i8x8 = 0; i8x8 < 4; i8x8++ )
1429 switch( h->mb.i_sub_partition[i8x8] )
1432 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
1433 i_cost += analysis.l0.me8x8[i8x8].cost;
1436 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
1437 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
1438 i_cost += analysis.l0.me8x4[i8x8][0].cost +
1439 analysis.l0.me8x4[i8x8][1].cost;
1442 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
1443 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
1444 i_cost += analysis.l0.me4x8[i8x8][0].cost +
1445 analysis.l0.me4x8[i8x8][1].cost;
1449 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
1450 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
1451 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
1452 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
1453 i_cost += analysis.l0.me4x4[i8x8][0].cost +
1454 analysis.l0.me4x4[i8x8][1].cost +
1455 analysis.l0.me4x4[i8x8][2].cost +
1456 analysis.l0.me4x4[i8x8][3].cost;
1459 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
1465 x264_mb_analyse_intra( h, &analysis );
1466 if( h->mb.b_chroma_me &&
1467 ( analysis.i_sad_i16x16 < i_cost
1468 || ( analysis.i_sad_i4x4 < i_cost )))
1470 x264_mb_analyse_intra_chroma( h, &analysis );
1471 analysis.i_sad_i16x16 += analysis.i_sad_i8x8;
1472 analysis.i_sad_i4x4 += analysis.i_sad_i8x8;
1475 i_intra_type = I_16x16;
1476 i_intra_cost = analysis.i_sad_i16x16;
1478 if( analysis.i_sad_i4x4 < i_intra_cost )
1480 i_intra_type = I_4x4;
1481 i_intra_cost = analysis.i_sad_i4x4;
1484 if( i_intra_cost < i_cost )
1486 h->mb.i_type = i_intra_type;
1487 i_cost = i_intra_cost;
1490 h->stat.frame.i_intra_cost += i_intra_cost;
1491 h->stat.frame.i_inter_cost += i_cost;
1494 else if( h->sh.i_type == SLICE_TYPE_B )
1498 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h );
1499 if( analysis.b_direct_available )
1501 h->mb.i_type = B_SKIP;
1504 /* Conditioning the probe on neighboring block types
1505 * doesn't seem to help speed or quality. */
1506 b_skip = x264_macroblock_probe_bskip( h );
1511 const unsigned int flags = h->param.analyse.inter;
1515 x264_mb_analyse_load_costs( h, &analysis );
1517 /* select best inter mode */
1518 /* direct must be first */
1519 if( analysis.b_direct_available )
1520 x264_mb_analyse_inter_direct( h, &analysis );
1522 x264_mb_analyse_inter_b16x16( h, &analysis );
1524 h->mb.i_type = B_L0_L0;
1525 i_partition = D_16x16;
1526 i_cost = analysis.l0.me16x16.cost;
1527 if( analysis.l1.me16x16.cost < i_cost )
1529 h->mb.i_type = B_L1_L1;
1530 i_cost = analysis.l1.me16x16.cost;
1532 if( analysis.i_cost16x16bi < i_cost )
1534 h->mb.i_type = B_BI_BI;
1535 i_cost = analysis.i_cost16x16bi;
1537 if( analysis.i_cost16x16direct < i_cost )
1539 h->mb.i_type = B_DIRECT;
1540 i_cost = analysis.i_cost16x16direct;
1543 if( flags & X264_ANALYSE_BSUB16x16 )
1545 x264_mb_analyse_inter_b8x8( h, &analysis );
1546 if( analysis.i_cost8x8bi < i_cost )
1548 h->mb.i_type = B_8x8;
1549 i_partition = D_8x8;
1550 i_cost = analysis.i_cost8x8bi;
1552 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
1553 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
1555 x264_mb_analyse_inter_b16x8( h, &analysis );
1556 if( analysis.i_cost16x8bi < i_cost )
1558 i_partition = D_16x8;
1559 i_cost = analysis.i_cost16x8bi;
1560 h->mb.i_type = analysis.i_mb_type16x8;
1563 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
1564 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
1566 x264_mb_analyse_inter_b8x16( h, &analysis );
1567 if( analysis.i_cost8x16bi < i_cost )
1569 i_partition = D_8x16;
1570 i_cost = analysis.i_cost8x16bi;
1571 h->mb.i_type = analysis.i_mb_type8x16;
1577 h->mb.i_partition = i_partition;
1580 if( i_partition == D_16x16 )
1582 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
1583 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
1584 if( h->mb.i_type == B_L0_L0 )
1586 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1587 i_cost = analysis.l0.me16x16.cost
1588 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
1590 else if( h->mb.i_type == B_L1_L1 )
1592 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
1593 i_cost = analysis.l1.me16x16.cost
1594 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
1596 else if( h->mb.i_type == B_BI_BI )
1598 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1599 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
1602 else if( i_partition == D_16x8 )
1604 for( i=0; i<2; i++ )
1606 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
1607 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
1608 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
1609 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
1612 else if( i_partition == D_8x16 )
1614 for( i=0; i<2; i++ )
1616 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
1617 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
1618 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
1619 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
1622 else if( i_partition == D_8x8 )
1624 for( i=0; i<4; i++ )
1627 int i_part_cost_old;
1629 int i_part_type = h->mb.i_sub_partition[i];
1630 int b_bidir = (i_part_type == D_BI_8x8);
1632 if( i_part_type == D_DIRECT_8x8 )
1634 if( x264_mb_partition_listX_table[0][i_part_type] )
1636 m = &analysis.l0.me8x8[i];
1637 i_part_cost_old = m->cost;
1638 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1639 m->cost -= i_type_cost;
1640 x264_me_refine_qpel( h, m );
1642 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
1644 if( x264_mb_partition_listX_table[1][i_part_type] )
1646 m = &analysis.l1.me8x8[i];
1647 i_part_cost_old = m->cost;
1648 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1649 m->cost -= i_type_cost;
1650 x264_me_refine_qpel( h, m );
1652 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
1654 /* TODO: update mvp? */
1658 /* best intra mode */
1659 x264_mb_analyse_intra( h, &analysis );
1661 if( analysis.i_sad_i16x16 < i_cost )
1663 h->mb.i_type = I_16x16;
1664 i_cost = analysis.i_sad_i16x16;
1666 if( analysis.i_sad_i4x4 < i_cost )
1668 h->mb.i_type = I_4x4;
1669 i_cost = analysis.i_sad_i4x4;
1674 /*-------------------- Update MB from the analysis ----------------------*/
1675 h->mb.type[h->mb.i_mb_xy] = h->mb.i_type;
1676 switch( h->mb.i_type )
1679 for( i = 0; i < 16; i++ )
1681 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] =
1682 analysis.i_predict4x4[block_idx_x[i]][block_idx_y[i]];
1685 x264_mb_analyse_intra_chroma( h, &analysis );
1686 h->mb.i_chroma_pred_mode = analysis.i_predict8x8;
1689 h->mb.i_intra16x16_pred_mode = analysis.i_predict16x16;
1691 x264_mb_analyse_intra_chroma( h, &analysis );
1692 h->mb.i_chroma_pred_mode = analysis.i_predict8x8;
1696 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.i_ref );
1697 switch( h->mb.i_partition )
1700 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.mv[0], analysis.l0.me16x16.mv[1] );
1704 x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].mv[0], analysis.l0.me16x8[0].mv[1] );
1705 x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].mv[0], analysis.l0.me16x8[1].mv[1] );
1709 x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].mv[0], analysis.l0.me8x16[0].mv[1] );
1710 x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].mv[0], analysis.l0.me8x16[1].mv[1] );
1714 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
1720 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.i_ref );
1721 for( i = 0; i < 4; i++ )
1723 const int x = 2*(i%2);
1724 const int y = 2*(i/2);
1726 switch( h->mb.i_sub_partition[i] )
1729 x264_macroblock_cache_mv( h, x, y, 2, 2, 0, analysis.l0.me8x8[i].mv[0], analysis.l0.me8x8[i].mv[1] );
1732 x264_macroblock_cache_mv( h, x, y+0, 2, 1, 0, analysis.l0.me8x4[i][0].mv[0], analysis.l0.me8x4[i][0].mv[1] );
1733 x264_macroblock_cache_mv( h, x, y+1, 2, 1, 0, analysis.l0.me8x4[i][1].mv[0], analysis.l0.me8x4[i][1].mv[1] );
1736 x264_macroblock_cache_mv( h, x+0, y, 1, 2, 0, analysis.l0.me4x8[i][0].mv[0], analysis.l0.me4x8[i][0].mv[1] );
1737 x264_macroblock_cache_mv( h, x+1, y, 1, 2, 0, analysis.l0.me4x8[i][1].mv[0], analysis.l0.me4x8[i][1].mv[1] );
1740 x264_macroblock_cache_mv( h, x+0, y+0, 1, 1, 0, analysis.l0.me4x4[i][0].mv[0], analysis.l0.me4x4[i][0].mv[1] );
1741 x264_macroblock_cache_mv( h, x+1, y+0, 1, 1, 0, analysis.l0.me4x4[i][1].mv[0], analysis.l0.me4x4[i][1].mv[1] );
1742 x264_macroblock_cache_mv( h, x+0, y+1, 1, 1, 0, analysis.l0.me4x4[i][2].mv[0], analysis.l0.me4x4[i][2].mv[1] );
1743 x264_macroblock_cache_mv( h, x+1, y+1, 1, 1, 0, analysis.l0.me4x4[i][3].mv[0], analysis.l0.me4x4[i][3].mv[1] );
1746 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1755 x264_mb_predict_mv_pskip( h, mvp );
1757 h->mb.i_partition = D_16x16;
1758 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
1759 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, mvp[0], mvp[1] );
1764 /* nothing has changed since x264_macroblock_probe_bskip */
1767 x264_mb_load_mv_direct8x8( h, 0 );
1768 x264_mb_load_mv_direct8x8( h, 1 );
1769 x264_mb_load_mv_direct8x8( h, 2 );
1770 x264_mb_load_mv_direct8x8( h, 3 );
1774 /* optimize: cache might not need to be rewritten */
1775 for( i = 0; i < 4; i++ )
1776 x264_mb_cache_mv_b8x8( h, &analysis, i, 1 );
1779 default: /* the rest of the B types */
1780 switch( h->mb.i_partition )
1783 switch( h->mb.i_type )
1786 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.i_ref );
1787 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.mv[0], analysis.l0.me16x16.mv[1] );
1789 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
1790 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0, 0 );
1791 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0, 0 );
1794 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
1795 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0, 0 );
1796 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0, 0 );
1798 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, analysis.l1.i_ref );
1799 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, analysis.l1.me16x16.mv[0], analysis.l1.me16x16.mv[1] );
1802 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.i_ref );
1803 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.mv[0], analysis.l0.me16x16.mv[1] );
1805 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, analysis.l1.i_ref );
1806 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, analysis.l1.me16x16.mv[0], analysis.l1.me16x16.mv[1] );
1811 x264_mb_cache_mv_b16x8( h, &analysis, 0, 1 );
1812 x264_mb_cache_mv_b16x8( h, &analysis, 1, 1 );
1815 x264_mb_cache_mv_b8x16( h, &analysis, 0, 1 );
1816 x264_mb_cache_mv_b8x16( h, &analysis, 1, 1 );
1819 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
1825 #include "slicetype_decision.c"