1 /*****************************************************************************
2 * analyse.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003 x264 project
5 * $Id: analyse.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Loren Merritt <lorenm@u.washington.edu>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
30 #include "common/common.h"
31 #include "macroblock.h"
33 #include "ratecontrol.h"
46 int mvc[32][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][2];
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 */
89 int i_satd_i16x16_dir[7];
93 int i_satd_i8x8_dir[12][4];
100 int i_satd_i8x8chroma;
101 int i_predict8x8chroma;
103 /* II: Inter part P/B frame */
104 x264_mb_analysis_list_t l0;
105 x264_mb_analysis_list_t l1;
107 int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
108 int i_cost16x16direct;
110 int i_cost8x8direct[4];
119 int i_mb_partition16x8[2]; /* mb_partition_e */
120 int i_mb_partition8x16[2];
121 int i_mb_type16x8; /* mb_class_e */
124 int b_direct_available;
126 } x264_mb_analysis_t;
128 /* lambda = pow(2,qp/6-2) */
129 static const int i_qp0_cost_table[52] = {
130 1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
131 1, 1, 1, 1, /* 8-11 */
132 1, 1, 1, 1, 2, 2, 2, 2, /* 12-19 */
133 3, 3, 3, 4, 4, 4, 5, 6, /* 20-27 */
134 6, 7, 8, 9,10,11,13,14, /* 28-35 */
135 16,18,20,23,25,29,32,36, /* 36-43 */
136 40,45,51,57,64,72,81,91 /* 44-51 */
139 /* pow(lambda,2) * .9 */
140 static const int i_qp0_cost2_table[52] = {
141 1, 1, 1, 1, 1, 1, /* 0-5 */
142 1, 1, 1, 1, 1, 1, /* 6-11 */
143 1, 1, 1, 2, 2, 3, /* 12-17 */
144 4, 5, 6, 7, 9, 11, /* 18-23 */
145 14, 18, 23, 29, 36, 46, /* 24-29 */
146 58, 73, 91, 115, 145, 183, /* 30-35 */
147 230, 290, 366, 461, 581, 731, /* 36-41 */
148 922,1161,1463,1843,2322,2926, /* 42-47 */
152 /* TODO: calculate CABAC costs */
153 static const int i_mb_b_cost_table[X264_MBTYPE_MAX] = {
154 9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
156 static const int i_mb_b16x8_cost_table[17] = {
157 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
159 static const int i_sub_mb_b_cost_table[13] = {
160 7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
162 static const int i_sub_mb_p_cost_table[4] = {
166 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
168 /* initialize an array of lambda*nbits for all possible mvs */
169 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
171 static int16_t *p_cost_mv[52];
173 if( !p_cost_mv[a->i_qp] )
175 /* could be faster, but isn't called many times */
176 /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
178 p_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) );
179 p_cost_mv[a->i_qp] += 2*4*2048;
180 for( i = 0; i <= 2*4*2048; i++ )
182 p_cost_mv[a->i_qp][-i] =
183 p_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i );
187 a->p_cost_mv = p_cost_mv[a->i_qp];
190 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
192 memset( a, 0, sizeof( x264_mb_analysis_t ) );
194 /* conduct the analysis using this lamda and QP */
195 a->i_qp = h->mb.i_qp = i_qp;
196 a->i_lambda = i_qp0_cost_table[i_qp];
197 a->i_lambda2 = i_qp0_cost2_table[i_qp];
198 a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
199 ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
201 h->mb.i_me_method = h->param.analyse.i_me_method;
202 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
203 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
204 && h->mb.i_subpel_refine >= 5;
205 h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
206 h->mb.b_transform_8x8 = 0;
207 h->mb.b_noise_reduction = 0;
213 a->i_satd_i8x8chroma = COST_MAX;
217 /* II: Inter part P/B frame */
218 if( h->sh.i_type != SLICE_TYPE_I )
221 int i_fmv_range = h->param.analyse.i_mv_range - 16;
223 /* Calculate max allowed MV range */
224 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range )
225 h->mb.mv_min[0] = 4*( -16*h->mb.i_mb_x - 24 );
226 h->mb.mv_max[0] = 4*( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 24 );
227 h->mb.mv_min_fpel[0] = CLIP_FMV( -16*h->mb.i_mb_x - 8 );
228 h->mb.mv_max_fpel[0] = CLIP_FMV( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 8 );
229 h->mb.mv_min_spel[0] = 4*( h->mb.mv_min_fpel[0] - 16 );
230 h->mb.mv_max_spel[0] = 4*( h->mb.mv_max_fpel[0] + 16 );
231 if( h->mb.i_mb_x == 0)
233 int mb_y = h->mb.i_mb_y >> h->sh.b_mbaff;
234 int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff;
235 h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
236 h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
237 h->mb.mv_min_fpel[1] = CLIP_FMV( -16*mb_y - 8 );
238 h->mb.mv_max_fpel[1] = CLIP_FMV( 16*( mb_height - mb_y - 1 ) + 8 );
239 h->mb.mv_min_spel[1] = 4*( h->mb.mv_min_fpel[1] - 16 );
240 h->mb.mv_max_spel[1] = 4*( h->mb.mv_max_fpel[1] + 16 );
246 a->l0.i_cost8x8 = COST_MAX;
248 for( i = 0; i < 4; i++ )
252 a->l0.i_cost4x8[i] = COST_MAX;
256 a->l0.i_cost8x16 = COST_MAX;
257 if( h->sh.i_type == SLICE_TYPE_B )
261 a->l1.i_cost8x8 = COST_MAX;
263 for( i = 0; i < 4; i++ )
268 a->i_cost8x8direct[i] = COST_MAX;
279 a->i_cost16x16direct =
282 a->i_cost8x16bi = COST_MAX;
285 /* Fast intra decision */
286 if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
288 if( IS_INTRA( h->mb.i_mb_type_left )
289 || IS_INTRA( h->mb.i_mb_type_top )
290 || IS_INTRA( h->mb.i_mb_type_topleft )
291 || IS_INTRA( h->mb.i_mb_type_topright )
292 || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
293 || (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])) )
294 { /* intra is likely */ }
309 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
311 if( i_neighbour & MB_TOPLEFT )
313 /* top and left available */
314 *mode++ = I_PRED_16x16_V;
315 *mode++ = I_PRED_16x16_H;
316 *mode++ = I_PRED_16x16_DC;
317 *mode++ = I_PRED_16x16_P;
320 else if( i_neighbour & MB_LEFT )
323 *mode++ = I_PRED_16x16_DC_LEFT;
324 *mode++ = I_PRED_16x16_H;
327 else if( i_neighbour & MB_TOP )
330 *mode++ = I_PRED_16x16_DC_TOP;
331 *mode++ = I_PRED_16x16_V;
337 *mode = I_PRED_16x16_DC_128;
343 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
345 if( i_neighbour & MB_TOPLEFT )
347 /* top and left available */
348 *mode++ = I_PRED_CHROMA_V;
349 *mode++ = I_PRED_CHROMA_H;
350 *mode++ = I_PRED_CHROMA_DC;
351 *mode++ = I_PRED_CHROMA_P;
354 else if( i_neighbour & MB_LEFT )
357 *mode++ = I_PRED_CHROMA_DC_LEFT;
358 *mode++ = I_PRED_CHROMA_H;
361 else if( i_neighbour & MB_TOP )
364 *mode++ = I_PRED_CHROMA_DC_TOP;
365 *mode++ = I_PRED_CHROMA_V;
371 *mode = I_PRED_CHROMA_DC_128;
377 static void predict_4x4_mode_available( unsigned int i_neighbour,
378 int *mode, int *pi_count )
380 int b_l = i_neighbour & MB_LEFT;
381 int b_t = i_neighbour & MB_TOP;
386 *mode++ = I_PRED_4x4_DC;
387 *mode++ = I_PRED_4x4_H;
388 *mode++ = I_PRED_4x4_V;
389 *mode++ = I_PRED_4x4_DDL;
390 if( i_neighbour & MB_TOPLEFT )
392 *mode++ = I_PRED_4x4_DDR;
393 *mode++ = I_PRED_4x4_VR;
394 *mode++ = I_PRED_4x4_HD;
397 *mode++ = I_PRED_4x4_VL;
398 *mode++ = I_PRED_4x4_HU;
402 *mode++ = I_PRED_4x4_DC_LEFT;
403 *mode++ = I_PRED_4x4_H;
404 *mode++ = I_PRED_4x4_HU;
409 *mode++ = I_PRED_4x4_DC_TOP;
410 *mode++ = I_PRED_4x4_V;
411 *mode++ = I_PRED_4x4_DDL;
412 *mode++ = I_PRED_4x4_VL;
417 *mode++ = I_PRED_4x4_DC_128;
422 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
429 uint8_t *p_dstc[2], *p_srcc[2];
431 if( a->i_satd_i8x8chroma < COST_MAX )
434 /* 8x8 prediction selection for chroma */
435 p_dstc[0] = h->mb.pic.p_fdec[1];
436 p_dstc[1] = h->mb.pic.p_fdec[2];
437 p_srcc[0] = h->mb.pic.p_fenc[1];
438 p_srcc[1] = h->mb.pic.p_fenc[2];
440 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
441 a->i_satd_i8x8chroma = COST_MAX;
442 if( i_max == 4 && h->pixf.intra_satd_x3_8x8c && h->pixf.mbcmp[0] == h->pixf.satd[0] )
444 int satdu[4], satdv[4];
445 h->pixf.intra_satd_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
446 h->pixf.intra_satd_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
447 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
448 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
449 satdu[I_PRED_CHROMA_P] =
450 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
451 satdv[I_PRED_CHROMA_P] =
452 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
454 for( i=0; i<i_max; i++ )
456 int i_mode = predict_mode[i];
457 int i_satd = satdu[i_mode] + satdv[i_mode]
458 + a->i_lambda * bs_size_ue(i_mode);
459 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
464 for( i=0; i<i_max; i++ )
467 int i_mode = predict_mode[i];
469 /* we do the prediction */
470 h->predict_8x8c[i_mode]( p_dstc[0] );
471 h->predict_8x8c[i_mode]( p_dstc[1] );
473 /* we calculate the cost */
474 i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
475 p_srcc[0], FENC_STRIDE ) +
476 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
477 p_srcc[1], FENC_STRIDE ) +
478 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
480 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
484 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
487 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
489 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
490 uint8_t *p_src = h->mb.pic.p_fenc[0];
491 uint8_t *p_dst = h->mb.pic.p_fdec[0];
496 int b_merged_satd = h->pixf.intra_satd_x3_16x16 && h->pixf.mbcmp[0] == h->pixf.satd[0];
498 /*---------------- Try all mode and calculate their score ---------------*/
500 /* 16x16 prediction selection */
501 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
503 if( b_merged_satd && i_max == 4 )
505 h->pixf.intra_satd_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
506 h->predict_16x16[I_PRED_16x16_P]( p_dst );
507 a->i_satd_i16x16_dir[I_PRED_16x16_P] =
508 h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
511 int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
512 COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
517 for( i = 0; i < i_max; i++ )
520 int i_mode = predict_mode[i];
521 h->predict_16x16[i_mode]( p_dst );
523 i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
524 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
525 COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
526 a->i_satd_i16x16_dir[i_mode] = i_satd;
530 if( h->sh.i_type == SLICE_TYPE_B )
531 /* cavlc mb type prefix */
532 a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
533 if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
536 /* 8x8 prediction selection */
537 if( flags & X264_ANALYSE_I8x8 )
539 DECLARE_ALIGNED( uint8_t, edge[33], 8 );
540 x264_pixel_cmp_t sa8d = (*h->pixf.mbcmp == *h->pixf.sad) ? h->pixf.sad[PIXEL_8x8] : h->pixf.sa8d[PIXEL_8x8];
541 int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
543 b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
545 // FIXME some bias like in i4x4?
546 if( h->sh.i_type == SLICE_TYPE_B )
547 i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
549 for( idx = 0;; idx++ )
553 uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
554 uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
555 int i_best = COST_MAX;
556 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
558 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
559 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
561 if( b_merged_satd && i_max == 9 )
564 h->pixf.intra_sa8d_x3_8x8( p_src_by, edge, satd );
565 if( i_pred_mode < 3 )
566 satd[i_pred_mode] -= 3 * a->i_lambda;
567 for( i=2; i>=0; i-- )
569 int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
570 COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
577 for( ; i<i_max; i++ )
580 int i_mode = predict_mode[i];
582 h->predict_8x8[i_mode]( p_dst_by, edge );
584 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
585 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
587 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
588 a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
592 if( idx == 3 || i_cost > i_satd_thresh )
595 /* we need to encode this block now (for next ones) */
596 h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
597 x264_mb_encode_i8x8( h, idx, a->i_qp );
599 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
603 a->i_satd_i8x8 = i_cost;
606 a->i_satd_i8x8 = COST_MAX;
607 i_cost = i_cost * 4/(idx+1);
609 if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 )
613 /* 4x4 prediction selection */
614 if( flags & X264_ANALYSE_I4x4 )
617 int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
618 b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
620 i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
622 i_cost = a->i_lambda * 24; /* from JVT (SATD0) */
623 if( h->sh.i_type == SLICE_TYPE_B )
624 i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];
626 for( idx = 0;; idx++ )
628 int x = block_idx_x[idx];
629 int y = block_idx_y[idx];
630 uint8_t *p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
631 uint8_t *p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
632 int i_best = COST_MAX;
633 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
635 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
637 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
638 /* emulate missing topright samples */
639 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
641 if( b_merged_satd && i_max >= 6 )
644 h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
645 if( i_pred_mode < 3 )
646 satd[i_pred_mode] -= 3 * a->i_lambda;
647 for( i=2; i>=0; i-- )
648 COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
649 a->i_predict4x4[idx], i );
655 for( ; i<i_max; i++ )
658 int i_mode = predict_mode[i];
660 h->predict_4x4[i_mode]( p_dst_by );
662 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
663 p_src_by, FENC_STRIDE )
664 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
666 COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
670 if( i_cost > i_satd_thresh || idx == 15 )
673 /* we need to encode this block now (for next ones) */
674 h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
675 x264_mb_encode_i4x4( h, idx, a->i_qp );
677 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
680 a->i_satd_i4x4 = i_cost;
682 a->i_satd_i4x4 = COST_MAX;
686 static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
688 if( a->i_satd_i16x16 <= i_satd_thresh )
690 h->mb.i_type = I_16x16;
691 x264_analyse_update_cache( h, a );
692 a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
695 a->i_satd_i16x16 = COST_MAX;
697 if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
699 h->mb.i_type = I_4x4;
700 x264_analyse_update_cache( h, a );
701 a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
704 a->i_satd_i4x4 = COST_MAX;
706 if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
708 h->mb.i_type = I_8x8;
709 x264_analyse_update_cache( h, a );
710 a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
713 a->i_satd_i8x8 = COST_MAX;
716 static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
718 uint8_t *p_src = h->mb.pic.p_fenc[0];
719 uint8_t *p_dst = h->mb.pic.p_fdec[0];
722 int i_max, i_satd, i_best, i_mode;
726 if( h->mb.i_type == I_16x16 )
728 int old_pred_mode = a->i_predict16x16;
729 int i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
730 i_best = a->i_satd_i16x16;
731 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
732 for( i = 0; i < i_max; i++ )
734 int i_mode = predict_mode[i];
735 if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
737 h->mb.i_intra16x16_pred_mode = i_mode;
738 i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
739 COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
742 else if( h->mb.i_type == I_4x4 )
744 uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
746 for( idx = 0; idx < 16; idx++ )
752 i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
753 x = block_idx_x[idx];
754 y = block_idx_y[idx];
756 p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
757 p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
758 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
760 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
761 /* emulate missing topright samples */
762 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
764 for( i = 0; i < i_max; i++ )
766 i_mode = predict_mode[i];
767 h->predict_4x4[i_mode]( p_dst_by );
768 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
770 if( i_best > i_satd )
772 a->i_predict4x4[idx] = i_mode;
774 pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
775 pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
776 pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
777 pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
778 i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
782 *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
783 *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
784 *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
785 *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
786 h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
788 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
791 else if( h->mb.i_type == I_8x8 )
793 DECLARE_ALIGNED( uint8_t, edge[33], 8 );
794 for( idx = 0; idx < 4; idx++ )
802 int i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
805 i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
809 p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
810 p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
811 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
812 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
814 for( i = 0; i < i_max; i++ )
816 i_mode = predict_mode[i];
817 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
819 h->predict_8x8[i_mode]( p_dst_by, edge );
820 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
822 if( i_best > i_satd )
824 a->i_predict8x8[idx] = i_mode;
827 pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
830 pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
832 i_nnz[j] = h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]];
836 *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
839 p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
841 h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]] = i_nnz[j];
843 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
848 #define LOAD_FENC( m, src, xoff, yoff) \
849 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
850 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
851 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
852 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
853 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
855 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
856 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
857 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
858 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
859 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
860 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
861 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
862 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
864 #define REF_COST(list, ref) \
865 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
867 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
871 int mvc[7][2], i_mvc;
872 int i_halfpel_thresh = INT_MAX;
873 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
875 /* 16x16 Search on all ref frame */
876 m.i_pixel = PIXEL_16x16;
877 m.p_cost_mv = a->p_cost_mv;
878 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
880 a->l0.me16x16.cost = INT_MAX;
881 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
883 const int i_ref_cost = REF_COST( 0, i_ref );
884 i_halfpel_thresh -= i_ref_cost;
885 m.i_ref_cost = i_ref_cost;
888 /* search with ref */
889 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
890 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
891 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
892 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
895 * SSD threshold would probably be better than SATD */
898 && m.cost-m.cost_mv < 300*a->i_lambda
899 && abs(m.mv[0]-h->mb.cache.pskip_mv[0])
900 + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
901 && x264_macroblock_probe_pskip( h ) )
903 h->mb.i_type = P_SKIP;
904 x264_analyse_update_cache( h, a );
908 m.cost += i_ref_cost;
909 i_halfpel_thresh += i_ref_cost;
911 if( m.cost < a->l0.me16x16.cost )
914 /* save mv for predicting neighbors */
915 a->l0.mvc[i_ref][0][0] =
916 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
917 a->l0.mvc[i_ref][0][1] =
918 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
921 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
924 if( a->b_mbrd && a->l0.i_ref == 0
925 && a->l0.me16x16.mv[0] == h->mb.cache.pskip_mv[0]
926 && a->l0.me16x16.mv[1] == h->mb.cache.pskip_mv[1] )
928 h->mb.i_partition = D_16x16;
929 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
930 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
934 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
938 uint8_t **p_fenc = h->mb.pic.p_fenc;
939 int i_halfpel_thresh = INT_MAX;
940 int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
942 int i_maxref = h->mb.pic.i_fref[0]-1;
944 h->mb.i_partition = D_8x8;
946 /* early termination: if 16x16 chose ref 0, then evalute no refs older
947 * than those used by the neighbors */
948 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
949 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
952 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
953 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
954 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
955 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
956 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
957 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
960 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
962 a->l0.mvc[i_ref][0][0] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0];
963 a->l0.mvc[i_ref][0][1] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1];
966 for( i = 0; i < 4; i++ )
968 x264_me_t *l0m = &a->l0.me8x8[i];
972 m.i_pixel = PIXEL_8x8;
973 m.p_cost_mv = a->p_cost_mv;
975 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
977 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
979 const int i_ref_cost = REF_COST( 0, i_ref );
980 i_halfpel_thresh -= i_ref_cost;
981 m.i_ref_cost = i_ref_cost;
984 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
985 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
986 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
987 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
989 m.cost += i_ref_cost;
990 i_halfpel_thresh += i_ref_cost;
991 *(uint64_t*)a->l0.mvc[i_ref][i+1] = *(uint64_t*)m.mv;
993 if( m.cost < l0m->cost )
996 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv[0], l0m->mv[1] );
997 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1000 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1003 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1004 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1005 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1006 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1009 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1011 const int i_ref = a->l0.me16x16.i_ref;
1012 const int i_ref_cost = REF_COST( 0, i_ref );
1013 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
1014 uint8_t **p_fenc = h->mb.pic.p_fenc;
1016 int (*mvc)[2] = a->l0.mvc[i_ref];
1019 /* XXX Needed for x264_mb_predict_mv */
1020 h->mb.i_partition = D_8x8;
1023 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.me16x16.mv;
1025 for( i = 0; i < 4; i++ )
1027 x264_me_t *m = &a->l0.me8x8[i];
1031 m->i_pixel = PIXEL_8x8;
1032 m->p_cost_mv = a->p_cost_mv;
1033 m->i_ref_cost = i_ref_cost;
1036 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1037 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1038 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1039 x264_me_search( h, m, mvc, i_mvc );
1041 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
1043 *(uint64_t*)mvc[i_mvc] = *(uint64_t*)m->mv;
1047 m->cost += i_ref_cost;
1048 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1051 /* theoretically this should include 4*ref_cost,
1052 * but 3 seems a better approximation of cabac. */
1053 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1054 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
1055 REF_COST( 0, a->l0.me16x16.i_ref );
1056 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1057 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1060 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
1063 uint8_t **p_fenc = h->mb.pic.p_fenc;
1067 /* XXX Needed for x264_mb_predict_mv */
1068 h->mb.i_partition = D_16x8;
1070 for( i = 0; i < 2; i++ )
1072 x264_me_t *l0m = &a->l0.me16x8[i];
1073 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1074 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1076 m.i_pixel = PIXEL_16x8;
1077 m.p_cost_mv = a->p_cost_mv;
1079 LOAD_FENC( &m, p_fenc, 0, 8*i );
1080 l0m->cost = INT_MAX;
1081 for( j = 0; j < i_ref8s; j++ )
1083 const int i_ref = ref8[j];
1084 const int i_ref_cost = REF_COST( 0, i_ref );
1085 m.i_ref_cost = i_ref_cost;
1088 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1089 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1090 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][2*i+1];
1091 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][2*i+2];
1093 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1094 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1095 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1096 x264_me_search( h, &m, mvc, 3 );
1098 m.cost += i_ref_cost;
1100 if( m.cost < l0m->cost )
1103 x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, l0m->mv[0], l0m->mv[1] );
1104 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1107 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1110 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1113 uint8_t **p_fenc = h->mb.pic.p_fenc;
1117 /* XXX Needed for x264_mb_predict_mv */
1118 h->mb.i_partition = D_8x16;
1120 for( i = 0; i < 2; i++ )
1122 x264_me_t *l0m = &a->l0.me8x16[i];
1123 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1124 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1126 m.i_pixel = PIXEL_8x16;
1127 m.p_cost_mv = a->p_cost_mv;
1129 LOAD_FENC( &m, p_fenc, 8*i, 0 );
1130 l0m->cost = INT_MAX;
1131 for( j = 0; j < i_ref8s; j++ )
1133 const int i_ref = ref8[j];
1134 const int i_ref_cost = REF_COST( 0, i_ref );
1135 m.i_ref_cost = i_ref_cost;
1138 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1139 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][i+1];
1140 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][i+3];
1142 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1143 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1144 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1145 x264_me_search( h, &m, mvc, 3 );
1147 m.cost += i_ref_cost;
1149 if( m.cost < l0m->cost )
1152 x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, l0m->mv[0], l0m->mv[1] );
1153 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1156 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1159 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1161 DECLARE_ALIGNED( uint8_t, pix1[16*8], 8 );
1162 uint8_t *pix2 = pix1+8;
1163 const int i_stride = h->mb.pic.i_stride[1];
1164 const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1165 const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1167 #define CHROMA4x4MC( width, height, me, x, y ) \
1168 h->mc.mc_chroma( &p_fref[4][or+x+y*i_stride], i_stride, &pix1[x+y*16], 16, (me).mv[0], (me).mv[1], width, height ); \
1169 h->mc.mc_chroma( &p_fref[5][or+x+y*i_stride], i_stride, &pix2[x+y*16], 16, (me).mv[0], (me).mv[1], width, height );
1171 if( pixel == PIXEL_4x4 )
1173 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1174 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 0,2 );
1175 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 2,0 );
1176 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1178 else if( pixel == PIXEL_8x4 )
1180 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1181 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1185 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1186 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1189 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1190 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1193 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1195 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1196 uint8_t **p_fenc = h->mb.pic.p_fenc;
1197 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1200 /* XXX Needed for x264_mb_predict_mv */
1201 h->mb.i_partition = D_8x8;
1203 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1205 const int idx = 4*i8x8 + i4x4;
1206 const int x4 = block_idx_x[idx];
1207 const int y4 = block_idx_y[idx];
1208 const int i_mvc = (i4x4 == 0);
1210 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1212 m->i_pixel = PIXEL_4x4;
1213 m->p_cost_mv = a->p_cost_mv;
1215 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1216 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1218 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1219 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1221 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
1223 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1224 a->l0.me4x4[i8x8][1].cost +
1225 a->l0.me4x4[i8x8][2].cost +
1226 a->l0.me4x4[i8x8][3].cost +
1227 REF_COST( 0, i_ref ) +
1228 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1229 if( h->mb.b_chroma_me )
1230 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1233 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1235 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1236 uint8_t **p_fenc = h->mb.pic.p_fenc;
1237 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1240 /* XXX Needed for x264_mb_predict_mv */
1241 h->mb.i_partition = D_8x8;
1243 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1245 const int idx = 4*i8x8 + 2*i8x4;
1246 const int x4 = block_idx_x[idx];
1247 const int y4 = block_idx_y[idx];
1248 const int i_mvc = (i8x4 == 0);
1250 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1252 m->i_pixel = PIXEL_8x4;
1253 m->p_cost_mv = a->p_cost_mv;
1255 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1256 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1258 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1259 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1261 x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
1263 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1264 REF_COST( 0, i_ref ) +
1265 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1266 if( h->mb.b_chroma_me )
1267 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1270 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1272 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1273 uint8_t **p_fenc = h->mb.pic.p_fenc;
1274 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1277 /* XXX Needed for x264_mb_predict_mv */
1278 h->mb.i_partition = D_8x8;
1280 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1282 const int idx = 4*i8x8 + i4x8;
1283 const int x4 = block_idx_x[idx];
1284 const int y4 = block_idx_y[idx];
1285 const int i_mvc = (i4x8 == 0);
1287 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1289 m->i_pixel = PIXEL_4x8;
1290 m->p_cost_mv = a->p_cost_mv;
1292 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1293 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1295 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1296 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1298 x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
1300 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1301 REF_COST( 0, i_ref ) +
1302 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1303 if( h->mb.b_chroma_me )
1304 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1307 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1309 /* Assumes that fdec still contains the results of
1310 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1312 uint8_t **p_fenc = h->mb.pic.p_fenc;
1313 uint8_t **p_fdec = h->mb.pic.p_fdec;
1316 a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1317 for( i = 0; i < 4; i++ )
1319 const int x = (i&1)*8;
1320 const int y = (i>>1)*8;
1321 a->i_cost16x16direct +=
1322 a->i_cost8x8direct[i] =
1323 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1326 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1330 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1332 if( h->param.analyse.b_weighted_bipred ) \
1333 h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1334 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1336 h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1339 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1341 uint8_t pix1[16*16], pix2[16*16];
1348 int mvc[8][2], i_mvc;
1349 int i_halfpel_thresh = INT_MAX;
1350 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1352 /* 16x16 Search on all ref frame */
1353 m.i_pixel = PIXEL_16x16;
1354 m.p_cost_mv = a->p_cost_mv;
1355 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1358 a->l0.me16x16.cost = INT_MAX;
1359 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1361 /* search with ref */
1362 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1363 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1364 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1365 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1368 m.cost += REF_COST( 0, i_ref );
1370 if( m.cost < a->l0.me16x16.cost )
1372 a->l0.i_ref = i_ref;
1376 /* save mv for predicting neighbors */
1377 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1378 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1380 /* subtract ref cost, so we don't have to add it for the other MB types */
1381 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1384 i_halfpel_thresh = INT_MAX;
1385 p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1386 a->l1.me16x16.cost = INT_MAX;
1387 for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1389 /* search with ref */
1390 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1391 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1392 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1393 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1396 m.cost += REF_COST( 1, i_ref );
1398 if( m.cost < a->l1.me16x16.cost )
1400 a->l1.i_ref = i_ref;
1404 /* save mv for predicting neighbors */
1405 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1406 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1408 /* subtract ref cost, so we don't have to add it for the other MB types */
1409 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1411 /* Set global ref, needed for other modes? */
1412 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1413 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1415 /* get cost of BI mode */
1416 weight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1417 if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
1419 /* l0 reference is halfpel, so get_ref on it will make it faster */
1420 src2 = h->mc.get_ref( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1422 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1424 h->mc.mc_luma( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1426 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1428 weight = 64 - weight;
1432 /* if l0 was qpel, we'll use get_ref on l1 instead */
1433 h->mc.mc_luma( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1435 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1437 src2 = h->mc.get_ref( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1439 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1443 if( h->param.analyse.b_weighted_bipred )
1444 h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2, weight );
1446 h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1448 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix1, 16 )
1449 + REF_COST( 0, a->l0.i_ref )
1450 + REF_COST( 1, a->l1.i_ref )
1451 + a->l0.me16x16.cost_mv
1452 + a->l1.me16x16.cost_mv;
1455 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1456 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1457 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1460 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1462 const int x = 2*(i%2);
1463 const int y = 2*(i/2);
1465 switch( h->mb.i_sub_partition[i] )
1468 x264_macroblock_cache_mv( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv[0], a->l0.me8x8[i].mv[1] );
1471 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] );
1472 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] );
1475 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] );
1476 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] );
1479 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] );
1480 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] );
1481 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] );
1482 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] );
1485 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1490 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1491 if( x264_mb_partition_listX_table[0][part] ) \
1493 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1494 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
1498 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1499 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0, 0 ); \
1501 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
1503 if( x264_mb_partition_listX_table[1][part] ) \
1505 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1506 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
1510 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1511 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0, 0 ); \
1513 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
1516 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1520 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1522 x264_mb_load_mv_direct8x8( h, i );
1525 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0, 0 );
1526 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0, 0 );
1527 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1532 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1535 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1537 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1539 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1541 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1545 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1547 uint8_t **p_fref[2] =
1548 { h->mb.pic.p_fref[0][a->l0.i_ref],
1549 h->mb.pic.p_fref[1][a->l1.i_ref] };
1550 uint8_t pix[2][8*8];
1553 /* XXX Needed for x264_mb_predict_mv */
1554 h->mb.i_partition = D_8x8;
1558 for( i = 0; i < 4; i++ )
1563 int i_part_cost_bi = 0;
1565 for( l = 0; l < 2; l++ )
1567 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1568 x264_me_t *m = &lX->me8x8[i];
1570 m->i_pixel = PIXEL_8x8;
1571 m->p_cost_mv = a->p_cost_mv;
1573 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1574 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1576 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1577 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1579 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
1582 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1583 m->mv[0], m->mv[1], 8, 8 );
1584 i_part_cost_bi += m->cost_mv;
1585 /* FIXME: ref cost */
1588 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1589 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1590 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1591 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1592 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1594 i_part_cost = a->l0.me8x8[i].cost;
1595 h->mb.i_sub_partition[i] = D_L0_8x8;
1596 COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1597 COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1598 COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1599 a->i_cost8x8bi += i_part_cost;
1601 /* XXX Needed for x264_mb_predict_mv */
1602 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1606 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1609 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1611 uint8_t **p_fref[2] =
1612 { h->mb.pic.p_fref[0][a->l0.i_ref],
1613 h->mb.pic.p_fref[1][a->l1.i_ref] };
1614 DECLARE_ALIGNED( uint8_t, pix[2][16*8], 16 );
1618 h->mb.i_partition = D_16x8;
1619 a->i_cost16x8bi = 0;
1621 for( i = 0; i < 2; i++ )
1624 int i_part_cost_bi = 0;
1626 /* TODO: check only the list(s) that were used in b8x8? */
1627 for( l = 0; l < 2; l++ )
1629 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1630 x264_me_t *m = &lX->me16x8[i];
1632 m->i_pixel = PIXEL_16x8;
1633 m->p_cost_mv = a->p_cost_mv;
1635 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1636 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1638 mvc[0][0] = lX->me8x8[2*i].mv[0];
1639 mvc[0][1] = lX->me8x8[2*i].mv[1];
1640 mvc[1][0] = lX->me8x8[2*i+1].mv[0];
1641 mvc[1][1] = lX->me8x8[2*i+1].mv[1];
1643 x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp );
1644 x264_me_search( h, m, mvc, 2 );
1647 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 16,
1648 m->mv[0], m->mv[1], 16, 8 );
1649 /* FIXME: ref cost */
1650 i_part_cost_bi += m->cost_mv;
1653 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1654 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1656 i_part_cost = a->l0.me16x8[i].cost;
1657 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1658 if( a->l1.me16x8[i].cost < i_part_cost )
1660 i_part_cost = a->l1.me16x8[i].cost;
1661 a->i_mb_partition16x8[i] = D_L1_8x8;
1663 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1665 i_part_cost = i_part_cost_bi;
1666 a->i_mb_partition16x8[i] = D_BI_8x8;
1668 a->i_cost16x8bi += i_part_cost;
1670 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1674 a->i_mb_type16x8 = B_L0_L0
1675 + (a->i_mb_partition16x8[0]>>2) * 3
1676 + (a->i_mb_partition16x8[1]>>2);
1677 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1680 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1682 uint8_t **p_fref[2] =
1683 { h->mb.pic.p_fref[0][a->l0.i_ref],
1684 h->mb.pic.p_fref[1][a->l1.i_ref] };
1685 uint8_t pix[2][8*16];
1689 h->mb.i_partition = D_8x16;
1690 a->i_cost8x16bi = 0;
1692 for( i = 0; i < 2; i++ )
1695 int i_part_cost_bi = 0;
1697 for( l = 0; l < 2; l++ )
1699 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1700 x264_me_t *m = &lX->me8x16[i];
1702 m->i_pixel = PIXEL_8x16;
1703 m->p_cost_mv = a->p_cost_mv;
1705 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1706 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1708 mvc[0][0] = lX->me8x8[i].mv[0];
1709 mvc[0][1] = lX->me8x8[i].mv[1];
1710 mvc[1][0] = lX->me8x8[i+2].mv[0];
1711 mvc[1][1] = lX->me8x8[i+2].mv[1];
1713 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1714 x264_me_search( h, m, mvc, 2 );
1717 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1718 m->mv[0], m->mv[1], 8, 16 );
1719 /* FIXME: ref cost */
1720 i_part_cost_bi += m->cost_mv;
1723 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1724 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1726 i_part_cost = a->l0.me8x16[i].cost;
1727 a->i_mb_partition8x16[i] = D_L0_8x8;
1728 if( a->l1.me8x16[i].cost < i_part_cost )
1730 i_part_cost = a->l1.me8x16[i].cost;
1731 a->i_mb_partition8x16[i] = D_L1_8x8;
1733 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1735 i_part_cost = i_part_cost_bi;
1736 a->i_mb_partition8x16[i] = D_BI_8x8;
1738 a->i_cost8x16bi += i_part_cost;
1740 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1744 a->i_mb_type8x16 = B_L0_L0
1745 + (a->i_mb_partition8x16[0]>>2) * 3
1746 + (a->i_mb_partition8x16[1]>>2);
1747 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1750 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1752 int thresh = i_satd * 5/4;
1754 h->mb.i_type = P_L0;
1755 if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1757 h->mb.i_partition = D_16x16;
1758 x264_analyse_update_cache( h, a );
1759 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1761 a->l0.me16x16.cost = a->l0.i_rd16x16;
1763 if( a->l0.i_cost16x8 <= thresh )
1765 h->mb.i_partition = D_16x8;
1766 x264_analyse_update_cache( h, a );
1767 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1770 a->l0.i_cost16x8 = COST_MAX;
1772 if( a->l0.i_cost8x16 <= thresh )
1774 h->mb.i_partition = D_8x16;
1775 x264_analyse_update_cache( h, a );
1776 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1779 a->l0.i_cost8x16 = COST_MAX;
1781 if( a->l0.i_cost8x8 <= thresh )
1783 h->mb.i_type = P_8x8;
1784 x264_analyse_update_cache( h, a );
1785 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1787 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1789 /* FIXME: RD per subpartition */
1793 for( i=0; i<4; i++ )
1795 part_bak[i] = h->mb.i_sub_partition[i];
1796 b_sub8x8 |= (part_bak[i] != D_L0_8x8);
1800 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1801 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1802 i_cost = x264_rd_cost_mb( h, a->i_lambda2 );
1803 if( a->l0.i_cost8x8 < i_cost )
1805 for( i=0; i<4; i++ )
1806 h->mb.i_sub_partition[i] = part_bak[i];
1809 a->l0.i_cost8x8 = i_cost;
1814 a->l0.i_cost8x8 = COST_MAX;
1817 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
1819 int thresh = i_satd_inter * 17/16;
1821 if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
1823 h->mb.i_type = B_DIRECT;
1824 x264_analyse_update_cache( h, a );
1825 a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
1828 //FIXME not all the update_cache calls are needed
1829 h->mb.i_partition = D_16x16;
1831 if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
1833 h->mb.i_type = B_L0_L0;
1834 x264_analyse_update_cache( h, a );
1835 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1839 if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
1841 h->mb.i_type = B_L1_L1;
1842 x264_analyse_update_cache( h, a );
1843 a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1847 if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
1849 h->mb.i_type = B_BI_BI;
1850 x264_analyse_update_cache( h, a );
1851 a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1855 if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
1857 h->mb.i_type = B_8x8;
1858 h->mb.i_partition = D_8x8;
1859 x264_analyse_update_cache( h, a );
1860 a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1861 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1865 if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
1867 h->mb.i_type = a->i_mb_type16x8;
1868 h->mb.i_partition = D_16x8;
1869 x264_analyse_update_cache( h, a );
1870 a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1874 if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
1876 h->mb.i_type = a->i_mb_type8x16;
1877 h->mb.i_partition = D_8x16;
1878 x264_analyse_update_cache( h, a );
1879 a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1883 static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
1885 const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1888 switch( h->mb.i_partition )
1891 if( h->mb.i_type == B_BI_BI )
1892 x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
1895 for( i=0; i<2; i++ )
1896 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
1897 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
1900 for( i=0; i<2; i++ )
1901 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
1902 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
1905 for( i=0; i<4; i++ )
1906 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
1907 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
1912 static inline void x264_mb_analyse_transform( x264_t *h )
1914 h->mb.cache.b_transform_8x8_allowed =
1915 h->pps->b_transform_8x8_mode
1916 && !IS_INTRA( h->mb.i_type ) && x264_mb_transform_8x8_allowed( h );
1918 if( h->mb.cache.b_transform_8x8_allowed && h->param.analyse.b_transform_8x8 )
1920 int i_cost4, i_cost8;
1921 /* FIXME only luma mc is needed */
1924 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
1925 h->mb.pic.p_fdec[0], FDEC_STRIDE );
1926 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
1927 h->mb.pic.p_fdec[0], FDEC_STRIDE );
1929 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
1933 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
1935 h->mb.cache.b_transform_8x8_allowed =
1936 h->pps->b_transform_8x8_mode && x264_mb_transform_8x8_allowed( h );
1938 if( h->mb.cache.b_transform_8x8_allowed && h->param.analyse.b_transform_8x8 )
1941 x264_analyse_update_cache( h, a );
1942 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1943 /* FIXME only luma is needed, but the score for comparison already includes chroma */
1944 i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
1946 if( *i_rd >= i_rd8 )
1949 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
1950 /* prevent a rare division by zero in estimated intra cost */
1957 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1962 /*****************************************************************************
1963 * x264_macroblock_analyse:
1964 *****************************************************************************/
1965 void x264_macroblock_analyse( x264_t *h )
1967 x264_mb_analysis_t analysis;
1968 int i_cost = COST_MAX;
1972 x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
1974 /*--------------------------- Do the analysis ---------------------------*/
1975 if( h->sh.i_type == SLICE_TYPE_I )
1977 x264_mb_analyse_intra( h, &analysis, COST_MAX );
1978 if( analysis.b_mbrd )
1979 x264_intra_rd( h, &analysis, COST_MAX );
1981 i_cost = analysis.i_satd_i16x16;
1982 h->mb.i_type = I_16x16;
1983 if( analysis.i_satd_i4x4 < i_cost )
1985 i_cost = analysis.i_satd_i4x4;
1986 h->mb.i_type = I_4x4;
1988 if( analysis.i_satd_i8x8 < i_cost )
1989 h->mb.i_type = I_8x8;
1991 if( h->mb.i_subpel_refine >= 7 )
1992 x264_intra_rd_refine( h, &analysis );
1994 else if( h->sh.i_type == SLICE_TYPE_P )
1997 int i_intra_cost, i_intra_type;
1999 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2001 /* Fast P_SKIP detection */
2002 analysis.b_try_pskip = 0;
2003 if( h->param.analyse.b_fast_pskip )
2005 if( h->param.analyse.i_subpel_refine >= 3 )
2006 analysis.b_try_pskip = 1;
2007 else if( h->mb.i_mb_type_left == P_SKIP ||
2008 h->mb.i_mb_type_top == P_SKIP ||
2009 h->mb.i_mb_type_topleft == P_SKIP ||
2010 h->mb.i_mb_type_topright == P_SKIP )
2011 b_skip = x264_macroblock_probe_pskip( h );
2014 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2018 h->mb.i_type = P_SKIP;
2019 h->mb.i_partition = D_16x16;
2023 const unsigned int flags = h->param.analyse.inter;
2027 int i_satd_inter, i_satd_intra;
2029 x264_mb_analyse_load_costs( h, &analysis );
2031 x264_mb_analyse_inter_p16x16( h, &analysis );
2033 if( h->mb.i_type == P_SKIP )
2036 if( flags & X264_ANALYSE_PSUB16x16 )
2038 if( h->param.analyse.b_mixed_references )
2039 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2041 x264_mb_analyse_inter_p8x8( h, &analysis );
2044 /* Select best inter mode */
2046 i_partition = D_16x16;
2047 i_cost = analysis.l0.me16x16.cost;
2049 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2050 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2053 i_partition = D_8x8;
2054 i_cost = analysis.l0.i_cost8x8;
2057 if( flags & X264_ANALYSE_PSUB8x8 )
2059 for( i = 0; i < 4; i++ )
2061 x264_mb_analyse_inter_p4x4( h, &analysis, i );
2062 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2064 int i_cost8x8 = analysis.l0.i_cost4x4[i];
2065 h->mb.i_sub_partition[i] = D_L0_4x4;
2067 x264_mb_analyse_inter_p8x4( h, &analysis, i );
2068 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2069 h->mb.i_sub_partition[i], D_L0_8x4 );
2071 x264_mb_analyse_inter_p4x8( h, &analysis, i );
2072 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2073 h->mb.i_sub_partition[i], D_L0_4x8 );
2075 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2077 x264_mb_cache_mv_p8x8( h, &analysis, i );
2079 analysis.l0.i_cost8x8 = i_cost;
2083 /* Now do 16x8/8x16 */
2084 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2085 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2086 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2088 x264_mb_analyse_inter_p16x8( h, &analysis );
2089 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2091 x264_mb_analyse_inter_p8x16( h, &analysis );
2092 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2095 h->mb.i_partition = i_partition;
2098 //FIXME mb_type costs?
2099 if( analysis.b_mbrd )
2103 else if( i_partition == D_16x16 )
2105 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2106 i_cost = analysis.l0.me16x16.cost;
2108 else if( i_partition == D_16x8 )
2110 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2111 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2112 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2114 else if( i_partition == D_8x16 )
2116 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2117 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2118 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2120 else if( i_partition == D_8x8 )
2124 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2126 switch( h->mb.i_sub_partition[i8x8] )
2129 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2130 i_cost += analysis.l0.me8x8[i8x8].cost;
2133 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2134 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2135 i_cost += analysis.l0.me8x4[i8x8][0].cost +
2136 analysis.l0.me8x4[i8x8][1].cost;
2139 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2140 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2141 i_cost += analysis.l0.me4x8[i8x8][0].cost +
2142 analysis.l0.me4x8[i8x8][1].cost;
2146 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2147 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2148 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2149 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2150 i_cost += analysis.l0.me4x4[i8x8][0].cost +
2151 analysis.l0.me4x4[i8x8][1].cost +
2152 analysis.l0.me4x4[i8x8][2].cost +
2153 analysis.l0.me4x4[i8x8][3].cost;
2156 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2162 if( h->mb.b_chroma_me )
2164 x264_mb_analyse_intra_chroma( h, &analysis );
2165 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2166 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2167 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2168 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2171 x264_mb_analyse_intra( h, &analysis, i_cost );
2173 i_satd_inter = i_cost;
2174 i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2175 analysis.i_satd_i8x8,
2176 analysis.i_satd_i4x4 );
2178 if( analysis.b_mbrd )
2180 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2182 i_partition = D_16x16;
2183 i_cost = analysis.l0.me16x16.cost;
2184 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2185 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2186 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2187 h->mb.i_type = i_type;
2188 h->mb.i_partition = i_partition;
2189 if( i_cost < COST_MAX )
2190 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2191 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2194 i_intra_type = I_16x16;
2195 i_intra_cost = analysis.i_satd_i16x16;
2196 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
2197 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
2198 COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
2200 if( i_intra_cost == COST_MAX )
2201 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
2203 h->mb.i_type = i_type;
2204 h->stat.frame.i_intra_cost += i_intra_cost;
2205 h->stat.frame.i_inter_cost += i_cost;
2207 if( h->mb.i_subpel_refine >= 7 )
2209 if( IS_INTRA( h->mb.i_type ) )
2211 x264_intra_rd_refine( h, &analysis );
2213 else if( i_partition == D_16x16 )
2215 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2216 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
2218 else if( i_partition == D_16x8 )
2220 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2221 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2222 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
2223 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
2225 else if( i_partition == D_8x16 )
2227 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2228 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2229 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
2230 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
2232 else if( i_partition == D_8x8 )
2235 x264_analyse_update_cache( h, &analysis );
2236 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2237 if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2238 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
2243 else if( h->sh.i_type == SLICE_TYPE_B )
2245 int i_bskip_cost = COST_MAX;
2248 h->mb.i_type = B_SKIP;
2249 if( h->mb.b_direct_auto_write )
2251 /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2252 for( i = 0; i < 2; i++ )
2255 h->sh.b_direct_spatial_mv_pred ^= 1;
2256 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2257 if( analysis.b_direct_available )
2262 b_skip = x264_macroblock_probe_bskip( h );
2264 h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2271 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2273 if( analysis.b_direct_available )
2275 if( !h->mb.b_direct_auto_write )
2277 if( h->mb.b_lossless )
2279 /* chance of skip is too small to bother */
2281 else if( analysis.b_mbrd )
2283 i_bskip_cost = ssd_mb( h );
2285 /* 6 = minimum cavlc cost of a non-skipped MB */
2286 if( i_bskip_cost <= 6 * analysis.i_lambda2 )
2288 h->mb.i_type = B_SKIP;
2289 x264_analyse_update_cache( h, &analysis );
2293 else if( !h->mb.b_direct_auto_write )
2295 /* Conditioning the probe on neighboring block types
2296 * doesn't seem to help speed or quality. */
2297 b_skip = x264_macroblock_probe_bskip( h );
2303 const unsigned int flags = h->param.analyse.inter;
2307 x264_mb_analyse_load_costs( h, &analysis );
2309 /* select best inter mode */
2310 /* direct must be first */
2311 if( analysis.b_direct_available )
2312 x264_mb_analyse_inter_direct( h, &analysis );
2314 x264_mb_analyse_inter_b16x16( h, &analysis );
2317 i_partition = D_16x16;
2318 i_cost = analysis.l0.me16x16.cost;
2319 COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2320 COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2321 COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2323 if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2325 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2326 if( i_bskip_cost < analysis.i_rd16x16direct &&
2327 i_bskip_cost < analysis.i_rd16x16bi &&
2328 i_bskip_cost < analysis.l0.i_rd16x16 &&
2329 i_bskip_cost < analysis.l1.i_rd16x16 )
2331 h->mb.i_type = B_SKIP;
2332 x264_analyse_update_cache( h, &analysis );
2337 if( flags & X264_ANALYSE_BSUB16x16 )
2339 x264_mb_analyse_inter_b8x8( h, &analysis );
2340 if( analysis.i_cost8x8bi < i_cost )
2343 i_partition = D_8x8;
2344 i_cost = analysis.i_cost8x8bi;
2346 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2347 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2349 x264_mb_analyse_inter_b16x8( h, &analysis );
2350 COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2351 i_type, analysis.i_mb_type16x8,
2352 i_partition, D_16x8 );
2354 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2355 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2357 x264_mb_analyse_inter_b8x16( h, &analysis );
2358 COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2359 i_type, analysis.i_mb_type8x16,
2360 i_partition, D_8x16 );
2365 if( analysis.b_mbrd )
2370 else if( i_partition == D_16x16 )
2372 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2373 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2374 if( i_type == B_L0_L0 )
2376 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2377 i_cost = analysis.l0.me16x16.cost
2378 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2380 else if( i_type == B_L1_L1 )
2382 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2383 i_cost = analysis.l1.me16x16.cost
2384 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2386 else if( i_type == B_BI_BI )
2388 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2389 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2392 else if( i_partition == D_16x8 )
2394 for( i=0; i<2; i++ )
2396 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2397 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2398 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2399 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2402 else if( i_partition == D_8x16 )
2404 for( i=0; i<2; i++ )
2406 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2407 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2408 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2409 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2412 else if( i_partition == D_8x8 )
2414 for( i=0; i<4; i++ )
2417 int i_part_cost_old;
2419 int i_part_type = h->mb.i_sub_partition[i];
2420 int b_bidir = (i_part_type == D_BI_8x8);
2422 if( i_part_type == D_DIRECT_8x8 )
2424 if( x264_mb_partition_listX_table[0][i_part_type] )
2426 m = &analysis.l0.me8x8[i];
2427 i_part_cost_old = m->cost;
2428 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2429 m->cost -= i_type_cost;
2430 x264_me_refine_qpel( h, m );
2432 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2434 if( x264_mb_partition_listX_table[1][i_part_type] )
2436 m = &analysis.l1.me8x8[i];
2437 i_part_cost_old = m->cost;
2438 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2439 m->cost -= i_type_cost;
2440 x264_me_refine_qpel( h, m );
2442 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2444 /* TODO: update mvp? */
2448 x264_mb_analyse_intra( h, &analysis, i_cost );
2450 if( analysis.b_mbrd )
2452 int i_satd_inter = i_cost;
2453 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2455 i_cost = i_bskip_cost;
2456 i_partition = D_16x16;
2457 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2458 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2459 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2460 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2461 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2462 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2463 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2465 h->mb.i_type = i_type;
2466 h->mb.i_partition = i_partition;
2467 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2468 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2471 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2472 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2473 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2475 h->mb.i_type = i_type;
2476 h->mb.i_partition = i_partition;
2478 if( h->param.analyse.b_bidir_me )
2479 refine_bidir( h, &analysis );
2483 x264_analyse_update_cache( h, &analysis );
2485 if( !analysis.b_mbrd )
2486 x264_mb_analyse_transform( h );
2488 h->mb.b_trellis = h->param.analyse.i_trellis;
2489 h->mb.b_noise_reduction = h->param.analyse.i_noise_reduction;
2492 /*-------------------- Update MB from the analysis ----------------------*/
2493 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2497 switch( h->mb.i_type )
2500 for( i = 0; i < 16; i++ )
2501 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2503 x264_mb_analyse_intra_chroma( h, a );
2506 for( i = 0; i < 4; i++ )
2507 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2509 x264_mb_analyse_intra_chroma( h, a );
2512 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2513 x264_mb_analyse_intra_chroma( h, a );
2517 switch( h->mb.i_partition )
2520 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2521 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2525 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2526 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2527 x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv[0], a->l0.me16x8[0].mv[1] );
2528 x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv[0], a->l0.me16x8[1].mv[1] );
2532 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2533 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2534 x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv[0], a->l0.me8x16[0].mv[1] );
2535 x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv[0], a->l0.me8x16[1].mv[1] );
2539 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2545 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2546 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2547 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2548 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2549 for( i = 0; i < 4; i++ )
2550 x264_mb_cache_mv_p8x8( h, a, i );
2555 h->mb.i_partition = D_16x16;
2556 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2557 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv[0],
2558 h->mb.cache.pskip_mv[1] );
2564 x264_mb_load_mv_direct8x8( h, 0 );
2565 x264_mb_load_mv_direct8x8( h, 1 );
2566 x264_mb_load_mv_direct8x8( h, 2 );
2567 x264_mb_load_mv_direct8x8( h, 3 );
2571 /* optimize: cache might not need to be rewritten */
2572 for( i = 0; i < 4; i++ )
2573 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2576 default: /* the rest of the B types */
2577 switch( h->mb.i_partition )
2580 switch( h->mb.i_type )
2583 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2584 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2586 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2587 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0, 0 );
2588 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0, 0 );
2591 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2592 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0, 0 );
2593 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0, 0 );
2595 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2596 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2599 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2600 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2602 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2603 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2608 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2609 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2612 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2613 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2616 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2622 #include "slicetype_decision.c"