1 /*****************************************************************************
2 * analyse.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003 x264 project
5 * $Id: analyse.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Loren Merritt <lorenm@u.washington.edu>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
31 #include "common/common.h"
32 #include "common/macroblock.h"
33 #include "macroblock.h"
35 #include "ratecontrol.h"
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 */
91 int i_predict8x8[2][2];
94 int i_predict4x4[4][4];
98 int i_predict8x8chroma;
100 /* II: Inter part P/B frame */
101 x264_mb_analysis_list_t l0;
102 x264_mb_analysis_list_t l1;
104 int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
105 int i_cost16x16direct;
107 int i_cost8x8direct[4];
111 int i_mb_partition16x8[2]; /* mb_partition_e */
112 int i_mb_partition8x16[2];
113 int i_mb_type16x8; /* mb_class_e */
116 int b_direct_available;
118 } x264_mb_analysis_t;
120 /* lambda = pow(2,qp/6-2) */
121 static const int i_qp0_cost_table[52] = {
122 1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
123 1, 1, 1, 1, /* 8-11 */
124 1, 1, 1, 1, 2, 2, 2, 2, /* 12-19 */
125 3, 3, 3, 4, 4, 4, 5, 6, /* 20-27 */
126 6, 7, 8, 9,10,11,13,14, /* 28-35 */
127 16,18,20,23,25,29,32,36, /* 36-43 */
128 40,45,51,57,64,72,81,91 /* 44-51 */
131 /* pow(lambda,2) * .9 */
132 static const int i_qp0_cost2_table[52] = {
133 1, 1, 1, 1, 1, 1, /* 0-5 */
134 1, 1, 1, 1, 1, 1, /* 6-11 */
135 1, 1, 1, 2, 2, 3, /* 12-17 */
136 4, 5, 6, 7, 9, 11, /* 18-23 */
137 14, 18, 23, 29, 36, 46, /* 24-29 */
138 58, 73, 91, 115, 145, 183, /* 30-35 */
139 230, 290, 366, 461, 581, 731, /* 36-41 */
140 922,1161,1463,1843,2322,2926, /* 42-47 */
144 static const uint8_t block_idx_x[16] = {
145 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
147 static const uint8_t block_idx_y[16] = {
148 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
151 /* TODO: calculate CABAC costs */
152 static const int i_mb_b_cost_table[19] = {
153 9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
155 static const int i_mb_b16x8_cost_table[16] = {
156 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
158 static const int i_sub_mb_b_cost_table[13] = {
159 7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
161 static const int i_sub_mb_p_cost_table[4] = {
165 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
167 /* initialize an array of lambda*nbits for all possible mvs */
168 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
170 static int16_t *p_cost_mv[52];
172 if( !p_cost_mv[a->i_qp] )
174 /* could be faster, but isn't called many times */
175 /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
177 p_cost_mv[a->i_qp] = x264_malloc( (4*4*h->param.analyse.i_mv_range + 1) * sizeof(int16_t) );
178 p_cost_mv[a->i_qp] += 2*4*h->param.analyse.i_mv_range;
179 for( i = 0; i <= 2*4*h->param.analyse.i_mv_range; i++ )
181 p_cost_mv[a->i_qp][-i] =
182 p_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i );
186 a->p_cost_mv = p_cost_mv[a->i_qp];
189 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
191 memset( a, 0, sizeof( x264_mb_analysis_t ) );
193 /* conduct the analysis using this lamda and QP */
195 a->i_lambda = i_qp0_cost_table[i_qp];
196 a->i_lambda2 = i_qp0_cost2_table[i_qp];
197 a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 && h->sh.i_type != SLICE_TYPE_B;
199 h->mb.i_me_method = h->param.analyse.i_me_method;
200 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
201 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
202 && h->mb.i_subpel_refine >= 5;
204 h->mb.b_transform_8x8 = 0;
210 a->i_sad_i8x8chroma = COST_MAX;
213 a->i_best_satd = COST_MAX;
215 /* II: Inter part P/B frame */
216 if( h->sh.i_type != SLICE_TYPE_I )
219 int i_fmv_range = h->param.analyse.i_mv_range - 16;
221 /* Calculate max allowed MV range */
222 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range )
223 h->mb.mv_min_fpel[0] = CLIP_FMV( -16*h->mb.i_mb_x - 8 );
224 h->mb.mv_max_fpel[0] = CLIP_FMV( 16*( h->sps->i_mb_width - h->mb.i_mb_x ) - 8 );
225 h->mb.mv_min[0] = 4*( h->mb.mv_min_fpel[0] - 16 );
226 h->mb.mv_max[0] = 4*( h->mb.mv_max_fpel[0] + 16 );
227 if( h->mb.i_mb_x == 0)
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 ) - 8 );
231 h->mb.mv_min[1] = 4*( h->mb.mv_min_fpel[1] - 16 );
232 h->mb.mv_max[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 /* FIXME even when b_tr == 0 there is some case where missing pixels
368 * are emulated and thus more mode are available TODO
369 * analysis and encode should be fixed too */
370 int b_l = i_neighbour & MB_LEFT;
371 int b_t = i_neighbour & MB_TOP;
372 int b_tr = i_neighbour & MB_TOPRIGHT;
376 *mode++ = I_PRED_4x4_DC;
377 *mode++ = I_PRED_4x4_H;
378 *mode++ = I_PRED_4x4_V;
379 *mode++ = I_PRED_4x4_DDR;
380 *mode++ = I_PRED_4x4_VR;
381 *mode++ = I_PRED_4x4_HD;
382 *mode++ = I_PRED_4x4_HU;
387 *mode++ = I_PRED_4x4_DC_LEFT;
388 *mode++ = I_PRED_4x4_H;
389 *mode++ = I_PRED_4x4_HU;
394 *mode++ = I_PRED_4x4_DC_TOP;
395 *mode++ = I_PRED_4x4_V;
400 *mode++ = I_PRED_4x4_DC_128;
406 *mode++ = I_PRED_4x4_DDL;
407 *mode++ = I_PRED_4x4_VL;
412 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
419 uint8_t *p_dstc[2], *p_srcc[2];
422 if( a->i_sad_i8x8chroma < COST_MAX )
425 /* 8x8 prediction selection for chroma */
426 p_dstc[0] = h->mb.pic.p_fdec[1];
427 p_dstc[1] = h->mb.pic.p_fdec[2];
428 p_srcc[0] = h->mb.pic.p_fenc[1];
429 p_srcc[1] = h->mb.pic.p_fenc[2];
431 i_stride[0] = h->mb.pic.i_stride[1];
432 i_stride[1] = h->mb.pic.i_stride[2];
434 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
435 a->i_sad_i8x8chroma = COST_MAX;
436 for( i = 0; i < i_max; i++ )
441 i_mode = predict_mode[i];
443 /* we do the prediction */
444 h->predict_8x8c[i_mode]( p_dstc[0], i_stride[0] );
445 h->predict_8x8c[i_mode]( p_dstc[1], i_stride[1] );
447 /* we calculate the cost */
448 i_sad = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], i_stride[0],
449 p_srcc[0], i_stride[0] ) +
450 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], i_stride[1],
451 p_srcc[1], i_stride[1] ) +
452 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
454 /* if i_score is lower it is better */
455 if( a->i_sad_i8x8chroma > i_sad )
457 a->i_predict8x8chroma = i_mode;
458 a->i_sad_i8x8chroma = i_sad;
462 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
465 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_cost_inter )
467 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
468 const int i_stride = h->mb.pic.i_stride[0];
469 uint8_t *p_src = h->mb.pic.p_fenc[0];
470 uint8_t *p_dst = h->mb.pic.p_fdec[0];
471 int f8_satd_rd_ratio = 0;
477 const int i_satd_thresh = a->i_best_satd * 5/4 + a->i_lambda * 10;
479 /*---------------- Try all mode and calculate their score ---------------*/
481 /* 16x16 prediction selection */
482 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
483 for( i = 0; i < i_max; i++ )
488 i_mode = predict_mode[i];
489 h->predict_16x16[i_mode]( p_dst, i_stride );
491 i_sad = h->pixf.mbcmp[PIXEL_16x16]( p_dst, i_stride, p_src, i_stride ) +
492 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
493 if( a->i_sad_i16x16 > i_sad )
495 a->i_predict16x16 = i_mode;
496 a->i_sad_i16x16 = i_sad;
502 f8_satd_rd_ratio = ((unsigned)i_cost_inter << 8) / a->i_best_satd + 1;
503 x264_mb_analyse_intra_chroma( h, a );
504 if( h->mb.b_chroma_me )
505 a->i_sad_i16x16 += a->i_sad_i8x8chroma;
506 if( a->i_sad_i16x16 < i_satd_thresh )
508 h->mb.i_type = I_16x16;
509 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
510 a->i_sad_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
513 a->i_sad_i16x16 = a->i_sad_i16x16 * f8_satd_rd_ratio >> 8;
517 if( h->sh.i_type == SLICE_TYPE_B )
518 /* cavlc mb type prefix */
519 a->i_sad_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
520 if( a->b_fast_intra && a->i_sad_i16x16 > 2*i_cost_inter )
524 /* 4x4 prediction selection */
525 if( flags & X264_ANALYSE_I4x4 )
528 for( idx = 0; idx < 16; idx++ )
536 i_pred_mode= x264_mb_predict_intra4x4_mode( h, idx );
537 x = block_idx_x[idx];
538 y = block_idx_y[idx];
540 p_src_by = p_src + 4 * x + 4 * y * i_stride;
541 p_dst_by = p_dst + 4 * x + 4 * y * i_stride;
544 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
545 for( i = 0; i < i_max; i++ )
550 i_mode = predict_mode[i];
551 h->predict_4x4[i_mode]( p_dst_by, i_stride );
553 i_sad = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, i_stride,
555 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
559 a->i_predict4x4[x][y] = i_mode;
563 a->i_sad_i4x4 += i_best;
565 /* we need to encode this block now (for next ones) */
566 h->predict_4x4[a->i_predict4x4[x][y]]( p_dst_by, i_stride );
567 x264_mb_encode_i4x4( h, idx, a->i_qp );
569 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[x][y];
572 a->i_sad_i4x4 += a->i_lambda * 24; /* from JVT (SATD0) */
575 if( h->mb.b_chroma_me )
576 a->i_sad_i4x4 += a->i_sad_i8x8chroma;
577 if( a->i_sad_i4x4 < i_satd_thresh )
579 h->mb.i_type = I_4x4;
580 a->i_sad_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
583 a->i_sad_i4x4 = a->i_sad_i4x4 * f8_satd_rd_ratio >> 8;
587 if( h->sh.i_type == SLICE_TYPE_B )
588 a->i_sad_i4x4 += a->i_lambda * i_mb_b_cost_table[I_4x4];
592 /* 8x8 prediction selection */
593 if( flags & X264_ANALYSE_I8x8 )
596 for( idx = 0; idx < 4; idx++ )
604 i_pred_mode= x264_mb_predict_intra4x4_mode( h, 4*idx );
608 p_src_by = p_src + 8 * x + 8 * y * i_stride;
609 p_dst_by = p_dst + 8 * x + 8 * y * i_stride;
612 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
613 for( i = 0; i < i_max; i++ )
618 i_mode = predict_mode[i];
619 h->predict_8x8[i_mode]( p_dst_by, i_stride, h->mb.i_neighbour );
621 /* could use sa8d, but it doesn't seem worth the speed cost (without mmx at least) */
622 i_sad = h->pixf.mbcmp[PIXEL_8x8]( p_dst_by, i_stride,
624 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
628 a->i_predict8x8[x][y] = i_mode;
632 a->i_sad_i8x8 += i_best;
634 /* we need to encode this block now (for next ones) */
635 h->predict_8x8[a->i_predict8x8[x][y]]( p_dst_by, i_stride, h->mb.i_neighbour );
636 x264_mb_encode_i8x8( h, idx, a->i_qp );
638 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[x][y] );
643 if( h->mb.b_chroma_me )
644 a->i_sad_i8x8 += a->i_sad_i8x8chroma;
645 if( a->i_sad_i8x8 < i_satd_thresh )
647 h->mb.i_type = I_8x8;
648 a->i_sad_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
651 a->i_sad_i8x8 = a->i_sad_i8x8 * f8_satd_rd_ratio >> 8;
655 // FIXME some bias like in i4x4?
656 if( h->sh.i_type == SLICE_TYPE_B )
657 a->i_sad_i8x8 += a->i_lambda * i_mb_b_cost_table[I_8x8];
662 #define LOAD_FENC( m, src, xoff, yoff) \
663 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
664 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
665 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
666 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
667 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]];
668 #define LOAD_HPELS(m, src, xoff, yoff) \
669 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
670 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
671 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
672 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
673 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
674 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]];
676 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
680 int mvc[4][2], i_mvc;
681 int i_fullpel_thresh = INT_MAX;
682 int *p_fullpel_thresh = h->i_ref0>1 ? &i_fullpel_thresh : NULL;
684 /* 16x16 Search on all ref frame */
685 m.i_pixel = PIXEL_16x16;
686 m.p_cost_mv = a->p_cost_mv;
687 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
689 a->l0.me16x16.cost = INT_MAX;
690 for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
692 const int i_ref_cost = a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, i_ref );
693 i_fullpel_thresh -= i_ref_cost;
695 /* search with ref */
696 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, 0 );
697 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
698 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
699 x264_me_search_ref( h, &m, mvc, i_mvc, p_fullpel_thresh );
701 m.cost += i_ref_cost;
702 i_fullpel_thresh += i_ref_cost;
704 if( m.cost < a->l0.me16x16.cost )
710 /* save mv for predicting neighbors */
711 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
712 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
715 /* Set global ref, needed for all others modes */
716 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
720 a->i_best_satd = a->l0.me16x16.cost;
722 h->mb.i_partition = D_16x16;
723 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
724 a->l0.me16x16.cost = x264_rd_cost_mb( h, a->i_lambda2 );
728 /* subtract ref cost, so we don't have to add it for the other P types */
729 a->l0.me16x16.cost -= a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, a->l0.i_ref );
733 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
735 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.i_ref];
736 uint8_t **p_fenc = h->mb.pic.p_fenc;
737 int mvc[5][2], i_mvc;
740 /* XXX Needed for x264_mb_predict_mv */
741 h->mb.i_partition = D_8x8;
744 mvc[0][0] = a->l0.me16x16.mv[0];
745 mvc[0][1] = a->l0.me16x16.mv[1];
747 for( i = 0; i < 4; i++ )
749 x264_me_t *m = &a->l0.me8x8[i];
753 m->i_pixel = PIXEL_8x8;
754 m->p_cost_mv = a->p_cost_mv;
756 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
757 LOAD_HPELS( m, p_fref, 8*x8, 8*y8 );
759 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
760 x264_me_search( h, m, mvc, i_mvc );
762 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
764 mvc[i_mvc][0] = m->mv[0];
765 mvc[i_mvc][1] = m->mv[1];
769 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
772 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
773 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
776 if( a->i_best_satd > a->l0.i_cost8x8 )
777 a->i_best_satd = a->l0.i_cost8x8;
778 h->mb.i_type = P_8x8;
779 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
780 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
781 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
785 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
787 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.i_ref];
788 uint8_t **p_fenc = h->mb.pic.p_fenc;
792 /* XXX Needed for x264_mb_predict_mv */
793 h->mb.i_partition = D_16x8;
795 for( i = 0; i < 2; i++ )
797 x264_me_t *m = &a->l0.me16x8[i];
799 m->i_pixel = PIXEL_16x8;
800 m->p_cost_mv = a->p_cost_mv;
802 LOAD_FENC( m, p_fenc, 0, 8*i );
803 LOAD_HPELS( m, p_fref, 0, 8*i );
805 mvc[0][0] = a->l0.me8x8[2*i].mv[0];
806 mvc[0][1] = a->l0.me8x8[2*i].mv[1];
807 mvc[1][0] = a->l0.me8x8[2*i+1].mv[0];
808 mvc[1][1] = a->l0.me8x8[2*i+1].mv[1];
810 x264_mb_predict_mv( h, 0, 8*i, 4, m->mvp );
811 x264_me_search( h, m, mvc, 2 );
813 x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, m->mv[0], m->mv[1] );
816 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
819 if( a->i_best_satd > a->l0.i_cost16x8 )
820 a->i_best_satd = a->l0.i_cost16x8;
822 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
826 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
828 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.i_ref];
829 uint8_t **p_fenc = h->mb.pic.p_fenc;
833 /* XXX Needed for x264_mb_predict_mv */
834 h->mb.i_partition = D_8x16;
836 for( i = 0; i < 2; i++ )
838 x264_me_t *m = &a->l0.me8x16[i];
840 m->i_pixel = PIXEL_8x16;
841 m->p_cost_mv = a->p_cost_mv;
843 LOAD_FENC( m, p_fenc, 8*i, 0 );
844 LOAD_HPELS( m, p_fref, 8*i, 0 );
846 mvc[0][0] = a->l0.me8x8[i].mv[0];
847 mvc[0][1] = a->l0.me8x8[i].mv[1];
848 mvc[1][0] = a->l0.me8x8[i+2].mv[0];
849 mvc[1][1] = a->l0.me8x8[i+2].mv[1];
851 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
852 x264_me_search( h, m, mvc, 2 );
854 x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, m->mv[0], m->mv[1] );
857 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
860 if( a->i_best_satd > a->l0.i_cost8x16 )
861 a->i_best_satd = a->l0.i_cost8x16;
863 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
867 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
869 uint8_t pix1[8*8], pix2[8*8];
870 const int i_stride = h->mb.pic.i_stride[1];
871 const int off = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
873 #define CHROMA4x4MC( width, height, me, x, y ) \
874 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 ); \
875 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 );
877 if( pixel == PIXEL_4x4 )
879 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
880 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 0,2 );
881 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 2,0 );
882 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
884 else if( pixel == PIXEL_8x4 )
886 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
887 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
891 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
892 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
895 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][off], i_stride, pix1, 8 )
896 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][off], i_stride, pix2, 8 );
899 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
901 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.i_ref];
902 uint8_t **p_fenc = h->mb.pic.p_fenc;
906 /* XXX Needed for x264_mb_predict_mv */
907 h->mb.i_partition = D_8x8;
909 for( i4x4 = 0; i4x4 < 4; i4x4++ )
911 const int idx = 4*i8x8 + i4x4;
912 const int x4 = block_idx_x[idx];
913 const int y4 = block_idx_y[idx];
914 const int i_mvc = (i4x4 == 0);
916 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
918 m->i_pixel = PIXEL_4x4;
919 m->p_cost_mv = a->p_cost_mv;
921 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
922 LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
924 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
925 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
927 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
930 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
931 a->l0.me4x4[i8x8][1].cost +
932 a->l0.me4x4[i8x8][2].cost +
933 a->l0.me4x4[i8x8][3].cost +
934 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
935 if( h->mb.b_chroma_me )
936 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
939 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
941 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.i_ref];
942 uint8_t **p_fenc = h->mb.pic.p_fenc;
946 /* XXX Needed for x264_mb_predict_mv */
947 h->mb.i_partition = D_8x8;
949 for( i8x4 = 0; i8x4 < 2; i8x4++ )
951 const int idx = 4*i8x8 + 2*i8x4;
952 const int x4 = block_idx_x[idx];
953 const int y4 = block_idx_y[idx];
954 const int i_mvc = (i8x4 == 0);
956 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
958 m->i_pixel = PIXEL_8x4;
959 m->p_cost_mv = a->p_cost_mv;
961 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
962 LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
964 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
965 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
967 x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
970 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
971 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
972 if( h->mb.b_chroma_me )
973 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
976 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
978 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.i_ref];
979 uint8_t **p_fenc = h->mb.pic.p_fenc;
983 /* XXX Needed for x264_mb_predict_mv */
984 h->mb.i_partition = D_8x8;
986 for( i4x8 = 0; i4x8 < 2; i4x8++ )
988 const int idx = 4*i8x8 + i4x8;
989 const int x4 = block_idx_x[idx];
990 const int y4 = block_idx_y[idx];
991 const int i_mvc = (i4x8 == 0);
993 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
995 m->i_pixel = PIXEL_4x8;
996 m->p_cost_mv = a->p_cost_mv;
998 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
999 LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
1001 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1002 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1004 x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
1007 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1008 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1009 if( h->mb.b_chroma_me )
1010 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1013 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1015 /* Assumes that fdec still contains the results of
1016 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1018 uint8_t **p_fenc = h->mb.pic.p_fenc;
1019 uint8_t **p_fdec = h->mb.pic.p_fdec;
1020 int i_stride= h->mb.pic.i_stride[0];
1023 a->i_cost16x16direct = 0;
1024 for( i = 0; i < 4; i++ )
1028 const int off = 8 * x8 + 8 * i_stride * y8;
1029 a->i_cost16x16direct +=
1030 a->i_cost8x8direct[i] =
1031 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][off], i_stride, &p_fdec[0][off], i_stride );
1034 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1037 a->i_cost16x16direct += a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1040 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1042 if( h->param.analyse.b_weighted_bipred ) \
1043 h->pixf.avg_weight[size]( pix1, stride1, src2, stride2, \
1044 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1046 h->pixf.avg[size]( pix1, stride1, src2, stride2 ); \
1049 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1051 uint8_t pix1[16*16], pix2[16*16];
1054 int src2_ref, pix1_ref;
1058 int mvc[5][2], i_mvc;
1059 int i_fullpel_thresh = INT_MAX;
1060 int *p_fullpel_thresh = h->i_ref0>1 ? &i_fullpel_thresh : NULL;
1062 /* 16x16 Search on all ref frame */
1063 m.i_pixel = PIXEL_16x16;
1064 m.p_cost_mv = a->p_cost_mv;
1065 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1068 a->l0.me16x16.cost = INT_MAX;
1069 for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
1071 /* search with ref */
1072 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, 0 );
1073 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1074 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1075 x264_me_search_ref( h, &m, mvc, i_mvc, p_fullpel_thresh );
1078 m.cost += a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, i_ref );
1080 if( m.cost < a->l0.me16x16.cost )
1082 a->l0.i_ref = i_ref;
1086 /* save mv for predicting neighbors */
1087 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1088 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1090 /* subtract ref cost, so we don't have to add it for the other MB types */
1091 a->l0.me16x16.cost -= a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, a->l0.i_ref );
1094 i_fullpel_thresh = INT_MAX;
1095 p_fullpel_thresh = h->i_ref1>1 ? &i_fullpel_thresh : NULL;
1096 a->l1.me16x16.cost = INT_MAX;
1097 for( i_ref = 0; i_ref < h->i_ref1; i_ref++ )
1099 /* search with ref */
1100 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 0, 0 );
1101 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1102 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1103 x264_me_search_ref( h, &m, mvc, i_mvc, p_fullpel_thresh );
1106 m.cost += a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l1_active - 1, i_ref );
1108 if( m.cost < a->l1.me16x16.cost )
1110 a->l1.i_ref = i_ref;
1114 /* save mv for predicting neighbors */
1115 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1116 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1118 /* subtract ref cost, so we don't have to add it for the other MB types */
1119 a->l1.me16x16.cost -= a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l1_active - 1, a->l1.i_ref );
1121 /* Set global ref, needed for other modes? */
1122 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1123 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1125 /* get cost of BI mode */
1126 if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
1128 /* l0 reference is halfpel, so get_ref on it will make it faster */
1129 src2 = h->mc.get_ref( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1131 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1133 h->mc.mc_luma( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1135 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1137 src2_ref = a->l0.i_ref;
1138 pix1_ref = a->l1.i_ref;
1142 /* if l0 was qpel, we'll use get_ref on l1 instead */
1143 h->mc.mc_luma( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1145 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1147 src2 = h->mc.get_ref( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1149 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1151 src2_ref = a->l1.i_ref;
1152 pix1_ref = a->l0.i_ref;
1155 if( h->param.analyse.b_weighted_bipred )
1156 h->pixf.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2,
1157 h->mb.bipred_weight[pix1_ref][src2_ref] );
1159 h->pixf.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1161 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0], pix1, 16 )
1162 + a->i_lambda * ( bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, a->l0.i_ref )
1163 + bs_size_te( h->sh.i_num_ref_idx_l1_active - 1, a->l1.i_ref ) )
1164 + a->l0.me16x16.cost_mv
1165 + a->l1.me16x16.cost_mv;
1168 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1169 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1170 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1173 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1175 const int x = 2*(i%2);
1176 const int y = 2*(i/2);
1178 switch( h->mb.i_sub_partition[i] )
1181 x264_macroblock_cache_mv( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv[0], a->l0.me8x8[i].mv[1] );
1184 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] );
1185 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] );
1188 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] );
1189 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] );
1192 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] );
1193 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] );
1194 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] );
1195 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] );
1198 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1203 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1204 if( x264_mb_partition_listX_table[0][part] ) \
1206 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1207 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
1211 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1212 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0, 0 ); \
1214 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
1216 if( x264_mb_partition_listX_table[1][part] ) \
1218 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1219 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
1223 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1224 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0, 0 ); \
1226 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
1229 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1233 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1235 x264_mb_load_mv_direct8x8( h, i );
1238 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0, 0 );
1239 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0, 0 );
1240 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1245 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1248 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1250 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1252 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1254 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1258 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1260 uint8_t **p_fref[2] =
1261 { h->mb.pic.p_fref[0][a->l0.i_ref],
1262 h->mb.pic.p_fref[1][a->l1.i_ref] };
1263 uint8_t pix[2][8*8];
1266 /* XXX Needed for x264_mb_predict_mv */
1267 h->mb.i_partition = D_8x8;
1271 for( i = 0; i < 4; i++ )
1276 int i_part_cost_bi = 0;
1278 for( l = 0; l < 2; l++ )
1280 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1281 x264_me_t *m = &lX->me8x8[i];
1283 m->i_pixel = PIXEL_8x8;
1284 m->p_cost_mv = a->p_cost_mv;
1286 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1287 LOAD_HPELS( m, p_fref[l], 8*x8, 8*y8 );
1289 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1290 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1292 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
1295 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1296 m->mv[0], m->mv[1], 8, 8 );
1297 i_part_cost_bi += m->cost_mv;
1298 /* FIXME: ref cost */
1301 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1302 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 )
1303 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1304 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1305 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1307 i_part_cost = a->l0.me8x8[i].cost;
1308 h->mb.i_sub_partition[i] = D_L0_8x8;
1309 if( a->l1.me8x8[i].cost < i_part_cost )
1311 i_part_cost = a->l1.me8x8[i].cost;
1312 h->mb.i_sub_partition[i] = D_L1_8x8;
1314 if( i_part_cost_bi < i_part_cost )
1316 i_part_cost = i_part_cost_bi;
1317 h->mb.i_sub_partition[i] = D_BI_8x8;
1319 if( a->i_cost8x8direct[i] < i_part_cost )
1321 i_part_cost = a->i_cost8x8direct[i];
1322 h->mb.i_sub_partition[i] = D_DIRECT_8x8;
1324 a->i_cost8x8bi += i_part_cost;
1326 /* XXX Needed for x264_mb_predict_mv */
1327 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1331 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1334 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1336 uint8_t **p_fref[2] =
1337 { h->mb.pic.p_fref[0][a->l0.i_ref],
1338 h->mb.pic.p_fref[1][a->l1.i_ref] };
1339 uint8_t pix[2][16*8];
1343 h->mb.i_partition = D_16x8;
1344 a->i_cost16x8bi = 0;
1346 for( i = 0; i < 2; i++ )
1349 int i_part_cost_bi = 0;
1351 /* TODO: check only the list(s) that were used in b8x8? */
1352 for( l = 0; l < 2; l++ )
1354 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1355 x264_me_t *m = &lX->me16x8[i];
1357 m->i_pixel = PIXEL_16x8;
1358 m->p_cost_mv = a->p_cost_mv;
1360 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1361 LOAD_HPELS( m, p_fref[l], 0, 8*i );
1363 mvc[0][0] = lX->me8x8[2*i].mv[0];
1364 mvc[0][1] = lX->me8x8[2*i].mv[1];
1365 mvc[1][0] = lX->me8x8[2*i+1].mv[0];
1366 mvc[1][1] = lX->me8x8[2*i+1].mv[1];
1368 x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp );
1369 x264_me_search( h, m, mvc, 2 );
1372 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 16,
1373 m->mv[0], m->mv[1], 16, 8 );
1374 /* FIXME: ref cost */
1375 i_part_cost_bi += m->cost_mv;
1378 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1379 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 );
1381 i_part_cost = a->l0.me16x8[i].cost;
1382 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1383 if( a->l1.me16x8[i].cost < i_part_cost )
1385 i_part_cost = a->l1.me16x8[i].cost;
1386 a->i_mb_partition16x8[i] = D_L1_8x8;
1388 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1390 i_part_cost = i_part_cost_bi;
1391 a->i_mb_partition16x8[i] = D_BI_8x8;
1393 a->i_cost16x8bi += i_part_cost;
1396 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1400 a->i_mb_type16x8 = B_L0_L0
1401 + (a->i_mb_partition16x8[0]>>2) * 3
1402 + (a->i_mb_partition16x8[1]>>2);
1403 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1405 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1407 uint8_t **p_fref[2] =
1408 { h->mb.pic.p_fref[0][a->l0.i_ref],
1409 h->mb.pic.p_fref[1][a->l1.i_ref] };
1410 uint8_t pix[2][8*16];
1414 h->mb.i_partition = D_8x16;
1415 a->i_cost8x16bi = 0;
1417 for( i = 0; i < 2; i++ )
1420 int i_part_cost_bi = 0;
1422 for( l = 0; l < 2; l++ )
1424 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1425 x264_me_t *m = &lX->me8x16[i];
1427 m->i_pixel = PIXEL_8x16;
1428 m->p_cost_mv = a->p_cost_mv;
1430 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1431 LOAD_HPELS( m, p_fref[l], 8*i, 0 );
1433 mvc[0][0] = lX->me8x8[i].mv[0];
1434 mvc[0][1] = lX->me8x8[i].mv[1];
1435 mvc[1][0] = lX->me8x8[i+2].mv[0];
1436 mvc[1][1] = lX->me8x8[i+2].mv[1];
1438 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1439 x264_me_search( h, m, mvc, 2 );
1442 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1443 m->mv[0], m->mv[1], 8, 16 );
1444 /* FIXME: ref cost */
1445 i_part_cost_bi += m->cost_mv;
1448 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1449 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 );
1451 i_part_cost = a->l0.me8x16[i].cost;
1452 a->i_mb_partition8x16[i] = D_L0_8x8;
1453 if( a->l1.me8x16[i].cost < i_part_cost )
1455 i_part_cost = a->l1.me8x16[i].cost;
1456 a->i_mb_partition8x16[i] = D_L1_8x8;
1458 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1460 i_part_cost = i_part_cost_bi;
1461 a->i_mb_partition8x16[i] = D_BI_8x8;
1463 a->i_cost8x16bi += i_part_cost;
1466 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1470 a->i_mb_type8x16 = B_L0_L0
1471 + (a->i_mb_partition8x16[0]>>2) * 3
1472 + (a->i_mb_partition8x16[1]>>2);
1473 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1476 static inline void x264_mb_analyse_transform( x264_t *h )
1478 h->mb.cache.b_transform_8x8_allowed =
1479 h->param.analyse.b_transform_8x8
1480 && !IS_INTRA( h->mb.i_type ) && x264_mb_transform_8x8_allowed( h );
1482 if( h->mb.cache.b_transform_8x8_allowed )
1484 int i_cost4, i_cost8;
1485 /* FIXME only luma mc is needed */
1488 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0],
1489 h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] );
1490 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0],
1491 h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] );
1493 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
1497 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_cost )
1499 h->mb.cache.b_transform_8x8_allowed =
1500 h->param.analyse.b_transform_8x8 && x264_mb_transform_8x8_allowed( h );
1502 if( h->mb.cache.b_transform_8x8_allowed )
1505 x264_analyse_update_cache( h, a );
1506 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1507 /* FIXME only luma is needed, but the score for comparison already includes chroma */
1508 i_cost8 = x264_rd_cost_mb( h, a->i_lambda2 );
1510 if( *i_cost >= i_cost8 )
1513 a->i_best_satd = (int64_t)a->i_best_satd * i_cost8 / *i_cost;
1517 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1522 /*****************************************************************************
1523 * x264_macroblock_analyse:
1524 *****************************************************************************/
1525 void x264_macroblock_analyse( x264_t *h )
1527 x264_mb_analysis_t analysis;
1531 h->mb.qp[h->mb.i_mb_xy] = x264_ratecontrol_qp( h );
1534 x264_mb_analyse_init( h, &analysis, h->mb.qp[h->mb.i_mb_xy] );
1536 /*--------------------------- Do the analysis ---------------------------*/
1537 if( h->sh.i_type == SLICE_TYPE_I )
1540 x264_mb_analyse_intra( h, &analysis, COST_MAX );
1542 i_cost = analysis.i_sad_i16x16;
1543 h->mb.i_type = I_16x16;
1544 if( analysis.i_sad_i4x4 < i_cost )
1546 i_cost = analysis.i_sad_i4x4;
1547 h->mb.i_type = I_4x4;
1549 if( analysis.i_sad_i8x8 < i_cost )
1550 h->mb.i_type = I_8x8;
1552 else if( h->sh.i_type == SLICE_TYPE_P )
1556 int i_intra_cost, i_intra_type;
1558 /* Fast P_SKIP detection */
1559 if( !h->mb.b_lossless &&
1560 (( h->mb.i_mb_type_left == P_SKIP ) ||
1561 ( h->mb.i_mb_type_top == P_SKIP ) ||
1562 ( h->mb.i_mb_type_topleft == P_SKIP ) ||
1563 ( h->mb.i_mb_type_topright == P_SKIP )))
1565 b_skip = x264_macroblock_probe_pskip( h );
1570 h->mb.i_type = P_SKIP;
1571 h->mb.i_partition = D_16x16;
1575 const unsigned int flags = h->param.analyse.inter;
1580 x264_mb_analyse_load_costs( h, &analysis );
1582 x264_mb_analyse_inter_p16x16( h, &analysis );
1583 if( flags & X264_ANALYSE_PSUB16x16 )
1584 x264_mb_analyse_inter_p8x8( h, &analysis );
1586 /* Select best inter mode */
1588 i_partition = D_16x16;
1589 i_cost = analysis.l0.me16x16.cost;
1591 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
1592 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
1597 i_partition = D_8x8;
1598 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1599 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1601 i_cost = analysis.l0.i_cost8x8;
1604 if( flags & X264_ANALYSE_PSUB8x8 )
1606 int i_cost_bak = i_cost;
1608 for( i = 0; i < 4; i++ )
1610 x264_mb_analyse_inter_p4x4( h, &analysis, i );
1611 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
1613 int i_cost8x8 = analysis.l0.i_cost4x4[i];
1614 h->mb.i_sub_partition[i] = D_L0_4x4;
1616 x264_mb_analyse_inter_p8x4( h, &analysis, i );
1617 if( analysis.l0.i_cost8x4[i] < i_cost8x8 )
1619 h->mb.i_sub_partition[i] = D_L0_8x4;
1620 i_cost8x8 = analysis.l0.i_cost8x4[i];
1623 x264_mb_analyse_inter_p4x8( h, &analysis, i );
1624 if( analysis.l0.i_cost4x8[i] < i_cost8x8 )
1626 h->mb.i_sub_partition[i] = D_L0_4x8;
1627 i_cost8x8 = analysis.l0.i_cost4x8[i];
1630 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
1633 x264_mb_cache_mv_p8x8( h, &analysis, i );
1635 /* TODO: RD per subpartition */
1636 if( b_sub8x8 && analysis.b_mbrd )
1638 i_cost = x264_rd_cost_mb( h, analysis.i_lambda2 );
1639 if( i_cost > i_cost_bak )
1641 i_cost = i_cost_bak;
1642 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1643 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1649 /* Now do 16x8/8x16 */
1650 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
1651 if( analysis.b_mbrd )
1652 i_thresh16x8 = i_thresh16x8 * analysis.i_lambda2 / analysis.i_lambda;
1653 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
1654 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
1656 x264_mb_analyse_inter_p16x8( h, &analysis );
1657 if( analysis.l0.i_cost16x8 < i_cost )
1660 i_partition = D_16x8;
1661 i_cost = analysis.l0.i_cost16x8;
1664 x264_mb_analyse_inter_p8x16( h, &analysis );
1665 if( analysis.l0.i_cost8x16 < i_cost )
1668 i_partition = D_8x16;
1669 i_cost = analysis.l0.i_cost8x16;
1673 h->mb.i_partition = i_partition;
1676 //FIXME mb_type costs?
1677 if( analysis.b_mbrd )
1679 h->mb.i_type = i_type;
1680 x264_mb_analyse_transform_rd( h, &analysis, &i_cost );
1682 else if( i_partition == D_16x16 )
1684 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1685 i_cost = analysis.l0.me16x16.cost;
1687 else if( i_partition == D_16x8 )
1689 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
1690 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
1691 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
1693 else if( i_partition == D_8x16 )
1695 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
1696 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
1697 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
1699 else if( i_partition == D_8x8 )
1703 for( i8x8 = 0; i8x8 < 4; i8x8++ )
1705 switch( h->mb.i_sub_partition[i8x8] )
1708 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
1709 i_cost += analysis.l0.me8x8[i8x8].cost;
1712 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
1713 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
1714 i_cost += analysis.l0.me8x4[i8x8][0].cost +
1715 analysis.l0.me8x4[i8x8][1].cost;
1718 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
1719 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
1720 i_cost += analysis.l0.me4x8[i8x8][0].cost +
1721 analysis.l0.me4x8[i8x8][1].cost;
1725 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
1726 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
1727 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
1728 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
1729 i_cost += analysis.l0.me4x4[i8x8][0].cost +
1730 analysis.l0.me4x4[i8x8][1].cost +
1731 analysis.l0.me4x4[i8x8][2].cost +
1732 analysis.l0.me4x4[i8x8][3].cost;
1735 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
1741 x264_mb_analyse_intra( h, &analysis, i_cost );
1742 if( h->mb.b_chroma_me && !analysis.b_mbrd &&
1743 ( analysis.i_sad_i16x16 < i_cost
1744 || analysis.i_sad_i8x8 < i_cost
1745 || analysis.i_sad_i4x4 < i_cost ))
1747 x264_mb_analyse_intra_chroma( h, &analysis );
1748 analysis.i_sad_i16x16 += analysis.i_sad_i8x8chroma;
1749 analysis.i_sad_i8x8 += analysis.i_sad_i8x8chroma;
1750 analysis.i_sad_i4x4 += analysis.i_sad_i8x8chroma;
1753 i_intra_type = I_16x16;
1754 i_intra_cost = analysis.i_sad_i16x16;
1756 if( analysis.i_sad_i8x8 < i_intra_cost )
1758 i_intra_type = I_8x8;
1759 i_intra_cost = analysis.i_sad_i8x8;
1761 if( analysis.i_sad_i4x4 < i_intra_cost )
1763 i_intra_type = I_4x4;
1764 i_intra_cost = analysis.i_sad_i4x4;
1767 if( i_intra_cost < i_cost )
1769 i_type = i_intra_type;
1770 i_cost = i_intra_cost;
1773 h->mb.i_type = i_type;
1774 h->stat.frame.i_intra_cost += i_intra_cost;
1775 h->stat.frame.i_inter_cost += i_cost;
1778 else if( h->sh.i_type == SLICE_TYPE_B )
1782 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h );
1783 if( analysis.b_direct_available )
1785 h->mb.i_type = B_SKIP;
1788 /* Conditioning the probe on neighboring block types
1789 * doesn't seem to help speed or quality. */
1790 b_skip = !h->mb.b_lossless && x264_macroblock_probe_bskip( h );
1795 const unsigned int flags = h->param.analyse.inter;
1799 x264_mb_analyse_load_costs( h, &analysis );
1801 /* select best inter mode */
1802 /* direct must be first */
1803 if( analysis.b_direct_available )
1804 x264_mb_analyse_inter_direct( h, &analysis );
1806 x264_mb_analyse_inter_b16x16( h, &analysis );
1808 h->mb.i_type = B_L0_L0;
1809 i_partition = D_16x16;
1810 i_cost = analysis.l0.me16x16.cost;
1811 if( analysis.l1.me16x16.cost < i_cost )
1813 h->mb.i_type = B_L1_L1;
1814 i_cost = analysis.l1.me16x16.cost;
1816 if( analysis.i_cost16x16bi < i_cost )
1818 h->mb.i_type = B_BI_BI;
1819 i_cost = analysis.i_cost16x16bi;
1821 if( analysis.i_cost16x16direct < i_cost )
1823 h->mb.i_type = B_DIRECT;
1824 i_cost = analysis.i_cost16x16direct;
1827 if( flags & X264_ANALYSE_BSUB16x16 )
1829 x264_mb_analyse_inter_b8x8( h, &analysis );
1830 if( analysis.i_cost8x8bi < i_cost )
1832 h->mb.i_type = B_8x8;
1833 i_partition = D_8x8;
1834 i_cost = analysis.i_cost8x8bi;
1836 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
1837 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
1839 x264_mb_analyse_inter_b16x8( h, &analysis );
1840 if( analysis.i_cost16x8bi < i_cost )
1842 i_partition = D_16x8;
1843 i_cost = analysis.i_cost16x8bi;
1844 h->mb.i_type = analysis.i_mb_type16x8;
1847 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
1848 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
1850 x264_mb_analyse_inter_b8x16( h, &analysis );
1851 if( analysis.i_cost8x16bi < i_cost )
1853 i_partition = D_8x16;
1854 i_cost = analysis.i_cost8x16bi;
1855 h->mb.i_type = analysis.i_mb_type8x16;
1861 h->mb.i_partition = i_partition;
1864 if( i_partition == D_16x16 )
1866 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
1867 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
1868 if( h->mb.i_type == B_L0_L0 )
1870 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1871 i_cost = analysis.l0.me16x16.cost
1872 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
1874 else if( h->mb.i_type == B_L1_L1 )
1876 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
1877 i_cost = analysis.l1.me16x16.cost
1878 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
1880 else if( h->mb.i_type == B_BI_BI )
1882 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1883 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
1886 else if( i_partition == D_16x8 )
1888 for( i=0; i<2; i++ )
1890 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
1891 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
1892 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
1893 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
1896 else if( i_partition == D_8x16 )
1898 for( i=0; i<2; i++ )
1900 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
1901 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
1902 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
1903 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
1906 else if( i_partition == D_8x8 )
1908 for( i=0; i<4; i++ )
1911 int i_part_cost_old;
1913 int i_part_type = h->mb.i_sub_partition[i];
1914 int b_bidir = (i_part_type == D_BI_8x8);
1916 if( i_part_type == D_DIRECT_8x8 )
1918 if( x264_mb_partition_listX_table[0][i_part_type] )
1920 m = &analysis.l0.me8x8[i];
1921 i_part_cost_old = m->cost;
1922 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1923 m->cost -= i_type_cost;
1924 x264_me_refine_qpel( h, m );
1926 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
1928 if( x264_mb_partition_listX_table[1][i_part_type] )
1930 m = &analysis.l1.me8x8[i];
1931 i_part_cost_old = m->cost;
1932 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1933 m->cost -= i_type_cost;
1934 x264_me_refine_qpel( h, m );
1936 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
1938 /* TODO: update mvp? */
1942 /* best intra mode */
1943 x264_mb_analyse_intra( h, &analysis, i_cost );
1945 if( analysis.i_sad_i16x16 < i_cost )
1947 h->mb.i_type = I_16x16;
1948 i_cost = analysis.i_sad_i16x16;
1950 if( analysis.i_sad_i8x8 < i_cost )
1952 h->mb.i_type = I_8x8;
1953 i_cost = analysis.i_sad_i8x8;
1955 if( analysis.i_sad_i4x4 < i_cost )
1957 h->mb.i_type = I_4x4;
1958 i_cost = analysis.i_sad_i4x4;
1963 x264_analyse_update_cache( h, &analysis );
1965 if( !analysis.b_mbrd )
1966 x264_mb_analyse_transform( h );
1969 /*-------------------- Update MB from the analysis ----------------------*/
1970 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
1974 switch( h->mb.i_type )
1977 for( i = 0; i < 16; i++ )
1979 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] =
1980 a->i_predict4x4[block_idx_x[i]][block_idx_y[i]];
1983 x264_mb_analyse_intra_chroma( h, a );
1986 for( i = 0; i < 4; i++ )
1987 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1),
1988 a->i_predict8x8[i&1][i>>1] );
1990 x264_mb_analyse_intra_chroma( h, a );
1993 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
1994 x264_mb_analyse_intra_chroma( h, a );
1998 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1999 switch( h->mb.i_partition )
2002 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2006 x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv[0], a->l0.me16x8[0].mv[1] );
2007 x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv[0], a->l0.me16x8[1].mv[1] );
2011 x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv[0], a->l0.me8x16[0].mv[1] );
2012 x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv[0], a->l0.me8x16[1].mv[1] );
2016 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2022 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2023 for( i = 0; i < 4; i++ )
2024 x264_mb_cache_mv_p8x8( h, a, i );
2030 x264_mb_predict_mv_pskip( h, mvp );
2032 h->mb.i_partition = D_16x16;
2033 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2034 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, mvp[0], mvp[1] );
2039 /* nothing has changed since x264_macroblock_probe_bskip */
2042 x264_mb_load_mv_direct8x8( h, 0 );
2043 x264_mb_load_mv_direct8x8( h, 1 );
2044 x264_mb_load_mv_direct8x8( h, 2 );
2045 x264_mb_load_mv_direct8x8( h, 3 );
2049 /* optimize: cache might not need to be rewritten */
2050 for( i = 0; i < 4; i++ )
2051 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2054 default: /* the rest of the B types */
2055 switch( h->mb.i_partition )
2058 switch( h->mb.i_type )
2061 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2062 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2064 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2065 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0, 0 );
2066 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0, 0 );
2069 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2070 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0, 0 );
2071 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0, 0 );
2073 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2074 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2077 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2078 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2080 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2081 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2086 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2087 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2090 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2091 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2094 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2100 #include "slicetype_decision.c"