1 /*****************************************************************************
2 * analyse.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003 x264 project
5 * $Id: analyse.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Loren Merritt <lorenm@u.washington.edu>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
31 #include "common/common.h"
32 #include "macroblock.h"
34 #include "ratecontrol.h"
46 int mvc[16][5][2]; /* [ref][0] is 16x16 mv,
47 [ref][1..4] are 8x8 mv from partition [0..3] */
51 int i_cost4x4[4]; /* cost per 8x8 partition */
52 x264_me_t me4x4[4][4];
55 int i_cost8x4[4]; /* cost per 8x8 partition */
56 x264_me_t me8x4[4][2];
59 int i_cost4x8[4]; /* cost per 8x8 partition */
60 x264_me_t me4x8[4][4];
70 } x264_mb_analysis_list_t;
74 /* conduct the analysis using this lamda and QP */
83 /* Take some shortcuts in intra search if intra is deemed unlikely */
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*h->param.analyse.i_mv_range + 1) * sizeof(int16_t) );
172 p_cost_mv[a->i_qp] += 2*4*h->param.analyse.i_mv_range;
173 for( i = 0; i <= 2*4*h->param.analyse.i_mv_range; 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 && h->sh.i_type != SLICE_TYPE_B;
193 h->mb.i_me_method = h->param.analyse.i_me_method;
194 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
195 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
196 && h->mb.i_subpel_refine >= 5;
197 h->mb.b_trellis = h->param.analyse.i_trellis > 1;
198 h->mb.b_transform_8x8 = 0;
204 a->i_sad_i8x8chroma = COST_MAX;
207 a->i_best_satd = COST_MAX;
209 /* II: Inter part P/B frame */
210 if( h->sh.i_type != SLICE_TYPE_I )
213 int i_fmv_range = h->param.analyse.i_mv_range - 16;
215 /* Calculate max allowed MV range */
216 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range )
217 h->mb.mv_min_fpel[0] = CLIP_FMV( -16*h->mb.i_mb_x - 8 );
218 h->mb.mv_max_fpel[0] = CLIP_FMV( 16*( h->sps->i_mb_width - h->mb.i_mb_x ) - 8 );
219 h->mb.mv_min[0] = 4*( h->mb.mv_min_fpel[0] - 16 );
220 h->mb.mv_max[0] = 4*( h->mb.mv_max_fpel[0] + 16 );
221 if( h->mb.i_mb_x == 0)
223 h->mb.mv_min_fpel[1] = CLIP_FMV( -16*h->mb.i_mb_y - 8 );
224 h->mb.mv_max_fpel[1] = CLIP_FMV( 16*( h->sps->i_mb_height - h->mb.i_mb_y ) - 8 );
225 h->mb.mv_min[1] = 4*( h->mb.mv_min_fpel[1] - 16 );
226 h->mb.mv_max[1] = 4*( h->mb.mv_max_fpel[1] + 16 );
231 a->l0.i_cost8x8 = COST_MAX;
233 for( i = 0; i < 4; i++ )
237 a->l0.i_cost4x8[i] = COST_MAX;
241 a->l0.i_cost8x16 = COST_MAX;
242 if( h->sh.i_type == SLICE_TYPE_B )
245 a->l1.i_cost8x8 = COST_MAX;
247 for( i = 0; i < 4; i++ )
252 a->i_cost8x8direct[i] = COST_MAX;
259 a->i_cost16x16direct =
262 a->i_cost8x16bi = COST_MAX;
265 /* Fast intra decision */
266 if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
269 || IS_INTRA( h->mb.i_mb_type_left )
270 || IS_INTRA( h->mb.i_mb_type_top )
271 || IS_INTRA( h->mb.i_mb_type_topleft )
272 || IS_INTRA( h->mb.i_mb_type_topright )
273 || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
274 || (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])) )
275 { /* intra is likely */ }
290 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
292 if( i_neighbour & MB_TOPLEFT )
294 /* top and left avaible */
295 *mode++ = I_PRED_16x16_V;
296 *mode++ = I_PRED_16x16_H;
297 *mode++ = I_PRED_16x16_DC;
298 *mode++ = I_PRED_16x16_P;
301 else if( i_neighbour & MB_LEFT )
304 *mode++ = I_PRED_16x16_DC_LEFT;
305 *mode++ = I_PRED_16x16_H;
308 else if( i_neighbour & MB_TOP )
311 *mode++ = I_PRED_16x16_DC_TOP;
312 *mode++ = I_PRED_16x16_V;
318 *mode = I_PRED_16x16_DC_128;
324 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
326 if( i_neighbour & MB_TOPLEFT )
328 /* top and left avaible */
329 *mode++ = I_PRED_CHROMA_V;
330 *mode++ = I_PRED_CHROMA_H;
331 *mode++ = I_PRED_CHROMA_DC;
332 *mode++ = I_PRED_CHROMA_P;
335 else if( i_neighbour & MB_LEFT )
338 *mode++ = I_PRED_CHROMA_DC_LEFT;
339 *mode++ = I_PRED_CHROMA_H;
342 else if( i_neighbour & MB_TOP )
345 *mode++ = I_PRED_CHROMA_DC_TOP;
346 *mode++ = I_PRED_CHROMA_V;
352 *mode = I_PRED_CHROMA_DC_128;
358 static void predict_4x4_mode_available( unsigned int i_neighbour,
359 int *mode, int *pi_count )
361 int b_l = i_neighbour & MB_LEFT;
362 int b_t = i_neighbour & MB_TOP;
366 *mode++ = I_PRED_4x4_DC;
367 *mode++ = I_PRED_4x4_H;
368 *mode++ = I_PRED_4x4_V;
369 *mode++ = I_PRED_4x4_DDL;
370 *mode++ = I_PRED_4x4_DDR;
371 *mode++ = I_PRED_4x4_VR;
372 *mode++ = I_PRED_4x4_HD;
373 *mode++ = I_PRED_4x4_VL;
374 *mode++ = I_PRED_4x4_HU;
379 *mode++ = I_PRED_4x4_DC_LEFT;
380 *mode++ = I_PRED_4x4_H;
381 *mode++ = I_PRED_4x4_HU;
386 *mode++ = I_PRED_4x4_DC_TOP;
387 *mode++ = I_PRED_4x4_V;
388 *mode++ = I_PRED_4x4_DDL;
389 *mode++ = I_PRED_4x4_VL;
394 *mode++ = I_PRED_4x4_DC_128;
399 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
406 uint8_t *p_dstc[2], *p_srcc[2];
409 if( a->i_sad_i8x8chroma < COST_MAX )
412 /* 8x8 prediction selection for chroma */
413 p_dstc[0] = h->mb.pic.p_fdec[1];
414 p_dstc[1] = h->mb.pic.p_fdec[2];
415 p_srcc[0] = h->mb.pic.p_fenc[1];
416 p_srcc[1] = h->mb.pic.p_fenc[2];
418 i_stride[0] = h->mb.pic.i_stride[1];
419 i_stride[1] = h->mb.pic.i_stride[2];
421 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
422 a->i_sad_i8x8chroma = COST_MAX;
423 for( i = 0; i < i_max; i++ )
428 i_mode = predict_mode[i];
430 /* we do the prediction */
431 h->predict_8x8c[i_mode]( p_dstc[0], i_stride[0] );
432 h->predict_8x8c[i_mode]( p_dstc[1], i_stride[1] );
434 /* we calculate the cost */
435 i_sad = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], i_stride[0],
436 p_srcc[0], i_stride[0] ) +
437 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], i_stride[1],
438 p_srcc[1], i_stride[1] ) +
439 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
441 /* if i_score is lower it is better */
442 if( a->i_sad_i8x8chroma > i_sad )
444 a->i_predict8x8chroma = i_mode;
445 a->i_sad_i8x8chroma = i_sad;
449 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
452 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_cost_inter )
454 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
455 const int i_stride = h->mb.pic.i_stride[0];
456 uint8_t *p_src = h->mb.pic.p_fenc[0];
457 uint8_t *p_dst = h->mb.pic.p_fdec[0];
458 int f8_satd_rd_ratio = 0;
464 const int i_satd_thresh = a->i_best_satd * 5/4 + a->i_lambda * 10;
466 /*---------------- Try all mode and calculate their score ---------------*/
468 /* 16x16 prediction selection */
469 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
470 for( i = 0; i < i_max; i++ )
475 i_mode = predict_mode[i];
476 h->predict_16x16[i_mode]( p_dst, i_stride );
478 i_sad = h->pixf.mbcmp[PIXEL_16x16]( p_dst, i_stride, p_src, i_stride ) +
479 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
480 if( a->i_sad_i16x16 > i_sad )
482 a->i_predict16x16 = i_mode;
483 a->i_sad_i16x16 = i_sad;
489 f8_satd_rd_ratio = ((unsigned)i_cost_inter << 8) / a->i_best_satd + 1;
490 x264_mb_analyse_intra_chroma( h, a );
491 if( h->mb.b_chroma_me )
492 a->i_sad_i16x16 += a->i_sad_i8x8chroma;
493 if( a->i_sad_i16x16 < i_satd_thresh )
495 h->mb.i_type = I_16x16;
496 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
497 a->i_sad_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
500 a->i_sad_i16x16 = a->i_sad_i16x16 * f8_satd_rd_ratio >> 8;
504 if( h->sh.i_type == SLICE_TYPE_B )
505 /* cavlc mb type prefix */
506 a->i_sad_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
507 if( a->b_fast_intra && a->i_sad_i16x16 > 2*i_cost_inter )
511 /* 4x4 prediction selection */
512 if( flags & X264_ANALYSE_I4x4 )
515 for( idx = 0; idx < 16; idx++ )
523 i_pred_mode= x264_mb_predict_intra4x4_mode( h, idx );
524 x = block_idx_x[idx];
525 y = block_idx_y[idx];
527 p_src_by = p_src + 4 * x + 4 * y * i_stride;
528 p_dst_by = p_dst + 4 * x + 4 * y * i_stride;
531 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
533 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
534 /* emulate missing topright samples */
535 *(uint32_t*) &p_dst_by[4 - i_stride] = p_dst_by[3 - i_stride] * 0x01010101U;
537 for( i = 0; i < i_max; i++ )
542 i_mode = predict_mode[i];
543 h->predict_4x4[i_mode]( p_dst_by, i_stride );
545 i_sad = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, i_stride,
547 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
551 a->i_predict4x4[x][y] = i_mode;
555 a->i_sad_i4x4 += i_best;
557 /* we need to encode this block now (for next ones) */
558 h->predict_4x4[a->i_predict4x4[x][y]]( p_dst_by, i_stride );
559 x264_mb_encode_i4x4( h, idx, a->i_qp );
561 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[x][y];
564 a->i_sad_i4x4 += a->i_lambda * 24; /* from JVT (SATD0) */
567 if( h->mb.b_chroma_me )
568 a->i_sad_i4x4 += a->i_sad_i8x8chroma;
569 if( a->i_sad_i4x4 < i_satd_thresh )
571 h->mb.i_type = I_4x4;
572 a->i_sad_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
575 a->i_sad_i4x4 = a->i_sad_i4x4 * f8_satd_rd_ratio >> 8;
579 if( h->sh.i_type == SLICE_TYPE_B )
580 a->i_sad_i4x4 += a->i_lambda * i_mb_b_cost_table[I_4x4];
584 /* 8x8 prediction selection */
585 if( flags & X264_ANALYSE_I8x8 )
588 for( idx = 0; idx < 4; idx++ )
596 i_pred_mode= x264_mb_predict_intra4x4_mode( h, 4*idx );
600 p_src_by = p_src + 8 * x + 8 * y * i_stride;
601 p_dst_by = p_dst + 8 * x + 8 * y * i_stride;
604 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
605 for( i = 0; i < i_max; i++ )
610 i_mode = predict_mode[i];
611 h->predict_8x8[i_mode]( p_dst_by, i_stride, h->mb.i_neighbour );
613 /* could use sa8d, but it doesn't seem worth the speed cost (without mmx at least) */
614 i_sad = h->pixf.mbcmp[PIXEL_8x8]( p_dst_by, i_stride,
616 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
620 a->i_predict8x8[x][y] = i_mode;
624 a->i_sad_i8x8 += i_best;
626 /* we need to encode this block now (for next ones) */
627 h->predict_8x8[a->i_predict8x8[x][y]]( p_dst_by, i_stride, h->mb.i_neighbour );
628 x264_mb_encode_i8x8( h, idx, a->i_qp );
630 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[x][y] );
635 if( h->mb.b_chroma_me )
636 a->i_sad_i8x8 += a->i_sad_i8x8chroma;
637 if( a->i_sad_i8x8 < i_satd_thresh )
639 h->mb.i_type = I_8x8;
640 a->i_sad_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
643 a->i_sad_i8x8 = a->i_sad_i8x8 * f8_satd_rd_ratio >> 8;
647 // FIXME some bias like in i4x4?
648 if( h->sh.i_type == SLICE_TYPE_B )
649 a->i_sad_i8x8 += a->i_lambda * i_mb_b_cost_table[I_8x8];
654 #define LOAD_FENC( m, src, xoff, yoff) \
655 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
656 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
657 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
658 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
659 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]];
661 #define LOAD_HPELS(m, src, xoff, yoff) \
662 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
663 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
664 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
665 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
666 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
667 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]];
669 #define REF_COST(list, ref) \
670 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
672 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
676 int mvc[7][2], i_mvc;
677 int i_halfpel_thresh = INT_MAX;
678 int *p_halfpel_thresh = h->i_ref0>1 ? &i_halfpel_thresh : NULL;
680 /* 16x16 Search on all ref frame */
681 m.i_pixel = PIXEL_16x16;
682 m.p_cost_mv = a->p_cost_mv;
683 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
685 a->l0.me16x16.cost = INT_MAX;
686 for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
688 const int i_ref_cost = REF_COST( 0, i_ref );
689 i_halfpel_thresh -= i_ref_cost;
690 m.i_ref_cost = i_ref_cost;
693 /* search with ref */
694 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, 0 );
695 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
696 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
697 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
699 m.cost += i_ref_cost;
700 i_halfpel_thresh += i_ref_cost;
702 if( m.cost < a->l0.me16x16.cost )
705 /* save mv for predicting neighbors */
706 a->l0.mvc[i_ref][0][0] =
707 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
708 a->l0.mvc[i_ref][0][1] =
709 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
712 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
716 a->i_best_satd = a->l0.me16x16.cost;
718 h->mb.i_partition = D_16x16;
719 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
720 a->l0.me16x16.cost = x264_rd_cost_mb( h, a->i_lambda2 );
724 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
728 uint8_t **p_fenc = h->mb.pic.p_fenc;
729 int i_halfpel_thresh = INT_MAX;
730 int *p_halfpel_thresh = /*h->i_ref0>1 ? &i_halfpel_thresh : */NULL;
732 int i_maxref = h->i_ref0-1;
734 h->mb.i_partition = D_8x8;
736 /* early termination: if 16x16 chose ref 0, then evalute no refs older
737 * than those used by the neighbors */
738 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
739 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
742 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
743 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
744 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
745 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
746 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
747 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
750 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
752 a->l0.mvc[i_ref][0][0] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0];
753 a->l0.mvc[i_ref][0][1] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1];
756 for( i = 0; i < 4; i++ )
758 x264_me_t *l0m = &a->l0.me8x8[i];
762 m.i_pixel = PIXEL_8x8;
763 m.p_cost_mv = a->p_cost_mv;
765 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
767 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
769 const int i_ref_cost = REF_COST( 0, i_ref );
770 i_halfpel_thresh -= i_ref_cost;
771 m.i_ref_cost = i_ref_cost;
774 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 8*x8, 8*y8 );
775 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
776 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
777 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
779 m.cost += i_ref_cost;
780 i_halfpel_thresh += i_ref_cost;
781 *(uint64_t*)a->l0.mvc[i_ref][i+1] = *(uint64_t*)m.mv;
783 if( m.cost < l0m->cost )
786 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv[0], l0m->mv[1] );
787 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
790 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
793 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
794 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
797 if( a->i_best_satd > a->l0.i_cost8x8 )
798 a->i_best_satd = a->l0.i_cost8x8;
799 h->mb.i_type = P_8x8;
800 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
801 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
802 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
806 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
808 const int i_ref = a->l0.me16x16.i_ref;
809 const int i_ref_cost = REF_COST( 0, i_ref );
810 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
811 uint8_t **p_fenc = h->mb.pic.p_fenc;
813 int (*mvc)[2] = a->l0.mvc[i_ref];
816 /* XXX Needed for x264_mb_predict_mv */
817 h->mb.i_partition = D_8x8;
820 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.me16x16.mv;
822 for( i = 0; i < 4; i++ )
824 x264_me_t *m = &a->l0.me8x8[i];
828 m->i_pixel = PIXEL_8x8;
829 m->p_cost_mv = a->p_cost_mv;
830 m->i_ref_cost = i_ref_cost;
833 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
834 LOAD_HPELS( m, p_fref, 8*x8, 8*y8 );
835 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
836 x264_me_search( h, m, mvc, i_mvc );
838 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
840 *(uint64_t*)mvc[i_mvc] = *(uint64_t*)m->mv;
844 m->cost += i_ref_cost;
845 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
848 /* theoretically this should include 4*ref_cost,
849 * but 3 seems a better approximation of cabac. */
850 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
851 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
852 REF_COST( 0, a->l0.me16x16.i_ref );
855 if( a->i_best_satd > a->l0.i_cost8x8 )
856 a->i_best_satd = a->l0.i_cost8x8;
857 h->mb.i_type = P_8x8;
858 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
859 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
860 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
864 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
867 uint8_t **p_fenc = h->mb.pic.p_fenc;
871 /* XXX Needed for x264_mb_predict_mv */
872 h->mb.i_partition = D_16x8;
874 for( i = 0; i < 2; i++ )
876 x264_me_t *l0m = &a->l0.me16x8[i];
877 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
878 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
880 m.i_pixel = PIXEL_16x8;
881 m.p_cost_mv = a->p_cost_mv;
883 LOAD_FENC( &m, p_fenc, 0, 8*i );
885 for( j = 0; j < i_ref8s; j++ )
887 const int i_ref = ref8[j];
888 const int i_ref_cost = REF_COST( 0, i_ref );
889 m.i_ref_cost = i_ref_cost;
892 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
893 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
894 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][2*i+1];
895 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][2*i+2];
897 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, 8*i );
898 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
899 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
900 x264_me_search( h, &m, mvc, 3 );
902 m.cost += i_ref_cost;
904 if( m.cost < l0m->cost )
907 x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, l0m->mv[0], l0m->mv[1] );
908 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
911 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
914 if( a->i_best_satd > a->l0.i_cost16x8 )
915 a->i_best_satd = a->l0.i_cost16x8;
917 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
921 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
924 uint8_t **p_fenc = h->mb.pic.p_fenc;
928 /* XXX Needed for x264_mb_predict_mv */
929 h->mb.i_partition = D_8x16;
931 for( i = 0; i < 2; i++ )
933 x264_me_t *l0m = &a->l0.me8x16[i];
934 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
935 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
937 m.i_pixel = PIXEL_8x16;
938 m.p_cost_mv = a->p_cost_mv;
940 LOAD_FENC( &m, p_fenc, 8*i, 0 );
942 for( j = 0; j < i_ref8s; j++ )
944 const int i_ref = ref8[j];
945 const int i_ref_cost = REF_COST( 0, i_ref );
946 m.i_ref_cost = i_ref_cost;
949 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
950 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][i+1];
951 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][i+3];
953 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 8*i, 0 );
954 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
955 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
956 x264_me_search( h, &m, mvc, 3 );
958 m.cost += i_ref_cost;
960 if( m.cost < l0m->cost )
963 x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, l0m->mv[0], l0m->mv[1] );
964 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
967 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
970 if( a->i_best_satd > a->l0.i_cost8x16 )
971 a->i_best_satd = a->l0.i_cost8x16;
973 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
977 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
979 uint8_t pix1[8*8], pix2[8*8];
980 const int i_stride = h->mb.pic.i_stride[1];
981 const int off = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
983 #define CHROMA4x4MC( width, height, me, x, y ) \
984 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 ); \
985 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 );
987 if( pixel == PIXEL_4x4 )
989 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
990 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 0,2 );
991 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 2,0 );
992 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
994 else if( pixel == PIXEL_8x4 )
996 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
997 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1001 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1002 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1005 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][off], i_stride, pix1, 8 )
1006 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][off], i_stride, pix2, 8 );
1009 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1011 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1012 uint8_t **p_fenc = h->mb.pic.p_fenc;
1016 /* XXX Needed for x264_mb_predict_mv */
1017 h->mb.i_partition = D_8x8;
1019 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1021 const int idx = 4*i8x8 + i4x4;
1022 const int x4 = block_idx_x[idx];
1023 const int y4 = block_idx_y[idx];
1024 const int i_mvc = (i4x4 == 0);
1026 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1028 m->i_pixel = PIXEL_4x4;
1029 m->p_cost_mv = a->p_cost_mv;
1031 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1032 LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
1034 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1035 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1037 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
1039 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1040 a->l0.me4x4[i8x8][1].cost +
1041 a->l0.me4x4[i8x8][2].cost +
1042 a->l0.me4x4[i8x8][3].cost +
1043 REF_COST( 0, a->l0.me8x8[i8x8].i_ref ) +
1044 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1045 if( h->mb.b_chroma_me )
1046 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1049 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1051 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1052 uint8_t **p_fenc = h->mb.pic.p_fenc;
1056 /* XXX Needed for x264_mb_predict_mv */
1057 h->mb.i_partition = D_8x8;
1059 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1061 const int idx = 4*i8x8 + 2*i8x4;
1062 const int x4 = block_idx_x[idx];
1063 const int y4 = block_idx_y[idx];
1064 const int i_mvc = (i8x4 == 0);
1066 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1068 m->i_pixel = PIXEL_8x4;
1069 m->p_cost_mv = a->p_cost_mv;
1071 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1072 LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
1074 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1075 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1077 x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
1079 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1080 REF_COST( 0, a->l0.me8x8[i8x8].i_ref ) +
1081 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1082 if( h->mb.b_chroma_me )
1083 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1086 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1088 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1089 uint8_t **p_fenc = h->mb.pic.p_fenc;
1093 /* XXX Needed for x264_mb_predict_mv */
1094 h->mb.i_partition = D_8x8;
1096 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1098 const int idx = 4*i8x8 + i4x8;
1099 const int x4 = block_idx_x[idx];
1100 const int y4 = block_idx_y[idx];
1101 const int i_mvc = (i4x8 == 0);
1103 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1105 m->i_pixel = PIXEL_4x8;
1106 m->p_cost_mv = a->p_cost_mv;
1108 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1109 LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
1111 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1112 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1114 x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
1116 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1117 REF_COST( 0, a->l0.me8x8[i8x8].i_ref ) +
1118 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1119 if( h->mb.b_chroma_me )
1120 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1123 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1125 /* Assumes that fdec still contains the results of
1126 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1128 uint8_t **p_fenc = h->mb.pic.p_fenc;
1129 uint8_t **p_fdec = h->mb.pic.p_fdec;
1130 int i_stride= h->mb.pic.i_stride[0];
1133 a->i_cost16x16direct = 0;
1134 for( i = 0; i < 4; i++ )
1138 const int off = 8 * x8 + 8 * i_stride * y8;
1139 a->i_cost16x16direct +=
1140 a->i_cost8x8direct[i] =
1141 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][off], i_stride, &p_fdec[0][off], i_stride );
1144 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1147 a->i_cost16x16direct += a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1150 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1152 if( h->param.analyse.b_weighted_bipred ) \
1153 h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1154 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1156 h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1159 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1161 uint8_t pix1[16*16], pix2[16*16];
1164 int src2_ref, pix1_ref;
1168 int mvc[8][2], i_mvc;
1169 int i_halfpel_thresh = INT_MAX;
1170 int *p_halfpel_thresh = h->i_ref0>1 ? &i_halfpel_thresh : NULL;
1172 /* 16x16 Search on all ref frame */
1173 m.i_pixel = PIXEL_16x16;
1174 m.p_cost_mv = a->p_cost_mv;
1175 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1178 a->l0.me16x16.cost = INT_MAX;
1179 for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
1181 /* search with ref */
1182 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, 0 );
1183 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1184 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1185 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1188 m.cost += REF_COST( 0, i_ref );
1190 if( m.cost < a->l0.me16x16.cost )
1192 a->l0.i_ref = i_ref;
1196 /* save mv for predicting neighbors */
1197 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1198 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1200 /* subtract ref cost, so we don't have to add it for the other MB types */
1201 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1204 i_halfpel_thresh = INT_MAX;
1205 p_halfpel_thresh = h->i_ref1>1 ? &i_halfpel_thresh : NULL;
1206 a->l1.me16x16.cost = INT_MAX;
1207 for( i_ref = 0; i_ref < h->i_ref1; i_ref++ )
1209 /* search with ref */
1210 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 0, 0 );
1211 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1212 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1213 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1216 m.cost += REF_COST( 1, i_ref );
1218 if( m.cost < a->l1.me16x16.cost )
1220 a->l1.i_ref = i_ref;
1224 /* save mv for predicting neighbors */
1225 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1226 h->mb.mvr[1][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->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1231 /* Set global ref, needed for other modes? */
1232 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1233 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1235 /* get cost of BI mode */
1236 if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
1238 /* l0 reference is halfpel, so get_ref on it will make it faster */
1239 src2 = h->mc.get_ref( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1241 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1243 h->mc.mc_luma( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1245 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1247 src2_ref = a->l0.i_ref;
1248 pix1_ref = a->l1.i_ref;
1252 /* if l0 was qpel, we'll use get_ref on l1 instead */
1253 h->mc.mc_luma( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1255 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1257 src2 = h->mc.get_ref( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1259 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1261 src2_ref = a->l1.i_ref;
1262 pix1_ref = a->l0.i_ref;
1265 if( h->param.analyse.b_weighted_bipred )
1266 h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2,
1267 h->mb.bipred_weight[pix1_ref][src2_ref] );
1269 h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1271 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0], pix1, 16 )
1272 + REF_COST( 0, a->l0.i_ref )
1273 + REF_COST( 1, a->l1.i_ref )
1274 + a->l0.me16x16.cost_mv
1275 + a->l1.me16x16.cost_mv;
1278 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1279 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1280 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1283 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1285 const int x = 2*(i%2);
1286 const int y = 2*(i/2);
1288 switch( h->mb.i_sub_partition[i] )
1291 x264_macroblock_cache_mv( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv[0], a->l0.me8x8[i].mv[1] );
1294 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] );
1295 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] );
1298 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] );
1299 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] );
1302 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] );
1303 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] );
1304 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] );
1305 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] );
1308 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1313 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1314 if( x264_mb_partition_listX_table[0][part] ) \
1316 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1317 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
1321 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1322 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0, 0 ); \
1324 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
1326 if( x264_mb_partition_listX_table[1][part] ) \
1328 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1329 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
1333 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1334 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0, 0 ); \
1336 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
1339 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1343 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1345 x264_mb_load_mv_direct8x8( h, i );
1348 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0, 0 );
1349 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0, 0 );
1350 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1355 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1358 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1360 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1362 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1364 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1368 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1370 uint8_t **p_fref[2] =
1371 { h->mb.pic.p_fref[0][a->l0.i_ref],
1372 h->mb.pic.p_fref[1][a->l1.i_ref] };
1373 uint8_t pix[2][8*8];
1376 /* XXX Needed for x264_mb_predict_mv */
1377 h->mb.i_partition = D_8x8;
1381 for( i = 0; i < 4; i++ )
1386 int i_part_cost_bi = 0;
1388 for( l = 0; l < 2; l++ )
1390 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1391 x264_me_t *m = &lX->me8x8[i];
1393 m->i_pixel = PIXEL_8x8;
1394 m->p_cost_mv = a->p_cost_mv;
1396 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1397 LOAD_HPELS( m, p_fref[l], 8*x8, 8*y8 );
1399 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1400 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1402 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
1405 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1406 m->mv[0], m->mv[1], 8, 8 );
1407 i_part_cost_bi += m->cost_mv;
1408 /* FIXME: ref cost */
1411 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1412 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 )
1413 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1414 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1415 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1417 i_part_cost = a->l0.me8x8[i].cost;
1418 h->mb.i_sub_partition[i] = D_L0_8x8;
1419 if( a->l1.me8x8[i].cost < i_part_cost )
1421 i_part_cost = a->l1.me8x8[i].cost;
1422 h->mb.i_sub_partition[i] = D_L1_8x8;
1424 if( i_part_cost_bi < i_part_cost )
1426 i_part_cost = i_part_cost_bi;
1427 h->mb.i_sub_partition[i] = D_BI_8x8;
1429 if( a->i_cost8x8direct[i] < i_part_cost )
1431 i_part_cost = a->i_cost8x8direct[i];
1432 h->mb.i_sub_partition[i] = D_DIRECT_8x8;
1434 a->i_cost8x8bi += i_part_cost;
1436 /* XXX Needed for x264_mb_predict_mv */
1437 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1441 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1444 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1446 uint8_t **p_fref[2] =
1447 { h->mb.pic.p_fref[0][a->l0.i_ref],
1448 h->mb.pic.p_fref[1][a->l1.i_ref] };
1449 uint8_t pix[2][16*8];
1453 h->mb.i_partition = D_16x8;
1454 a->i_cost16x8bi = 0;
1456 for( i = 0; i < 2; i++ )
1459 int i_part_cost_bi = 0;
1461 /* TODO: check only the list(s) that were used in b8x8? */
1462 for( l = 0; l < 2; l++ )
1464 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1465 x264_me_t *m = &lX->me16x8[i];
1467 m->i_pixel = PIXEL_16x8;
1468 m->p_cost_mv = a->p_cost_mv;
1470 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1471 LOAD_HPELS( m, p_fref[l], 0, 8*i );
1473 mvc[0][0] = lX->me8x8[2*i].mv[0];
1474 mvc[0][1] = lX->me8x8[2*i].mv[1];
1475 mvc[1][0] = lX->me8x8[2*i+1].mv[0];
1476 mvc[1][1] = lX->me8x8[2*i+1].mv[1];
1478 x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp );
1479 x264_me_search( h, m, mvc, 2 );
1482 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 16,
1483 m->mv[0], m->mv[1], 16, 8 );
1484 /* FIXME: ref cost */
1485 i_part_cost_bi += m->cost_mv;
1488 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1489 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 );
1491 i_part_cost = a->l0.me16x8[i].cost;
1492 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1493 if( a->l1.me16x8[i].cost < i_part_cost )
1495 i_part_cost = a->l1.me16x8[i].cost;
1496 a->i_mb_partition16x8[i] = D_L1_8x8;
1498 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1500 i_part_cost = i_part_cost_bi;
1501 a->i_mb_partition16x8[i] = D_BI_8x8;
1503 a->i_cost16x8bi += i_part_cost;
1506 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1510 a->i_mb_type16x8 = B_L0_L0
1511 + (a->i_mb_partition16x8[0]>>2) * 3
1512 + (a->i_mb_partition16x8[1]>>2);
1513 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1515 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1517 uint8_t **p_fref[2] =
1518 { h->mb.pic.p_fref[0][a->l0.i_ref],
1519 h->mb.pic.p_fref[1][a->l1.i_ref] };
1520 uint8_t pix[2][8*16];
1524 h->mb.i_partition = D_8x16;
1525 a->i_cost8x16bi = 0;
1527 for( i = 0; i < 2; i++ )
1530 int i_part_cost_bi = 0;
1532 for( l = 0; l < 2; l++ )
1534 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1535 x264_me_t *m = &lX->me8x16[i];
1537 m->i_pixel = PIXEL_8x16;
1538 m->p_cost_mv = a->p_cost_mv;
1540 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1541 LOAD_HPELS( m, p_fref[l], 8*i, 0 );
1543 mvc[0][0] = lX->me8x8[i].mv[0];
1544 mvc[0][1] = lX->me8x8[i].mv[1];
1545 mvc[1][0] = lX->me8x8[i+2].mv[0];
1546 mvc[1][1] = lX->me8x8[i+2].mv[1];
1548 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1549 x264_me_search( h, m, mvc, 2 );
1552 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1553 m->mv[0], m->mv[1], 8, 16 );
1554 /* FIXME: ref cost */
1555 i_part_cost_bi += m->cost_mv;
1558 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1559 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 );
1561 i_part_cost = a->l0.me8x16[i].cost;
1562 a->i_mb_partition8x16[i] = D_L0_8x8;
1563 if( a->l1.me8x16[i].cost < i_part_cost )
1565 i_part_cost = a->l1.me8x16[i].cost;
1566 a->i_mb_partition8x16[i] = D_L1_8x8;
1568 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1570 i_part_cost = i_part_cost_bi;
1571 a->i_mb_partition8x16[i] = D_BI_8x8;
1573 a->i_cost8x16bi += i_part_cost;
1576 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1580 a->i_mb_type8x16 = B_L0_L0
1581 + (a->i_mb_partition8x16[0]>>2) * 3
1582 + (a->i_mb_partition8x16[1]>>2);
1583 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1586 static inline void x264_mb_analyse_transform( x264_t *h )
1588 h->mb.cache.b_transform_8x8_allowed =
1589 h->param.analyse.b_transform_8x8
1590 && !IS_INTRA( h->mb.i_type ) && x264_mb_transform_8x8_allowed( h );
1592 if( h->mb.cache.b_transform_8x8_allowed )
1594 int i_cost4, i_cost8;
1595 /* FIXME only luma mc is needed */
1598 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0],
1599 h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] );
1600 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0],
1601 h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] );
1603 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
1607 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_cost )
1609 h->mb.cache.b_transform_8x8_allowed =
1610 h->param.analyse.b_transform_8x8 && x264_mb_transform_8x8_allowed( h );
1612 if( h->mb.cache.b_transform_8x8_allowed )
1615 x264_analyse_update_cache( h, a );
1616 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1617 /* FIXME only luma is needed, but the score for comparison already includes chroma */
1618 i_cost8 = x264_rd_cost_mb( h, a->i_lambda2 );
1620 if( *i_cost >= i_cost8 )
1623 a->i_best_satd = (int64_t)a->i_best_satd * i_cost8 / *i_cost;
1627 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1632 /*****************************************************************************
1633 * x264_macroblock_analyse:
1634 *****************************************************************************/
1635 void x264_macroblock_analyse( x264_t *h )
1637 x264_mb_analysis_t analysis;
1638 int i_cost = COST_MAX;
1642 x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
1644 /*--------------------------- Do the analysis ---------------------------*/
1645 if( h->sh.i_type == SLICE_TYPE_I )
1647 x264_mb_analyse_intra( h, &analysis, COST_MAX );
1649 i_cost = analysis.i_sad_i16x16;
1650 h->mb.i_type = I_16x16;
1651 if( analysis.i_sad_i4x4 < i_cost )
1653 i_cost = analysis.i_sad_i4x4;
1654 h->mb.i_type = I_4x4;
1656 if( analysis.i_sad_i8x8 < i_cost )
1657 h->mb.i_type = I_8x8;
1659 else if( h->sh.i_type == SLICE_TYPE_P )
1662 int i_intra_cost, i_intra_type;
1664 /* Fast P_SKIP detection */
1665 if( !h->mb.b_lossless &&
1666 (( h->mb.i_mb_type_left == P_SKIP ) ||
1667 ( h->mb.i_mb_type_top == P_SKIP ) ||
1668 ( h->mb.i_mb_type_topleft == P_SKIP ) ||
1669 ( h->mb.i_mb_type_topright == P_SKIP )))
1671 b_skip = x264_macroblock_probe_pskip( h );
1676 h->mb.i_type = P_SKIP;
1677 h->mb.i_partition = D_16x16;
1681 const unsigned int flags = h->param.analyse.inter;
1686 x264_mb_analyse_load_costs( h, &analysis );
1688 x264_mb_analyse_inter_p16x16( h, &analysis );
1689 if( flags & X264_ANALYSE_PSUB16x16 )
1691 if( h->param.analyse.b_mixed_references )
1692 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
1694 x264_mb_analyse_inter_p8x8( h, &analysis );
1697 /* Select best inter mode */
1699 i_partition = D_16x16;
1700 i_cost = analysis.l0.me16x16.cost;
1702 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
1703 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
1708 i_partition = D_8x8;
1709 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1710 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1712 i_cost = analysis.l0.i_cost8x8;
1715 if( flags & X264_ANALYSE_PSUB8x8 )
1717 int i_cost_bak = i_cost;
1719 for( i = 0; i < 4; i++ )
1721 x264_mb_analyse_inter_p4x4( h, &analysis, i );
1722 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
1724 int i_cost8x8 = analysis.l0.i_cost4x4[i];
1725 h->mb.i_sub_partition[i] = D_L0_4x4;
1727 x264_mb_analyse_inter_p8x4( h, &analysis, i );
1728 if( analysis.l0.i_cost8x4[i] < i_cost8x8 )
1730 h->mb.i_sub_partition[i] = D_L0_8x4;
1731 i_cost8x8 = analysis.l0.i_cost8x4[i];
1734 x264_mb_analyse_inter_p4x8( h, &analysis, i );
1735 if( analysis.l0.i_cost4x8[i] < i_cost8x8 )
1737 h->mb.i_sub_partition[i] = D_L0_4x8;
1738 i_cost8x8 = analysis.l0.i_cost4x8[i];
1741 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
1744 x264_mb_cache_mv_p8x8( h, &analysis, i );
1746 /* TODO: RD per subpartition */
1747 if( b_sub8x8 && analysis.b_mbrd )
1749 i_cost = x264_rd_cost_mb( h, analysis.i_lambda2 );
1750 if( i_cost > i_cost_bak )
1752 i_cost = i_cost_bak;
1753 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1754 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1760 /* Now do 16x8/8x16 */
1761 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
1762 if( analysis.b_mbrd )
1763 i_thresh16x8 = i_thresh16x8 * analysis.i_lambda2 / analysis.i_lambda;
1764 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
1765 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
1767 x264_mb_analyse_inter_p16x8( h, &analysis );
1768 if( analysis.l0.i_cost16x8 < i_cost )
1771 i_partition = D_16x8;
1772 i_cost = analysis.l0.i_cost16x8;
1775 x264_mb_analyse_inter_p8x16( h, &analysis );
1776 if( analysis.l0.i_cost8x16 < i_cost )
1779 i_partition = D_8x16;
1780 i_cost = analysis.l0.i_cost8x16;
1784 h->mb.i_partition = i_partition;
1787 //FIXME mb_type costs?
1788 if( analysis.b_mbrd )
1790 h->mb.i_type = i_type;
1791 x264_mb_analyse_transform_rd( h, &analysis, &i_cost );
1793 else if( i_partition == D_16x16 )
1795 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1796 i_cost = analysis.l0.me16x16.cost;
1798 else if( i_partition == D_16x8 )
1800 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
1801 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
1802 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
1804 else if( i_partition == D_8x16 )
1806 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
1807 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
1808 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
1810 else if( i_partition == D_8x8 )
1814 for( i8x8 = 0; i8x8 < 4; i8x8++ )
1816 switch( h->mb.i_sub_partition[i8x8] )
1819 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
1820 i_cost += analysis.l0.me8x8[i8x8].cost;
1823 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
1824 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
1825 i_cost += analysis.l0.me8x4[i8x8][0].cost +
1826 analysis.l0.me8x4[i8x8][1].cost;
1829 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
1830 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
1831 i_cost += analysis.l0.me4x8[i8x8][0].cost +
1832 analysis.l0.me4x8[i8x8][1].cost;
1836 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
1837 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
1838 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
1839 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
1840 i_cost += analysis.l0.me4x4[i8x8][0].cost +
1841 analysis.l0.me4x4[i8x8][1].cost +
1842 analysis.l0.me4x4[i8x8][2].cost +
1843 analysis.l0.me4x4[i8x8][3].cost;
1846 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
1852 x264_mb_analyse_intra( h, &analysis, i_cost );
1853 if( h->mb.b_chroma_me && !analysis.b_mbrd &&
1854 ( analysis.i_sad_i16x16 < i_cost
1855 || analysis.i_sad_i8x8 < i_cost
1856 || analysis.i_sad_i4x4 < i_cost ))
1858 x264_mb_analyse_intra_chroma( h, &analysis );
1859 analysis.i_sad_i16x16 += analysis.i_sad_i8x8chroma;
1860 analysis.i_sad_i8x8 += analysis.i_sad_i8x8chroma;
1861 analysis.i_sad_i4x4 += analysis.i_sad_i8x8chroma;
1864 i_intra_type = I_16x16;
1865 i_intra_cost = analysis.i_sad_i16x16;
1867 if( analysis.i_sad_i8x8 < i_intra_cost )
1869 i_intra_type = I_8x8;
1870 i_intra_cost = analysis.i_sad_i8x8;
1872 if( analysis.i_sad_i4x4 < i_intra_cost )
1874 i_intra_type = I_4x4;
1875 i_intra_cost = analysis.i_sad_i4x4;
1878 if( i_intra_cost < i_cost )
1880 i_type = i_intra_type;
1881 i_cost = i_intra_cost;
1884 h->mb.i_type = i_type;
1885 h->stat.frame.i_intra_cost += i_intra_cost;
1886 h->stat.frame.i_inter_cost += i_cost;
1889 else if( h->sh.i_type == SLICE_TYPE_B )
1893 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h );
1894 if( analysis.b_direct_available )
1896 h->mb.i_type = B_SKIP;
1899 /* Conditioning the probe on neighboring block types
1900 * doesn't seem to help speed or quality. */
1901 b_skip = !h->mb.b_lossless && x264_macroblock_probe_bskip( h );
1906 const unsigned int flags = h->param.analyse.inter;
1909 x264_mb_analyse_load_costs( h, &analysis );
1911 /* select best inter mode */
1912 /* direct must be first */
1913 if( analysis.b_direct_available )
1914 x264_mb_analyse_inter_direct( h, &analysis );
1916 x264_mb_analyse_inter_b16x16( h, &analysis );
1918 h->mb.i_type = B_L0_L0;
1919 i_partition = D_16x16;
1920 i_cost = analysis.l0.me16x16.cost;
1921 if( analysis.l1.me16x16.cost < i_cost )
1923 h->mb.i_type = B_L1_L1;
1924 i_cost = analysis.l1.me16x16.cost;
1926 if( analysis.i_cost16x16bi < i_cost )
1928 h->mb.i_type = B_BI_BI;
1929 i_cost = analysis.i_cost16x16bi;
1931 if( analysis.i_cost16x16direct < i_cost )
1933 h->mb.i_type = B_DIRECT;
1934 i_cost = analysis.i_cost16x16direct;
1937 if( flags & X264_ANALYSE_BSUB16x16 )
1939 x264_mb_analyse_inter_b8x8( h, &analysis );
1940 if( analysis.i_cost8x8bi < i_cost )
1942 h->mb.i_type = B_8x8;
1943 i_partition = D_8x8;
1944 i_cost = analysis.i_cost8x8bi;
1946 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
1947 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
1949 x264_mb_analyse_inter_b16x8( h, &analysis );
1950 if( analysis.i_cost16x8bi < i_cost )
1952 i_partition = D_16x8;
1953 i_cost = analysis.i_cost16x8bi;
1954 h->mb.i_type = analysis.i_mb_type16x8;
1957 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
1958 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
1960 x264_mb_analyse_inter_b8x16( h, &analysis );
1961 if( analysis.i_cost8x16bi < i_cost )
1963 i_partition = D_8x16;
1964 i_cost = analysis.i_cost8x16bi;
1965 h->mb.i_type = analysis.i_mb_type8x16;
1971 h->mb.i_partition = i_partition;
1974 if( i_partition == D_16x16 )
1976 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
1977 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
1978 if( h->mb.i_type == B_L0_L0 )
1980 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1981 i_cost = analysis.l0.me16x16.cost
1982 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
1984 else if( h->mb.i_type == B_L1_L1 )
1986 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
1987 i_cost = analysis.l1.me16x16.cost
1988 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
1990 else if( h->mb.i_type == B_BI_BI )
1992 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1993 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
1996 else if( i_partition == D_16x8 )
1998 for( i=0; i<2; i++ )
2000 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2001 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2002 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2003 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2006 else if( i_partition == D_8x16 )
2008 for( i=0; i<2; i++ )
2010 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2011 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2012 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2013 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2016 else if( i_partition == D_8x8 )
2018 for( i=0; i<4; i++ )
2021 int i_part_cost_old;
2023 int i_part_type = h->mb.i_sub_partition[i];
2024 int b_bidir = (i_part_type == D_BI_8x8);
2026 if( i_part_type == D_DIRECT_8x8 )
2028 if( x264_mb_partition_listX_table[0][i_part_type] )
2030 m = &analysis.l0.me8x8[i];
2031 i_part_cost_old = m->cost;
2032 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2033 m->cost -= i_type_cost;
2034 x264_me_refine_qpel( h, m );
2036 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2038 if( x264_mb_partition_listX_table[1][i_part_type] )
2040 m = &analysis.l1.me8x8[i];
2041 i_part_cost_old = m->cost;
2042 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2043 m->cost -= i_type_cost;
2044 x264_me_refine_qpel( h, m );
2046 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2048 /* TODO: update mvp? */
2052 /* best intra mode */
2053 x264_mb_analyse_intra( h, &analysis, i_cost );
2055 if( analysis.i_sad_i16x16 < i_cost )
2057 h->mb.i_type = I_16x16;
2058 i_cost = analysis.i_sad_i16x16;
2060 if( analysis.i_sad_i8x8 < i_cost )
2062 h->mb.i_type = I_8x8;
2063 i_cost = analysis.i_sad_i8x8;
2065 if( analysis.i_sad_i4x4 < i_cost )
2067 h->mb.i_type = I_4x4;
2068 i_cost = analysis.i_sad_i4x4;
2073 x264_analyse_update_cache( h, &analysis );
2075 if( !analysis.b_mbrd )
2076 x264_mb_analyse_transform( h );
2078 h->mb.b_trellis = h->param.analyse.i_trellis;
2081 /*-------------------- Update MB from the analysis ----------------------*/
2082 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2086 switch( h->mb.i_type )
2089 for( i = 0; i < 16; i++ )
2091 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] =
2092 a->i_predict4x4[block_idx_x[i]][block_idx_y[i]];
2095 x264_mb_analyse_intra_chroma( h, a );
2098 for( i = 0; i < 4; i++ )
2099 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1),
2100 a->i_predict8x8[i&1][i>>1] );
2102 x264_mb_analyse_intra_chroma( h, a );
2105 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2106 x264_mb_analyse_intra_chroma( h, a );
2110 switch( h->mb.i_partition )
2113 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2114 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2118 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2119 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2120 x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv[0], a->l0.me16x8[0].mv[1] );
2121 x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv[0], a->l0.me16x8[1].mv[1] );
2125 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2126 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2127 x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv[0], a->l0.me8x16[0].mv[1] );
2128 x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv[0], a->l0.me8x16[1].mv[1] );
2132 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2138 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2139 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2140 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2141 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2142 for( i = 0; i < 4; i++ )
2143 x264_mb_cache_mv_p8x8( h, a, i );
2149 x264_mb_predict_mv_pskip( h, mvp );
2151 h->mb.i_partition = D_16x16;
2152 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2153 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, mvp[0], mvp[1] );
2158 /* nothing has changed since x264_macroblock_probe_bskip */
2161 x264_mb_load_mv_direct8x8( h, 0 );
2162 x264_mb_load_mv_direct8x8( h, 1 );
2163 x264_mb_load_mv_direct8x8( h, 2 );
2164 x264_mb_load_mv_direct8x8( h, 3 );
2168 /* optimize: cache might not need to be rewritten */
2169 for( i = 0; i < 4; i++ )
2170 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2173 default: /* the rest of the B types */
2174 switch( h->mb.i_partition )
2177 switch( h->mb.i_type )
2180 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2181 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2183 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2184 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0, 0 );
2185 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0, 0 );
2188 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2189 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0, 0 );
2190 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0, 0 );
2192 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2193 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2196 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2197 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2199 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2200 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2205 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2206 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2209 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2210 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2213 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2219 #include "slicetype_decision.c"