1 /*****************************************************************************
2 * analyse.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003 x264 project
5 * $Id: analyse.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Loren Merritt <lorenm@u.washington.edu>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
31 #include "common/common.h"
32 #include "macroblock.h"
34 #include "ratecontrol.h"
46 int mvc[16][5][2]; /* [ref][0] is 16x16 mv,
47 [ref][1..4] are 8x8 mv from partition [0..3] */
51 int i_cost4x4[4]; /* cost per 8x8 partition */
52 x264_me_t me4x4[4][4];
55 int i_cost8x4[4]; /* cost per 8x8 partition */
56 x264_me_t me8x4[4][2];
59 int i_cost4x8[4]; /* cost per 8x8 partition */
60 x264_me_t me4x8[4][4];
70 } x264_mb_analysis_list_t;
74 /* conduct the analysis using this lamda and QP */
83 /* Take some shortcuts in intra search if intra is deemed unlikely */
93 int i_predict8x8[2][2];
96 int i_predict4x4[4][4];
100 int i_predict8x8chroma;
102 /* II: Inter part P/B frame */
103 x264_mb_analysis_list_t l0;
104 x264_mb_analysis_list_t l1;
106 int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
107 int i_cost16x16direct;
109 int i_cost8x8direct[4];
113 int i_mb_partition16x8[2]; /* mb_partition_e */
114 int i_mb_partition8x16[2];
115 int i_mb_type16x8; /* mb_class_e */
118 int b_direct_available;
120 } x264_mb_analysis_t;
122 /* lambda = pow(2,qp/6-2) */
123 static const int i_qp0_cost_table[52] = {
124 1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
125 1, 1, 1, 1, /* 8-11 */
126 1, 1, 1, 1, 2, 2, 2, 2, /* 12-19 */
127 3, 3, 3, 4, 4, 4, 5, 6, /* 20-27 */
128 6, 7, 8, 9,10,11,13,14, /* 28-35 */
129 16,18,20,23,25,29,32,36, /* 36-43 */
130 40,45,51,57,64,72,81,91 /* 44-51 */
133 /* pow(lambda,2) * .9 */
134 static const int i_qp0_cost2_table[52] = {
135 1, 1, 1, 1, 1, 1, /* 0-5 */
136 1, 1, 1, 1, 1, 1, /* 6-11 */
137 1, 1, 1, 2, 2, 3, /* 12-17 */
138 4, 5, 6, 7, 9, 11, /* 18-23 */
139 14, 18, 23, 29, 36, 46, /* 24-29 */
140 58, 73, 91, 115, 145, 183, /* 30-35 */
141 230, 290, 366, 461, 581, 731, /* 36-41 */
142 922,1161,1463,1843,2322,2926, /* 42-47 */
146 /* TODO: calculate CABAC costs */
147 static const int i_mb_b_cost_table[19] = {
148 9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
150 static const int i_mb_b16x8_cost_table[17] = {
151 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
153 static const int i_sub_mb_b_cost_table[13] = {
154 7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
156 static const int i_sub_mb_p_cost_table[4] = {
160 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
162 /* initialize an array of lambda*nbits for all possible mvs */
163 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
165 static int16_t *p_cost_mv[52];
167 if( !p_cost_mv[a->i_qp] )
169 /* could be faster, but isn't called many times */
170 /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
172 p_cost_mv[a->i_qp] = x264_malloc( (4*4*h->param.analyse.i_mv_range + 1) * sizeof(int16_t) );
173 p_cost_mv[a->i_qp] += 2*4*h->param.analyse.i_mv_range;
174 for( i = 0; i <= 2*4*h->param.analyse.i_mv_range; i++ )
176 p_cost_mv[a->i_qp][-i] =
177 p_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i );
181 a->p_cost_mv = p_cost_mv[a->i_qp];
184 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
186 memset( a, 0, sizeof( x264_mb_analysis_t ) );
188 /* conduct the analysis using this lamda and QP */
189 a->i_qp = h->mb.i_qp = i_qp;
190 a->i_lambda = i_qp0_cost_table[i_qp];
191 a->i_lambda2 = i_qp0_cost2_table[i_qp];
192 a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
193 ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
195 h->mb.i_me_method = h->param.analyse.i_me_method;
196 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
197 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
198 && h->mb.i_subpel_refine >= 5;
199 h->mb.b_trellis = h->param.analyse.i_trellis > 1;
200 h->mb.b_transform_8x8 = 0;
206 a->i_sad_i8x8chroma = COST_MAX;
209 a->i_best_satd = COST_MAX;
211 /* II: Inter part P/B frame */
212 if( h->sh.i_type != SLICE_TYPE_I )
215 int i_fmv_range = h->param.analyse.i_mv_range - 16;
217 /* Calculate max allowed MV range */
218 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range )
219 h->mb.mv_min_fpel[0] = CLIP_FMV( -16*h->mb.i_mb_x - 8 );
220 h->mb.mv_max_fpel[0] = CLIP_FMV( 16*( h->sps->i_mb_width - h->mb.i_mb_x ) - 8 );
221 h->mb.mv_min[0] = 4*( h->mb.mv_min_fpel[0] - 16 );
222 h->mb.mv_max[0] = 4*( h->mb.mv_max_fpel[0] + 16 );
223 if( h->mb.i_mb_x == 0)
225 h->mb.mv_min_fpel[1] = CLIP_FMV( -16*h->mb.i_mb_y - 8 );
226 h->mb.mv_max_fpel[1] = CLIP_FMV( 16*( h->sps->i_mb_height - h->mb.i_mb_y ) - 8 );
227 h->mb.mv_min[1] = 4*( h->mb.mv_min_fpel[1] - 16 );
228 h->mb.mv_max[1] = 4*( h->mb.mv_max_fpel[1] + 16 );
233 a->l0.i_cost8x8 = COST_MAX;
235 for( i = 0; i < 4; i++ )
239 a->l0.i_cost4x8[i] = COST_MAX;
243 a->l0.i_cost8x16 = COST_MAX;
244 if( h->sh.i_type == SLICE_TYPE_B )
247 a->l1.i_cost8x8 = COST_MAX;
249 for( i = 0; i < 4; i++ )
254 a->i_cost8x8direct[i] = COST_MAX;
261 a->i_cost16x16direct =
264 a->i_cost8x16bi = COST_MAX;
267 /* Fast intra decision */
268 if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
271 || IS_INTRA( h->mb.i_mb_type_left )
272 || IS_INTRA( h->mb.i_mb_type_top )
273 || IS_INTRA( h->mb.i_mb_type_topleft )
274 || IS_INTRA( h->mb.i_mb_type_topright )
275 || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
276 || (h->mb.i_mb_xy - h->sh.i_first_mb < 3*(h->stat.frame.i_mb_count[I_4x4] + h->stat.frame.i_mb_count[I_8x8] + h->stat.frame.i_mb_count[I_16x16])) )
277 { /* intra is likely */ }
292 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
294 if( i_neighbour & MB_TOPLEFT )
296 /* top and left avaible */
297 *mode++ = I_PRED_16x16_V;
298 *mode++ = I_PRED_16x16_H;
299 *mode++ = I_PRED_16x16_DC;
300 *mode++ = I_PRED_16x16_P;
303 else if( i_neighbour & MB_LEFT )
306 *mode++ = I_PRED_16x16_DC_LEFT;
307 *mode++ = I_PRED_16x16_H;
310 else if( i_neighbour & MB_TOP )
313 *mode++ = I_PRED_16x16_DC_TOP;
314 *mode++ = I_PRED_16x16_V;
320 *mode = I_PRED_16x16_DC_128;
326 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
328 if( i_neighbour & MB_TOPLEFT )
330 /* top and left avaible */
331 *mode++ = I_PRED_CHROMA_V;
332 *mode++ = I_PRED_CHROMA_H;
333 *mode++ = I_PRED_CHROMA_DC;
334 *mode++ = I_PRED_CHROMA_P;
337 else if( i_neighbour & MB_LEFT )
340 *mode++ = I_PRED_CHROMA_DC_LEFT;
341 *mode++ = I_PRED_CHROMA_H;
344 else if( i_neighbour & MB_TOP )
347 *mode++ = I_PRED_CHROMA_DC_TOP;
348 *mode++ = I_PRED_CHROMA_V;
354 *mode = I_PRED_CHROMA_DC_128;
360 static void predict_4x4_mode_available( unsigned int i_neighbour,
361 int *mode, int *pi_count )
363 int b_l = i_neighbour & MB_LEFT;
364 int b_t = i_neighbour & MB_TOP;
368 *mode++ = I_PRED_4x4_DC;
369 *mode++ = I_PRED_4x4_H;
370 *mode++ = I_PRED_4x4_V;
371 *mode++ = I_PRED_4x4_DDL;
372 *mode++ = I_PRED_4x4_DDR;
373 *mode++ = I_PRED_4x4_VR;
374 *mode++ = I_PRED_4x4_HD;
375 *mode++ = I_PRED_4x4_VL;
376 *mode++ = I_PRED_4x4_HU;
381 *mode++ = I_PRED_4x4_DC_LEFT;
382 *mode++ = I_PRED_4x4_H;
383 *mode++ = I_PRED_4x4_HU;
388 *mode++ = I_PRED_4x4_DC_TOP;
389 *mode++ = I_PRED_4x4_V;
390 *mode++ = I_PRED_4x4_DDL;
391 *mode++ = I_PRED_4x4_VL;
396 *mode++ = I_PRED_4x4_DC_128;
401 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
408 uint8_t *p_dstc[2], *p_srcc[2];
411 if( a->i_sad_i8x8chroma < COST_MAX )
414 /* 8x8 prediction selection for chroma */
415 p_dstc[0] = h->mb.pic.p_fdec[1];
416 p_dstc[1] = h->mb.pic.p_fdec[2];
417 p_srcc[0] = h->mb.pic.p_fenc[1];
418 p_srcc[1] = h->mb.pic.p_fenc[2];
420 i_stride[0] = h->mb.pic.i_stride[1];
421 i_stride[1] = h->mb.pic.i_stride[2];
423 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
424 a->i_sad_i8x8chroma = COST_MAX;
425 for( i = 0; i < i_max; i++ )
430 i_mode = predict_mode[i];
432 /* we do the prediction */
433 h->predict_8x8c[i_mode]( p_dstc[0], i_stride[0] );
434 h->predict_8x8c[i_mode]( p_dstc[1], i_stride[1] );
436 /* we calculate the cost */
437 i_sad = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], i_stride[0],
438 p_srcc[0], i_stride[0] ) +
439 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], i_stride[1],
440 p_srcc[1], i_stride[1] ) +
441 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
443 /* if i_score is lower it is better */
444 if( a->i_sad_i8x8chroma > i_sad )
446 a->i_predict8x8chroma = i_mode;
447 a->i_sad_i8x8chroma = i_sad;
451 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
454 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_cost_inter )
456 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
457 const int i_stride = h->mb.pic.i_stride[0];
458 uint8_t *p_src = h->mb.pic.p_fenc[0];
459 uint8_t *p_dst = h->mb.pic.p_fdec[0];
460 int f8_satd_rd_ratio = 0;
467 if( h->sh.i_type == SLICE_TYPE_B )
468 i_satd_thresh = a->i_best_satd * 9/8;
470 i_satd_thresh = a->i_best_satd * 5/4 + a->i_lambda * 10;
472 /*---------------- Try all mode and calculate their score ---------------*/
474 /* 16x16 prediction selection */
475 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
476 for( i = 0; i < i_max; i++ )
481 i_mode = predict_mode[i];
482 h->predict_16x16[i_mode]( p_dst, i_stride );
484 i_sad = h->pixf.mbcmp[PIXEL_16x16]( p_dst, i_stride, p_src, i_stride ) +
485 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
486 if( a->i_sad_i16x16 > i_sad )
488 a->i_predict16x16 = i_mode;
489 a->i_sad_i16x16 = i_sad;
495 f8_satd_rd_ratio = ((unsigned)i_cost_inter << 8) / a->i_best_satd + 1;
496 x264_mb_analyse_intra_chroma( h, a );
497 if( h->mb.b_chroma_me )
498 a->i_sad_i16x16 += a->i_sad_i8x8chroma;
499 if( a->i_sad_i16x16 < i_satd_thresh )
501 h->mb.i_type = I_16x16;
502 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
503 a->i_sad_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
506 a->i_sad_i16x16 = a->i_sad_i16x16 * f8_satd_rd_ratio >> 8;
510 if( h->sh.i_type == SLICE_TYPE_B )
511 /* cavlc mb type prefix */
512 a->i_sad_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
513 if( a->b_fast_intra && a->i_sad_i16x16 > 2*i_cost_inter )
517 /* 4x4 prediction selection */
518 if( flags & X264_ANALYSE_I4x4 )
521 for( idx = 0; idx < 16; idx++ )
529 i_pred_mode= x264_mb_predict_intra4x4_mode( h, idx );
530 x = block_idx_x[idx];
531 y = block_idx_y[idx];
533 p_src_by = p_src + 4 * x + 4 * y * i_stride;
534 p_dst_by = p_dst + 4 * x + 4 * y * i_stride;
537 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
539 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
540 /* emulate missing topright samples */
541 *(uint32_t*) &p_dst_by[4 - i_stride] = p_dst_by[3 - i_stride] * 0x01010101U;
543 for( i = 0; i < i_max; i++ )
548 i_mode = predict_mode[i];
549 h->predict_4x4[i_mode]( p_dst_by, i_stride );
551 i_sad = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, i_stride,
553 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
557 a->i_predict4x4[x][y] = i_mode;
561 a->i_sad_i4x4 += i_best;
563 /* we need to encode this block now (for next ones) */
564 h->predict_4x4[a->i_predict4x4[x][y]]( p_dst_by, i_stride );
565 x264_mb_encode_i4x4( h, idx, a->i_qp );
567 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[x][y];
570 a->i_sad_i4x4 += a->i_lambda * 24; /* from JVT (SATD0) */
573 if( h->mb.b_chroma_me )
574 a->i_sad_i4x4 += a->i_sad_i8x8chroma;
575 if( a->i_sad_i4x4 < i_satd_thresh )
577 h->mb.i_type = I_4x4;
578 a->i_sad_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
581 a->i_sad_i4x4 = a->i_sad_i4x4 * f8_satd_rd_ratio >> 8;
585 if( h->sh.i_type == SLICE_TYPE_B )
586 a->i_sad_i4x4 += a->i_lambda * i_mb_b_cost_table[I_4x4];
590 /* 8x8 prediction selection */
591 if( flags & X264_ANALYSE_I8x8 )
594 for( idx = 0; idx < 4; idx++ )
602 i_pred_mode= x264_mb_predict_intra4x4_mode( h, 4*idx );
606 p_src_by = p_src + 8 * x + 8 * y * i_stride;
607 p_dst_by = p_dst + 8 * x + 8 * y * i_stride;
610 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
611 for( i = 0; i < i_max; i++ )
616 i_mode = predict_mode[i];
617 h->predict_8x8[i_mode]( p_dst_by, i_stride, h->mb.i_neighbour8[idx] );
619 /* could use sa8d, but it doesn't seem worth the speed cost (without mmx at least) */
620 i_sad = h->pixf.mbcmp[PIXEL_8x8]( p_dst_by, i_stride,
622 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
626 a->i_predict8x8[x][y] = i_mode;
630 a->i_sad_i8x8 += i_best;
632 /* we need to encode this block now (for next ones) */
633 h->predict_8x8[a->i_predict8x8[x][y]]( p_dst_by, i_stride, h->mb.i_neighbour );
634 x264_mb_encode_i8x8( h, idx, a->i_qp );
636 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[x][y] );
641 if( h->mb.b_chroma_me )
642 a->i_sad_i8x8 += a->i_sad_i8x8chroma;
643 if( a->i_sad_i8x8 < i_satd_thresh )
645 h->mb.i_type = I_8x8;
646 a->i_sad_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
649 a->i_sad_i8x8 = a->i_sad_i8x8 * f8_satd_rd_ratio >> 8;
653 // FIXME some bias like in i4x4?
654 if( h->sh.i_type == SLICE_TYPE_B )
655 a->i_sad_i8x8 += a->i_lambda * i_mb_b_cost_table[I_8x8];
660 #define LOAD_FENC( m, src, xoff, yoff) \
661 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
662 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
663 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
664 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
665 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]];
667 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
668 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
669 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
670 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
671 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
672 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
673 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
674 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
676 #define REF_COST(list, ref) \
677 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
679 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
683 int mvc[7][2], i_mvc;
684 int i_halfpel_thresh = INT_MAX;
685 int *p_halfpel_thresh = h->i_ref0>1 ? &i_halfpel_thresh : NULL;
687 /* 16x16 Search on all ref frame */
688 m.i_pixel = PIXEL_16x16;
689 m.p_cost_mv = a->p_cost_mv;
690 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
692 a->l0.me16x16.cost = INT_MAX;
693 for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
695 const int i_ref_cost = REF_COST( 0, i_ref );
696 i_halfpel_thresh -= i_ref_cost;
697 m.i_ref_cost = i_ref_cost;
700 /* search with ref */
701 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
702 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
703 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
704 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
707 * SSD threshold would probably be better than SATD */
708 if( i_ref == 0 && a->b_try_pskip && m.cost-m.cost_mv < 300*a->i_lambda )
711 x264_mb_predict_mv_pskip( h, mvskip );
712 if( abs(m.mv[0]-mvskip[0]) + abs(m.mv[1]-mvskip[1]) <= 1
713 && x264_macroblock_probe_pskip( h ) )
715 h->mb.i_type = P_SKIP;
716 x264_analyse_update_cache( h, a );
721 m.cost += i_ref_cost;
722 i_halfpel_thresh += i_ref_cost;
724 if( m.cost < a->l0.me16x16.cost )
727 /* save mv for predicting neighbors */
728 a->l0.mvc[i_ref][0][0] =
729 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
730 a->l0.mvc[i_ref][0][1] =
731 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
734 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
739 a->i_best_satd = a->l0.me16x16.cost;
740 h->mb.i_partition = D_16x16;
741 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
742 a->l0.me16x16.cost = x264_rd_cost_mb( h, a->i_lambda2 );
746 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
750 uint8_t **p_fenc = h->mb.pic.p_fenc;
751 int i_halfpel_thresh = INT_MAX;
752 int *p_halfpel_thresh = /*h->i_ref0>1 ? &i_halfpel_thresh : */NULL;
754 int i_maxref = h->i_ref0-1;
756 h->mb.i_partition = D_8x8;
758 /* early termination: if 16x16 chose ref 0, then evalute no refs older
759 * than those used by the neighbors */
760 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
761 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
764 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
765 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
766 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
767 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
768 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
769 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
772 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
774 a->l0.mvc[i_ref][0][0] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0];
775 a->l0.mvc[i_ref][0][1] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1];
778 for( i = 0; i < 4; i++ )
780 x264_me_t *l0m = &a->l0.me8x8[i];
784 m.i_pixel = PIXEL_8x8;
785 m.p_cost_mv = a->p_cost_mv;
787 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
789 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
791 const int i_ref_cost = REF_COST( 0, i_ref );
792 i_halfpel_thresh -= i_ref_cost;
793 m.i_ref_cost = i_ref_cost;
796 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
797 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
798 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
799 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
801 m.cost += i_ref_cost;
802 i_halfpel_thresh += i_ref_cost;
803 *(uint64_t*)a->l0.mvc[i_ref][i+1] = *(uint64_t*)m.mv;
805 if( m.cost < l0m->cost )
808 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv[0], l0m->mv[1] );
809 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
812 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
815 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
816 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
819 if( a->i_best_satd > a->l0.i_cost8x8 )
820 a->i_best_satd = a->l0.i_cost8x8;
821 h->mb.i_type = P_8x8;
822 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
823 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
824 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
828 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
830 const int i_ref = a->l0.me16x16.i_ref;
831 const int i_ref_cost = REF_COST( 0, i_ref );
832 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
833 uint8_t **p_fenc = h->mb.pic.p_fenc;
835 int (*mvc)[2] = a->l0.mvc[i_ref];
838 /* XXX Needed for x264_mb_predict_mv */
839 h->mb.i_partition = D_8x8;
842 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.me16x16.mv;
844 for( i = 0; i < 4; i++ )
846 x264_me_t *m = &a->l0.me8x8[i];
850 m->i_pixel = PIXEL_8x8;
851 m->p_cost_mv = a->p_cost_mv;
852 m->i_ref_cost = i_ref_cost;
855 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
856 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
857 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
858 x264_me_search( h, m, mvc, i_mvc );
860 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
862 *(uint64_t*)mvc[i_mvc] = *(uint64_t*)m->mv;
866 m->cost += i_ref_cost;
867 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
870 /* theoretically this should include 4*ref_cost,
871 * but 3 seems a better approximation of cabac. */
872 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
873 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
874 REF_COST( 0, a->l0.me16x16.i_ref );
877 if( a->i_best_satd > a->l0.i_cost8x8 )
878 a->i_best_satd = a->l0.i_cost8x8;
879 h->mb.i_type = P_8x8;
880 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
881 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
882 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
886 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
889 uint8_t **p_fenc = h->mb.pic.p_fenc;
893 /* XXX Needed for x264_mb_predict_mv */
894 h->mb.i_partition = D_16x8;
896 for( i = 0; i < 2; i++ )
898 x264_me_t *l0m = &a->l0.me16x8[i];
899 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
900 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
902 m.i_pixel = PIXEL_16x8;
903 m.p_cost_mv = a->p_cost_mv;
905 LOAD_FENC( &m, p_fenc, 0, 8*i );
907 for( j = 0; j < i_ref8s; j++ )
909 const int i_ref = ref8[j];
910 const int i_ref_cost = REF_COST( 0, i_ref );
911 m.i_ref_cost = i_ref_cost;
914 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
915 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
916 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][2*i+1];
917 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][2*i+2];
919 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
920 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
921 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
922 x264_me_search( h, &m, mvc, 3 );
924 m.cost += i_ref_cost;
926 if( m.cost < l0m->cost )
929 x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, l0m->mv[0], l0m->mv[1] );
930 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
933 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
936 if( a->i_best_satd > a->l0.i_cost16x8 )
937 a->i_best_satd = a->l0.i_cost16x8;
939 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
943 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
946 uint8_t **p_fenc = h->mb.pic.p_fenc;
950 /* XXX Needed for x264_mb_predict_mv */
951 h->mb.i_partition = D_8x16;
953 for( i = 0; i < 2; i++ )
955 x264_me_t *l0m = &a->l0.me8x16[i];
956 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
957 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
959 m.i_pixel = PIXEL_8x16;
960 m.p_cost_mv = a->p_cost_mv;
962 LOAD_FENC( &m, p_fenc, 8*i, 0 );
964 for( j = 0; j < i_ref8s; j++ )
966 const int i_ref = ref8[j];
967 const int i_ref_cost = REF_COST( 0, i_ref );
968 m.i_ref_cost = i_ref_cost;
971 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
972 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][i+1];
973 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][i+3];
975 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
976 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
977 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
978 x264_me_search( h, &m, mvc, 3 );
980 m.cost += i_ref_cost;
982 if( m.cost < l0m->cost )
985 x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, l0m->mv[0], l0m->mv[1] );
986 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
989 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
992 if( a->i_best_satd > a->l0.i_cost8x16 )
993 a->i_best_satd = a->l0.i_cost8x16;
995 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
999 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1001 uint8_t pix1[8*8], pix2[8*8];
1002 const int i_stride = h->mb.pic.i_stride[1];
1003 const int off = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1005 #define CHROMA4x4MC( width, height, me, x, y ) \
1006 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 ); \
1007 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 );
1009 if( pixel == PIXEL_4x4 )
1011 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1012 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 0,2 );
1013 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 2,0 );
1014 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1016 else if( pixel == PIXEL_8x4 )
1018 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1019 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1023 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1024 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1027 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][off], i_stride, pix1, 8 )
1028 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][off], i_stride, pix2, 8 );
1031 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1033 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1034 uint8_t **p_fenc = h->mb.pic.p_fenc;
1035 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1038 /* XXX Needed for x264_mb_predict_mv */
1039 h->mb.i_partition = D_8x8;
1041 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1043 const int idx = 4*i8x8 + i4x4;
1044 const int x4 = block_idx_x[idx];
1045 const int y4 = block_idx_y[idx];
1046 const int i_mvc = (i4x4 == 0);
1048 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1050 m->i_pixel = PIXEL_4x4;
1051 m->p_cost_mv = a->p_cost_mv;
1053 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1054 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1056 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1057 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1059 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
1061 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1062 a->l0.me4x4[i8x8][1].cost +
1063 a->l0.me4x4[i8x8][2].cost +
1064 a->l0.me4x4[i8x8][3].cost +
1065 REF_COST( 0, i_ref ) +
1066 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1067 if( h->mb.b_chroma_me )
1068 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1071 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1073 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1074 uint8_t **p_fenc = h->mb.pic.p_fenc;
1075 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1078 /* XXX Needed for x264_mb_predict_mv */
1079 h->mb.i_partition = D_8x8;
1081 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1083 const int idx = 4*i8x8 + 2*i8x4;
1084 const int x4 = block_idx_x[idx];
1085 const int y4 = block_idx_y[idx];
1086 const int i_mvc = (i8x4 == 0);
1088 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1090 m->i_pixel = PIXEL_8x4;
1091 m->p_cost_mv = a->p_cost_mv;
1093 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1094 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1096 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1097 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1099 x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
1101 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1102 REF_COST( 0, i_ref ) +
1103 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1104 if( h->mb.b_chroma_me )
1105 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1108 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1110 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1111 uint8_t **p_fenc = h->mb.pic.p_fenc;
1112 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1115 /* XXX Needed for x264_mb_predict_mv */
1116 h->mb.i_partition = D_8x8;
1118 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1120 const int idx = 4*i8x8 + i4x8;
1121 const int x4 = block_idx_x[idx];
1122 const int y4 = block_idx_y[idx];
1123 const int i_mvc = (i4x8 == 0);
1125 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1127 m->i_pixel = PIXEL_4x8;
1128 m->p_cost_mv = a->p_cost_mv;
1130 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1131 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1133 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1134 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1136 x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
1138 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1139 REF_COST( 0, i_ref ) +
1140 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1141 if( h->mb.b_chroma_me )
1142 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1145 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1147 /* Assumes that fdec still contains the results of
1148 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1150 uint8_t **p_fenc = h->mb.pic.p_fenc;
1151 uint8_t **p_fdec = h->mb.pic.p_fdec;
1152 int i_stride= h->mb.pic.i_stride[0];
1155 a->i_cost16x16direct = 0;
1156 for( i = 0; i < 4; i++ )
1160 const int off = 8 * x8 + 8 * i_stride * y8;
1161 a->i_cost16x16direct +=
1162 a->i_cost8x8direct[i] =
1163 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][off], i_stride, &p_fdec[0][off], i_stride );
1166 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1168 a->i_cost16x16direct += a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1172 if( a->i_cost16x16direct < a->i_best_satd )
1173 a->i_best_satd = a->i_cost16x16direct;
1175 h->mb.i_type = B_DIRECT;
1176 a->i_cost16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
1180 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1182 if( h->param.analyse.b_weighted_bipred ) \
1183 h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1184 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1186 h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1189 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1191 uint8_t pix1[16*16], pix2[16*16];
1194 int src2_ref, pix1_ref;
1198 int mvc[8][2], i_mvc;
1199 int i_halfpel_thresh = INT_MAX;
1200 int *p_halfpel_thresh = h->i_ref0>1 ? &i_halfpel_thresh : NULL;
1202 /* 16x16 Search on all ref frame */
1203 m.i_pixel = PIXEL_16x16;
1204 m.p_cost_mv = a->p_cost_mv;
1205 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1208 a->l0.me16x16.cost = INT_MAX;
1209 for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
1211 /* search with ref */
1212 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1213 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1214 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1215 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1218 m.cost += REF_COST( 0, i_ref );
1220 if( m.cost < a->l0.me16x16.cost )
1222 a->l0.i_ref = i_ref;
1226 /* save mv for predicting neighbors */
1227 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1228 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1230 /* subtract ref cost, so we don't have to add it for the other MB types */
1231 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1234 i_halfpel_thresh = INT_MAX;
1235 p_halfpel_thresh = h->i_ref1>1 ? &i_halfpel_thresh : NULL;
1236 a->l1.me16x16.cost = INT_MAX;
1237 for( i_ref = 0; i_ref < h->i_ref1; i_ref++ )
1239 /* search with ref */
1240 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1241 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1242 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1243 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1246 m.cost += REF_COST( 1, i_ref );
1248 if( m.cost < a->l1.me16x16.cost )
1250 a->l1.i_ref = i_ref;
1254 /* save mv for predicting neighbors */
1255 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1256 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1258 /* subtract ref cost, so we don't have to add it for the other MB types */
1259 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1261 /* Set global ref, needed for other modes? */
1262 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1263 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1265 /* get cost of BI mode */
1266 if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
1268 /* l0 reference is halfpel, so get_ref on it will make it faster */
1269 src2 = h->mc.get_ref( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1271 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1273 h->mc.mc_luma( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1275 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1277 src2_ref = a->l0.i_ref;
1278 pix1_ref = a->l1.i_ref;
1282 /* if l0 was qpel, we'll use get_ref on l1 instead */
1283 h->mc.mc_luma( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1285 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1287 src2 = h->mc.get_ref( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1289 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1291 src2_ref = a->l1.i_ref;
1292 pix1_ref = a->l0.i_ref;
1295 if( h->param.analyse.b_weighted_bipred )
1296 h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2,
1297 h->mb.bipred_weight[pix1_ref][src2_ref] );
1299 h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1301 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0], pix1, 16 )
1302 + REF_COST( 0, a->l0.i_ref )
1303 + REF_COST( 1, a->l1.i_ref )
1304 + a->l0.me16x16.cost_mv
1305 + a->l1.me16x16.cost_mv;
1308 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1309 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1310 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1316 if( a->l0.me16x16.cost < a->i_best_satd )
1317 a->i_best_satd = a->l0.me16x16.cost;
1318 if( a->l1.me16x16.cost < a->i_best_satd )
1319 a->i_best_satd = a->l1.me16x16.cost;
1320 if( a->i_cost16x16bi < a->i_best_satd )
1321 a->i_best_satd = a->i_cost16x16bi;
1323 i_satd_thresh = a->i_best_satd * 3/2;
1325 h->mb.i_partition = D_16x16;
1327 if( a->l0.me16x16.cost < i_satd_thresh )
1329 h->mb.i_type = B_L0_L0;
1330 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
1331 a->l0.me16x16.cost = x264_rd_cost_mb( h, a->i_lambda2 );
1334 a->l0.me16x16.cost = COST_MAX;
1337 if( a->l1.me16x16.cost < i_satd_thresh )
1339 h->mb.i_type = B_L1_L1;
1340 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
1341 a->l1.me16x16.cost = x264_rd_cost_mb( h, a->i_lambda2 );
1344 a->l1.me16x16.cost = COST_MAX;
1347 if( a->i_cost16x16bi < i_satd_thresh )
1349 h->mb.i_type = B_BI_BI;
1350 a->i_cost16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1353 a->i_cost16x16bi = COST_MAX;
1357 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1359 const int x = 2*(i%2);
1360 const int y = 2*(i/2);
1362 switch( h->mb.i_sub_partition[i] )
1365 x264_macroblock_cache_mv( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv[0], a->l0.me8x8[i].mv[1] );
1368 x264_macroblock_cache_mv( h, x, y+0, 2, 1, 0, a->l0.me8x4[i][0].mv[0], a->l0.me8x4[i][0].mv[1] );
1369 x264_macroblock_cache_mv( h, x, y+1, 2, 1, 0, a->l0.me8x4[i][1].mv[0], a->l0.me8x4[i][1].mv[1] );
1372 x264_macroblock_cache_mv( h, x+0, y, 1, 2, 0, a->l0.me4x8[i][0].mv[0], a->l0.me4x8[i][0].mv[1] );
1373 x264_macroblock_cache_mv( h, x+1, y, 1, 2, 0, a->l0.me4x8[i][1].mv[0], a->l0.me4x8[i][1].mv[1] );
1376 x264_macroblock_cache_mv( h, x+0, y+0, 1, 1, 0, a->l0.me4x4[i][0].mv[0], a->l0.me4x4[i][0].mv[1] );
1377 x264_macroblock_cache_mv( h, x+1, y+0, 1, 1, 0, a->l0.me4x4[i][1].mv[0], a->l0.me4x4[i][1].mv[1] );
1378 x264_macroblock_cache_mv( h, x+0, y+1, 1, 1, 0, a->l0.me4x4[i][2].mv[0], a->l0.me4x4[i][2].mv[1] );
1379 x264_macroblock_cache_mv( h, x+1, y+1, 1, 1, 0, a->l0.me4x4[i][3].mv[0], a->l0.me4x4[i][3].mv[1] );
1382 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1387 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1388 if( x264_mb_partition_listX_table[0][part] ) \
1390 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1391 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
1395 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1396 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0, 0 ); \
1398 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
1400 if( x264_mb_partition_listX_table[1][part] ) \
1402 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1403 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
1407 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1408 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0, 0 ); \
1410 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
1413 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1417 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1419 x264_mb_load_mv_direct8x8( h, i );
1422 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0, 0 );
1423 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0, 0 );
1424 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1429 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1432 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1434 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1436 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1438 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1442 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1444 uint8_t **p_fref[2] =
1445 { h->mb.pic.p_fref[0][a->l0.i_ref],
1446 h->mb.pic.p_fref[1][a->l1.i_ref] };
1447 uint8_t pix[2][8*8];
1450 /* XXX Needed for x264_mb_predict_mv */
1451 h->mb.i_partition = D_8x8;
1455 for( i = 0; i < 4; i++ )
1460 int i_part_cost_bi = 0;
1462 for( l = 0; l < 2; l++ )
1464 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1465 x264_me_t *m = &lX->me8x8[i];
1467 m->i_pixel = PIXEL_8x8;
1468 m->p_cost_mv = a->p_cost_mv;
1470 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1471 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1473 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1474 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1476 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
1479 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1480 m->mv[0], m->mv[1], 8, 8 );
1481 i_part_cost_bi += m->cost_mv;
1482 /* FIXME: ref cost */
1485 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1486 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], h->mb.pic.i_stride[0], pix[0], 8 )
1487 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1488 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1489 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1491 i_part_cost = a->l0.me8x8[i].cost;
1492 h->mb.i_sub_partition[i] = D_L0_8x8;
1493 if( a->l1.me8x8[i].cost < i_part_cost )
1495 i_part_cost = a->l1.me8x8[i].cost;
1496 h->mb.i_sub_partition[i] = D_L1_8x8;
1498 if( i_part_cost_bi < i_part_cost )
1500 i_part_cost = i_part_cost_bi;
1501 h->mb.i_sub_partition[i] = D_BI_8x8;
1503 if( a->i_cost8x8direct[i] < i_part_cost )
1505 i_part_cost = a->i_cost8x8direct[i];
1506 h->mb.i_sub_partition[i] = D_DIRECT_8x8;
1508 a->i_cost8x8bi += i_part_cost;
1510 /* XXX Needed for x264_mb_predict_mv */
1511 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1515 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1519 if( a->i_cost8x8bi < a->i_best_satd )
1520 a->i_best_satd = a->i_cost8x8bi;
1522 if( a->i_cost8x8bi < a->i_best_satd * 3/2 )
1524 h->mb.i_type = B_8x8;
1525 h->mb.i_partition = D_8x8;
1526 a->i_cost8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1529 a->i_cost8x8bi = COST_MAX;
1533 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1535 uint8_t **p_fref[2] =
1536 { h->mb.pic.p_fref[0][a->l0.i_ref],
1537 h->mb.pic.p_fref[1][a->l1.i_ref] };
1538 uint8_t pix[2][16*8];
1542 h->mb.i_partition = D_16x8;
1543 a->i_cost16x8bi = 0;
1545 for( i = 0; i < 2; i++ )
1548 int i_part_cost_bi = 0;
1550 /* TODO: check only the list(s) that were used in b8x8? */
1551 for( l = 0; l < 2; l++ )
1553 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1554 x264_me_t *m = &lX->me16x8[i];
1556 m->i_pixel = PIXEL_16x8;
1557 m->p_cost_mv = a->p_cost_mv;
1559 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1560 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1562 mvc[0][0] = lX->me8x8[2*i].mv[0];
1563 mvc[0][1] = lX->me8x8[2*i].mv[1];
1564 mvc[1][0] = lX->me8x8[2*i+1].mv[0];
1565 mvc[1][1] = lX->me8x8[2*i+1].mv[1];
1567 x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp );
1568 x264_me_search( h, m, mvc, 2 );
1571 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 16,
1572 m->mv[0], m->mv[1], 16, 8 );
1573 /* FIXME: ref cost */
1574 i_part_cost_bi += m->cost_mv;
1577 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1578 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], h->mb.pic.i_stride[0], pix[0], 16 );
1580 i_part_cost = a->l0.me16x8[i].cost;
1581 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1582 if( a->l1.me16x8[i].cost < i_part_cost )
1584 i_part_cost = a->l1.me16x8[i].cost;
1585 a->i_mb_partition16x8[i] = D_L1_8x8;
1587 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1589 i_part_cost = i_part_cost_bi;
1590 a->i_mb_partition16x8[i] = D_BI_8x8;
1592 a->i_cost16x8bi += i_part_cost;
1594 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1598 a->i_mb_type16x8 = B_L0_L0
1599 + (a->i_mb_partition16x8[0]>>2) * 3
1600 + (a->i_mb_partition16x8[1]>>2);
1601 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1605 if( a->i_cost16x8bi < a->i_best_satd )
1606 a->i_best_satd = a->i_cost16x8bi;
1608 if( a->i_cost16x8bi < a->i_best_satd * 3/2 )
1610 h->mb.i_type = a->i_mb_type16x8;
1611 h->mb.i_partition = D_16x8;
1612 a->i_cost16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1615 a->i_cost16x8bi = COST_MAX;
1618 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1620 uint8_t **p_fref[2] =
1621 { h->mb.pic.p_fref[0][a->l0.i_ref],
1622 h->mb.pic.p_fref[1][a->l1.i_ref] };
1623 uint8_t pix[2][8*16];
1627 h->mb.i_partition = D_8x16;
1628 a->i_cost8x16bi = 0;
1630 for( i = 0; i < 2; i++ )
1633 int i_part_cost_bi = 0;
1635 for( l = 0; l < 2; l++ )
1637 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1638 x264_me_t *m = &lX->me8x16[i];
1640 m->i_pixel = PIXEL_8x16;
1641 m->p_cost_mv = a->p_cost_mv;
1643 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1644 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1646 mvc[0][0] = lX->me8x8[i].mv[0];
1647 mvc[0][1] = lX->me8x8[i].mv[1];
1648 mvc[1][0] = lX->me8x8[i+2].mv[0];
1649 mvc[1][1] = lX->me8x8[i+2].mv[1];
1651 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1652 x264_me_search( h, m, mvc, 2 );
1655 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1656 m->mv[0], m->mv[1], 8, 16 );
1657 /* FIXME: ref cost */
1658 i_part_cost_bi += m->cost_mv;
1661 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1662 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], h->mb.pic.i_stride[0], pix[0], 8 );
1664 i_part_cost = a->l0.me8x16[i].cost;
1665 a->i_mb_partition8x16[i] = D_L0_8x8;
1666 if( a->l1.me8x16[i].cost < i_part_cost )
1668 i_part_cost = a->l1.me8x16[i].cost;
1669 a->i_mb_partition8x16[i] = D_L1_8x8;
1671 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1673 i_part_cost = i_part_cost_bi;
1674 a->i_mb_partition8x16[i] = D_BI_8x8;
1676 a->i_cost8x16bi += i_part_cost;
1678 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1682 a->i_mb_type8x16 = B_L0_L0
1683 + (a->i_mb_partition8x16[0]>>2) * 3
1684 + (a->i_mb_partition8x16[1]>>2);
1685 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1689 if( a->i_cost8x16bi < a->i_best_satd )
1690 a->i_best_satd = a->i_cost8x16bi;
1692 if( a->i_cost8x16bi < a->i_best_satd * 3/2 )
1694 h->mb.i_type = a->i_mb_type8x16;
1695 h->mb.i_partition = D_8x16;
1696 a->i_cost8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1699 a->i_cost8x16bi = COST_MAX;
1703 static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
1705 const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1708 switch( h->mb.i_partition )
1711 if( h->mb.i_type == B_BI_BI )
1712 x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
1715 for( i=0; i<2; i++ )
1716 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
1717 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
1720 for( i=0; i<2; i++ )
1721 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
1722 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
1725 for( i=0; i<4; i++ )
1726 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
1727 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
1732 static inline void x264_mb_analyse_transform( x264_t *h )
1734 h->mb.cache.b_transform_8x8_allowed =
1735 h->param.analyse.b_transform_8x8
1736 && !IS_INTRA( h->mb.i_type ) && x264_mb_transform_8x8_allowed( h );
1738 if( h->mb.cache.b_transform_8x8_allowed )
1740 int i_cost4, i_cost8;
1741 /* FIXME only luma mc is needed */
1744 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0],
1745 h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] );
1746 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0],
1747 h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] );
1749 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
1753 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_cost )
1755 h->mb.cache.b_transform_8x8_allowed =
1756 h->param.analyse.b_transform_8x8 && x264_mb_transform_8x8_allowed( h );
1758 if( h->mb.cache.b_transform_8x8_allowed )
1761 x264_analyse_update_cache( h, a );
1762 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1763 /* FIXME only luma is needed, but the score for comparison already includes chroma */
1764 i_cost8 = x264_rd_cost_mb( h, a->i_lambda2 );
1766 if( *i_cost >= i_cost8 )
1769 a->i_best_satd = (int64_t)a->i_best_satd * i_cost8 / *i_cost;
1770 /* prevent a rare division by zero in x264_mb_analyse_intra */
1771 if( a->i_best_satd == 0 )
1777 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1782 /*****************************************************************************
1783 * x264_macroblock_analyse:
1784 *****************************************************************************/
1785 void x264_macroblock_analyse( x264_t *h )
1787 x264_mb_analysis_t analysis;
1788 int i_cost = COST_MAX;
1792 x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
1794 /*--------------------------- Do the analysis ---------------------------*/
1795 if( h->sh.i_type == SLICE_TYPE_I )
1797 x264_mb_analyse_intra( h, &analysis, COST_MAX );
1799 i_cost = analysis.i_sad_i16x16;
1800 h->mb.i_type = I_16x16;
1801 if( analysis.i_sad_i4x4 < i_cost )
1803 i_cost = analysis.i_sad_i4x4;
1804 h->mb.i_type = I_4x4;
1806 if( analysis.i_sad_i8x8 < i_cost )
1807 h->mb.i_type = I_8x8;
1809 else if( h->sh.i_type == SLICE_TYPE_P )
1812 int i_intra_cost, i_intra_type;
1814 /* Fast P_SKIP detection */
1815 analysis.b_try_pskip = 0;
1816 if( h->param.analyse.b_fast_pskip )
1818 if( h->param.analyse.i_subpel_refine >= 3 )
1819 analysis.b_try_pskip = 1;
1820 else if( h->mb.i_mb_type_left == P_SKIP ||
1821 h->mb.i_mb_type_top == P_SKIP ||
1822 h->mb.i_mb_type_topleft == P_SKIP ||
1823 h->mb.i_mb_type_topright == P_SKIP )
1824 b_skip = x264_macroblock_probe_pskip( h );
1829 h->mb.i_type = P_SKIP;
1830 h->mb.i_partition = D_16x16;
1834 const unsigned int flags = h->param.analyse.inter;
1839 x264_mb_analyse_load_costs( h, &analysis );
1841 x264_mb_analyse_inter_p16x16( h, &analysis );
1843 if( h->mb.i_type == P_SKIP )
1846 if( flags & X264_ANALYSE_PSUB16x16 )
1848 if( h->param.analyse.b_mixed_references )
1849 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
1851 x264_mb_analyse_inter_p8x8( h, &analysis );
1854 /* Select best inter mode */
1856 i_partition = D_16x16;
1857 i_cost = analysis.l0.me16x16.cost;
1859 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
1860 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
1865 i_partition = D_8x8;
1866 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1867 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1869 i_cost = analysis.l0.i_cost8x8;
1872 if( flags & X264_ANALYSE_PSUB8x8 )
1874 int i_cost_bak = i_cost;
1876 for( i = 0; i < 4; i++ )
1878 x264_mb_analyse_inter_p4x4( h, &analysis, i );
1879 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
1881 int i_cost8x8 = analysis.l0.i_cost4x4[i];
1882 h->mb.i_sub_partition[i] = D_L0_4x4;
1884 x264_mb_analyse_inter_p8x4( h, &analysis, i );
1885 if( analysis.l0.i_cost8x4[i] < i_cost8x8 )
1887 h->mb.i_sub_partition[i] = D_L0_8x4;
1888 i_cost8x8 = analysis.l0.i_cost8x4[i];
1891 x264_mb_analyse_inter_p4x8( h, &analysis, i );
1892 if( analysis.l0.i_cost4x8[i] < i_cost8x8 )
1894 h->mb.i_sub_partition[i] = D_L0_4x8;
1895 i_cost8x8 = analysis.l0.i_cost4x8[i];
1898 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
1901 x264_mb_cache_mv_p8x8( h, &analysis, i );
1903 /* TODO: RD per subpartition */
1904 if( b_sub8x8 && analysis.b_mbrd )
1906 i_cost = x264_rd_cost_mb( h, analysis.i_lambda2 );
1907 if( i_cost > i_cost_bak )
1909 i_cost = i_cost_bak;
1910 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1911 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1917 /* Now do 16x8/8x16 */
1918 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
1919 if( analysis.b_mbrd )
1920 i_thresh16x8 = i_thresh16x8 * analysis.i_lambda2 / analysis.i_lambda;
1921 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
1922 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
1924 x264_mb_analyse_inter_p16x8( h, &analysis );
1925 if( analysis.l0.i_cost16x8 < i_cost )
1928 i_partition = D_16x8;
1929 i_cost = analysis.l0.i_cost16x8;
1932 x264_mb_analyse_inter_p8x16( h, &analysis );
1933 if( analysis.l0.i_cost8x16 < i_cost )
1936 i_partition = D_8x16;
1937 i_cost = analysis.l0.i_cost8x16;
1941 h->mb.i_partition = i_partition;
1944 //FIXME mb_type costs?
1945 if( analysis.b_mbrd )
1947 h->mb.i_type = i_type;
1948 x264_mb_analyse_transform_rd( h, &analysis, &i_cost );
1950 else if( i_partition == D_16x16 )
1952 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1953 i_cost = analysis.l0.me16x16.cost;
1955 else if( i_partition == D_16x8 )
1957 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
1958 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
1959 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
1961 else if( i_partition == D_8x16 )
1963 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
1964 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
1965 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
1967 else if( i_partition == D_8x8 )
1971 for( i8x8 = 0; i8x8 < 4; i8x8++ )
1973 switch( h->mb.i_sub_partition[i8x8] )
1976 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
1977 i_cost += analysis.l0.me8x8[i8x8].cost;
1980 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
1981 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
1982 i_cost += analysis.l0.me8x4[i8x8][0].cost +
1983 analysis.l0.me8x4[i8x8][1].cost;
1986 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
1987 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
1988 i_cost += analysis.l0.me4x8[i8x8][0].cost +
1989 analysis.l0.me4x8[i8x8][1].cost;
1993 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
1994 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
1995 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
1996 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
1997 i_cost += analysis.l0.me4x4[i8x8][0].cost +
1998 analysis.l0.me4x4[i8x8][1].cost +
1999 analysis.l0.me4x4[i8x8][2].cost +
2000 analysis.l0.me4x4[i8x8][3].cost;
2003 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2009 x264_mb_analyse_intra( h, &analysis, i_cost );
2010 if( h->mb.b_chroma_me && !analysis.b_mbrd &&
2011 ( analysis.i_sad_i16x16 < i_cost
2012 || analysis.i_sad_i8x8 < i_cost
2013 || analysis.i_sad_i4x4 < i_cost ))
2015 x264_mb_analyse_intra_chroma( h, &analysis );
2016 analysis.i_sad_i16x16 += analysis.i_sad_i8x8chroma;
2017 analysis.i_sad_i8x8 += analysis.i_sad_i8x8chroma;
2018 analysis.i_sad_i4x4 += analysis.i_sad_i8x8chroma;
2021 i_intra_type = I_16x16;
2022 i_intra_cost = analysis.i_sad_i16x16;
2024 if( analysis.i_sad_i8x8 < i_intra_cost )
2026 i_intra_type = I_8x8;
2027 i_intra_cost = analysis.i_sad_i8x8;
2029 if( analysis.i_sad_i4x4 < i_intra_cost )
2031 i_intra_type = I_4x4;
2032 i_intra_cost = analysis.i_sad_i4x4;
2035 if( i_intra_cost < i_cost )
2037 i_type = i_intra_type;
2038 i_cost = i_intra_cost;
2041 h->mb.i_type = i_type;
2042 h->stat.frame.i_intra_cost += i_intra_cost;
2043 h->stat.frame.i_inter_cost += i_cost;
2046 else if( h->sh.i_type == SLICE_TYPE_B )
2048 int i_bskip_cost = COST_MAX;
2051 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h );
2052 if( analysis.b_direct_available )
2054 h->mb.i_type = B_SKIP;
2056 if( analysis.b_mbrd )
2058 i_bskip_cost = h->pixf.ssd[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0],
2059 h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] )
2060 + h->pixf.ssd[PIXEL_8x8]( h->mb.pic.p_fenc[1], h->mb.pic.i_stride[1],
2061 h->mb.pic.p_fdec[1], h->mb.pic.i_stride[1] )
2062 + h->pixf.ssd[PIXEL_8x8]( h->mb.pic.p_fenc[2], h->mb.pic.i_stride[2],
2063 h->mb.pic.p_fdec[2], h->mb.pic.i_stride[2] );
2065 /* 6 = minimum cavlc cost of a non-skipped MB */
2066 if( i_bskip_cost <= 6 * analysis.i_lambda2 )
2068 h->mb.i_type = B_SKIP;
2069 x264_analyse_update_cache( h, &analysis );
2075 /* Conditioning the probe on neighboring block types
2076 * doesn't seem to help speed or quality. */
2077 b_skip = !h->mb.b_lossless && x264_macroblock_probe_bskip( h );
2083 const unsigned int flags = h->param.analyse.inter;
2087 x264_mb_analyse_load_costs( h, &analysis );
2089 /* select best inter mode */
2090 /* direct must be first */
2091 if( analysis.b_direct_available )
2092 x264_mb_analyse_inter_direct( h, &analysis );
2094 x264_mb_analyse_inter_b16x16( h, &analysis );
2097 i_partition = D_16x16;
2098 i_cost = analysis.l0.me16x16.cost;
2099 if( analysis.l1.me16x16.cost < i_cost )
2102 i_cost = analysis.l1.me16x16.cost;
2104 if( analysis.i_cost16x16bi < i_cost )
2107 i_cost = analysis.i_cost16x16bi;
2109 if( analysis.i_cost16x16direct < i_cost )
2112 i_cost = analysis.i_cost16x16direct;
2115 if( i_bskip_cost <= i_cost )
2117 h->mb.i_type = B_SKIP;
2118 x264_analyse_update_cache( h, &analysis );
2122 if( flags & X264_ANALYSE_BSUB16x16 )
2124 x264_mb_analyse_inter_b8x8( h, &analysis );
2125 if( analysis.i_cost8x8bi < i_cost )
2128 i_partition = D_8x8;
2129 i_cost = analysis.i_cost8x8bi;
2131 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2132 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2134 x264_mb_analyse_inter_b16x8( h, &analysis );
2135 if( analysis.i_cost16x8bi < i_cost )
2137 i_partition = D_16x8;
2138 i_cost = analysis.i_cost16x8bi;
2139 i_type = analysis.i_mb_type16x8;
2142 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2143 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2145 x264_mb_analyse_inter_b8x16( h, &analysis );
2146 if( analysis.i_cost8x16bi < i_cost )
2148 i_partition = D_8x16;
2149 i_cost = analysis.i_cost8x16bi;
2150 i_type = analysis.i_mb_type8x16;
2156 h->mb.i_partition = i_partition;
2158 if( analysis.b_mbrd )
2160 h->mb.i_type = i_type;
2161 x264_mb_analyse_transform_rd( h, &analysis, &i_cost );
2164 else if( i_partition == D_16x16 )
2166 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2167 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2168 if( i_type == B_L0_L0 )
2170 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2171 i_cost = analysis.l0.me16x16.cost
2172 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2174 else if( i_type == B_L1_L1 )
2176 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2177 i_cost = analysis.l1.me16x16.cost
2178 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2180 else if( i_type == B_BI_BI )
2182 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2183 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2186 else if( i_partition == D_16x8 )
2188 for( i=0; i<2; i++ )
2190 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2191 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2192 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2193 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2196 else if( i_partition == D_8x16 )
2198 for( i=0; i<2; i++ )
2200 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2201 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2202 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2203 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2206 else if( i_partition == D_8x8 )
2208 for( i=0; i<4; i++ )
2211 int i_part_cost_old;
2213 int i_part_type = h->mb.i_sub_partition[i];
2214 int b_bidir = (i_part_type == D_BI_8x8);
2216 if( i_part_type == D_DIRECT_8x8 )
2218 if( x264_mb_partition_listX_table[0][i_part_type] )
2220 m = &analysis.l0.me8x8[i];
2221 i_part_cost_old = m->cost;
2222 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2223 m->cost -= i_type_cost;
2224 x264_me_refine_qpel( h, m );
2226 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2228 if( x264_mb_partition_listX_table[1][i_part_type] )
2230 m = &analysis.l1.me8x8[i];
2231 i_part_cost_old = m->cost;
2232 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2233 m->cost -= i_type_cost;
2234 x264_me_refine_qpel( h, m );
2236 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2238 /* TODO: update mvp? */
2242 /* best intra mode */
2243 x264_mb_analyse_intra( h, &analysis, i_cost );
2245 if( analysis.i_sad_i16x16 < i_cost )
2248 i_cost = analysis.i_sad_i16x16;
2250 if( analysis.i_sad_i8x8 < i_cost )
2253 i_cost = analysis.i_sad_i8x8;
2255 if( analysis.i_sad_i4x4 < i_cost )
2258 i_cost = analysis.i_sad_i4x4;
2261 h->mb.i_type = i_type;
2263 if( h->param.analyse.b_bidir_me )
2264 refine_bidir( h, &analysis );
2268 x264_analyse_update_cache( h, &analysis );
2270 if( !analysis.b_mbrd )
2271 x264_mb_analyse_transform( h );
2273 h->mb.b_trellis = h->param.analyse.i_trellis;
2276 /*-------------------- Update MB from the analysis ----------------------*/
2277 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2281 switch( h->mb.i_type )
2284 for( i = 0; i < 16; i++ )
2286 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] =
2287 a->i_predict4x4[block_idx_x[i]][block_idx_y[i]];
2290 x264_mb_analyse_intra_chroma( h, a );
2293 for( i = 0; i < 4; i++ )
2294 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1),
2295 a->i_predict8x8[i&1][i>>1] );
2297 x264_mb_analyse_intra_chroma( h, a );
2300 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2301 x264_mb_analyse_intra_chroma( h, a );
2305 switch( h->mb.i_partition )
2308 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2309 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2313 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2314 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2315 x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv[0], a->l0.me16x8[0].mv[1] );
2316 x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv[0], a->l0.me16x8[1].mv[1] );
2320 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2321 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2322 x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv[0], a->l0.me8x16[0].mv[1] );
2323 x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv[0], a->l0.me8x16[1].mv[1] );
2327 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2333 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2334 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2335 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2336 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2337 for( i = 0; i < 4; i++ )
2338 x264_mb_cache_mv_p8x8( h, a, i );
2344 x264_mb_predict_mv_pskip( h, mvp );
2346 h->mb.i_partition = D_16x16;
2347 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2348 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, mvp[0], mvp[1] );
2354 x264_mb_load_mv_direct8x8( h, 0 );
2355 x264_mb_load_mv_direct8x8( h, 1 );
2356 x264_mb_load_mv_direct8x8( h, 2 );
2357 x264_mb_load_mv_direct8x8( h, 3 );
2361 /* optimize: cache might not need to be rewritten */
2362 for( i = 0; i < 4; i++ )
2363 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2366 default: /* the rest of the B types */
2367 switch( h->mb.i_partition )
2370 switch( h->mb.i_type )
2373 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2374 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2376 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2377 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0, 0 );
2378 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0, 0 );
2381 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2382 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0, 0 );
2383 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0, 0 );
2385 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2386 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2389 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2390 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2392 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2393 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2398 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2399 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2402 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2403 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2406 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2412 #include "slicetype_decision.c"