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 "common/macroblock.h"
33 #include "macroblock.h"
35 #include "ratecontrol.h"
47 int mvc[16][5][2]; /* [ref][0] is 16x16 mv,
48 [ref][1..4] are 8x8 mv from partition [0..3] */
52 int i_cost4x4[4]; /* cost per 8x8 partition */
53 x264_me_t me4x4[4][4];
56 int i_cost8x4[4]; /* cost per 8x8 partition */
57 x264_me_t me8x4[4][2];
60 int i_cost4x8[4]; /* cost per 8x8 partition */
61 x264_me_t me4x8[4][4];
71 } x264_mb_analysis_list_t;
75 /* conduct the analysis using this lamda and QP */
84 /* 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 static const uint8_t block_idx_x[16] = {
147 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
149 static const uint8_t block_idx_y[16] = {
150 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
153 /* TODO: calculate CABAC costs */
154 static const int i_mb_b_cost_table[19] = {
155 9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
157 static const int i_mb_b16x8_cost_table[17] = {
158 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
160 static const int i_sub_mb_b_cost_table[13] = {
161 7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
163 static const int i_sub_mb_p_cost_table[4] = {
167 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
169 /* initialize an array of lambda*nbits for all possible mvs */
170 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
172 static int16_t *p_cost_mv[52];
174 if( !p_cost_mv[a->i_qp] )
176 /* could be faster, but isn't called many times */
177 /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
179 p_cost_mv[a->i_qp] = x264_malloc( (4*4*h->param.analyse.i_mv_range + 1) * sizeof(int16_t) );
180 p_cost_mv[a->i_qp] += 2*4*h->param.analyse.i_mv_range;
181 for( i = 0; i <= 2*4*h->param.analyse.i_mv_range; i++ )
183 p_cost_mv[a->i_qp][-i] =
184 p_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i );
188 a->p_cost_mv = p_cost_mv[a->i_qp];
191 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
193 memset( a, 0, sizeof( x264_mb_analysis_t ) );
195 /* conduct the analysis using this lamda and QP */
196 a->i_qp = h->mb.i_qp = i_qp;
197 a->i_lambda = i_qp0_cost_table[i_qp];
198 a->i_lambda2 = i_qp0_cost2_table[i_qp];
199 a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 && h->sh.i_type != SLICE_TYPE_B;
201 h->mb.i_me_method = h->param.analyse.i_me_method;
202 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
203 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
204 && h->mb.i_subpel_refine >= 5;
206 h->mb.b_transform_8x8 = 0;
212 a->i_sad_i8x8chroma = COST_MAX;
215 a->i_best_satd = COST_MAX;
217 /* II: Inter part P/B frame */
218 if( h->sh.i_type != SLICE_TYPE_I )
221 int i_fmv_range = h->param.analyse.i_mv_range - 16;
223 /* Calculate max allowed MV range */
224 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range )
225 h->mb.mv_min_fpel[0] = CLIP_FMV( -16*h->mb.i_mb_x - 8 );
226 h->mb.mv_max_fpel[0] = CLIP_FMV( 16*( h->sps->i_mb_width - h->mb.i_mb_x ) - 8 );
227 h->mb.mv_min[0] = 4*( h->mb.mv_min_fpel[0] - 16 );
228 h->mb.mv_max[0] = 4*( h->mb.mv_max_fpel[0] + 16 );
229 if( h->mb.i_mb_x == 0)
231 h->mb.mv_min_fpel[1] = CLIP_FMV( -16*h->mb.i_mb_y - 8 );
232 h->mb.mv_max_fpel[1] = CLIP_FMV( 16*( h->sps->i_mb_height - h->mb.i_mb_y ) - 8 );
233 h->mb.mv_min[1] = 4*( h->mb.mv_min_fpel[1] - 16 );
234 h->mb.mv_max[1] = 4*( h->mb.mv_max_fpel[1] + 16 );
239 a->l0.i_cost8x8 = COST_MAX;
241 for( i = 0; i < 4; i++ )
245 a->l0.i_cost4x8[i] = COST_MAX;
249 a->l0.i_cost8x16 = COST_MAX;
250 if( h->sh.i_type == SLICE_TYPE_B )
253 a->l1.i_cost8x8 = COST_MAX;
255 for( i = 0; i < 4; i++ )
260 a->i_cost8x8direct[i] = COST_MAX;
267 a->i_cost16x16direct =
270 a->i_cost8x16bi = COST_MAX;
273 /* Fast intra decision */
274 if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
277 || IS_INTRA( h->mb.i_mb_type_left )
278 || IS_INTRA( h->mb.i_mb_type_top )
279 || IS_INTRA( h->mb.i_mb_type_topleft )
280 || IS_INTRA( h->mb.i_mb_type_topright )
281 || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
282 || (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])) )
283 { /* intra is likely */ }
298 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
300 if( i_neighbour & MB_TOPLEFT )
302 /* top and left avaible */
303 *mode++ = I_PRED_16x16_V;
304 *mode++ = I_PRED_16x16_H;
305 *mode++ = I_PRED_16x16_DC;
306 *mode++ = I_PRED_16x16_P;
309 else if( i_neighbour & MB_LEFT )
312 *mode++ = I_PRED_16x16_DC_LEFT;
313 *mode++ = I_PRED_16x16_H;
316 else if( i_neighbour & MB_TOP )
319 *mode++ = I_PRED_16x16_DC_TOP;
320 *mode++ = I_PRED_16x16_V;
326 *mode = I_PRED_16x16_DC_128;
332 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
334 if( i_neighbour & MB_TOPLEFT )
336 /* top and left avaible */
337 *mode++ = I_PRED_CHROMA_V;
338 *mode++ = I_PRED_CHROMA_H;
339 *mode++ = I_PRED_CHROMA_DC;
340 *mode++ = I_PRED_CHROMA_P;
343 else if( i_neighbour & MB_LEFT )
346 *mode++ = I_PRED_CHROMA_DC_LEFT;
347 *mode++ = I_PRED_CHROMA_H;
350 else if( i_neighbour & MB_TOP )
353 *mode++ = I_PRED_CHROMA_DC_TOP;
354 *mode++ = I_PRED_CHROMA_V;
360 *mode = I_PRED_CHROMA_DC_128;
366 static void predict_4x4_mode_available( unsigned int i_neighbour,
367 int *mode, int *pi_count )
369 int b_l = i_neighbour & MB_LEFT;
370 int b_t = i_neighbour & MB_TOP;
374 *mode++ = I_PRED_4x4_DC;
375 *mode++ = I_PRED_4x4_H;
376 *mode++ = I_PRED_4x4_V;
377 *mode++ = I_PRED_4x4_DDL;
378 *mode++ = I_PRED_4x4_DDR;
379 *mode++ = I_PRED_4x4_VR;
380 *mode++ = I_PRED_4x4_HD;
381 *mode++ = I_PRED_4x4_VL;
382 *mode++ = I_PRED_4x4_HU;
387 *mode++ = I_PRED_4x4_DC_LEFT;
388 *mode++ = I_PRED_4x4_H;
389 *mode++ = I_PRED_4x4_HU;
394 *mode++ = I_PRED_4x4_DC_TOP;
395 *mode++ = I_PRED_4x4_V;
396 *mode++ = I_PRED_4x4_DDL;
397 *mode++ = I_PRED_4x4_VL;
402 *mode++ = I_PRED_4x4_DC_128;
407 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
414 uint8_t *p_dstc[2], *p_srcc[2];
417 if( a->i_sad_i8x8chroma < COST_MAX )
420 /* 8x8 prediction selection for chroma */
421 p_dstc[0] = h->mb.pic.p_fdec[1];
422 p_dstc[1] = h->mb.pic.p_fdec[2];
423 p_srcc[0] = h->mb.pic.p_fenc[1];
424 p_srcc[1] = h->mb.pic.p_fenc[2];
426 i_stride[0] = h->mb.pic.i_stride[1];
427 i_stride[1] = h->mb.pic.i_stride[2];
429 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
430 a->i_sad_i8x8chroma = COST_MAX;
431 for( i = 0; i < i_max; i++ )
436 i_mode = predict_mode[i];
438 /* we do the prediction */
439 h->predict_8x8c[i_mode]( p_dstc[0], i_stride[0] );
440 h->predict_8x8c[i_mode]( p_dstc[1], i_stride[1] );
442 /* we calculate the cost */
443 i_sad = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], i_stride[0],
444 p_srcc[0], i_stride[0] ) +
445 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], i_stride[1],
446 p_srcc[1], i_stride[1] ) +
447 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
449 /* if i_score is lower it is better */
450 if( a->i_sad_i8x8chroma > i_sad )
452 a->i_predict8x8chroma = i_mode;
453 a->i_sad_i8x8chroma = i_sad;
457 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
460 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_cost_inter )
462 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
463 const int i_stride = h->mb.pic.i_stride[0];
464 uint8_t *p_src = h->mb.pic.p_fenc[0];
465 uint8_t *p_dst = h->mb.pic.p_fdec[0];
466 int f8_satd_rd_ratio = 0;
472 const int i_satd_thresh = a->i_best_satd * 5/4 + a->i_lambda * 10;
474 /*---------------- Try all mode and calculate their score ---------------*/
476 /* 16x16 prediction selection */
477 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
478 for( i = 0; i < i_max; i++ )
483 i_mode = predict_mode[i];
484 h->predict_16x16[i_mode]( p_dst, i_stride );
486 i_sad = h->pixf.mbcmp[PIXEL_16x16]( p_dst, i_stride, p_src, i_stride ) +
487 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
488 if( a->i_sad_i16x16 > i_sad )
490 a->i_predict16x16 = i_mode;
491 a->i_sad_i16x16 = i_sad;
497 f8_satd_rd_ratio = ((unsigned)i_cost_inter << 8) / a->i_best_satd + 1;
498 x264_mb_analyse_intra_chroma( h, a );
499 if( h->mb.b_chroma_me )
500 a->i_sad_i16x16 += a->i_sad_i8x8chroma;
501 if( a->i_sad_i16x16 < i_satd_thresh )
503 h->mb.i_type = I_16x16;
504 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
505 a->i_sad_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
508 a->i_sad_i16x16 = a->i_sad_i16x16 * f8_satd_rd_ratio >> 8;
512 if( h->sh.i_type == SLICE_TYPE_B )
513 /* cavlc mb type prefix */
514 a->i_sad_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
515 if( a->b_fast_intra && a->i_sad_i16x16 > 2*i_cost_inter )
519 /* 4x4 prediction selection */
520 if( flags & X264_ANALYSE_I4x4 )
523 for( idx = 0; idx < 16; idx++ )
531 i_pred_mode= x264_mb_predict_intra4x4_mode( h, idx );
532 x = block_idx_x[idx];
533 y = block_idx_y[idx];
535 p_src_by = p_src + 4 * x + 4 * y * i_stride;
536 p_dst_by = p_dst + 4 * x + 4 * y * i_stride;
539 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
541 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
542 /* emulate missing topright samples */
543 *(uint32_t*) &p_dst_by[4 - i_stride] = p_dst_by[3 - i_stride] * 0x01010101U;
545 for( i = 0; i < i_max; i++ )
550 i_mode = predict_mode[i];
551 h->predict_4x4[i_mode]( p_dst_by, i_stride );
553 i_sad = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, i_stride,
555 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
559 a->i_predict4x4[x][y] = i_mode;
563 a->i_sad_i4x4 += i_best;
565 /* we need to encode this block now (for next ones) */
566 h->predict_4x4[a->i_predict4x4[x][y]]( p_dst_by, i_stride );
567 x264_mb_encode_i4x4( h, idx, a->i_qp );
569 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[x][y];
572 a->i_sad_i4x4 += a->i_lambda * 24; /* from JVT (SATD0) */
575 if( h->mb.b_chroma_me )
576 a->i_sad_i4x4 += a->i_sad_i8x8chroma;
577 if( a->i_sad_i4x4 < i_satd_thresh )
579 h->mb.i_type = I_4x4;
580 a->i_sad_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
583 a->i_sad_i4x4 = a->i_sad_i4x4 * f8_satd_rd_ratio >> 8;
587 if( h->sh.i_type == SLICE_TYPE_B )
588 a->i_sad_i4x4 += a->i_lambda * i_mb_b_cost_table[I_4x4];
592 /* 8x8 prediction selection */
593 if( flags & X264_ANALYSE_I8x8 )
596 for( idx = 0; idx < 4; idx++ )
604 i_pred_mode= x264_mb_predict_intra4x4_mode( h, 4*idx );
608 p_src_by = p_src + 8 * x + 8 * y * i_stride;
609 p_dst_by = p_dst + 8 * x + 8 * y * i_stride;
612 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
613 for( i = 0; i < i_max; i++ )
618 i_mode = predict_mode[i];
619 h->predict_8x8[i_mode]( p_dst_by, i_stride, h->mb.i_neighbour );
621 /* could use sa8d, but it doesn't seem worth the speed cost (without mmx at least) */
622 i_sad = h->pixf.mbcmp[PIXEL_8x8]( p_dst_by, i_stride,
624 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
628 a->i_predict8x8[x][y] = i_mode;
632 a->i_sad_i8x8 += i_best;
634 /* we need to encode this block now (for next ones) */
635 h->predict_8x8[a->i_predict8x8[x][y]]( p_dst_by, i_stride, h->mb.i_neighbour );
636 x264_mb_encode_i8x8( h, idx, a->i_qp );
638 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[x][y] );
643 if( h->mb.b_chroma_me )
644 a->i_sad_i8x8 += a->i_sad_i8x8chroma;
645 if( a->i_sad_i8x8 < i_satd_thresh )
647 h->mb.i_type = I_8x8;
648 a->i_sad_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
651 a->i_sad_i8x8 = a->i_sad_i8x8 * f8_satd_rd_ratio >> 8;
655 // FIXME some bias like in i4x4?
656 if( h->sh.i_type == SLICE_TYPE_B )
657 a->i_sad_i8x8 += a->i_lambda * i_mb_b_cost_table[I_8x8];
662 #define LOAD_FENC( m, src, xoff, yoff) \
663 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
664 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
665 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
666 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
667 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]];
669 #define LOAD_HPELS(m, src, xoff, yoff) \
670 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
671 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
672 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
673 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
674 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
675 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]];
677 #define REF_COST(list, ref) \
678 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
680 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
684 int mvc[7][2], i_mvc;
685 int i_fullpel_thresh = INT_MAX;
686 int *p_fullpel_thresh = h->i_ref0>1 ? &i_fullpel_thresh : NULL;
688 /* 16x16 Search on all ref frame */
689 m.i_pixel = PIXEL_16x16;
690 m.p_cost_mv = a->p_cost_mv;
691 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
693 a->l0.me16x16.cost = INT_MAX;
694 for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
696 const int i_ref_cost = REF_COST( 0, i_ref );
697 i_fullpel_thresh -= i_ref_cost;
698 m.i_ref_cost = i_ref_cost;
701 /* search with ref */
702 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, 0 );
703 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
704 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
705 x264_me_search_ref( h, &m, mvc, i_mvc, p_fullpel_thresh );
707 m.cost += i_ref_cost;
708 i_fullpel_thresh += i_ref_cost;
710 if( m.cost < a->l0.me16x16.cost )
713 /* save mv for predicting neighbors */
714 a->l0.mvc[i_ref][0][0] =
715 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
716 a->l0.mvc[i_ref][0][1] =
717 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
720 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
724 a->i_best_satd = a->l0.me16x16.cost;
726 h->mb.i_partition = D_16x16;
727 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
728 a->l0.me16x16.cost = x264_rd_cost_mb( h, a->i_lambda2 );
732 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
736 uint8_t **p_fenc = h->mb.pic.p_fenc;
737 int i_fullpel_thresh = INT_MAX;
738 int *p_fullpel_thresh = /*h->i_ref0>1 ? &i_fullpel_thresh : */NULL;
740 int i_maxref = h->i_ref0-1;
742 h->mb.i_partition = D_8x8;
744 /* early termination: if 16x16 chose ref 0, then evalute no refs older
745 * than those used by the neighbors */
746 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
747 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
750 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
751 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
752 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
753 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
754 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
755 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
758 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
760 a->l0.mvc[i_ref][0][0] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0];
761 a->l0.mvc[i_ref][0][1] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1];
764 for( i = 0; i < 4; i++ )
766 x264_me_t *l0m = &a->l0.me8x8[i];
770 m.i_pixel = PIXEL_8x8;
771 m.p_cost_mv = a->p_cost_mv;
773 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
775 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
777 const int i_ref_cost = REF_COST( 0, i_ref );
778 i_fullpel_thresh -= i_ref_cost;
779 m.i_ref_cost = i_ref_cost;
782 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 8*x8, 8*y8 );
783 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
784 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
785 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_fullpel_thresh );
787 m.cost += i_ref_cost;
788 i_fullpel_thresh += i_ref_cost;
789 *(uint64_t*)a->l0.mvc[i_ref][i+1] = *(uint64_t*)m.mv;
791 if( m.cost < l0m->cost )
794 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv[0], l0m->mv[1] );
795 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
798 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
801 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
802 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
805 if( a->i_best_satd > a->l0.i_cost8x8 )
806 a->i_best_satd = a->l0.i_cost8x8;
807 h->mb.i_type = P_8x8;
808 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
809 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
810 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
814 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
816 const int i_ref = a->l0.me16x16.i_ref;
817 const int i_ref_cost = REF_COST( 0, i_ref );
818 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
819 uint8_t **p_fenc = h->mb.pic.p_fenc;
821 int (*mvc)[2] = a->l0.mvc[i_ref];
824 /* XXX Needed for x264_mb_predict_mv */
825 h->mb.i_partition = D_8x8;
828 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.me16x16.mv;
830 for( i = 0; i < 4; i++ )
832 x264_me_t *m = &a->l0.me8x8[i];
836 m->i_pixel = PIXEL_8x8;
837 m->p_cost_mv = a->p_cost_mv;
838 m->i_ref_cost = i_ref_cost;
841 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
842 LOAD_HPELS( m, p_fref, 8*x8, 8*y8 );
843 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
844 x264_me_search( h, m, mvc, i_mvc );
846 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
848 *(uint64_t*)mvc[i_mvc] = *(uint64_t*)m->mv;
852 m->cost += i_ref_cost;
853 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
856 /* theoretically this should include 4*ref_cost,
857 * but 3 seems a better approximation of cabac. */
858 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
859 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
860 REF_COST( 0, a->l0.me16x16.i_ref );
863 if( a->i_best_satd > a->l0.i_cost8x8 )
864 a->i_best_satd = a->l0.i_cost8x8;
865 h->mb.i_type = P_8x8;
866 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
867 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
868 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
872 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
875 uint8_t **p_fenc = h->mb.pic.p_fenc;
879 /* XXX Needed for x264_mb_predict_mv */
880 h->mb.i_partition = D_16x8;
882 for( i = 0; i < 2; i++ )
884 x264_me_t *l0m = &a->l0.me16x8[i];
885 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
886 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
888 m.i_pixel = PIXEL_16x8;
889 m.p_cost_mv = a->p_cost_mv;
891 LOAD_FENC( &m, p_fenc, 0, 8*i );
893 for( j = 0; j < i_ref8s; j++ )
895 const int i_ref = ref8[j];
896 const int i_ref_cost = REF_COST( 0, i_ref );
897 m.i_ref_cost = i_ref_cost;
900 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
901 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
902 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][2*i+1];
903 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][2*i+2];
905 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, 8*i );
906 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
907 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
908 x264_me_search( h, &m, mvc, 3 );
910 m.cost += i_ref_cost;
912 if( m.cost < l0m->cost )
915 x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, l0m->mv[0], l0m->mv[1] );
916 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
919 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
922 if( a->i_best_satd > a->l0.i_cost16x8 )
923 a->i_best_satd = a->l0.i_cost16x8;
925 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
929 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
932 uint8_t **p_fenc = h->mb.pic.p_fenc;
936 /* XXX Needed for x264_mb_predict_mv */
937 h->mb.i_partition = D_8x16;
939 for( i = 0; i < 2; i++ )
941 x264_me_t *l0m = &a->l0.me8x16[i];
942 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
943 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
945 m.i_pixel = PIXEL_8x16;
946 m.p_cost_mv = a->p_cost_mv;
948 LOAD_FENC( &m, p_fenc, 8*i, 0 );
950 for( j = 0; j < i_ref8s; j++ )
952 const int i_ref = ref8[j];
953 const int i_ref_cost = REF_COST( 0, i_ref );
954 m.i_ref_cost = i_ref_cost;
957 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
958 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][i+1];
959 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][i+3];
961 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 8*i, 0 );
962 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
963 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
964 x264_me_search( h, &m, mvc, 3 );
966 m.cost += i_ref_cost;
968 if( m.cost < l0m->cost )
971 x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, l0m->mv[0], l0m->mv[1] );
972 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
975 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
978 if( a->i_best_satd > a->l0.i_cost8x16 )
979 a->i_best_satd = a->l0.i_cost8x16;
981 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
985 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
987 uint8_t pix1[8*8], pix2[8*8];
988 const int i_stride = h->mb.pic.i_stride[1];
989 const int off = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
991 #define CHROMA4x4MC( width, height, me, x, y ) \
992 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 ); \
993 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 );
995 if( pixel == PIXEL_4x4 )
997 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
998 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 0,2 );
999 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 2,0 );
1000 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1002 else if( pixel == PIXEL_8x4 )
1004 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1005 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1009 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1010 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1013 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][off], i_stride, pix1, 8 )
1014 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][off], i_stride, pix2, 8 );
1017 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1019 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1020 uint8_t **p_fenc = h->mb.pic.p_fenc;
1024 /* XXX Needed for x264_mb_predict_mv */
1025 h->mb.i_partition = D_8x8;
1027 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1029 const int idx = 4*i8x8 + i4x4;
1030 const int x4 = block_idx_x[idx];
1031 const int y4 = block_idx_y[idx];
1032 const int i_mvc = (i4x4 == 0);
1034 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1036 m->i_pixel = PIXEL_4x4;
1037 m->p_cost_mv = a->p_cost_mv;
1039 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1040 LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
1042 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1043 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1045 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
1047 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1048 a->l0.me4x4[i8x8][1].cost +
1049 a->l0.me4x4[i8x8][2].cost +
1050 a->l0.me4x4[i8x8][3].cost +
1051 REF_COST( 0, a->l0.me8x8[i8x8].i_ref ) +
1052 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1053 if( h->mb.b_chroma_me )
1054 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1057 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1059 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1060 uint8_t **p_fenc = h->mb.pic.p_fenc;
1064 /* XXX Needed for x264_mb_predict_mv */
1065 h->mb.i_partition = D_8x8;
1067 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1069 const int idx = 4*i8x8 + 2*i8x4;
1070 const int x4 = block_idx_x[idx];
1071 const int y4 = block_idx_y[idx];
1072 const int i_mvc = (i8x4 == 0);
1074 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1076 m->i_pixel = PIXEL_8x4;
1077 m->p_cost_mv = a->p_cost_mv;
1079 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1080 LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
1082 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1083 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1085 x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
1087 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1088 REF_COST( 0, a->l0.me8x8[i8x8].i_ref ) +
1089 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1090 if( h->mb.b_chroma_me )
1091 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1094 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1096 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1097 uint8_t **p_fenc = h->mb.pic.p_fenc;
1101 /* XXX Needed for x264_mb_predict_mv */
1102 h->mb.i_partition = D_8x8;
1104 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1106 const int idx = 4*i8x8 + i4x8;
1107 const int x4 = block_idx_x[idx];
1108 const int y4 = block_idx_y[idx];
1109 const int i_mvc = (i4x8 == 0);
1111 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1113 m->i_pixel = PIXEL_4x8;
1114 m->p_cost_mv = a->p_cost_mv;
1116 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1117 LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
1119 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1120 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1122 x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
1124 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1125 REF_COST( 0, a->l0.me8x8[i8x8].i_ref ) +
1126 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1127 if( h->mb.b_chroma_me )
1128 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1131 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1133 /* Assumes that fdec still contains the results of
1134 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1136 uint8_t **p_fenc = h->mb.pic.p_fenc;
1137 uint8_t **p_fdec = h->mb.pic.p_fdec;
1138 int i_stride= h->mb.pic.i_stride[0];
1141 a->i_cost16x16direct = 0;
1142 for( i = 0; i < 4; i++ )
1146 const int off = 8 * x8 + 8 * i_stride * y8;
1147 a->i_cost16x16direct +=
1148 a->i_cost8x8direct[i] =
1149 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][off], i_stride, &p_fdec[0][off], i_stride );
1152 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1155 a->i_cost16x16direct += a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1158 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1160 if( h->param.analyse.b_weighted_bipred ) \
1161 h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1162 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1164 h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1167 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1169 uint8_t pix1[16*16], pix2[16*16];
1172 int src2_ref, pix1_ref;
1176 int mvc[8][2], i_mvc;
1177 int i_fullpel_thresh = INT_MAX;
1178 int *p_fullpel_thresh = h->i_ref0>1 ? &i_fullpel_thresh : NULL;
1180 /* 16x16 Search on all ref frame */
1181 m.i_pixel = PIXEL_16x16;
1182 m.p_cost_mv = a->p_cost_mv;
1183 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1186 a->l0.me16x16.cost = INT_MAX;
1187 for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
1189 /* search with ref */
1190 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, 0 );
1191 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1192 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1193 x264_me_search_ref( h, &m, mvc, i_mvc, p_fullpel_thresh );
1196 m.cost += REF_COST( 0, i_ref );
1198 if( m.cost < a->l0.me16x16.cost )
1200 a->l0.i_ref = i_ref;
1204 /* save mv for predicting neighbors */
1205 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1206 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1208 /* subtract ref cost, so we don't have to add it for the other MB types */
1209 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1212 i_fullpel_thresh = INT_MAX;
1213 p_fullpel_thresh = h->i_ref1>1 ? &i_fullpel_thresh : NULL;
1214 a->l1.me16x16.cost = INT_MAX;
1215 for( i_ref = 0; i_ref < h->i_ref1; i_ref++ )
1217 /* search with ref */
1218 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 0, 0 );
1219 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1220 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1221 x264_me_search_ref( h, &m, mvc, i_mvc, p_fullpel_thresh );
1224 m.cost += REF_COST( 1, i_ref );
1226 if( m.cost < a->l1.me16x16.cost )
1228 a->l1.i_ref = i_ref;
1232 /* save mv for predicting neighbors */
1233 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1234 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1236 /* subtract ref cost, so we don't have to add it for the other MB types */
1237 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1239 /* Set global ref, needed for other modes? */
1240 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1241 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1243 /* get cost of BI mode */
1244 if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
1246 /* l0 reference is halfpel, so get_ref on it will make it faster */
1247 src2 = h->mc.get_ref( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1249 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1251 h->mc.mc_luma( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1253 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1255 src2_ref = a->l0.i_ref;
1256 pix1_ref = a->l1.i_ref;
1260 /* if l0 was qpel, we'll use get_ref on l1 instead */
1261 h->mc.mc_luma( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1263 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1265 src2 = h->mc.get_ref( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1267 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1269 src2_ref = a->l1.i_ref;
1270 pix1_ref = a->l0.i_ref;
1273 if( h->param.analyse.b_weighted_bipred )
1274 h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2,
1275 h->mb.bipred_weight[pix1_ref][src2_ref] );
1277 h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1279 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0], pix1, 16 )
1280 + REF_COST( 0, a->l0.i_ref )
1281 + REF_COST( 1, a->l1.i_ref )
1282 + a->l0.me16x16.cost_mv
1283 + a->l1.me16x16.cost_mv;
1286 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1287 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1288 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1291 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1293 const int x = 2*(i%2);
1294 const int y = 2*(i/2);
1296 switch( h->mb.i_sub_partition[i] )
1299 x264_macroblock_cache_mv( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv[0], a->l0.me8x8[i].mv[1] );
1302 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] );
1303 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] );
1306 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] );
1307 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] );
1310 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] );
1311 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] );
1312 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] );
1313 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] );
1316 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1321 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1322 if( x264_mb_partition_listX_table[0][part] ) \
1324 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1325 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
1329 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1330 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0, 0 ); \
1332 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
1334 if( x264_mb_partition_listX_table[1][part] ) \
1336 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1337 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
1341 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1342 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0, 0 ); \
1344 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
1347 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1351 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1353 x264_mb_load_mv_direct8x8( h, i );
1356 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0, 0 );
1357 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0, 0 );
1358 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1363 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1366 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1368 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1370 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1372 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1376 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1378 uint8_t **p_fref[2] =
1379 { h->mb.pic.p_fref[0][a->l0.i_ref],
1380 h->mb.pic.p_fref[1][a->l1.i_ref] };
1381 uint8_t pix[2][8*8];
1384 /* XXX Needed for x264_mb_predict_mv */
1385 h->mb.i_partition = D_8x8;
1389 for( i = 0; i < 4; i++ )
1394 int i_part_cost_bi = 0;
1396 for( l = 0; l < 2; l++ )
1398 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1399 x264_me_t *m = &lX->me8x8[i];
1401 m->i_pixel = PIXEL_8x8;
1402 m->p_cost_mv = a->p_cost_mv;
1404 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1405 LOAD_HPELS( m, p_fref[l], 8*x8, 8*y8 );
1407 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1408 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1410 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
1413 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1414 m->mv[0], m->mv[1], 8, 8 );
1415 i_part_cost_bi += m->cost_mv;
1416 /* FIXME: ref cost */
1419 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1420 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 )
1421 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1422 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1423 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1425 i_part_cost = a->l0.me8x8[i].cost;
1426 h->mb.i_sub_partition[i] = D_L0_8x8;
1427 if( a->l1.me8x8[i].cost < i_part_cost )
1429 i_part_cost = a->l1.me8x8[i].cost;
1430 h->mb.i_sub_partition[i] = D_L1_8x8;
1432 if( i_part_cost_bi < i_part_cost )
1434 i_part_cost = i_part_cost_bi;
1435 h->mb.i_sub_partition[i] = D_BI_8x8;
1437 if( a->i_cost8x8direct[i] < i_part_cost )
1439 i_part_cost = a->i_cost8x8direct[i];
1440 h->mb.i_sub_partition[i] = D_DIRECT_8x8;
1442 a->i_cost8x8bi += i_part_cost;
1444 /* XXX Needed for x264_mb_predict_mv */
1445 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1449 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1452 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1454 uint8_t **p_fref[2] =
1455 { h->mb.pic.p_fref[0][a->l0.i_ref],
1456 h->mb.pic.p_fref[1][a->l1.i_ref] };
1457 uint8_t pix[2][16*8];
1461 h->mb.i_partition = D_16x8;
1462 a->i_cost16x8bi = 0;
1464 for( i = 0; i < 2; i++ )
1467 int i_part_cost_bi = 0;
1469 /* TODO: check only the list(s) that were used in b8x8? */
1470 for( l = 0; l < 2; l++ )
1472 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1473 x264_me_t *m = &lX->me16x8[i];
1475 m->i_pixel = PIXEL_16x8;
1476 m->p_cost_mv = a->p_cost_mv;
1478 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1479 LOAD_HPELS( m, p_fref[l], 0, 8*i );
1481 mvc[0][0] = lX->me8x8[2*i].mv[0];
1482 mvc[0][1] = lX->me8x8[2*i].mv[1];
1483 mvc[1][0] = lX->me8x8[2*i+1].mv[0];
1484 mvc[1][1] = lX->me8x8[2*i+1].mv[1];
1486 x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp );
1487 x264_me_search( h, m, mvc, 2 );
1490 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 16,
1491 m->mv[0], m->mv[1], 16, 8 );
1492 /* FIXME: ref cost */
1493 i_part_cost_bi += m->cost_mv;
1496 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1497 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 );
1499 i_part_cost = a->l0.me16x8[i].cost;
1500 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1501 if( a->l1.me16x8[i].cost < i_part_cost )
1503 i_part_cost = a->l1.me16x8[i].cost;
1504 a->i_mb_partition16x8[i] = D_L1_8x8;
1506 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1508 i_part_cost = i_part_cost_bi;
1509 a->i_mb_partition16x8[i] = D_BI_8x8;
1511 a->i_cost16x8bi += i_part_cost;
1514 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1518 a->i_mb_type16x8 = B_L0_L0
1519 + (a->i_mb_partition16x8[0]>>2) * 3
1520 + (a->i_mb_partition16x8[1]>>2);
1521 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1523 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1525 uint8_t **p_fref[2] =
1526 { h->mb.pic.p_fref[0][a->l0.i_ref],
1527 h->mb.pic.p_fref[1][a->l1.i_ref] };
1528 uint8_t pix[2][8*16];
1532 h->mb.i_partition = D_8x16;
1533 a->i_cost8x16bi = 0;
1535 for( i = 0; i < 2; i++ )
1538 int i_part_cost_bi = 0;
1540 for( l = 0; l < 2; l++ )
1542 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1543 x264_me_t *m = &lX->me8x16[i];
1545 m->i_pixel = PIXEL_8x16;
1546 m->p_cost_mv = a->p_cost_mv;
1548 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1549 LOAD_HPELS( m, p_fref[l], 8*i, 0 );
1551 mvc[0][0] = lX->me8x8[i].mv[0];
1552 mvc[0][1] = lX->me8x8[i].mv[1];
1553 mvc[1][0] = lX->me8x8[i+2].mv[0];
1554 mvc[1][1] = lX->me8x8[i+2].mv[1];
1556 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1557 x264_me_search( h, m, mvc, 2 );
1560 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1561 m->mv[0], m->mv[1], 8, 16 );
1562 /* FIXME: ref cost */
1563 i_part_cost_bi += m->cost_mv;
1566 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1567 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 );
1569 i_part_cost = a->l0.me8x16[i].cost;
1570 a->i_mb_partition8x16[i] = D_L0_8x8;
1571 if( a->l1.me8x16[i].cost < i_part_cost )
1573 i_part_cost = a->l1.me8x16[i].cost;
1574 a->i_mb_partition8x16[i] = D_L1_8x8;
1576 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1578 i_part_cost = i_part_cost_bi;
1579 a->i_mb_partition8x16[i] = D_BI_8x8;
1581 a->i_cost8x16bi += i_part_cost;
1584 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1588 a->i_mb_type8x16 = B_L0_L0
1589 + (a->i_mb_partition8x16[0]>>2) * 3
1590 + (a->i_mb_partition8x16[1]>>2);
1591 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1594 static inline void x264_mb_analyse_transform( x264_t *h )
1596 h->mb.cache.b_transform_8x8_allowed =
1597 h->param.analyse.b_transform_8x8
1598 && !IS_INTRA( h->mb.i_type ) && x264_mb_transform_8x8_allowed( h );
1600 if( h->mb.cache.b_transform_8x8_allowed )
1602 int i_cost4, i_cost8;
1603 /* FIXME only luma mc is needed */
1606 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0],
1607 h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] );
1608 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0],
1609 h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] );
1611 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
1615 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_cost )
1617 h->mb.cache.b_transform_8x8_allowed =
1618 h->param.analyse.b_transform_8x8 && x264_mb_transform_8x8_allowed( h );
1620 if( h->mb.cache.b_transform_8x8_allowed )
1623 x264_analyse_update_cache( h, a );
1624 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1625 /* FIXME only luma is needed, but the score for comparison already includes chroma */
1626 i_cost8 = x264_rd_cost_mb( h, a->i_lambda2 );
1628 if( *i_cost >= i_cost8 )
1631 a->i_best_satd = (int64_t)a->i_best_satd * i_cost8 / *i_cost;
1635 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1640 /*****************************************************************************
1641 * x264_macroblock_analyse:
1642 *****************************************************************************/
1643 void x264_macroblock_analyse( x264_t *h )
1645 x264_mb_analysis_t analysis;
1646 int i_cost = COST_MAX;
1650 x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
1652 /*--------------------------- Do the analysis ---------------------------*/
1653 if( h->sh.i_type == SLICE_TYPE_I )
1655 x264_mb_analyse_intra( h, &analysis, COST_MAX );
1657 i_cost = analysis.i_sad_i16x16;
1658 h->mb.i_type = I_16x16;
1659 if( analysis.i_sad_i4x4 < i_cost )
1661 i_cost = analysis.i_sad_i4x4;
1662 h->mb.i_type = I_4x4;
1664 if( analysis.i_sad_i8x8 < i_cost )
1665 h->mb.i_type = I_8x8;
1667 else if( h->sh.i_type == SLICE_TYPE_P )
1670 int i_intra_cost, i_intra_type;
1672 /* Fast P_SKIP detection */
1673 if( !h->mb.b_lossless &&
1674 (( h->mb.i_mb_type_left == P_SKIP ) ||
1675 ( h->mb.i_mb_type_top == P_SKIP ) ||
1676 ( h->mb.i_mb_type_topleft == P_SKIP ) ||
1677 ( h->mb.i_mb_type_topright == P_SKIP )))
1679 b_skip = x264_macroblock_probe_pskip( h );
1684 h->mb.i_type = P_SKIP;
1685 h->mb.i_partition = D_16x16;
1689 const unsigned int flags = h->param.analyse.inter;
1694 x264_mb_analyse_load_costs( h, &analysis );
1696 x264_mb_analyse_inter_p16x16( h, &analysis );
1697 if( flags & X264_ANALYSE_PSUB16x16 )
1699 if( h->param.analyse.b_mixed_references )
1700 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
1702 x264_mb_analyse_inter_p8x8( h, &analysis );
1705 /* Select best inter mode */
1707 i_partition = D_16x16;
1708 i_cost = analysis.l0.me16x16.cost;
1710 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
1711 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
1716 i_partition = D_8x8;
1717 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1718 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1720 i_cost = analysis.l0.i_cost8x8;
1723 if( flags & X264_ANALYSE_PSUB8x8 )
1725 int i_cost_bak = i_cost;
1727 for( i = 0; i < 4; i++ )
1729 x264_mb_analyse_inter_p4x4( h, &analysis, i );
1730 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
1732 int i_cost8x8 = analysis.l0.i_cost4x4[i];
1733 h->mb.i_sub_partition[i] = D_L0_4x4;
1735 x264_mb_analyse_inter_p8x4( h, &analysis, i );
1736 if( analysis.l0.i_cost8x4[i] < i_cost8x8 )
1738 h->mb.i_sub_partition[i] = D_L0_8x4;
1739 i_cost8x8 = analysis.l0.i_cost8x4[i];
1742 x264_mb_analyse_inter_p4x8( h, &analysis, i );
1743 if( analysis.l0.i_cost4x8[i] < i_cost8x8 )
1745 h->mb.i_sub_partition[i] = D_L0_4x8;
1746 i_cost8x8 = analysis.l0.i_cost4x8[i];
1749 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
1752 x264_mb_cache_mv_p8x8( h, &analysis, i );
1754 /* TODO: RD per subpartition */
1755 if( b_sub8x8 && analysis.b_mbrd )
1757 i_cost = x264_rd_cost_mb( h, analysis.i_lambda2 );
1758 if( i_cost > i_cost_bak )
1760 i_cost = i_cost_bak;
1761 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1762 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1768 /* Now do 16x8/8x16 */
1769 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
1770 if( analysis.b_mbrd )
1771 i_thresh16x8 = i_thresh16x8 * analysis.i_lambda2 / analysis.i_lambda;
1772 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
1773 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
1775 x264_mb_analyse_inter_p16x8( h, &analysis );
1776 if( analysis.l0.i_cost16x8 < i_cost )
1779 i_partition = D_16x8;
1780 i_cost = analysis.l0.i_cost16x8;
1783 x264_mb_analyse_inter_p8x16( h, &analysis );
1784 if( analysis.l0.i_cost8x16 < i_cost )
1787 i_partition = D_8x16;
1788 i_cost = analysis.l0.i_cost8x16;
1792 h->mb.i_partition = i_partition;
1795 //FIXME mb_type costs?
1796 if( analysis.b_mbrd )
1798 h->mb.i_type = i_type;
1799 x264_mb_analyse_transform_rd( h, &analysis, &i_cost );
1801 else if( i_partition == D_16x16 )
1803 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1804 i_cost = analysis.l0.me16x16.cost;
1806 else if( i_partition == D_16x8 )
1808 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
1809 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
1810 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
1812 else if( i_partition == D_8x16 )
1814 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
1815 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
1816 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
1818 else if( i_partition == D_8x8 )
1822 for( i8x8 = 0; i8x8 < 4; i8x8++ )
1824 switch( h->mb.i_sub_partition[i8x8] )
1827 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
1828 i_cost += analysis.l0.me8x8[i8x8].cost;
1831 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
1832 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
1833 i_cost += analysis.l0.me8x4[i8x8][0].cost +
1834 analysis.l0.me8x4[i8x8][1].cost;
1837 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
1838 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
1839 i_cost += analysis.l0.me4x8[i8x8][0].cost +
1840 analysis.l0.me4x8[i8x8][1].cost;
1844 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
1845 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
1846 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
1847 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
1848 i_cost += analysis.l0.me4x4[i8x8][0].cost +
1849 analysis.l0.me4x4[i8x8][1].cost +
1850 analysis.l0.me4x4[i8x8][2].cost +
1851 analysis.l0.me4x4[i8x8][3].cost;
1854 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
1860 x264_mb_analyse_intra( h, &analysis, i_cost );
1861 if( h->mb.b_chroma_me && !analysis.b_mbrd &&
1862 ( analysis.i_sad_i16x16 < i_cost
1863 || analysis.i_sad_i8x8 < i_cost
1864 || analysis.i_sad_i4x4 < i_cost ))
1866 x264_mb_analyse_intra_chroma( h, &analysis );
1867 analysis.i_sad_i16x16 += analysis.i_sad_i8x8chroma;
1868 analysis.i_sad_i8x8 += analysis.i_sad_i8x8chroma;
1869 analysis.i_sad_i4x4 += analysis.i_sad_i8x8chroma;
1872 i_intra_type = I_16x16;
1873 i_intra_cost = analysis.i_sad_i16x16;
1875 if( analysis.i_sad_i8x8 < i_intra_cost )
1877 i_intra_type = I_8x8;
1878 i_intra_cost = analysis.i_sad_i8x8;
1880 if( analysis.i_sad_i4x4 < i_intra_cost )
1882 i_intra_type = I_4x4;
1883 i_intra_cost = analysis.i_sad_i4x4;
1886 if( i_intra_cost < i_cost )
1888 i_type = i_intra_type;
1889 i_cost = i_intra_cost;
1892 h->mb.i_type = i_type;
1893 h->stat.frame.i_intra_cost += i_intra_cost;
1894 h->stat.frame.i_inter_cost += i_cost;
1897 else if( h->sh.i_type == SLICE_TYPE_B )
1901 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h );
1902 if( analysis.b_direct_available )
1904 h->mb.i_type = B_SKIP;
1907 /* Conditioning the probe on neighboring block types
1908 * doesn't seem to help speed or quality. */
1909 b_skip = !h->mb.b_lossless && x264_macroblock_probe_bskip( h );
1914 const unsigned int flags = h->param.analyse.inter;
1917 x264_mb_analyse_load_costs( h, &analysis );
1919 /* select best inter mode */
1920 /* direct must be first */
1921 if( analysis.b_direct_available )
1922 x264_mb_analyse_inter_direct( h, &analysis );
1924 x264_mb_analyse_inter_b16x16( h, &analysis );
1926 h->mb.i_type = B_L0_L0;
1927 i_partition = D_16x16;
1928 i_cost = analysis.l0.me16x16.cost;
1929 if( analysis.l1.me16x16.cost < i_cost )
1931 h->mb.i_type = B_L1_L1;
1932 i_cost = analysis.l1.me16x16.cost;
1934 if( analysis.i_cost16x16bi < i_cost )
1936 h->mb.i_type = B_BI_BI;
1937 i_cost = analysis.i_cost16x16bi;
1939 if( analysis.i_cost16x16direct < i_cost )
1941 h->mb.i_type = B_DIRECT;
1942 i_cost = analysis.i_cost16x16direct;
1945 if( flags & X264_ANALYSE_BSUB16x16 )
1947 x264_mb_analyse_inter_b8x8( h, &analysis );
1948 if( analysis.i_cost8x8bi < i_cost )
1950 h->mb.i_type = B_8x8;
1951 i_partition = D_8x8;
1952 i_cost = analysis.i_cost8x8bi;
1954 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
1955 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
1957 x264_mb_analyse_inter_b16x8( h, &analysis );
1958 if( analysis.i_cost16x8bi < i_cost )
1960 i_partition = D_16x8;
1961 i_cost = analysis.i_cost16x8bi;
1962 h->mb.i_type = analysis.i_mb_type16x8;
1965 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
1966 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
1968 x264_mb_analyse_inter_b8x16( h, &analysis );
1969 if( analysis.i_cost8x16bi < i_cost )
1971 i_partition = D_8x16;
1972 i_cost = analysis.i_cost8x16bi;
1973 h->mb.i_type = analysis.i_mb_type8x16;
1979 h->mb.i_partition = i_partition;
1982 if( i_partition == D_16x16 )
1984 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
1985 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
1986 if( h->mb.i_type == B_L0_L0 )
1988 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1989 i_cost = analysis.l0.me16x16.cost
1990 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
1992 else if( h->mb.i_type == B_L1_L1 )
1994 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
1995 i_cost = analysis.l1.me16x16.cost
1996 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
1998 else if( h->mb.i_type == B_BI_BI )
2000 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2001 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2004 else if( i_partition == D_16x8 )
2006 for( i=0; i<2; i++ )
2008 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2009 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2010 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2011 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2014 else if( i_partition == D_8x16 )
2016 for( i=0; i<2; i++ )
2018 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2019 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2020 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2021 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2024 else if( i_partition == D_8x8 )
2026 for( i=0; i<4; i++ )
2029 int i_part_cost_old;
2031 int i_part_type = h->mb.i_sub_partition[i];
2032 int b_bidir = (i_part_type == D_BI_8x8);
2034 if( i_part_type == D_DIRECT_8x8 )
2036 if( x264_mb_partition_listX_table[0][i_part_type] )
2038 m = &analysis.l0.me8x8[i];
2039 i_part_cost_old = m->cost;
2040 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2041 m->cost -= i_type_cost;
2042 x264_me_refine_qpel( h, m );
2044 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2046 if( x264_mb_partition_listX_table[1][i_part_type] )
2048 m = &analysis.l1.me8x8[i];
2049 i_part_cost_old = m->cost;
2050 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2051 m->cost -= i_type_cost;
2052 x264_me_refine_qpel( h, m );
2054 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2056 /* TODO: update mvp? */
2060 /* best intra mode */
2061 x264_mb_analyse_intra( h, &analysis, i_cost );
2063 if( analysis.i_sad_i16x16 < i_cost )
2065 h->mb.i_type = I_16x16;
2066 i_cost = analysis.i_sad_i16x16;
2068 if( analysis.i_sad_i8x8 < i_cost )
2070 h->mb.i_type = I_8x8;
2071 i_cost = analysis.i_sad_i8x8;
2073 if( analysis.i_sad_i4x4 < i_cost )
2075 h->mb.i_type = I_4x4;
2076 i_cost = analysis.i_sad_i4x4;
2081 x264_analyse_update_cache( h, &analysis );
2083 if( !analysis.b_mbrd )
2084 x264_mb_analyse_transform( h );
2087 /*-------------------- Update MB from the analysis ----------------------*/
2088 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2092 switch( h->mb.i_type )
2095 for( i = 0; i < 16; i++ )
2097 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] =
2098 a->i_predict4x4[block_idx_x[i]][block_idx_y[i]];
2101 x264_mb_analyse_intra_chroma( h, a );
2104 for( i = 0; i < 4; i++ )
2105 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1),
2106 a->i_predict8x8[i&1][i>>1] );
2108 x264_mb_analyse_intra_chroma( h, a );
2111 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2112 x264_mb_analyse_intra_chroma( h, a );
2116 switch( h->mb.i_partition )
2119 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2120 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2124 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2125 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2126 x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv[0], a->l0.me16x8[0].mv[1] );
2127 x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv[0], a->l0.me16x8[1].mv[1] );
2131 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2132 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2133 x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv[0], a->l0.me8x16[0].mv[1] );
2134 x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv[0], a->l0.me8x16[1].mv[1] );
2138 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2144 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2145 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2146 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2147 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2148 for( i = 0; i < 4; i++ )
2149 x264_mb_cache_mv_p8x8( h, a, i );
2155 x264_mb_predict_mv_pskip( h, mvp );
2157 h->mb.i_partition = D_16x16;
2158 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2159 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, mvp[0], mvp[1] );
2164 /* nothing has changed since x264_macroblock_probe_bskip */
2167 x264_mb_load_mv_direct8x8( h, 0 );
2168 x264_mb_load_mv_direct8x8( h, 1 );
2169 x264_mb_load_mv_direct8x8( h, 2 );
2170 x264_mb_load_mv_direct8x8( h, 3 );
2174 /* optimize: cache might not need to be rewritten */
2175 for( i = 0; i < 4; i++ )
2176 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2179 default: /* the rest of the B types */
2180 switch( h->mb.i_partition )
2183 switch( h->mb.i_type )
2186 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2187 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2189 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2190 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0, 0 );
2191 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0, 0 );
2194 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2195 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0, 0 );
2196 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0, 0 );
2198 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2199 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2202 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2203 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2205 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2206 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2211 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2212 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2215 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2216 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2219 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2225 #include "slicetype_decision.c"