1 /*****************************************************************************
2 * analyse.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003 x264 project
5 * $Id: analyse.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Loren Merritt <lorenm@u.washington.edu>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
31 #include "common/common.h"
32 #include "macroblock.h"
34 #include "ratecontrol.h"
47 int mvc[32][5][2]; /* [ref][0] is 16x16 mv,
48 [ref][1..4] are 8x8 mv from partition [0..3] */
52 int i_cost4x4[4]; /* cost per 8x8 partition */
53 x264_me_t me4x4[4][4];
56 int i_cost8x4[4]; /* cost per 8x8 partition */
57 x264_me_t me8x4[4][2];
60 int i_cost4x8[4]; /* cost per 8x8 partition */
61 x264_me_t me4x8[4][2];
71 } x264_mb_analysis_list_t;
75 /* conduct the analysis using this lamda and QP */
84 /* Take some shortcuts in intra search if intra is deemed unlikely */
90 int i_satd_i16x16_dir[7];
94 int i_satd_i8x8_dir[12][4];
101 int i_satd_i8x8chroma;
102 int i_satd_i8x8chroma_dir[4];
103 int i_predict8x8chroma;
105 /* II: Inter part P/B frame */
106 x264_mb_analysis_list_t l0;
107 x264_mb_analysis_list_t l1;
109 int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
110 int i_cost16x16direct;
112 int i_cost8x8direct[4];
121 int i_mb_partition16x8[2]; /* mb_partition_e */
122 int i_mb_partition8x16[2];
123 int i_mb_type16x8; /* mb_class_e */
126 int b_direct_available;
128 } x264_mb_analysis_t;
130 /* lambda = pow(2,qp/6-2) */
131 static const int i_qp0_cost_table[52] = {
132 1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
133 1, 1, 1, 1, /* 8-11 */
134 1, 1, 1, 1, 2, 2, 2, 2, /* 12-19 */
135 3, 3, 3, 4, 4, 4, 5, 6, /* 20-27 */
136 6, 7, 8, 9,10,11,13,14, /* 28-35 */
137 16,18,20,23,25,29,32,36, /* 36-43 */
138 40,45,51,57,64,72,81,91 /* 44-51 */
141 /* pow(lambda,2) * .9 */
142 static const int i_qp0_cost2_table[52] = {
143 1, 1, 1, 1, 1, 1, /* 0-5 */
144 1, 1, 1, 1, 1, 1, /* 6-11 */
145 1, 1, 1, 2, 2, 3, /* 12-17 */
146 4, 5, 6, 7, 9, 11, /* 18-23 */
147 14, 18, 23, 29, 36, 46, /* 24-29 */
148 58, 73, 91, 115, 145, 183, /* 30-35 */
149 230, 290, 366, 461, 581, 731, /* 36-41 */
150 922,1161,1463,1843,2322,2926, /* 42-47 */
154 /* TODO: calculate CABAC costs */
155 static const int i_mb_b_cost_table[X264_MBTYPE_MAX] = {
156 9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
158 static const int i_mb_b16x8_cost_table[17] = {
159 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
161 static const int i_sub_mb_b_cost_table[13] = {
162 7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
164 static const int i_sub_mb_p_cost_table[4] = {
168 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
170 /* initialize an array of lambda*nbits for all possible mvs */
171 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
173 static int16_t *p_cost_mv[52];
175 if( !p_cost_mv[a->i_qp] )
177 /* could be faster, but isn't called many times */
178 /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
180 p_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) );
181 p_cost_mv[a->i_qp] += 2*4*2048;
182 for( i = 0; i <= 2*4*2048; i++ )
184 p_cost_mv[a->i_qp][-i] =
185 p_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i );
189 a->p_cost_mv = p_cost_mv[a->i_qp];
192 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
194 memset( a, 0, sizeof( x264_mb_analysis_t ) );
196 /* conduct the analysis using this lamda and QP */
197 a->i_qp = h->mb.i_qp = i_qp;
198 h->mb.i_chroma_qp = i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )];
199 a->i_lambda = i_qp0_cost_table[i_qp];
200 a->i_lambda2 = i_qp0_cost2_table[i_qp];
201 a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
202 ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
204 h->mb.i_me_method = h->param.analyse.i_me_method;
205 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
206 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
207 && h->mb.i_subpel_refine >= 5;
208 h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
209 h->mb.b_transform_8x8 = 0;
210 h->mb.b_noise_reduction = 0;
216 a->i_satd_i8x8chroma = COST_MAX;
220 /* II: Inter part P/B frame */
221 if( h->sh.i_type != SLICE_TYPE_I )
224 int i_fmv_range = 4 * h->param.analyse.i_mv_range;
225 // limit motion search to a slightly smaller range than the theoretical limit,
226 // since the search may go a few iterations past its given range
227 int i_fpel_border = 5; // umh unconditional radius
228 int i_spel_border = 8; // 1.5 for subpel_satd, 1.5 for subpel_rd, 2 for bime, round up
230 /* Calculate max allowed MV range */
231 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range-1 )
232 h->mb.mv_min[0] = 4*( -16*h->mb.i_mb_x - 24 );
233 h->mb.mv_max[0] = 4*( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 24 );
234 h->mb.mv_min_spel[0] = CLIP_FMV( h->mb.mv_min[0] );
235 h->mb.mv_max_spel[0] = CLIP_FMV( h->mb.mv_max[0] );
236 h->mb.mv_min_fpel[0] = (h->mb.mv_min_spel[0]>>2) + i_fpel_border;
237 h->mb.mv_max_fpel[0] = (h->mb.mv_max_spel[0]>>2) - i_fpel_border;
238 if( h->mb.i_mb_x == 0)
240 int mb_y = h->mb.i_mb_y >> h->sh.b_mbaff;
241 int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff;
242 int thread_mvy_range = i_fmv_range;
244 if( h->param.i_threads > 1 )
246 int pix_y = (h->mb.i_mb_y | h->mb.b_interlaced) * 16;
247 int thresh = pix_y + h->param.analyse.i_mv_range_thread;
248 for( i = (h->sh.i_type == SLICE_TYPE_B); i >= 0; i-- )
250 x264_frame_t **fref = i ? h->fref1 : h->fref0;
251 int i_ref = i ? h->i_ref1 : h->i_ref0;
252 for( j=0; j<i_ref; j++ )
254 x264_frame_cond_wait( fref[j], thresh );
255 thread_mvy_range = X264_MIN( thread_mvy_range, fref[j]->i_lines_completed - pix_y );
258 if( h->param.b_deterministic )
259 thread_mvy_range = h->param.analyse.i_mv_range_thread;
260 if( h->mb.b_interlaced )
261 thread_mvy_range >>= 1;
264 h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
265 h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
266 h->mb.mv_min_spel[1] = x264_clip3( h->mb.mv_min[1], X264_MAX(4*(-512+i_spel_border), -i_fmv_range), i_fmv_range );
267 h->mb.mv_max_spel[1] = CLIP_FMV( h->mb.mv_max[1] );
268 h->mb.mv_max_spel[1] = X264_MIN( h->mb.mv_max_spel[1], thread_mvy_range*4 );
269 h->mb.mv_min_fpel[1] = (h->mb.mv_min_spel[1]>>2) + i_fpel_border;
270 h->mb.mv_max_fpel[1] = (h->mb.mv_max_spel[1]>>2) - i_fpel_border;
276 a->l0.i_cost8x8 = COST_MAX;
278 for( i = 0; i < 4; i++ )
282 a->l0.i_cost4x8[i] = COST_MAX;
286 a->l0.i_cost8x16 = COST_MAX;
287 if( h->sh.i_type == SLICE_TYPE_B )
291 a->l1.i_cost8x8 = COST_MAX;
293 for( i = 0; i < 4; i++ )
298 a->i_cost8x8direct[i] = COST_MAX;
309 a->i_cost16x16direct =
312 a->i_cost8x16bi = COST_MAX;
315 /* Fast intra decision */
316 if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
318 if( IS_INTRA( h->mb.i_mb_type_left )
319 || IS_INTRA( h->mb.i_mb_type_top )
320 || IS_INTRA( h->mb.i_mb_type_topleft )
321 || IS_INTRA( h->mb.i_mb_type_topright )
322 || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
323 || (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])) )
324 { /* intra is likely */ }
339 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
341 if( i_neighbour & MB_TOPLEFT )
343 /* top and left available */
344 *mode++ = I_PRED_16x16_V;
345 *mode++ = I_PRED_16x16_H;
346 *mode++ = I_PRED_16x16_DC;
347 *mode++ = I_PRED_16x16_P;
350 else if( i_neighbour & MB_LEFT )
353 *mode++ = I_PRED_16x16_DC_LEFT;
354 *mode++ = I_PRED_16x16_H;
357 else if( i_neighbour & MB_TOP )
360 *mode++ = I_PRED_16x16_DC_TOP;
361 *mode++ = I_PRED_16x16_V;
367 *mode = I_PRED_16x16_DC_128;
373 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
375 if( i_neighbour & MB_TOPLEFT )
377 /* top and left available */
378 *mode++ = I_PRED_CHROMA_V;
379 *mode++ = I_PRED_CHROMA_H;
380 *mode++ = I_PRED_CHROMA_DC;
381 *mode++ = I_PRED_CHROMA_P;
384 else if( i_neighbour & MB_LEFT )
387 *mode++ = I_PRED_CHROMA_DC_LEFT;
388 *mode++ = I_PRED_CHROMA_H;
391 else if( i_neighbour & MB_TOP )
394 *mode++ = I_PRED_CHROMA_DC_TOP;
395 *mode++ = I_PRED_CHROMA_V;
401 *mode = I_PRED_CHROMA_DC_128;
407 static void predict_4x4_mode_available( unsigned int i_neighbour,
408 int *mode, int *pi_count )
410 int b_l = i_neighbour & MB_LEFT;
411 int b_t = i_neighbour & MB_TOP;
416 *mode++ = I_PRED_4x4_DC;
417 *mode++ = I_PRED_4x4_H;
418 *mode++ = I_PRED_4x4_V;
419 *mode++ = I_PRED_4x4_DDL;
420 if( i_neighbour & MB_TOPLEFT )
422 *mode++ = I_PRED_4x4_DDR;
423 *mode++ = I_PRED_4x4_VR;
424 *mode++ = I_PRED_4x4_HD;
427 *mode++ = I_PRED_4x4_VL;
428 *mode++ = I_PRED_4x4_HU;
432 *mode++ = I_PRED_4x4_DC_LEFT;
433 *mode++ = I_PRED_4x4_H;
434 *mode++ = I_PRED_4x4_HU;
439 *mode++ = I_PRED_4x4_DC_TOP;
440 *mode++ = I_PRED_4x4_V;
441 *mode++ = I_PRED_4x4_DDL;
442 *mode++ = I_PRED_4x4_VL;
447 *mode++ = I_PRED_4x4_DC_128;
452 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
459 uint8_t *p_dstc[2], *p_srcc[2];
461 if( a->i_satd_i8x8chroma < COST_MAX )
464 /* 8x8 prediction selection for chroma */
465 p_dstc[0] = h->mb.pic.p_fdec[1];
466 p_dstc[1] = h->mb.pic.p_fdec[2];
467 p_srcc[0] = h->mb.pic.p_fenc[1];
468 p_srcc[1] = h->mb.pic.p_fenc[2];
470 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
471 a->i_satd_i8x8chroma = COST_MAX;
472 if( i_max == 4 && h->pixf.intra_satd_x3_8x8c && h->pixf.mbcmp[0] == h->pixf.satd[0] )
474 int satdu[4], satdv[4];
475 h->pixf.intra_satd_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
476 h->pixf.intra_satd_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
477 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
478 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
479 satdu[I_PRED_CHROMA_P] =
480 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
481 satdv[I_PRED_CHROMA_P] =
482 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
484 for( i=0; i<i_max; i++ )
486 int i_mode = predict_mode[i];
487 int i_satd = satdu[i_mode] + satdv[i_mode]
488 + a->i_lambda * bs_size_ue(i_mode);
490 a->i_satd_i8x8chroma_dir[i] = i_satd;
491 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
496 for( i=0; i<i_max; i++ )
499 int i_mode = predict_mode[i];
501 /* we do the prediction */
502 h->predict_8x8c[i_mode]( p_dstc[0] );
503 h->predict_8x8c[i_mode]( p_dstc[1] );
505 /* we calculate the cost */
506 i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
507 p_srcc[0], FENC_STRIDE ) +
508 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
509 p_srcc[1], FENC_STRIDE ) +
510 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
512 a->i_satd_i8x8chroma_dir[i] = i_satd;
513 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
517 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
520 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
522 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
523 uint8_t *p_src = h->mb.pic.p_fenc[0];
524 uint8_t *p_dst = h->mb.pic.p_fdec[0];
529 int b_merged_satd = h->pixf.intra_satd_x3_16x16 && h->pixf.mbcmp[0] == h->pixf.satd[0];
531 /*---------------- Try all mode and calculate their score ---------------*/
533 /* 16x16 prediction selection */
534 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
536 if( b_merged_satd && i_max == 4 )
538 h->pixf.intra_satd_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
539 h->predict_16x16[I_PRED_16x16_P]( p_dst );
540 a->i_satd_i16x16_dir[I_PRED_16x16_P] =
541 h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
544 int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
545 COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
550 for( i = 0; i < i_max; i++ )
553 int i_mode = predict_mode[i];
554 h->predict_16x16[i_mode]( p_dst );
556 i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
557 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
558 COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
559 a->i_satd_i16x16_dir[i_mode] = i_satd;
563 if( h->sh.i_type == SLICE_TYPE_B )
564 /* cavlc mb type prefix */
565 a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
566 if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
569 /* 8x8 prediction selection */
570 if( flags & X264_ANALYSE_I8x8 )
572 DECLARE_ALIGNED( uint8_t, edge[33], 8 );
573 x264_pixel_cmp_t sa8d = (*h->pixf.mbcmp == *h->pixf.sad) ? h->pixf.sad[PIXEL_8x8] : h->pixf.sa8d[PIXEL_8x8];
574 int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
576 b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
578 // FIXME some bias like in i4x4?
579 if( h->sh.i_type == SLICE_TYPE_B )
580 i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
582 for( idx = 0;; idx++ )
586 uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
587 uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
588 int i_best = COST_MAX;
589 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
591 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
592 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
594 if( b_merged_satd && i_max == 9 )
597 h->pixf.intra_sa8d_x3_8x8( p_src_by, edge, satd );
598 if( i_pred_mode < 3 )
599 satd[i_pred_mode] -= 3 * a->i_lambda;
600 for( i=2; i>=0; i-- )
602 int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
603 COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
610 for( ; i<i_max; i++ )
613 int i_mode = predict_mode[i];
615 h->predict_8x8[i_mode]( p_dst_by, edge );
617 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
618 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
620 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
621 a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
625 if( idx == 3 || i_cost > i_satd_thresh )
628 /* we need to encode this block now (for next ones) */
629 h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
630 x264_mb_encode_i8x8( h, idx, a->i_qp );
632 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
636 a->i_satd_i8x8 = i_cost;
639 a->i_satd_i8x8 = COST_MAX;
640 i_cost = i_cost * 4/(idx+1);
642 if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 )
646 /* 4x4 prediction selection */
647 if( flags & X264_ANALYSE_I4x4 )
650 int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
651 b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
653 i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
655 i_cost = a->i_lambda * 24; /* from JVT (SATD0) */
656 if( h->sh.i_type == SLICE_TYPE_B )
657 i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];
659 for( idx = 0;; idx++ )
661 int x = block_idx_x[idx];
662 int y = block_idx_y[idx];
663 uint8_t *p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
664 uint8_t *p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
665 int i_best = COST_MAX;
666 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
668 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
670 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
671 /* emulate missing topright samples */
672 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
674 if( b_merged_satd && i_max >= 6 )
677 h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
678 if( i_pred_mode < 3 )
679 satd[i_pred_mode] -= 3 * a->i_lambda;
680 for( i=2; i>=0; i-- )
681 COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
682 a->i_predict4x4[idx], i );
688 for( ; i<i_max; i++ )
691 int i_mode = predict_mode[i];
693 h->predict_4x4[i_mode]( p_dst_by );
695 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
696 p_src_by, FENC_STRIDE )
697 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
699 COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
703 if( i_cost > i_satd_thresh || idx == 15 )
706 /* we need to encode this block now (for next ones) */
707 h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
708 x264_mb_encode_i4x4( h, idx, a->i_qp );
710 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
713 a->i_satd_i4x4 = i_cost;
715 a->i_satd_i4x4 = COST_MAX;
719 static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
721 if( a->i_satd_i16x16 <= i_satd_thresh )
723 h->mb.i_type = I_16x16;
724 x264_analyse_update_cache( h, a );
725 a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
728 a->i_satd_i16x16 = COST_MAX;
730 if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
732 h->mb.i_type = I_4x4;
733 x264_analyse_update_cache( h, a );
734 a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
737 a->i_satd_i4x4 = COST_MAX;
739 if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
741 h->mb.i_type = I_8x8;
742 x264_analyse_update_cache( h, a );
743 a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
746 a->i_satd_i8x8 = COST_MAX;
749 static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
751 uint8_t *p_src = h->mb.pic.p_fenc[0];
752 uint8_t *p_dst = h->mb.pic.p_fdec[0];
755 int i_max, i_satd, i_best, i_mode, i_thresh;
759 if( h->mb.i_type == I_16x16 )
761 int old_pred_mode = a->i_predict16x16;
762 i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
763 i_best = a->i_satd_i16x16;
764 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
765 for( i = 0; i < i_max; i++ )
767 int i_mode = predict_mode[i];
768 if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
770 h->mb.i_intra16x16_pred_mode = i_mode;
771 i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
772 COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
775 else if( h->mb.i_type == I_4x4 )
777 uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
779 for( idx = 0; idx < 16; idx++ )
785 i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
786 x = block_idx_x[idx];
787 y = block_idx_y[idx];
789 p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
790 p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
791 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
793 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
794 /* emulate missing topright samples */
795 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
797 for( i = 0; i < i_max; i++ )
799 i_mode = predict_mode[i];
800 h->predict_4x4[i_mode]( p_dst_by );
801 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
803 if( i_best > i_satd )
805 a->i_predict4x4[idx] = i_mode;
807 pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
808 pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
809 pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
810 pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
811 i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
815 *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
816 *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
817 *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
818 *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
819 h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
821 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
824 else if( h->mb.i_type == I_8x8 )
826 DECLARE_ALIGNED( uint8_t, edge[33], 8 );
827 for( idx = 0; idx < 4; idx++ )
835 i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
838 i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
842 p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
843 p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
844 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
845 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
847 for( i = 0; i < i_max; i++ )
849 i_mode = predict_mode[i];
850 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
852 h->predict_8x8[i_mode]( p_dst_by, edge );
853 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
855 if( i_best > i_satd )
857 a->i_predict8x8[idx] = i_mode;
860 pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
863 pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
865 i_nnz[j] = h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]];
869 *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
872 p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
874 h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]] = i_nnz[j];
876 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
880 /* RD selection for chroma prediction */
881 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
884 i_thresh = a->i_satd_i8x8chroma * 5/4;
886 for( i = j = 0; i < i_max; i++ )
887 if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
888 predict_mode[i] != a->i_predict8x8chroma )
890 predict_mode[j++] = predict_mode[i];
896 int i_chroma_lambda = i_qp0_cost2_table[h->mb.i_chroma_qp];
897 /* the previous thing encoded was x264_intra_rd(), so the pixels and
898 * coefs for the current chroma mode are still around, so we only
899 * have to recount the bits. */
900 i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
901 for( i = 0; i < i_max; i++ )
903 i_mode = predict_mode[i];
904 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
905 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
906 /* if we've already found a mode that needs no residual, then
907 * probably any mode with a residual will be worse.
908 * so avoid dct on the remaining modes to improve speed. */
909 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
910 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode );
912 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
917 #define LOAD_FENC( m, src, xoff, yoff) \
918 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
919 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
920 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
921 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
922 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
924 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
925 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
926 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
927 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
928 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
929 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
930 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
931 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
933 #define REF_COST(list, ref) \
934 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
936 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
940 int mvc[7][2], i_mvc;
941 int i_halfpel_thresh = INT_MAX;
942 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
944 /* 16x16 Search on all ref frame */
945 m.i_pixel = PIXEL_16x16;
946 m.p_cost_mv = a->p_cost_mv;
947 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
949 a->l0.me16x16.cost = INT_MAX;
950 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
952 const int i_ref_cost = REF_COST( 0, i_ref );
953 i_halfpel_thresh -= i_ref_cost;
954 m.i_ref_cost = i_ref_cost;
957 /* search with ref */
958 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
959 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
960 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
961 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
964 * SSD threshold would probably be better than SATD */
967 && m.cost-m.cost_mv < 300*a->i_lambda
968 && abs(m.mv[0]-h->mb.cache.pskip_mv[0])
969 + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
970 && x264_macroblock_probe_pskip( h ) )
972 h->mb.i_type = P_SKIP;
973 x264_analyse_update_cache( h, a );
974 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
978 m.cost += i_ref_cost;
979 i_halfpel_thresh += i_ref_cost;
981 if( m.cost < a->l0.me16x16.cost )
984 /* save mv for predicting neighbors */
985 a->l0.mvc[i_ref][0][0] =
986 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
987 a->l0.mvc[i_ref][0][1] =
988 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
991 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
992 assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
995 if( a->b_mbrd && a->l0.i_ref == 0
996 && a->l0.me16x16.mv[0] == h->mb.cache.pskip_mv[0]
997 && a->l0.me16x16.mv[1] == h->mb.cache.pskip_mv[1] )
999 h->mb.i_partition = D_16x16;
1000 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
1001 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1005 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
1009 uint8_t **p_fenc = h->mb.pic.p_fenc;
1010 int i_halfpel_thresh = INT_MAX;
1011 int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
1013 int i_maxref = h->mb.pic.i_fref[0]-1;
1015 h->mb.i_partition = D_8x8;
1017 /* early termination: if 16x16 chose ref 0, then evalute no refs older
1018 * than those used by the neighbors */
1019 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
1020 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
1023 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
1024 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
1025 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
1026 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
1027 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
1028 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
1031 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1033 a->l0.mvc[i_ref][0][0] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0];
1034 a->l0.mvc[i_ref][0][1] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1];
1037 for( i = 0; i < 4; i++ )
1039 x264_me_t *l0m = &a->l0.me8x8[i];
1043 m.i_pixel = PIXEL_8x8;
1044 m.p_cost_mv = a->p_cost_mv;
1046 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
1047 l0m->cost = INT_MAX;
1048 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1050 const int i_ref_cost = REF_COST( 0, i_ref );
1051 i_halfpel_thresh -= i_ref_cost;
1052 m.i_ref_cost = i_ref_cost;
1055 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
1056 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
1057 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1058 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
1060 m.cost += i_ref_cost;
1061 i_halfpel_thresh += i_ref_cost;
1062 *(uint64_t*)a->l0.mvc[i_ref][i+1] = *(uint64_t*)m.mv;
1064 if( m.cost < l0m->cost )
1067 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv[0], l0m->mv[1] );
1068 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1071 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1074 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1075 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1076 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1077 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1080 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1082 const int i_ref = a->l0.me16x16.i_ref;
1083 const int i_ref_cost = REF_COST( 0, i_ref );
1084 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
1085 uint8_t **p_fenc = h->mb.pic.p_fenc;
1087 int (*mvc)[2] = a->l0.mvc[i_ref];
1090 /* XXX Needed for x264_mb_predict_mv */
1091 h->mb.i_partition = D_8x8;
1094 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.me16x16.mv;
1096 for( i = 0; i < 4; i++ )
1098 x264_me_t *m = &a->l0.me8x8[i];
1102 m->i_pixel = PIXEL_8x8;
1103 m->p_cost_mv = a->p_cost_mv;
1104 m->i_ref_cost = i_ref_cost;
1107 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1108 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1109 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1110 x264_me_search( h, m, mvc, i_mvc );
1112 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
1114 *(uint64_t*)mvc[i_mvc] = *(uint64_t*)m->mv;
1118 m->cost += i_ref_cost;
1119 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1122 /* theoretically this should include 4*ref_cost,
1123 * but 3 seems a better approximation of cabac. */
1124 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1125 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
1126 REF_COST( 0, a->l0.me16x16.i_ref );
1127 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1128 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1131 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
1134 uint8_t **p_fenc = h->mb.pic.p_fenc;
1138 /* XXX Needed for x264_mb_predict_mv */
1139 h->mb.i_partition = D_16x8;
1141 for( i = 0; i < 2; i++ )
1143 x264_me_t *l0m = &a->l0.me16x8[i];
1144 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1145 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1147 m.i_pixel = PIXEL_16x8;
1148 m.p_cost_mv = a->p_cost_mv;
1150 LOAD_FENC( &m, p_fenc, 0, 8*i );
1151 l0m->cost = INT_MAX;
1152 for( j = 0; j < i_ref8s; j++ )
1154 const int i_ref = ref8[j];
1155 const int i_ref_cost = REF_COST( 0, i_ref );
1156 m.i_ref_cost = i_ref_cost;
1159 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1160 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1161 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][2*i+1];
1162 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][2*i+2];
1164 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1165 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1166 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1167 x264_me_search( h, &m, mvc, 3 );
1169 m.cost += i_ref_cost;
1171 if( m.cost < l0m->cost )
1174 x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, l0m->mv[0], l0m->mv[1] );
1175 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1178 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1181 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1184 uint8_t **p_fenc = h->mb.pic.p_fenc;
1188 /* XXX Needed for x264_mb_predict_mv */
1189 h->mb.i_partition = D_8x16;
1191 for( i = 0; i < 2; i++ )
1193 x264_me_t *l0m = &a->l0.me8x16[i];
1194 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1195 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1197 m.i_pixel = PIXEL_8x16;
1198 m.p_cost_mv = a->p_cost_mv;
1200 LOAD_FENC( &m, p_fenc, 8*i, 0 );
1201 l0m->cost = INT_MAX;
1202 for( j = 0; j < i_ref8s; j++ )
1204 const int i_ref = ref8[j];
1205 const int i_ref_cost = REF_COST( 0, i_ref );
1206 m.i_ref_cost = i_ref_cost;
1209 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1210 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][i+1];
1211 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][i+3];
1213 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1214 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1215 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1216 x264_me_search( h, &m, mvc, 3 );
1218 m.cost += i_ref_cost;
1220 if( m.cost < l0m->cost )
1223 x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, l0m->mv[0], l0m->mv[1] );
1224 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1227 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1230 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1232 DECLARE_ALIGNED( uint8_t, pix1[16*8], 8 );
1233 uint8_t *pix2 = pix1+8;
1234 const int i_stride = h->mb.pic.i_stride[1];
1235 const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1236 const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1238 #define CHROMA4x4MC( width, height, me, x, y ) \
1239 h->mc.mc_chroma( &pix1[x+y*16], 16, &p_fref[4][or+x+y*i_stride], i_stride, (me).mv[0], (me).mv[1], width, height ); \
1240 h->mc.mc_chroma( &pix2[x+y*16], 16, &p_fref[5][or+x+y*i_stride], i_stride, (me).mv[0], (me).mv[1], width, height );
1242 if( pixel == PIXEL_4x4 )
1244 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1245 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 0,2 );
1246 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 2,0 );
1247 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1249 else if( pixel == PIXEL_8x4 )
1251 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1252 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1256 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1257 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1260 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1261 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1264 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1266 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1267 uint8_t **p_fenc = h->mb.pic.p_fenc;
1268 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1271 /* XXX Needed for x264_mb_predict_mv */
1272 h->mb.i_partition = D_8x8;
1274 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1276 const int idx = 4*i8x8 + i4x4;
1277 const int x4 = block_idx_x[idx];
1278 const int y4 = block_idx_y[idx];
1279 const int i_mvc = (i4x4 == 0);
1281 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1283 m->i_pixel = PIXEL_4x4;
1284 m->p_cost_mv = a->p_cost_mv;
1286 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1287 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1289 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1290 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1292 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
1294 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1295 a->l0.me4x4[i8x8][1].cost +
1296 a->l0.me4x4[i8x8][2].cost +
1297 a->l0.me4x4[i8x8][3].cost +
1298 REF_COST( 0, i_ref ) +
1299 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1300 if( h->mb.b_chroma_me )
1301 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1304 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1306 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1307 uint8_t **p_fenc = h->mb.pic.p_fenc;
1308 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1311 /* XXX Needed for x264_mb_predict_mv */
1312 h->mb.i_partition = D_8x8;
1314 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1316 const int idx = 4*i8x8 + 2*i8x4;
1317 const int x4 = block_idx_x[idx];
1318 const int y4 = block_idx_y[idx];
1319 const int i_mvc = (i8x4 == 0);
1321 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1323 m->i_pixel = PIXEL_8x4;
1324 m->p_cost_mv = a->p_cost_mv;
1326 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1327 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1329 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1330 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1332 x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
1334 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1335 REF_COST( 0, i_ref ) +
1336 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1337 if( h->mb.b_chroma_me )
1338 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1341 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1343 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1344 uint8_t **p_fenc = h->mb.pic.p_fenc;
1345 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1348 /* XXX Needed for x264_mb_predict_mv */
1349 h->mb.i_partition = D_8x8;
1351 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1353 const int idx = 4*i8x8 + i4x8;
1354 const int x4 = block_idx_x[idx];
1355 const int y4 = block_idx_y[idx];
1356 const int i_mvc = (i4x8 == 0);
1358 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1360 m->i_pixel = PIXEL_4x8;
1361 m->p_cost_mv = a->p_cost_mv;
1363 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1364 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1366 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1367 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1369 x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
1371 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1372 REF_COST( 0, i_ref ) +
1373 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1374 if( h->mb.b_chroma_me )
1375 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1378 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1380 /* Assumes that fdec still contains the results of
1381 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1383 uint8_t **p_fenc = h->mb.pic.p_fenc;
1384 uint8_t **p_fdec = h->mb.pic.p_fdec;
1387 a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1388 for( i = 0; i < 4; i++ )
1390 const int x = (i&1)*8;
1391 const int y = (i>>1)*8;
1392 a->i_cost16x16direct +=
1393 a->i_cost8x8direct[i] =
1394 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1397 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1401 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1403 if( h->param.analyse.b_weighted_bipred ) \
1404 h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1405 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1407 h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1410 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1412 uint8_t pix1[16*16], pix2[16*16];
1419 int mvc[8][2], i_mvc;
1420 int i_halfpel_thresh = INT_MAX;
1421 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1423 /* 16x16 Search on all ref frame */
1424 m.i_pixel = PIXEL_16x16;
1425 m.p_cost_mv = a->p_cost_mv;
1426 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1429 a->l0.me16x16.cost = INT_MAX;
1430 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1432 /* search with ref */
1433 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1434 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1435 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1436 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1439 m.cost += REF_COST( 0, i_ref );
1441 if( m.cost < a->l0.me16x16.cost )
1443 a->l0.i_ref = i_ref;
1447 /* save mv for predicting neighbors */
1448 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1449 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1451 /* subtract ref cost, so we don't have to add it for the other MB types */
1452 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1455 i_halfpel_thresh = INT_MAX;
1456 p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1457 a->l1.me16x16.cost = INT_MAX;
1458 for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1460 /* search with ref */
1461 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1462 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1463 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1464 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1467 m.cost += REF_COST( 1, i_ref );
1469 if( m.cost < a->l1.me16x16.cost )
1471 a->l1.i_ref = i_ref;
1475 /* save mv for predicting neighbors */
1476 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1477 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1479 /* subtract ref cost, so we don't have to add it for the other MB types */
1480 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1482 /* Set global ref, needed for other modes? */
1483 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1484 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1486 /* get cost of BI mode */
1487 weight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1488 if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
1490 /* l0 reference is halfpel, so get_ref on it will make it faster */
1492 h->mc.get_ref( pix2, &stride2,
1493 h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1494 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1496 h->mc.mc_luma( pix1, 16,
1497 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1498 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1500 weight = 64 - weight;
1504 /* if l0 was qpel, we'll use get_ref on l1 instead */
1505 h->mc.mc_luma( pix1, 16,
1506 h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1507 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1510 h->mc.get_ref( pix2, &stride2,
1511 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1512 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1516 if( h->param.analyse.b_weighted_bipred )
1517 h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2, weight );
1519 h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1521 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix1, 16 )
1522 + REF_COST( 0, a->l0.i_ref )
1523 + REF_COST( 1, a->l1.i_ref )
1524 + a->l0.me16x16.cost_mv
1525 + a->l1.me16x16.cost_mv;
1528 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1529 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1530 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1533 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1535 const int x = 2*(i%2);
1536 const int y = 2*(i/2);
1538 switch( h->mb.i_sub_partition[i] )
1541 x264_macroblock_cache_mv( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv[0], a->l0.me8x8[i].mv[1] );
1544 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] );
1545 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] );
1548 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] );
1549 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] );
1552 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] );
1553 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] );
1554 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] );
1555 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] );
1558 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1563 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1564 if( x264_mb_partition_listX_table[0][part] ) \
1566 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1567 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
1571 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1572 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0, 0 ); \
1574 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
1576 if( x264_mb_partition_listX_table[1][part] ) \
1578 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1579 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
1583 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1584 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0, 0 ); \
1586 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
1589 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1593 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1595 x264_mb_load_mv_direct8x8( h, i );
1598 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0, 0 );
1599 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0, 0 );
1600 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1605 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1608 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1610 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1612 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1614 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1618 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1620 uint8_t **p_fref[2] =
1621 { h->mb.pic.p_fref[0][a->l0.i_ref],
1622 h->mb.pic.p_fref[1][a->l1.i_ref] };
1623 uint8_t pix[2][8*8];
1626 /* XXX Needed for x264_mb_predict_mv */
1627 h->mb.i_partition = D_8x8;
1631 for( i = 0; i < 4; i++ )
1636 int i_part_cost_bi = 0;
1638 for( l = 0; l < 2; l++ )
1640 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1641 x264_me_t *m = &lX->me8x8[i];
1643 m->i_pixel = PIXEL_8x8;
1644 m->p_cost_mv = a->p_cost_mv;
1646 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1647 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1649 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1650 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1652 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
1655 h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
1656 m->mv[0], m->mv[1], 8, 8 );
1657 i_part_cost_bi += m->cost_mv;
1658 /* FIXME: ref cost */
1661 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1662 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1663 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1664 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1665 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1667 i_part_cost = a->l0.me8x8[i].cost;
1668 h->mb.i_sub_partition[i] = D_L0_8x8;
1669 COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1670 COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1671 COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1672 a->i_cost8x8bi += i_part_cost;
1674 /* XXX Needed for x264_mb_predict_mv */
1675 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1679 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1682 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1684 uint8_t **p_fref[2] =
1685 { h->mb.pic.p_fref[0][a->l0.i_ref],
1686 h->mb.pic.p_fref[1][a->l1.i_ref] };
1687 DECLARE_ALIGNED( uint8_t, pix[2][16*8], 16 );
1691 h->mb.i_partition = D_16x8;
1692 a->i_cost16x8bi = 0;
1694 for( i = 0; i < 2; i++ )
1697 int i_part_cost_bi = 0;
1699 /* TODO: check only the list(s) that were used in b8x8? */
1700 for( l = 0; l < 2; l++ )
1702 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1703 x264_me_t *m = &lX->me16x8[i];
1705 m->i_pixel = PIXEL_16x8;
1706 m->p_cost_mv = a->p_cost_mv;
1708 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1709 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1711 mvc[0][0] = lX->me8x8[2*i].mv[0];
1712 mvc[0][1] = lX->me8x8[2*i].mv[1];
1713 mvc[1][0] = lX->me8x8[2*i+1].mv[0];
1714 mvc[1][1] = lX->me8x8[2*i+1].mv[1];
1716 x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp );
1717 x264_me_search( h, m, mvc, 2 );
1720 h->mc.mc_luma( pix[l], 16, m->p_fref, m->i_stride[0],
1721 m->mv[0], m->mv[1], 16, 8 );
1722 /* FIXME: ref cost */
1723 i_part_cost_bi += m->cost_mv;
1726 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1727 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1729 i_part_cost = a->l0.me16x8[i].cost;
1730 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1731 if( a->l1.me16x8[i].cost < i_part_cost )
1733 i_part_cost = a->l1.me16x8[i].cost;
1734 a->i_mb_partition16x8[i] = D_L1_8x8;
1736 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1738 i_part_cost = i_part_cost_bi;
1739 a->i_mb_partition16x8[i] = D_BI_8x8;
1741 a->i_cost16x8bi += i_part_cost;
1743 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1747 a->i_mb_type16x8 = B_L0_L0
1748 + (a->i_mb_partition16x8[0]>>2) * 3
1749 + (a->i_mb_partition16x8[1]>>2);
1750 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1753 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1755 uint8_t **p_fref[2] =
1756 { h->mb.pic.p_fref[0][a->l0.i_ref],
1757 h->mb.pic.p_fref[1][a->l1.i_ref] };
1758 uint8_t pix[2][8*16];
1762 h->mb.i_partition = D_8x16;
1763 a->i_cost8x16bi = 0;
1765 for( i = 0; i < 2; i++ )
1768 int i_part_cost_bi = 0;
1770 for( l = 0; l < 2; l++ )
1772 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1773 x264_me_t *m = &lX->me8x16[i];
1775 m->i_pixel = PIXEL_8x16;
1776 m->p_cost_mv = a->p_cost_mv;
1778 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1779 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1781 mvc[0][0] = lX->me8x8[i].mv[0];
1782 mvc[0][1] = lX->me8x8[i].mv[1];
1783 mvc[1][0] = lX->me8x8[i+2].mv[0];
1784 mvc[1][1] = lX->me8x8[i+2].mv[1];
1786 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1787 x264_me_search( h, m, mvc, 2 );
1790 h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
1791 m->mv[0], m->mv[1], 8, 16 );
1792 /* FIXME: ref cost */
1793 i_part_cost_bi += m->cost_mv;
1796 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1797 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1799 i_part_cost = a->l0.me8x16[i].cost;
1800 a->i_mb_partition8x16[i] = D_L0_8x8;
1801 if( a->l1.me8x16[i].cost < i_part_cost )
1803 i_part_cost = a->l1.me8x16[i].cost;
1804 a->i_mb_partition8x16[i] = D_L1_8x8;
1806 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1808 i_part_cost = i_part_cost_bi;
1809 a->i_mb_partition8x16[i] = D_BI_8x8;
1811 a->i_cost8x16bi += i_part_cost;
1813 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1817 a->i_mb_type8x16 = B_L0_L0
1818 + (a->i_mb_partition8x16[0]>>2) * 3
1819 + (a->i_mb_partition8x16[1]>>2);
1820 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1823 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1825 int thresh = i_satd * 5/4;
1827 h->mb.i_type = P_L0;
1828 if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1830 h->mb.i_partition = D_16x16;
1831 x264_analyse_update_cache( h, a );
1832 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1834 a->l0.me16x16.cost = a->l0.i_rd16x16;
1836 if( a->l0.i_cost16x8 <= thresh )
1838 h->mb.i_partition = D_16x8;
1839 x264_analyse_update_cache( h, a );
1840 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1843 a->l0.i_cost16x8 = COST_MAX;
1845 if( a->l0.i_cost8x16 <= thresh )
1847 h->mb.i_partition = D_8x16;
1848 x264_analyse_update_cache( h, a );
1849 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1852 a->l0.i_cost8x16 = COST_MAX;
1854 if( a->l0.i_cost8x8 <= thresh )
1856 h->mb.i_type = P_8x8;
1857 x264_analyse_update_cache( h, a );
1858 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1860 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1862 /* FIXME: RD per subpartition */
1866 for( i=0; i<4; i++ )
1868 part_bak[i] = h->mb.i_sub_partition[i];
1869 b_sub8x8 |= (part_bak[i] != D_L0_8x8);
1873 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1874 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1875 i_cost = x264_rd_cost_mb( h, a->i_lambda2 );
1876 if( a->l0.i_cost8x8 < i_cost )
1878 for( i=0; i<4; i++ )
1879 h->mb.i_sub_partition[i] = part_bak[i];
1882 a->l0.i_cost8x8 = i_cost;
1887 a->l0.i_cost8x8 = COST_MAX;
1890 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
1892 int thresh = i_satd_inter * 17/16;
1894 if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
1896 h->mb.i_type = B_DIRECT;
1897 x264_analyse_update_cache( h, a );
1898 a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
1901 //FIXME not all the update_cache calls are needed
1902 h->mb.i_partition = D_16x16;
1904 if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
1906 h->mb.i_type = B_L0_L0;
1907 x264_analyse_update_cache( h, a );
1908 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1912 if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
1914 h->mb.i_type = B_L1_L1;
1915 x264_analyse_update_cache( h, a );
1916 a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1920 if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
1922 h->mb.i_type = B_BI_BI;
1923 x264_analyse_update_cache( h, a );
1924 a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1928 if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
1930 h->mb.i_type = B_8x8;
1931 h->mb.i_partition = D_8x8;
1932 x264_analyse_update_cache( h, a );
1933 a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1934 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1938 if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
1940 h->mb.i_type = a->i_mb_type16x8;
1941 h->mb.i_partition = D_16x8;
1942 x264_analyse_update_cache( h, a );
1943 a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1947 if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
1949 h->mb.i_type = a->i_mb_type8x16;
1950 h->mb.i_partition = D_8x16;
1951 x264_analyse_update_cache( h, a );
1952 a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1956 static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
1958 const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1961 switch( h->mb.i_partition )
1964 if( h->mb.i_type == B_BI_BI )
1965 x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
1968 for( i=0; i<2; i++ )
1969 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
1970 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
1973 for( i=0; i<2; i++ )
1974 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
1975 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
1978 for( i=0; i<4; i++ )
1979 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
1980 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
1985 static inline void x264_mb_analyse_transform( x264_t *h )
1987 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
1989 int i_cost4, i_cost8;
1990 /* FIXME only luma mc is needed */
1993 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
1994 h->mb.pic.p_fdec[0], FDEC_STRIDE );
1995 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
1996 h->mb.pic.p_fdec[0], FDEC_STRIDE );
1998 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2002 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2004 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2007 x264_analyse_update_cache( h, a );
2008 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2009 /* FIXME only luma is needed, but the score for comparison already includes chroma */
2010 i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2012 if( *i_rd >= i_rd8 )
2015 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2016 /* prevent a rare division by zero in estimated intra cost */
2023 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2028 /*****************************************************************************
2029 * x264_macroblock_analyse:
2030 *****************************************************************************/
2031 void x264_macroblock_analyse( x264_t *h )
2033 x264_mb_analysis_t analysis;
2034 int i_cost = COST_MAX;
2038 x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
2040 /*--------------------------- Do the analysis ---------------------------*/
2041 if( h->sh.i_type == SLICE_TYPE_I )
2043 x264_mb_analyse_intra( h, &analysis, COST_MAX );
2044 if( analysis.b_mbrd )
2045 x264_intra_rd( h, &analysis, COST_MAX );
2047 i_cost = analysis.i_satd_i16x16;
2048 h->mb.i_type = I_16x16;
2049 if( analysis.i_satd_i4x4 < i_cost )
2051 i_cost = analysis.i_satd_i4x4;
2052 h->mb.i_type = I_4x4;
2054 if( analysis.i_satd_i8x8 < i_cost )
2055 h->mb.i_type = I_8x8;
2057 if( h->mb.i_subpel_refine >= 7 )
2058 x264_intra_rd_refine( h, &analysis );
2060 else if( h->sh.i_type == SLICE_TYPE_P )
2063 int i_intra_cost, i_intra_type;
2065 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2067 /* Fast P_SKIP detection */
2068 analysis.b_try_pskip = 0;
2069 if( h->param.analyse.b_fast_pskip )
2071 if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2072 // FIXME don't need to check this if the reference frame is done
2074 else if( h->param.analyse.i_subpel_refine >= 3 )
2075 analysis.b_try_pskip = 1;
2076 else if( h->mb.i_mb_type_left == P_SKIP ||
2077 h->mb.i_mb_type_top == P_SKIP ||
2078 h->mb.i_mb_type_topleft == P_SKIP ||
2079 h->mb.i_mb_type_topright == P_SKIP )
2080 b_skip = x264_macroblock_probe_pskip( h );
2083 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2087 h->mb.i_type = P_SKIP;
2088 h->mb.i_partition = D_16x16;
2089 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2093 const unsigned int flags = h->param.analyse.inter;
2097 int i_satd_inter, i_satd_intra;
2099 x264_mb_analyse_load_costs( h, &analysis );
2101 x264_mb_analyse_inter_p16x16( h, &analysis );
2103 if( h->mb.i_type == P_SKIP )
2106 if( flags & X264_ANALYSE_PSUB16x16 )
2108 if( h->param.analyse.b_mixed_references )
2109 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2111 x264_mb_analyse_inter_p8x8( h, &analysis );
2114 /* Select best inter mode */
2116 i_partition = D_16x16;
2117 i_cost = analysis.l0.me16x16.cost;
2119 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2120 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2123 i_partition = D_8x8;
2124 i_cost = analysis.l0.i_cost8x8;
2127 if( flags & X264_ANALYSE_PSUB8x8 )
2129 for( i = 0; i < 4; i++ )
2131 x264_mb_analyse_inter_p4x4( h, &analysis, i );
2132 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2134 int i_cost8x8 = analysis.l0.i_cost4x4[i];
2135 h->mb.i_sub_partition[i] = D_L0_4x4;
2137 x264_mb_analyse_inter_p8x4( h, &analysis, i );
2138 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2139 h->mb.i_sub_partition[i], D_L0_8x4 );
2141 x264_mb_analyse_inter_p4x8( h, &analysis, i );
2142 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2143 h->mb.i_sub_partition[i], D_L0_4x8 );
2145 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2147 x264_mb_cache_mv_p8x8( h, &analysis, i );
2149 analysis.l0.i_cost8x8 = i_cost;
2153 /* Now do 16x8/8x16 */
2154 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2155 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2156 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2158 x264_mb_analyse_inter_p16x8( h, &analysis );
2159 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2161 x264_mb_analyse_inter_p8x16( h, &analysis );
2162 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2165 h->mb.i_partition = i_partition;
2168 //FIXME mb_type costs?
2169 if( analysis.b_mbrd )
2173 else if( i_partition == D_16x16 )
2175 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2176 i_cost = analysis.l0.me16x16.cost;
2178 else if( i_partition == D_16x8 )
2180 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2181 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2182 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2184 else if( i_partition == D_8x16 )
2186 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2187 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2188 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2190 else if( i_partition == D_8x8 )
2194 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2196 switch( h->mb.i_sub_partition[i8x8] )
2199 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2200 i_cost += analysis.l0.me8x8[i8x8].cost;
2203 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2204 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2205 i_cost += analysis.l0.me8x4[i8x8][0].cost +
2206 analysis.l0.me8x4[i8x8][1].cost;
2209 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2210 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2211 i_cost += analysis.l0.me4x8[i8x8][0].cost +
2212 analysis.l0.me4x8[i8x8][1].cost;
2216 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2217 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2218 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2219 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2220 i_cost += analysis.l0.me4x4[i8x8][0].cost +
2221 analysis.l0.me4x4[i8x8][1].cost +
2222 analysis.l0.me4x4[i8x8][2].cost +
2223 analysis.l0.me4x4[i8x8][3].cost;
2226 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2232 if( h->mb.b_chroma_me )
2234 x264_mb_analyse_intra_chroma( h, &analysis );
2235 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2236 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2237 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2238 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2241 x264_mb_analyse_intra( h, &analysis, i_cost );
2243 i_satd_inter = i_cost;
2244 i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2245 analysis.i_satd_i8x8,
2246 analysis.i_satd_i4x4 );
2248 if( analysis.b_mbrd )
2250 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2252 i_partition = D_16x16;
2253 i_cost = analysis.l0.me16x16.cost;
2254 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2255 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2256 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2257 h->mb.i_type = i_type;
2258 h->mb.i_partition = i_partition;
2259 if( i_cost < COST_MAX )
2260 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2261 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2264 i_intra_type = I_16x16;
2265 i_intra_cost = analysis.i_satd_i16x16;
2266 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
2267 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
2268 COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
2270 if( i_intra_cost == COST_MAX )
2271 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
2273 h->mb.i_type = i_type;
2274 h->stat.frame.i_intra_cost += i_intra_cost;
2275 h->stat.frame.i_inter_cost += i_cost;
2276 h->stat.frame.i_mbs_analysed++;
2278 if( h->mb.i_subpel_refine >= 7 )
2280 if( IS_INTRA( h->mb.i_type ) )
2282 x264_intra_rd_refine( h, &analysis );
2284 else if( i_partition == D_16x16 )
2286 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2287 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
2289 else if( i_partition == D_16x8 )
2291 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2292 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2293 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
2294 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
2296 else if( i_partition == D_8x16 )
2298 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2299 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2300 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
2301 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
2303 else if( i_partition == D_8x8 )
2306 x264_analyse_update_cache( h, &analysis );
2307 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2308 if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2309 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
2314 else if( h->sh.i_type == SLICE_TYPE_B )
2316 int i_bskip_cost = COST_MAX;
2319 h->mb.i_type = B_SKIP;
2320 if( h->mb.b_direct_auto_write )
2322 /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2323 for( i = 0; i < 2; i++ )
2326 h->sh.b_direct_spatial_mv_pred ^= 1;
2327 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2328 if( analysis.b_direct_available )
2333 b_skip = x264_macroblock_probe_bskip( h );
2335 h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2342 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2344 if( analysis.b_direct_available )
2346 if( !h->mb.b_direct_auto_write )
2348 if( h->mb.b_lossless )
2350 /* chance of skip is too small to bother */
2352 else if( analysis.b_mbrd )
2354 i_bskip_cost = ssd_mb( h );
2356 /* 6 = minimum cavlc cost of a non-skipped MB */
2357 if( i_bskip_cost <= 6 * analysis.i_lambda2 )
2359 h->mb.i_type = B_SKIP;
2360 x264_analyse_update_cache( h, &analysis );
2364 else if( !h->mb.b_direct_auto_write )
2366 /* Conditioning the probe on neighboring block types
2367 * doesn't seem to help speed or quality. */
2368 b_skip = x264_macroblock_probe_bskip( h );
2374 const unsigned int flags = h->param.analyse.inter;
2378 x264_mb_analyse_load_costs( h, &analysis );
2380 /* select best inter mode */
2381 /* direct must be first */
2382 if( analysis.b_direct_available )
2383 x264_mb_analyse_inter_direct( h, &analysis );
2385 x264_mb_analyse_inter_b16x16( h, &analysis );
2388 i_partition = D_16x16;
2389 i_cost = analysis.l0.me16x16.cost;
2390 COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2391 COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2392 COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2394 if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2396 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2397 if( i_bskip_cost < analysis.i_rd16x16direct &&
2398 i_bskip_cost < analysis.i_rd16x16bi &&
2399 i_bskip_cost < analysis.l0.i_rd16x16 &&
2400 i_bskip_cost < analysis.l1.i_rd16x16 )
2402 h->mb.i_type = B_SKIP;
2403 x264_analyse_update_cache( h, &analysis );
2408 if( flags & X264_ANALYSE_BSUB16x16 )
2410 x264_mb_analyse_inter_b8x8( h, &analysis );
2411 if( analysis.i_cost8x8bi < i_cost )
2414 i_partition = D_8x8;
2415 i_cost = analysis.i_cost8x8bi;
2417 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2418 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2420 x264_mb_analyse_inter_b16x8( h, &analysis );
2421 COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2422 i_type, analysis.i_mb_type16x8,
2423 i_partition, D_16x8 );
2425 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2426 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2428 x264_mb_analyse_inter_b8x16( h, &analysis );
2429 COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2430 i_type, analysis.i_mb_type8x16,
2431 i_partition, D_8x16 );
2436 if( analysis.b_mbrd )
2441 else if( i_partition == D_16x16 )
2443 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2444 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2445 if( i_type == B_L0_L0 )
2447 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2448 i_cost = analysis.l0.me16x16.cost
2449 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2451 else if( i_type == B_L1_L1 )
2453 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2454 i_cost = analysis.l1.me16x16.cost
2455 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2457 else if( i_type == B_BI_BI )
2459 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2460 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2463 else if( i_partition == D_16x8 )
2465 for( i=0; i<2; i++ )
2467 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2468 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2469 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2470 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2473 else if( i_partition == D_8x16 )
2475 for( i=0; i<2; i++ )
2477 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2478 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2479 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2480 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2483 else if( i_partition == D_8x8 )
2485 for( i=0; i<4; i++ )
2488 int i_part_cost_old;
2490 int i_part_type = h->mb.i_sub_partition[i];
2491 int b_bidir = (i_part_type == D_BI_8x8);
2493 if( i_part_type == D_DIRECT_8x8 )
2495 if( x264_mb_partition_listX_table[0][i_part_type] )
2497 m = &analysis.l0.me8x8[i];
2498 i_part_cost_old = m->cost;
2499 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2500 m->cost -= i_type_cost;
2501 x264_me_refine_qpel( h, m );
2503 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2505 if( x264_mb_partition_listX_table[1][i_part_type] )
2507 m = &analysis.l1.me8x8[i];
2508 i_part_cost_old = m->cost;
2509 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2510 m->cost -= i_type_cost;
2511 x264_me_refine_qpel( h, m );
2513 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2515 /* TODO: update mvp? */
2519 x264_mb_analyse_intra( h, &analysis, i_cost );
2521 if( analysis.b_mbrd )
2523 int i_satd_inter = i_cost;
2524 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2526 i_cost = i_bskip_cost;
2527 i_partition = D_16x16;
2528 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2529 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2530 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2531 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2532 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2533 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2534 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2536 h->mb.i_type = i_type;
2537 h->mb.i_partition = i_partition;
2538 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2539 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2542 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2543 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2544 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2546 h->mb.i_type = i_type;
2547 h->mb.i_partition = i_partition;
2549 if( h->param.analyse.b_bidir_me )
2550 refine_bidir( h, &analysis );
2554 x264_analyse_update_cache( h, &analysis );
2556 if( !analysis.b_mbrd )
2557 x264_mb_analyse_transform( h );
2559 h->mb.b_trellis = h->param.analyse.i_trellis;
2560 h->mb.b_noise_reduction = h->param.analyse.i_noise_reduction;
2563 /*-------------------- Update MB from the analysis ----------------------*/
2564 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2568 switch( h->mb.i_type )
2571 for( i = 0; i < 16; i++ )
2572 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2574 x264_mb_analyse_intra_chroma( h, a );
2577 for( i = 0; i < 4; i++ )
2578 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2580 x264_mb_analyse_intra_chroma( h, a );
2583 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2584 x264_mb_analyse_intra_chroma( h, a );
2588 switch( h->mb.i_partition )
2591 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2592 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2596 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2597 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2598 x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv[0], a->l0.me16x8[0].mv[1] );
2599 x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv[0], a->l0.me16x8[1].mv[1] );
2603 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2604 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2605 x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv[0], a->l0.me8x16[0].mv[1] );
2606 x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv[0], a->l0.me8x16[1].mv[1] );
2610 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2616 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2617 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2618 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2619 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2620 for( i = 0; i < 4; i++ )
2621 x264_mb_cache_mv_p8x8( h, a, i );
2626 h->mb.i_partition = D_16x16;
2627 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2628 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv[0],
2629 h->mb.cache.pskip_mv[1] );
2635 x264_mb_load_mv_direct8x8( h, 0 );
2636 x264_mb_load_mv_direct8x8( h, 1 );
2637 x264_mb_load_mv_direct8x8( h, 2 );
2638 x264_mb_load_mv_direct8x8( h, 3 );
2642 /* optimize: cache might not need to be rewritten */
2643 for( i = 0; i < 4; i++ )
2644 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2647 default: /* the rest of the B types */
2648 switch( h->mb.i_partition )
2651 switch( h->mb.i_type )
2654 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2655 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2657 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2658 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0, 0 );
2659 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0, 0 );
2662 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2663 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0, 0 );
2664 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0, 0 );
2666 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2667 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2670 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2671 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2673 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2674 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2679 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2680 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2683 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2684 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2687 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2693 if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2696 for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2699 int ref = h->mb.cache.ref[l][x264_scan8[0]];
2702 completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2703 if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2705 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2706 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2707 h->mb.cache.mv[l][x264_scan8[15]][0],
2708 h->mb.cache.mv[l][x264_scan8[15]][1] );
2709 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2710 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2711 fprintf(stderr, "completed: %d \n", completed );
2719 #include "slicetype.c"