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 *****************************************************************************/
30 #include "common/common.h"
31 #include "macroblock.h"
33 #include "ratecontrol.h"
45 int mvc[16][5][2]; /* [ref][0] is 16x16 mv,
46 [ref][1..4] are 8x8 mv from partition [0..3] */
50 int i_cost4x4[4]; /* cost per 8x8 partition */
51 x264_me_t me4x4[4][4];
54 int i_cost8x4[4]; /* cost per 8x8 partition */
55 x264_me_t me8x4[4][2];
58 int i_cost4x8[4]; /* cost per 8x8 partition */
59 x264_me_t me4x8[4][4];
69 } x264_mb_analysis_list_t;
73 /* conduct the analysis using this lamda and QP */
82 /* Take some shortcuts in intra search if intra is deemed unlikely */
92 int i_predict8x8[2][2];
95 int i_predict4x4[4][4];
99 int i_predict8x8chroma;
101 /* II: Inter part P/B frame */
102 x264_mb_analysis_list_t l0;
103 x264_mb_analysis_list_t l1;
105 int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
106 int i_cost16x16direct;
108 int i_cost8x8direct[4];
112 int i_mb_partition16x8[2]; /* mb_partition_e */
113 int i_mb_partition8x16[2];
114 int i_mb_type16x8; /* mb_class_e */
117 int b_direct_available;
119 } x264_mb_analysis_t;
121 /* lambda = pow(2,qp/6-2) */
122 static const int i_qp0_cost_table[52] = {
123 1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
124 1, 1, 1, 1, /* 8-11 */
125 1, 1, 1, 1, 2, 2, 2, 2, /* 12-19 */
126 3, 3, 3, 4, 4, 4, 5, 6, /* 20-27 */
127 6, 7, 8, 9,10,11,13,14, /* 28-35 */
128 16,18,20,23,25,29,32,36, /* 36-43 */
129 40,45,51,57,64,72,81,91 /* 44-51 */
132 /* pow(lambda,2) * .9 */
133 static const int i_qp0_cost2_table[52] = {
134 1, 1, 1, 1, 1, 1, /* 0-5 */
135 1, 1, 1, 1, 1, 1, /* 6-11 */
136 1, 1, 1, 2, 2, 3, /* 12-17 */
137 4, 5, 6, 7, 9, 11, /* 18-23 */
138 14, 18, 23, 29, 36, 46, /* 24-29 */
139 58, 73, 91, 115, 145, 183, /* 30-35 */
140 230, 290, 366, 461, 581, 731, /* 36-41 */
141 922,1161,1463,1843,2322,2926, /* 42-47 */
145 /* TODO: calculate CABAC costs */
146 static const int i_mb_b_cost_table[19] = {
147 9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
149 static const int i_mb_b16x8_cost_table[17] = {
150 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
152 static const int i_sub_mb_b_cost_table[13] = {
153 7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
155 static const int i_sub_mb_p_cost_table[4] = {
159 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
161 /* initialize an array of lambda*nbits for all possible mvs */
162 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
164 static int16_t *p_cost_mv[52];
166 if( !p_cost_mv[a->i_qp] )
168 /* could be faster, but isn't called many times */
169 /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
171 p_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) );
172 p_cost_mv[a->i_qp] += 2*4*2048;
173 for( i = 0; i <= 2*4*2048; i++ )
175 p_cost_mv[a->i_qp][-i] =
176 p_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i );
180 a->p_cost_mv = p_cost_mv[a->i_qp];
183 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
185 memset( a, 0, sizeof( x264_mb_analysis_t ) );
187 /* conduct the analysis using this lamda and QP */
188 a->i_qp = h->mb.i_qp = i_qp;
189 a->i_lambda = i_qp0_cost_table[i_qp];
190 a->i_lambda2 = i_qp0_cost2_table[i_qp];
191 a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
192 ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
194 h->mb.i_me_method = h->param.analyse.i_me_method;
195 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
196 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
197 && h->mb.i_subpel_refine >= 5;
198 h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
199 h->mb.b_transform_8x8 = 0;
200 h->mb.b_noise_reduction = 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[0] = 4*( -16*h->mb.i_mb_x - 24 );
220 h->mb.mv_max[0] = 4*( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 24 );
221 h->mb.mv_min_fpel[0] = CLIP_FMV( -16*h->mb.i_mb_x - 8 );
222 h->mb.mv_max_fpel[0] = CLIP_FMV( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 8 );
223 h->mb.mv_min_spel[0] = 4*( h->mb.mv_min_fpel[0] - 16 );
224 h->mb.mv_max_spel[0] = 4*( h->mb.mv_max_fpel[0] + 16 );
225 if( h->mb.i_mb_x == 0)
227 h->mb.mv_min[1] = 4*( -16*h->mb.i_mb_y - 24 );
228 h->mb.mv_max[1] = 4*( 16*( h->sps->i_mb_height - h->mb.i_mb_y - 1 ) + 24 );
229 h->mb.mv_min_fpel[1] = CLIP_FMV( -16*h->mb.i_mb_y - 8 );
230 h->mb.mv_max_fpel[1] = CLIP_FMV( 16*( h->sps->i_mb_height - h->mb.i_mb_y - 1 ) + 8 );
231 h->mb.mv_min_spel[1] = 4*( h->mb.mv_min_fpel[1] - 16 );
232 h->mb.mv_max_spel[1] = 4*( h->mb.mv_max_fpel[1] + 16 );
237 a->l0.i_cost8x8 = COST_MAX;
239 for( i = 0; i < 4; i++ )
243 a->l0.i_cost4x8[i] = COST_MAX;
247 a->l0.i_cost8x16 = COST_MAX;
248 if( h->sh.i_type == SLICE_TYPE_B )
251 a->l1.i_cost8x8 = COST_MAX;
253 for( i = 0; i < 4; i++ )
258 a->i_cost8x8direct[i] = COST_MAX;
265 a->i_cost16x16direct =
268 a->i_cost8x16bi = COST_MAX;
271 /* Fast intra decision */
272 if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
275 || IS_INTRA( h->mb.i_mb_type_left )
276 || IS_INTRA( h->mb.i_mb_type_top )
277 || IS_INTRA( h->mb.i_mb_type_topleft )
278 || IS_INTRA( h->mb.i_mb_type_topright )
279 || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
280 || (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])) )
281 { /* intra is likely */ }
296 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
298 if( i_neighbour & MB_TOPLEFT )
300 /* top and left avaible */
301 *mode++ = I_PRED_16x16_V;
302 *mode++ = I_PRED_16x16_H;
303 *mode++ = I_PRED_16x16_DC;
304 *mode++ = I_PRED_16x16_P;
307 else if( i_neighbour & MB_LEFT )
310 *mode++ = I_PRED_16x16_DC_LEFT;
311 *mode++ = I_PRED_16x16_H;
314 else if( i_neighbour & MB_TOP )
317 *mode++ = I_PRED_16x16_DC_TOP;
318 *mode++ = I_PRED_16x16_V;
324 *mode = I_PRED_16x16_DC_128;
330 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
332 if( i_neighbour & MB_TOPLEFT )
334 /* top and left avaible */
335 *mode++ = I_PRED_CHROMA_V;
336 *mode++ = I_PRED_CHROMA_H;
337 *mode++ = I_PRED_CHROMA_DC;
338 *mode++ = I_PRED_CHROMA_P;
341 else if( i_neighbour & MB_LEFT )
344 *mode++ = I_PRED_CHROMA_DC_LEFT;
345 *mode++ = I_PRED_CHROMA_H;
348 else if( i_neighbour & MB_TOP )
351 *mode++ = I_PRED_CHROMA_DC_TOP;
352 *mode++ = I_PRED_CHROMA_V;
358 *mode = I_PRED_CHROMA_DC_128;
364 static void predict_4x4_mode_available( unsigned int i_neighbour,
365 int *mode, int *pi_count )
367 int b_l = i_neighbour & MB_LEFT;
368 int b_t = i_neighbour & MB_TOP;
372 *mode++ = I_PRED_4x4_DC;
373 *mode++ = I_PRED_4x4_H;
374 *mode++ = I_PRED_4x4_V;
375 *mode++ = I_PRED_4x4_DDL;
376 *mode++ = I_PRED_4x4_DDR;
377 *mode++ = I_PRED_4x4_VR;
378 *mode++ = I_PRED_4x4_HD;
379 *mode++ = I_PRED_4x4_VL;
380 *mode++ = I_PRED_4x4_HU;
385 *mode++ = I_PRED_4x4_DC_LEFT;
386 *mode++ = I_PRED_4x4_H;
387 *mode++ = I_PRED_4x4_HU;
392 *mode++ = I_PRED_4x4_DC_TOP;
393 *mode++ = I_PRED_4x4_V;
394 *mode++ = I_PRED_4x4_DDL;
395 *mode++ = I_PRED_4x4_VL;
400 *mode++ = I_PRED_4x4_DC_128;
405 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
412 uint8_t *p_dstc[2], *p_srcc[2];
414 if( a->i_sad_i8x8chroma < COST_MAX )
417 /* 8x8 prediction selection for chroma */
418 p_dstc[0] = h->mb.pic.p_fdec[1];
419 p_dstc[1] = h->mb.pic.p_fdec[2];
420 p_srcc[0] = h->mb.pic.p_fenc[1];
421 p_srcc[1] = h->mb.pic.p_fenc[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] );
434 h->predict_8x8c[i_mode]( p_dstc[1] );
436 /* we calculate the cost */
437 i_sad = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
438 p_srcc[0], FENC_STRIDE ) +
439 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
440 p_srcc[1], FENC_STRIDE ) +
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 uint8_t *p_src = h->mb.pic.p_fenc[0];
458 uint8_t *p_dst = h->mb.pic.p_fdec[0];
459 int f8_satd_rd_ratio = 0;
466 if( h->sh.i_type == SLICE_TYPE_B )
467 i_satd_thresh = a->i_best_satd * 9/8;
469 i_satd_thresh = a->i_best_satd * 5/4 + a->i_lambda * 10;
471 /*---------------- Try all mode and calculate their score ---------------*/
473 /* 16x16 prediction selection */
474 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
475 for( i = 0; i < i_max; i++ )
480 i_mode = predict_mode[i];
481 h->predict_16x16[i_mode]( p_dst );
483 i_sad = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
484 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
485 if( a->i_sad_i16x16 > i_sad )
487 a->i_predict16x16 = i_mode;
488 a->i_sad_i16x16 = i_sad;
494 f8_satd_rd_ratio = ((unsigned)i_cost_inter << 8) / a->i_best_satd + 1;
495 x264_mb_analyse_intra_chroma( h, a );
496 if( h->mb.b_chroma_me )
497 a->i_sad_i16x16 += a->i_sad_i8x8chroma;
498 if( a->i_sad_i16x16 < i_satd_thresh )
500 h->mb.i_type = I_16x16;
501 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
502 a->i_sad_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
505 a->i_sad_i16x16 = a->i_sad_i16x16 * f8_satd_rd_ratio >> 8;
509 if( h->sh.i_type == SLICE_TYPE_B )
510 /* cavlc mb type prefix */
511 a->i_sad_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
512 if( a->b_fast_intra && a->i_sad_i16x16 > 2*i_cost_inter )
516 /* 4x4 prediction selection */
517 if( flags & X264_ANALYSE_I4x4 )
520 for( idx = 0; idx < 16; idx++ )
528 i_pred_mode= x264_mb_predict_intra4x4_mode( h, idx );
529 x = block_idx_x[idx];
530 y = block_idx_y[idx];
532 p_src_by = p_src + 4 * x + 4 * y * FENC_STRIDE;
533 p_dst_by = p_dst + 4 * x + 4 * y * FDEC_STRIDE;
536 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
538 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
539 /* emulate missing topright samples */
540 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
542 for( i = 0; i < i_max; i++ )
547 i_mode = predict_mode[i];
548 h->predict_4x4[i_mode]( p_dst_by );
550 i_sad = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
551 p_src_by, FENC_STRIDE )
552 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
556 a->i_predict4x4[x][y] = i_mode;
560 a->i_sad_i4x4 += i_best;
562 /* we need to encode this block now (for next ones) */
563 h->predict_4x4[a->i_predict4x4[x][y]]( p_dst_by );
564 x264_mb_encode_i4x4( h, idx, a->i_qp );
566 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[x][y];
569 a->i_sad_i4x4 += a->i_lambda * 24; /* from JVT (SATD0) */
572 if( h->mb.b_chroma_me )
573 a->i_sad_i4x4 += a->i_sad_i8x8chroma;
574 if( a->i_sad_i4x4 < i_satd_thresh )
576 h->mb.i_type = I_4x4;
577 a->i_sad_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
580 a->i_sad_i4x4 = a->i_sad_i4x4 * f8_satd_rd_ratio >> 8;
584 if( h->sh.i_type == SLICE_TYPE_B )
585 a->i_sad_i4x4 += a->i_lambda * i_mb_b_cost_table[I_4x4];
589 /* 8x8 prediction selection */
590 if( flags & X264_ANALYSE_I8x8 )
593 for( idx = 0; idx < 4; idx++ )
601 i_pred_mode= x264_mb_predict_intra4x4_mode( h, 4*idx );
605 p_src_by = p_src + 8 * x + 8 * y * FENC_STRIDE;
606 p_dst_by = p_dst + 8 * x + 8 * y * FDEC_STRIDE;
609 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
610 for( i = 0; i < i_max; i++ )
615 i_mode = predict_mode[i];
616 h->predict_8x8[i_mode]( p_dst_by, h->mb.i_neighbour8[idx] );
618 /* could use sa8d, but it doesn't seem worth the speed cost (without mmx at least) */
619 i_sad = h->pixf.mbcmp[PIXEL_8x8]( p_dst_by, FDEC_STRIDE,
620 p_src_by, FENC_STRIDE )
621 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
625 a->i_predict8x8[x][y] = i_mode;
629 a->i_sad_i8x8 += i_best;
631 /* we need to encode this block now (for next ones) */
632 h->predict_8x8[a->i_predict8x8[x][y]]( p_dst_by, h->mb.i_neighbour );
633 x264_mb_encode_i8x8( h, idx, a->i_qp );
635 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[x][y] );
640 if( h->mb.b_chroma_me )
641 a->i_sad_i8x8 += a->i_sad_i8x8chroma;
642 if( a->i_sad_i8x8 < i_satd_thresh )
644 h->mb.i_type = I_8x8;
645 a->i_sad_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
648 a->i_sad_i8x8 = a->i_sad_i8x8 * f8_satd_rd_ratio >> 8;
652 // FIXME some bias like in i4x4?
653 if( h->sh.i_type == SLICE_TYPE_B )
654 a->i_sad_i8x8 += a->i_lambda * i_mb_b_cost_table[I_8x8];
659 #define LOAD_FENC( m, src, xoff, yoff) \
660 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
661 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
662 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
663 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
664 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
666 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
667 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
668 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
669 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
670 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
671 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
672 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
673 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
675 #define REF_COST(list, ref) \
676 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
678 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
682 int mvc[7][2], i_mvc;
683 int i_halfpel_thresh = INT_MAX;
684 int *p_halfpel_thresh = h->i_ref0>1 ? &i_halfpel_thresh : NULL;
686 /* 16x16 Search on all ref frame */
687 m.i_pixel = PIXEL_16x16;
688 m.p_cost_mv = a->p_cost_mv;
689 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
691 a->l0.me16x16.cost = INT_MAX;
692 for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
694 const int i_ref_cost = REF_COST( 0, i_ref );
695 i_halfpel_thresh -= i_ref_cost;
696 m.i_ref_cost = i_ref_cost;
699 /* search with ref */
700 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
701 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
702 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
703 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
706 * SSD threshold would probably be better than SATD */
707 if( i_ref == 0 && a->b_try_pskip && m.cost-m.cost_mv < 300*a->i_lambda )
710 x264_mb_predict_mv_pskip( h, mvskip );
711 if( abs(m.mv[0]-mvskip[0]) + abs(m.mv[1]-mvskip[1]) <= 1
712 && x264_macroblock_probe_pskip( h ) )
714 h->mb.i_type = P_SKIP;
715 x264_analyse_update_cache( h, a );
720 m.cost += i_ref_cost;
721 i_halfpel_thresh += i_ref_cost;
723 if( m.cost < a->l0.me16x16.cost )
726 /* save mv for predicting neighbors */
727 a->l0.mvc[i_ref][0][0] =
728 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
729 a->l0.mvc[i_ref][0][1] =
730 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
733 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
738 a->i_best_satd = a->l0.me16x16.cost;
739 h->mb.i_partition = D_16x16;
740 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
741 a->l0.me16x16.cost = x264_rd_cost_mb( h, a->i_lambda2 );
745 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
749 uint8_t **p_fenc = h->mb.pic.p_fenc;
750 int i_halfpel_thresh = INT_MAX;
751 int *p_halfpel_thresh = /*h->i_ref0>1 ? &i_halfpel_thresh : */NULL;
753 int i_maxref = h->i_ref0-1;
755 h->mb.i_partition = D_8x8;
757 /* early termination: if 16x16 chose ref 0, then evalute no refs older
758 * than those used by the neighbors */
759 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
760 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
763 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
764 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
765 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
766 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
767 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
768 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
771 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
773 a->l0.mvc[i_ref][0][0] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0];
774 a->l0.mvc[i_ref][0][1] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1];
777 for( i = 0; i < 4; i++ )
779 x264_me_t *l0m = &a->l0.me8x8[i];
783 m.i_pixel = PIXEL_8x8;
784 m.p_cost_mv = a->p_cost_mv;
786 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
788 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
790 const int i_ref_cost = REF_COST( 0, i_ref );
791 i_halfpel_thresh -= i_ref_cost;
792 m.i_ref_cost = i_ref_cost;
795 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
796 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
797 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
798 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
800 m.cost += i_ref_cost;
801 i_halfpel_thresh += i_ref_cost;
802 *(uint64_t*)a->l0.mvc[i_ref][i+1] = *(uint64_t*)m.mv;
804 if( m.cost < l0m->cost )
807 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv[0], l0m->mv[1] );
808 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
811 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
814 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
815 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
818 if( a->i_best_satd > a->l0.i_cost8x8 )
819 a->i_best_satd = a->l0.i_cost8x8;
820 h->mb.i_type = P_8x8;
821 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
822 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
823 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
827 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
829 const int i_ref = a->l0.me16x16.i_ref;
830 const int i_ref_cost = REF_COST( 0, i_ref );
831 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
832 uint8_t **p_fenc = h->mb.pic.p_fenc;
834 int (*mvc)[2] = a->l0.mvc[i_ref];
837 /* XXX Needed for x264_mb_predict_mv */
838 h->mb.i_partition = D_8x8;
841 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.me16x16.mv;
843 for( i = 0; i < 4; i++ )
845 x264_me_t *m = &a->l0.me8x8[i];
849 m->i_pixel = PIXEL_8x8;
850 m->p_cost_mv = a->p_cost_mv;
851 m->i_ref_cost = i_ref_cost;
854 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
855 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
856 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
857 x264_me_search( h, m, mvc, i_mvc );
859 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
861 *(uint64_t*)mvc[i_mvc] = *(uint64_t*)m->mv;
865 m->cost += i_ref_cost;
866 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
869 /* theoretically this should include 4*ref_cost,
870 * but 3 seems a better approximation of cabac. */
871 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
872 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
873 REF_COST( 0, a->l0.me16x16.i_ref );
876 if( a->i_best_satd > a->l0.i_cost8x8 )
877 a->i_best_satd = a->l0.i_cost8x8;
878 h->mb.i_type = P_8x8;
879 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
880 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
881 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
885 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
888 uint8_t **p_fenc = h->mb.pic.p_fenc;
892 /* XXX Needed for x264_mb_predict_mv */
893 h->mb.i_partition = D_16x8;
895 for( i = 0; i < 2; i++ )
897 x264_me_t *l0m = &a->l0.me16x8[i];
898 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
899 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
901 m.i_pixel = PIXEL_16x8;
902 m.p_cost_mv = a->p_cost_mv;
904 LOAD_FENC( &m, p_fenc, 0, 8*i );
906 for( j = 0; j < i_ref8s; j++ )
908 const int i_ref = ref8[j];
909 const int i_ref_cost = REF_COST( 0, i_ref );
910 m.i_ref_cost = i_ref_cost;
913 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
914 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
915 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][2*i+1];
916 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][2*i+2];
918 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
919 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
920 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
921 x264_me_search( h, &m, mvc, 3 );
923 m.cost += i_ref_cost;
925 if( m.cost < l0m->cost )
928 x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, l0m->mv[0], l0m->mv[1] );
929 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
932 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
935 if( a->i_best_satd > a->l0.i_cost16x8 )
936 a->i_best_satd = a->l0.i_cost16x8;
938 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
942 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
945 uint8_t **p_fenc = h->mb.pic.p_fenc;
949 /* XXX Needed for x264_mb_predict_mv */
950 h->mb.i_partition = D_8x16;
952 for( i = 0; i < 2; i++ )
954 x264_me_t *l0m = &a->l0.me8x16[i];
955 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
956 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
958 m.i_pixel = PIXEL_8x16;
959 m.p_cost_mv = a->p_cost_mv;
961 LOAD_FENC( &m, p_fenc, 8*i, 0 );
963 for( j = 0; j < i_ref8s; j++ )
965 const int i_ref = ref8[j];
966 const int i_ref_cost = REF_COST( 0, i_ref );
967 m.i_ref_cost = i_ref_cost;
970 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
971 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][i+1];
972 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][i+3];
974 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
975 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
976 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
977 x264_me_search( h, &m, mvc, 3 );
979 m.cost += i_ref_cost;
981 if( m.cost < l0m->cost )
984 x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, l0m->mv[0], l0m->mv[1] );
985 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
988 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
991 if( a->i_best_satd > a->l0.i_cost8x16 )
992 a->i_best_satd = a->l0.i_cost8x16;
994 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
998 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1000 DECLARE_ALIGNED( uint8_t, pix1[8*8], 8 );
1001 DECLARE_ALIGNED( uint8_t, pix2[8*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], FENC_STRIDE, pix1, 8 )
1028 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][off], FENC_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;
1154 a->i_cost16x16direct = 0;
1155 for( i = 0; i < 4; i++ )
1157 const int x = (i&1)*8;
1158 const int y = (i>>1)*8;
1159 a->i_cost16x16direct +=
1160 a->i_cost8x8direct[i] =
1161 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1164 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1166 a->i_cost16x16direct += a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1170 if( a->i_cost16x16direct < a->i_best_satd )
1171 a->i_best_satd = a->i_cost16x16direct;
1173 h->mb.i_type = B_DIRECT;
1174 a->i_cost16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
1178 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1180 if( h->param.analyse.b_weighted_bipred ) \
1181 h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1182 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1184 h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1187 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1189 uint8_t pix1[16*16], pix2[16*16];
1192 int src2_ref, pix1_ref;
1196 int mvc[8][2], i_mvc;
1197 int i_halfpel_thresh = INT_MAX;
1198 int *p_halfpel_thresh = h->i_ref0>1 ? &i_halfpel_thresh : NULL;
1200 /* 16x16 Search on all ref frame */
1201 m.i_pixel = PIXEL_16x16;
1202 m.p_cost_mv = a->p_cost_mv;
1203 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1206 a->l0.me16x16.cost = INT_MAX;
1207 for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
1209 /* search with ref */
1210 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1211 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1212 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1213 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1216 m.cost += REF_COST( 0, i_ref );
1218 if( m.cost < a->l0.me16x16.cost )
1220 a->l0.i_ref = i_ref;
1224 /* save mv for predicting neighbors */
1225 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1226 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1228 /* subtract ref cost, so we don't have to add it for the other MB types */
1229 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1232 i_halfpel_thresh = INT_MAX;
1233 p_halfpel_thresh = h->i_ref1>1 ? &i_halfpel_thresh : NULL;
1234 a->l1.me16x16.cost = INT_MAX;
1235 for( i_ref = 0; i_ref < h->i_ref1; i_ref++ )
1237 /* search with ref */
1238 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1239 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1240 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1241 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1244 m.cost += REF_COST( 1, i_ref );
1246 if( m.cost < a->l1.me16x16.cost )
1248 a->l1.i_ref = i_ref;
1252 /* save mv for predicting neighbors */
1253 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1254 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1256 /* subtract ref cost, so we don't have to add it for the other MB types */
1257 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1259 /* Set global ref, needed for other modes? */
1260 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1261 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1263 /* get cost of BI mode */
1264 if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
1266 /* l0 reference is halfpel, so get_ref on it will make it faster */
1267 src2 = h->mc.get_ref( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1269 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1271 h->mc.mc_luma( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1273 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1275 src2_ref = a->l0.i_ref;
1276 pix1_ref = a->l1.i_ref;
1280 /* if l0 was qpel, we'll use get_ref on l1 instead */
1281 h->mc.mc_luma( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1283 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1285 src2 = h->mc.get_ref( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1287 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1289 src2_ref = a->l1.i_ref;
1290 pix1_ref = a->l0.i_ref;
1293 if( h->param.analyse.b_weighted_bipred )
1294 h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2,
1295 h->mb.bipred_weight[pix1_ref][src2_ref] );
1297 h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1299 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix1, 16 )
1300 + REF_COST( 0, a->l0.i_ref )
1301 + REF_COST( 1, a->l1.i_ref )
1302 + a->l0.me16x16.cost_mv
1303 + a->l1.me16x16.cost_mv;
1306 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1307 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1308 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1314 if( a->l0.me16x16.cost < a->i_best_satd )
1315 a->i_best_satd = a->l0.me16x16.cost;
1316 if( a->l1.me16x16.cost < a->i_best_satd )
1317 a->i_best_satd = a->l1.me16x16.cost;
1318 if( a->i_cost16x16bi < a->i_best_satd )
1319 a->i_best_satd = a->i_cost16x16bi;
1321 i_satd_thresh = a->i_best_satd * 3/2;
1323 h->mb.i_partition = D_16x16;
1325 if( a->l0.me16x16.cost < i_satd_thresh )
1327 h->mb.i_type = B_L0_L0;
1328 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
1329 a->l0.me16x16.cost = x264_rd_cost_mb( h, a->i_lambda2 );
1332 a->l0.me16x16.cost = COST_MAX;
1335 if( a->l1.me16x16.cost < i_satd_thresh )
1337 h->mb.i_type = B_L1_L1;
1338 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
1339 a->l1.me16x16.cost = x264_rd_cost_mb( h, a->i_lambda2 );
1342 a->l1.me16x16.cost = COST_MAX;
1345 if( a->i_cost16x16bi < i_satd_thresh )
1347 h->mb.i_type = B_BI_BI;
1348 a->i_cost16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1351 a->i_cost16x16bi = COST_MAX;
1355 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1357 const int x = 2*(i%2);
1358 const int y = 2*(i/2);
1360 switch( h->mb.i_sub_partition[i] )
1363 x264_macroblock_cache_mv( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv[0], a->l0.me8x8[i].mv[1] );
1366 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] );
1367 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] );
1370 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] );
1371 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] );
1374 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] );
1375 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] );
1376 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] );
1377 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] );
1380 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1385 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1386 if( x264_mb_partition_listX_table[0][part] ) \
1388 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1389 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
1393 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1394 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0, 0 ); \
1396 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
1398 if( x264_mb_partition_listX_table[1][part] ) \
1400 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1401 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
1405 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1406 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0, 0 ); \
1408 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
1411 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1415 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1417 x264_mb_load_mv_direct8x8( h, i );
1420 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0, 0 );
1421 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0, 0 );
1422 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1427 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1430 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1432 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1434 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1436 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1440 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1442 uint8_t **p_fref[2] =
1443 { h->mb.pic.p_fref[0][a->l0.i_ref],
1444 h->mb.pic.p_fref[1][a->l1.i_ref] };
1445 uint8_t pix[2][8*8];
1448 /* XXX Needed for x264_mb_predict_mv */
1449 h->mb.i_partition = D_8x8;
1453 for( i = 0; i < 4; i++ )
1458 int i_part_cost_bi = 0;
1460 for( l = 0; l < 2; l++ )
1462 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1463 x264_me_t *m = &lX->me8x8[i];
1465 m->i_pixel = PIXEL_8x8;
1466 m->p_cost_mv = a->p_cost_mv;
1468 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1469 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1471 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1472 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1474 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
1477 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1478 m->mv[0], m->mv[1], 8, 8 );
1479 i_part_cost_bi += m->cost_mv;
1480 /* FIXME: ref cost */
1483 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1484 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1485 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1486 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1487 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1489 i_part_cost = a->l0.me8x8[i].cost;
1490 h->mb.i_sub_partition[i] = D_L0_8x8;
1491 if( a->l1.me8x8[i].cost < i_part_cost )
1493 i_part_cost = a->l1.me8x8[i].cost;
1494 h->mb.i_sub_partition[i] = D_L1_8x8;
1496 if( i_part_cost_bi < i_part_cost )
1498 i_part_cost = i_part_cost_bi;
1499 h->mb.i_sub_partition[i] = D_BI_8x8;
1501 if( a->i_cost8x8direct[i] < i_part_cost )
1503 i_part_cost = a->i_cost8x8direct[i];
1504 h->mb.i_sub_partition[i] = D_DIRECT_8x8;
1506 a->i_cost8x8bi += i_part_cost;
1508 /* XXX Needed for x264_mb_predict_mv */
1509 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1513 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1517 if( a->i_cost8x8bi < a->i_best_satd )
1518 a->i_best_satd = a->i_cost8x8bi;
1520 if( a->i_cost8x8bi < a->i_best_satd * 3/2 )
1522 h->mb.i_type = B_8x8;
1523 h->mb.i_partition = D_8x8;
1524 a->i_cost8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1527 a->i_cost8x8bi = COST_MAX;
1531 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1533 uint8_t **p_fref[2] =
1534 { h->mb.pic.p_fref[0][a->l0.i_ref],
1535 h->mb.pic.p_fref[1][a->l1.i_ref] };
1536 DECLARE_ALIGNED( uint8_t, pix[2][16*8], 16 );
1540 h->mb.i_partition = D_16x8;
1541 a->i_cost16x8bi = 0;
1543 for( i = 0; i < 2; i++ )
1546 int i_part_cost_bi = 0;
1548 /* TODO: check only the list(s) that were used in b8x8? */
1549 for( l = 0; l < 2; l++ )
1551 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1552 x264_me_t *m = &lX->me16x8[i];
1554 m->i_pixel = PIXEL_16x8;
1555 m->p_cost_mv = a->p_cost_mv;
1557 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1558 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1560 mvc[0][0] = lX->me8x8[2*i].mv[0];
1561 mvc[0][1] = lX->me8x8[2*i].mv[1];
1562 mvc[1][0] = lX->me8x8[2*i+1].mv[0];
1563 mvc[1][1] = lX->me8x8[2*i+1].mv[1];
1565 x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp );
1566 x264_me_search( h, m, mvc, 2 );
1569 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 16,
1570 m->mv[0], m->mv[1], 16, 8 );
1571 /* FIXME: ref cost */
1572 i_part_cost_bi += m->cost_mv;
1575 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1576 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1578 i_part_cost = a->l0.me16x8[i].cost;
1579 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1580 if( a->l1.me16x8[i].cost < i_part_cost )
1582 i_part_cost = a->l1.me16x8[i].cost;
1583 a->i_mb_partition16x8[i] = D_L1_8x8;
1585 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1587 i_part_cost = i_part_cost_bi;
1588 a->i_mb_partition16x8[i] = D_BI_8x8;
1590 a->i_cost16x8bi += i_part_cost;
1592 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1596 a->i_mb_type16x8 = B_L0_L0
1597 + (a->i_mb_partition16x8[0]>>2) * 3
1598 + (a->i_mb_partition16x8[1]>>2);
1599 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1603 if( a->i_cost16x8bi < a->i_best_satd )
1604 a->i_best_satd = a->i_cost16x8bi;
1606 if( a->i_cost16x8bi < a->i_best_satd * 3/2 )
1608 h->mb.i_type = a->i_mb_type16x8;
1609 h->mb.i_partition = D_16x8;
1610 a->i_cost16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1613 a->i_cost16x8bi = COST_MAX;
1616 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1618 uint8_t **p_fref[2] =
1619 { h->mb.pic.p_fref[0][a->l0.i_ref],
1620 h->mb.pic.p_fref[1][a->l1.i_ref] };
1621 uint8_t pix[2][8*16];
1625 h->mb.i_partition = D_8x16;
1626 a->i_cost8x16bi = 0;
1628 for( i = 0; i < 2; i++ )
1631 int i_part_cost_bi = 0;
1633 for( l = 0; l < 2; l++ )
1635 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1636 x264_me_t *m = &lX->me8x16[i];
1638 m->i_pixel = PIXEL_8x16;
1639 m->p_cost_mv = a->p_cost_mv;
1641 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1642 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1644 mvc[0][0] = lX->me8x8[i].mv[0];
1645 mvc[0][1] = lX->me8x8[i].mv[1];
1646 mvc[1][0] = lX->me8x8[i+2].mv[0];
1647 mvc[1][1] = lX->me8x8[i+2].mv[1];
1649 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1650 x264_me_search( h, m, mvc, 2 );
1653 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1654 m->mv[0], m->mv[1], 8, 16 );
1655 /* FIXME: ref cost */
1656 i_part_cost_bi += m->cost_mv;
1659 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1660 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1662 i_part_cost = a->l0.me8x16[i].cost;
1663 a->i_mb_partition8x16[i] = D_L0_8x8;
1664 if( a->l1.me8x16[i].cost < i_part_cost )
1666 i_part_cost = a->l1.me8x16[i].cost;
1667 a->i_mb_partition8x16[i] = D_L1_8x8;
1669 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1671 i_part_cost = i_part_cost_bi;
1672 a->i_mb_partition8x16[i] = D_BI_8x8;
1674 a->i_cost8x16bi += i_part_cost;
1676 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1680 a->i_mb_type8x16 = B_L0_L0
1681 + (a->i_mb_partition8x16[0]>>2) * 3
1682 + (a->i_mb_partition8x16[1]>>2);
1683 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1687 if( a->i_cost8x16bi < a->i_best_satd )
1688 a->i_best_satd = a->i_cost8x16bi;
1690 if( a->i_cost8x16bi < a->i_best_satd * 3/2 )
1692 h->mb.i_type = a->i_mb_type8x16;
1693 h->mb.i_partition = D_8x16;
1694 a->i_cost8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1697 a->i_cost8x16bi = COST_MAX;
1701 static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
1703 const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1706 switch( h->mb.i_partition )
1709 if( h->mb.i_type == B_BI_BI )
1710 x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
1713 for( i=0; i<2; i++ )
1714 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
1715 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
1718 for( i=0; i<2; i++ )
1719 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
1720 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
1723 for( i=0; i<4; i++ )
1724 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
1725 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
1730 static inline void x264_mb_analyse_transform( x264_t *h )
1732 h->mb.cache.b_transform_8x8_allowed =
1733 h->param.analyse.b_transform_8x8
1734 && !IS_INTRA( h->mb.i_type ) && x264_mb_transform_8x8_allowed( h );
1736 if( h->mb.cache.b_transform_8x8_allowed )
1738 int i_cost4, i_cost8;
1739 /* FIXME only luma mc is needed */
1742 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
1743 h->mb.pic.p_fdec[0], FDEC_STRIDE );
1744 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
1745 h->mb.pic.p_fdec[0], FDEC_STRIDE );
1747 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
1751 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_cost )
1753 h->mb.cache.b_transform_8x8_allowed =
1754 h->param.analyse.b_transform_8x8 && x264_mb_transform_8x8_allowed( h );
1756 if( h->mb.cache.b_transform_8x8_allowed )
1759 x264_analyse_update_cache( h, a );
1760 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1761 /* FIXME only luma is needed, but the score for comparison already includes chroma */
1762 i_cost8 = x264_rd_cost_mb( h, a->i_lambda2 );
1764 if( *i_cost >= i_cost8 )
1767 a->i_best_satd = (int64_t)a->i_best_satd * i_cost8 / *i_cost;
1768 /* prevent a rare division by zero in x264_mb_analyse_intra */
1769 if( a->i_best_satd == 0 )
1775 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1780 /*****************************************************************************
1781 * x264_macroblock_analyse:
1782 *****************************************************************************/
1783 void x264_macroblock_analyse( x264_t *h )
1785 x264_mb_analysis_t analysis;
1786 int i_cost = COST_MAX;
1790 x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
1792 /*--------------------------- Do the analysis ---------------------------*/
1793 if( h->sh.i_type == SLICE_TYPE_I )
1795 x264_mb_analyse_intra( h, &analysis, COST_MAX );
1797 i_cost = analysis.i_sad_i16x16;
1798 h->mb.i_type = I_16x16;
1799 if( analysis.i_sad_i4x4 < i_cost )
1801 i_cost = analysis.i_sad_i4x4;
1802 h->mb.i_type = I_4x4;
1804 if( analysis.i_sad_i8x8 < i_cost )
1805 h->mb.i_type = I_8x8;
1807 else if( h->sh.i_type == SLICE_TYPE_P )
1810 int i_intra_cost, i_intra_type;
1812 /* Fast P_SKIP detection */
1813 analysis.b_try_pskip = 0;
1814 if( h->param.analyse.b_fast_pskip )
1816 if( h->param.analyse.i_subpel_refine >= 3 )
1817 analysis.b_try_pskip = 1;
1818 else if( h->mb.i_mb_type_left == P_SKIP ||
1819 h->mb.i_mb_type_top == P_SKIP ||
1820 h->mb.i_mb_type_topleft == P_SKIP ||
1821 h->mb.i_mb_type_topright == P_SKIP )
1822 b_skip = x264_macroblock_probe_pskip( h );
1827 h->mb.i_type = P_SKIP;
1828 h->mb.i_partition = D_16x16;
1832 const unsigned int flags = h->param.analyse.inter;
1837 x264_mb_analyse_load_costs( h, &analysis );
1839 x264_mb_analyse_inter_p16x16( h, &analysis );
1841 if( h->mb.i_type == P_SKIP )
1844 if( flags & X264_ANALYSE_PSUB16x16 )
1846 if( h->param.analyse.b_mixed_references )
1847 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
1849 x264_mb_analyse_inter_p8x8( h, &analysis );
1852 /* Select best inter mode */
1854 i_partition = D_16x16;
1855 i_cost = analysis.l0.me16x16.cost;
1857 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
1858 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
1863 i_partition = D_8x8;
1864 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1865 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1867 i_cost = analysis.l0.i_cost8x8;
1870 if( flags & X264_ANALYSE_PSUB8x8 )
1872 int i_cost_bak = i_cost;
1874 for( i = 0; i < 4; i++ )
1876 x264_mb_analyse_inter_p4x4( h, &analysis, i );
1877 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
1879 int i_cost8x8 = analysis.l0.i_cost4x4[i];
1880 h->mb.i_sub_partition[i] = D_L0_4x4;
1882 x264_mb_analyse_inter_p8x4( h, &analysis, i );
1883 if( analysis.l0.i_cost8x4[i] < i_cost8x8 )
1885 h->mb.i_sub_partition[i] = D_L0_8x4;
1886 i_cost8x8 = analysis.l0.i_cost8x4[i];
1889 x264_mb_analyse_inter_p4x8( h, &analysis, i );
1890 if( analysis.l0.i_cost4x8[i] < i_cost8x8 )
1892 h->mb.i_sub_partition[i] = D_L0_4x8;
1893 i_cost8x8 = analysis.l0.i_cost4x8[i];
1896 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
1899 x264_mb_cache_mv_p8x8( h, &analysis, i );
1901 /* TODO: RD per subpartition */
1902 if( b_sub8x8 && analysis.b_mbrd )
1904 i_cost = x264_rd_cost_mb( h, analysis.i_lambda2 );
1905 if( i_cost > i_cost_bak )
1907 i_cost = i_cost_bak;
1908 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1909 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1915 /* Now do 16x8/8x16 */
1916 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
1917 if( analysis.b_mbrd )
1918 i_thresh16x8 = i_thresh16x8 * analysis.i_lambda2 / analysis.i_lambda;
1919 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
1920 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
1922 x264_mb_analyse_inter_p16x8( h, &analysis );
1923 if( analysis.l0.i_cost16x8 < i_cost )
1926 i_partition = D_16x8;
1927 i_cost = analysis.l0.i_cost16x8;
1930 x264_mb_analyse_inter_p8x16( h, &analysis );
1931 if( analysis.l0.i_cost8x16 < i_cost )
1934 i_partition = D_8x16;
1935 i_cost = analysis.l0.i_cost8x16;
1939 h->mb.i_partition = i_partition;
1942 //FIXME mb_type costs?
1943 if( analysis.b_mbrd )
1945 h->mb.i_type = i_type;
1946 x264_mb_analyse_transform_rd( h, &analysis, &i_cost );
1948 else if( i_partition == D_16x16 )
1950 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1951 i_cost = analysis.l0.me16x16.cost;
1953 else if( i_partition == D_16x8 )
1955 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
1956 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
1957 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
1959 else if( i_partition == D_8x16 )
1961 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
1962 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
1963 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
1965 else if( i_partition == D_8x8 )
1969 for( i8x8 = 0; i8x8 < 4; i8x8++ )
1971 switch( h->mb.i_sub_partition[i8x8] )
1974 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
1975 i_cost += analysis.l0.me8x8[i8x8].cost;
1978 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
1979 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
1980 i_cost += analysis.l0.me8x4[i8x8][0].cost +
1981 analysis.l0.me8x4[i8x8][1].cost;
1984 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
1985 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
1986 i_cost += analysis.l0.me4x8[i8x8][0].cost +
1987 analysis.l0.me4x8[i8x8][1].cost;
1991 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
1992 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
1993 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
1994 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
1995 i_cost += analysis.l0.me4x4[i8x8][0].cost +
1996 analysis.l0.me4x4[i8x8][1].cost +
1997 analysis.l0.me4x4[i8x8][2].cost +
1998 analysis.l0.me4x4[i8x8][3].cost;
2001 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2007 x264_mb_analyse_intra( h, &analysis, i_cost );
2008 if( h->mb.b_chroma_me && !analysis.b_mbrd &&
2009 ( analysis.i_sad_i16x16 < i_cost
2010 || analysis.i_sad_i8x8 < i_cost
2011 || analysis.i_sad_i4x4 < i_cost ))
2013 x264_mb_analyse_intra_chroma( h, &analysis );
2014 analysis.i_sad_i16x16 += analysis.i_sad_i8x8chroma;
2015 analysis.i_sad_i8x8 += analysis.i_sad_i8x8chroma;
2016 analysis.i_sad_i4x4 += analysis.i_sad_i8x8chroma;
2019 i_intra_type = I_16x16;
2020 i_intra_cost = analysis.i_sad_i16x16;
2022 if( analysis.i_sad_i8x8 < i_intra_cost )
2024 i_intra_type = I_8x8;
2025 i_intra_cost = analysis.i_sad_i8x8;
2027 if( analysis.i_sad_i4x4 < i_intra_cost )
2029 i_intra_type = I_4x4;
2030 i_intra_cost = analysis.i_sad_i4x4;
2033 if( i_intra_cost < i_cost )
2035 i_type = i_intra_type;
2036 i_cost = i_intra_cost;
2039 h->mb.i_type = i_type;
2040 h->stat.frame.i_intra_cost += i_intra_cost;
2041 h->stat.frame.i_inter_cost += i_cost;
2044 else if( h->sh.i_type == SLICE_TYPE_B )
2046 int i_bskip_cost = COST_MAX;
2049 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h );
2050 if( analysis.b_direct_available )
2052 h->mb.i_type = B_SKIP;
2054 if( h->mb.b_lossless )
2056 /* chance of skip is too small to bother */
2058 else if( analysis.b_mbrd )
2060 i_bskip_cost = ssd_mb( h );
2062 /* 6 = minimum cavlc cost of a non-skipped MB */
2063 if( i_bskip_cost <= 6 * analysis.i_lambda2 )
2065 h->mb.i_type = B_SKIP;
2066 x264_analyse_update_cache( h, &analysis );
2072 /* Conditioning the probe on neighboring block types
2073 * doesn't seem to help speed or quality. */
2074 b_skip = x264_macroblock_probe_bskip( h );
2080 const unsigned int flags = h->param.analyse.inter;
2084 x264_mb_analyse_load_costs( h, &analysis );
2086 /* select best inter mode */
2087 /* direct must be first */
2088 if( analysis.b_direct_available )
2089 x264_mb_analyse_inter_direct( h, &analysis );
2091 x264_mb_analyse_inter_b16x16( h, &analysis );
2094 i_partition = D_16x16;
2095 i_cost = analysis.l0.me16x16.cost;
2096 if( analysis.l1.me16x16.cost < i_cost )
2099 i_cost = analysis.l1.me16x16.cost;
2101 if( analysis.i_cost16x16bi < i_cost )
2104 i_cost = analysis.i_cost16x16bi;
2106 if( analysis.i_cost16x16direct < i_cost )
2109 i_cost = analysis.i_cost16x16direct;
2112 if( i_bskip_cost <= i_cost )
2114 h->mb.i_type = B_SKIP;
2115 x264_analyse_update_cache( h, &analysis );
2119 if( flags & X264_ANALYSE_BSUB16x16 )
2121 x264_mb_analyse_inter_b8x8( h, &analysis );
2122 if( analysis.i_cost8x8bi < i_cost )
2125 i_partition = D_8x8;
2126 i_cost = analysis.i_cost8x8bi;
2128 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2129 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2131 x264_mb_analyse_inter_b16x8( h, &analysis );
2132 if( analysis.i_cost16x8bi < i_cost )
2134 i_partition = D_16x8;
2135 i_cost = analysis.i_cost16x8bi;
2136 i_type = analysis.i_mb_type16x8;
2139 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2140 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2142 x264_mb_analyse_inter_b8x16( h, &analysis );
2143 if( analysis.i_cost8x16bi < i_cost )
2145 i_partition = D_8x16;
2146 i_cost = analysis.i_cost8x16bi;
2147 i_type = analysis.i_mb_type8x16;
2153 h->mb.i_partition = i_partition;
2155 if( analysis.b_mbrd )
2157 h->mb.i_type = i_type;
2158 x264_mb_analyse_transform_rd( h, &analysis, &i_cost );
2161 else if( i_partition == D_16x16 )
2163 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2164 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2165 if( i_type == B_L0_L0 )
2167 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2168 i_cost = analysis.l0.me16x16.cost
2169 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2171 else if( i_type == B_L1_L1 )
2173 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2174 i_cost = analysis.l1.me16x16.cost
2175 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2177 else if( i_type == B_BI_BI )
2179 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2180 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2183 else if( i_partition == D_16x8 )
2185 for( i=0; i<2; i++ )
2187 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2188 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2189 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2190 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2193 else if( i_partition == D_8x16 )
2195 for( i=0; i<2; i++ )
2197 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2198 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2199 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2200 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2203 else if( i_partition == D_8x8 )
2205 for( i=0; i<4; i++ )
2208 int i_part_cost_old;
2210 int i_part_type = h->mb.i_sub_partition[i];
2211 int b_bidir = (i_part_type == D_BI_8x8);
2213 if( i_part_type == D_DIRECT_8x8 )
2215 if( x264_mb_partition_listX_table[0][i_part_type] )
2217 m = &analysis.l0.me8x8[i];
2218 i_part_cost_old = m->cost;
2219 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2220 m->cost -= i_type_cost;
2221 x264_me_refine_qpel( h, m );
2223 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2225 if( x264_mb_partition_listX_table[1][i_part_type] )
2227 m = &analysis.l1.me8x8[i];
2228 i_part_cost_old = m->cost;
2229 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2230 m->cost -= i_type_cost;
2231 x264_me_refine_qpel( h, m );
2233 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2235 /* TODO: update mvp? */
2239 /* best intra mode */
2240 x264_mb_analyse_intra( h, &analysis, i_cost );
2242 if( analysis.i_sad_i16x16 < i_cost )
2245 i_cost = analysis.i_sad_i16x16;
2247 if( analysis.i_sad_i8x8 < i_cost )
2250 i_cost = analysis.i_sad_i8x8;
2252 if( analysis.i_sad_i4x4 < i_cost )
2255 i_cost = analysis.i_sad_i4x4;
2258 h->mb.i_type = i_type;
2260 if( h->param.analyse.b_bidir_me )
2261 refine_bidir( h, &analysis );
2265 x264_analyse_update_cache( h, &analysis );
2267 if( !analysis.b_mbrd )
2268 x264_mb_analyse_transform( h );
2270 h->mb.b_trellis = h->param.analyse.i_trellis;
2271 h->mb.b_noise_reduction = h->param.analyse.i_noise_reduction;
2274 /*-------------------- Update MB from the analysis ----------------------*/
2275 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2279 switch( h->mb.i_type )
2282 for( i = 0; i < 16; i++ )
2284 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] =
2285 a->i_predict4x4[block_idx_x[i]][block_idx_y[i]];
2288 x264_mb_analyse_intra_chroma( h, a );
2291 for( i = 0; i < 4; i++ )
2292 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1),
2293 a->i_predict8x8[i&1][i>>1] );
2295 x264_mb_analyse_intra_chroma( h, a );
2298 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2299 x264_mb_analyse_intra_chroma( h, a );
2303 switch( h->mb.i_partition )
2306 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2307 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2311 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2312 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2313 x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv[0], a->l0.me16x8[0].mv[1] );
2314 x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv[0], a->l0.me16x8[1].mv[1] );
2318 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2319 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2320 x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv[0], a->l0.me8x16[0].mv[1] );
2321 x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv[0], a->l0.me8x16[1].mv[1] );
2325 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2331 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2332 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2333 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2334 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2335 for( i = 0; i < 4; i++ )
2336 x264_mb_cache_mv_p8x8( h, a, i );
2342 x264_mb_predict_mv_pskip( h, mvp );
2344 h->mb.i_partition = D_16x16;
2345 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2346 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, mvp[0], mvp[1] );
2352 x264_mb_load_mv_direct8x8( h, 0 );
2353 x264_mb_load_mv_direct8x8( h, 1 );
2354 x264_mb_load_mv_direct8x8( h, 2 );
2355 x264_mb_load_mv_direct8x8( h, 3 );
2359 /* optimize: cache might not need to be rewritten */
2360 for( i = 0; i < 4; i++ )
2361 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2364 default: /* the rest of the B types */
2365 switch( h->mb.i_partition )
2368 switch( h->mb.i_type )
2371 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2372 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2374 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2375 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0, 0 );
2376 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0, 0 );
2379 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2380 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0, 0 );
2381 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0, 0 );
2383 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2384 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2387 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2388 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2390 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2391 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2396 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2397 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2400 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2401 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2404 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2410 #include "slicetype_decision.c"