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 *****************************************************************************/
33 #include "common/common.h"
34 #include "macroblock.h"
36 #include "ratecontrol.h"
49 int mvc[32][5][2]; /* [ref][0] is 16x16 mv,
50 [ref][1..4] are 8x8 mv from partition [0..3] */
54 int i_cost4x4[4]; /* cost per 8x8 partition */
55 x264_me_t me4x4[4][4];
58 int i_cost8x4[4]; /* cost per 8x8 partition */
59 x264_me_t me8x4[4][2];
62 int i_cost4x8[4]; /* cost per 8x8 partition */
63 x264_me_t me4x8[4][2];
73 } x264_mb_analysis_list_t;
77 /* conduct the analysis using this lamda and QP */
86 /* Take some shortcuts in intra search if intra is deemed unlikely */
92 int i_satd_i16x16_dir[7];
96 int i_satd_i8x8_dir[12][4];
100 int i_predict4x4[16];
103 int i_satd_i8x8chroma;
104 int i_satd_i8x8chroma_dir[4];
105 int i_predict8x8chroma;
107 /* II: Inter part P/B frame */
108 x264_mb_analysis_list_t l0;
109 x264_mb_analysis_list_t l1;
111 int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
112 int i_cost16x16direct;
114 int i_cost8x8direct[4];
123 int i_mb_partition16x8[2]; /* mb_partition_e */
124 int i_mb_partition8x16[2];
125 int i_mb_type16x8; /* mb_class_e */
128 int b_direct_available;
130 } x264_mb_analysis_t;
132 /* lambda = pow(2,qp/6-2) */
133 static const int i_qp0_cost_table[52] = {
134 1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
135 1, 1, 1, 1, /* 8-11 */
136 1, 1, 1, 1, 2, 2, 2, 2, /* 12-19 */
137 3, 3, 3, 4, 4, 4, 5, 6, /* 20-27 */
138 6, 7, 8, 9,10,11,13,14, /* 28-35 */
139 16,18,20,23,25,29,32,36, /* 36-43 */
140 40,45,51,57,64,72,81,91 /* 44-51 */
143 /* pow(lambda,2) * .9 */
144 static const int i_qp0_cost2_table[52] = {
145 1, 1, 1, 1, 1, 1, /* 0-5 */
146 1, 1, 1, 1, 1, 1, /* 6-11 */
147 1, 1, 1, 2, 2, 3, /* 12-17 */
148 4, 5, 6, 7, 9, 11, /* 18-23 */
149 14, 18, 23, 29, 36, 46, /* 24-29 */
150 58, 73, 91, 115, 145, 183, /* 30-35 */
151 230, 290, 366, 461, 581, 731, /* 36-41 */
152 922,1161,1463,1843,2322,2926, /* 42-47 */
156 /* TODO: calculate CABAC costs */
157 static const int i_mb_b_cost_table[X264_MBTYPE_MAX] = {
158 9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
160 static const int i_mb_b16x8_cost_table[17] = {
161 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
163 static const int i_sub_mb_b_cost_table[13] = {
164 7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
166 static const int i_sub_mb_p_cost_table[4] = {
170 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
172 /* initialize an array of lambda*nbits for all possible mvs */
173 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
175 static int16_t *p_cost_mv[52];
177 if( !p_cost_mv[a->i_qp] )
179 /* could be faster, but isn't called many times */
180 /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
182 p_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) );
183 p_cost_mv[a->i_qp] += 2*4*2048;
184 for( i = 0; i <= 2*4*2048; i++ )
186 p_cost_mv[a->i_qp][-i] =
187 p_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i );
191 a->p_cost_mv = p_cost_mv[a->i_qp];
194 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
196 memset( a, 0, sizeof( x264_mb_analysis_t ) );
198 /* conduct the analysis using this lamda and QP */
199 a->i_qp = h->mb.i_qp = i_qp;
200 h->mb.i_chroma_qp = i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )];
201 a->i_lambda = i_qp0_cost_table[i_qp];
202 a->i_lambda2 = i_qp0_cost2_table[i_qp];
203 a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
204 ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
206 h->mb.i_me_method = h->param.analyse.i_me_method;
207 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
208 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
209 && h->mb.i_subpel_refine >= 5;
210 h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
211 h->mb.b_transform_8x8 = 0;
212 h->mb.b_noise_reduction = 0;
218 a->i_satd_i8x8chroma = COST_MAX;
222 /* II: Inter part P/B frame */
223 if( h->sh.i_type != SLICE_TYPE_I )
226 int i_fmv_range = 4 * h->param.analyse.i_mv_range;
227 // limit motion search to a slightly smaller range than the theoretical limit,
228 // since the search may go a few iterations past its given range
229 int i_fpel_border = 5; // umh unconditional radius
230 int i_spel_border = 8; // 1.5 for subpel_satd, 1.5 for subpel_rd, 2 for bime, round up
232 /* Calculate max allowed MV range */
233 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range )
234 h->mb.mv_min[0] = 4*( -16*h->mb.i_mb_x - 24 );
235 h->mb.mv_max[0] = 4*( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 24 );
236 h->mb.mv_min_spel[0] = CLIP_FMV( h->mb.mv_min[0] );
237 h->mb.mv_max_spel[0] = CLIP_FMV( h->mb.mv_max[0] );
238 h->mb.mv_min_fpel[0] = (h->mb.mv_min_spel[0]>>2) + i_fpel_border;
239 h->mb.mv_max_fpel[0] = (h->mb.mv_max_spel[0]>>2) - i_fpel_border;
240 if( h->mb.i_mb_x == 0)
242 int mb_y = h->mb.i_mb_y >> h->sh.b_mbaff;
243 int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff;
244 int thread_mvy_range = i_fmv_range;
246 if( h->param.i_threads > 1 )
248 int pix_y = (h->mb.i_mb_y | h->mb.b_interlaced) * 16;
249 int thresh = pix_y + h->param.analyse.i_mv_range_thread;
250 for( i = (h->sh.i_type == SLICE_TYPE_B); i >= 0; i-- )
252 x264_frame_t **fref = i ? h->fref1 : h->fref0;
253 int i_ref = i ? h->i_ref1 : h->i_ref0;
254 for( j=0; j<i_ref; j++ )
256 x264_frame_cond_wait( fref[j], thresh );
257 thread_mvy_range = X264_MIN( thread_mvy_range, fref[j]->i_lines_completed - pix_y );
260 if( h->param.b_deterministic )
261 thread_mvy_range = h->param.analyse.i_mv_range_thread;
262 if( h->mb.b_interlaced )
263 thread_mvy_range >>= 1;
266 h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
267 h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
268 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 );
269 h->mb.mv_max_spel[1] = CLIP_FMV( h->mb.mv_max[1] );
270 h->mb.mv_max_spel[1] = X264_MIN( h->mb.mv_max_spel[1], thread_mvy_range*4 );
271 h->mb.mv_min_fpel[1] = (h->mb.mv_min_spel[1]>>2) + i_fpel_border;
272 h->mb.mv_max_fpel[1] = (h->mb.mv_max_spel[1]>>2) - i_fpel_border;
278 a->l0.i_cost8x8 = COST_MAX;
280 for( i = 0; i < 4; i++ )
284 a->l0.i_cost4x8[i] = COST_MAX;
288 a->l0.i_cost8x16 = COST_MAX;
289 if( h->sh.i_type == SLICE_TYPE_B )
293 a->l1.i_cost8x8 = COST_MAX;
295 for( i = 0; i < 4; i++ )
300 a->i_cost8x8direct[i] = COST_MAX;
311 a->i_cost16x16direct =
314 a->i_cost8x16bi = COST_MAX;
317 /* Fast intra decision */
318 if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
320 if( IS_INTRA( h->mb.i_mb_type_left )
321 || IS_INTRA( h->mb.i_mb_type_top )
322 || IS_INTRA( h->mb.i_mb_type_topleft )
323 || IS_INTRA( h->mb.i_mb_type_topright )
324 || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
325 || (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])) )
326 { /* intra is likely */ }
341 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
343 if( i_neighbour & MB_TOPLEFT )
345 /* top and left available */
346 *mode++ = I_PRED_16x16_V;
347 *mode++ = I_PRED_16x16_H;
348 *mode++ = I_PRED_16x16_DC;
349 *mode++ = I_PRED_16x16_P;
352 else if( i_neighbour & MB_LEFT )
355 *mode++ = I_PRED_16x16_DC_LEFT;
356 *mode++ = I_PRED_16x16_H;
359 else if( i_neighbour & MB_TOP )
362 *mode++ = I_PRED_16x16_DC_TOP;
363 *mode++ = I_PRED_16x16_V;
369 *mode = I_PRED_16x16_DC_128;
375 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
377 if( i_neighbour & MB_TOPLEFT )
379 /* top and left available */
380 *mode++ = I_PRED_CHROMA_V;
381 *mode++ = I_PRED_CHROMA_H;
382 *mode++ = I_PRED_CHROMA_DC;
383 *mode++ = I_PRED_CHROMA_P;
386 else if( i_neighbour & MB_LEFT )
389 *mode++ = I_PRED_CHROMA_DC_LEFT;
390 *mode++ = I_PRED_CHROMA_H;
393 else if( i_neighbour & MB_TOP )
396 *mode++ = I_PRED_CHROMA_DC_TOP;
397 *mode++ = I_PRED_CHROMA_V;
403 *mode = I_PRED_CHROMA_DC_128;
409 static void predict_4x4_mode_available( unsigned int i_neighbour,
410 int *mode, int *pi_count )
412 int b_l = i_neighbour & MB_LEFT;
413 int b_t = i_neighbour & MB_TOP;
418 *mode++ = I_PRED_4x4_DC;
419 *mode++ = I_PRED_4x4_H;
420 *mode++ = I_PRED_4x4_V;
421 *mode++ = I_PRED_4x4_DDL;
422 if( i_neighbour & MB_TOPLEFT )
424 *mode++ = I_PRED_4x4_DDR;
425 *mode++ = I_PRED_4x4_VR;
426 *mode++ = I_PRED_4x4_HD;
429 *mode++ = I_PRED_4x4_VL;
430 *mode++ = I_PRED_4x4_HU;
434 *mode++ = I_PRED_4x4_DC_LEFT;
435 *mode++ = I_PRED_4x4_H;
436 *mode++ = I_PRED_4x4_HU;
441 *mode++ = I_PRED_4x4_DC_TOP;
442 *mode++ = I_PRED_4x4_V;
443 *mode++ = I_PRED_4x4_DDL;
444 *mode++ = I_PRED_4x4_VL;
449 *mode++ = I_PRED_4x4_DC_128;
454 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
461 uint8_t *p_dstc[2], *p_srcc[2];
463 if( a->i_satd_i8x8chroma < COST_MAX )
466 /* 8x8 prediction selection for chroma */
467 p_dstc[0] = h->mb.pic.p_fdec[1];
468 p_dstc[1] = h->mb.pic.p_fdec[2];
469 p_srcc[0] = h->mb.pic.p_fenc[1];
470 p_srcc[1] = h->mb.pic.p_fenc[2];
472 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
473 a->i_satd_i8x8chroma = COST_MAX;
474 if( i_max == 4 && h->pixf.intra_satd_x3_8x8c && h->pixf.mbcmp[0] == h->pixf.satd[0] )
476 int satdu[4], satdv[4];
477 h->pixf.intra_satd_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
478 h->pixf.intra_satd_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
479 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
480 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
481 satdu[I_PRED_CHROMA_P] =
482 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
483 satdv[I_PRED_CHROMA_P] =
484 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
486 for( i=0; i<i_max; i++ )
488 int i_mode = predict_mode[i];
489 int i_satd = satdu[i_mode] + satdv[i_mode]
490 + a->i_lambda * bs_size_ue(i_mode);
492 a->i_satd_i8x8chroma_dir[i] = i_satd;
493 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
498 for( i=0; i<i_max; i++ )
501 int i_mode = predict_mode[i];
503 /* we do the prediction */
504 h->predict_8x8c[i_mode]( p_dstc[0] );
505 h->predict_8x8c[i_mode]( p_dstc[1] );
507 /* we calculate the cost */
508 i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
509 p_srcc[0], FENC_STRIDE ) +
510 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
511 p_srcc[1], FENC_STRIDE ) +
512 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
514 a->i_satd_i8x8chroma_dir[i] = i_satd;
515 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
519 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
522 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
524 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
525 uint8_t *p_src = h->mb.pic.p_fenc[0];
526 uint8_t *p_dst = h->mb.pic.p_fdec[0];
531 int b_merged_satd = h->pixf.intra_satd_x3_16x16 && h->pixf.mbcmp[0] == h->pixf.satd[0];
533 /*---------------- Try all mode and calculate their score ---------------*/
535 /* 16x16 prediction selection */
536 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
538 if( b_merged_satd && i_max == 4 )
540 h->pixf.intra_satd_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
541 h->predict_16x16[I_PRED_16x16_P]( p_dst );
542 a->i_satd_i16x16_dir[I_PRED_16x16_P] =
543 h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
546 int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
547 COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
552 for( i = 0; i < i_max; i++ )
555 int i_mode = predict_mode[i];
556 h->predict_16x16[i_mode]( p_dst );
558 i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
559 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
560 COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
561 a->i_satd_i16x16_dir[i_mode] = i_satd;
565 if( h->sh.i_type == SLICE_TYPE_B )
566 /* cavlc mb type prefix */
567 a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
568 if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
571 /* 8x8 prediction selection */
572 if( flags & X264_ANALYSE_I8x8 )
574 DECLARE_ALIGNED( uint8_t, edge[33], 8 );
575 x264_pixel_cmp_t sa8d = (*h->pixf.mbcmp == *h->pixf.sad) ? h->pixf.sad[PIXEL_8x8] : h->pixf.sa8d[PIXEL_8x8];
576 int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
578 b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
580 // FIXME some bias like in i4x4?
581 if( h->sh.i_type == SLICE_TYPE_B )
582 i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
584 for( idx = 0;; idx++ )
588 uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
589 uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
590 int i_best = COST_MAX;
591 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
593 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
594 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
596 if( b_merged_satd && i_max == 9 )
599 h->pixf.intra_sa8d_x3_8x8( p_src_by, edge, satd );
600 if( i_pred_mode < 3 )
601 satd[i_pred_mode] -= 3 * a->i_lambda;
602 for( i=2; i>=0; i-- )
604 int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
605 COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
612 for( ; i<i_max; i++ )
615 int i_mode = predict_mode[i];
617 h->predict_8x8[i_mode]( p_dst_by, edge );
619 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
620 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
622 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
623 a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
627 if( idx == 3 || i_cost > i_satd_thresh )
630 /* we need to encode this block now (for next ones) */
631 h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
632 x264_mb_encode_i8x8( h, idx, a->i_qp );
634 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
638 a->i_satd_i8x8 = i_cost;
641 a->i_satd_i8x8 = COST_MAX;
642 i_cost = i_cost * 4/(idx+1);
644 if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 )
648 /* 4x4 prediction selection */
649 if( flags & X264_ANALYSE_I4x4 )
652 int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
653 b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
655 i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
657 i_cost = a->i_lambda * 24; /* from JVT (SATD0) */
658 if( h->sh.i_type == SLICE_TYPE_B )
659 i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];
661 for( idx = 0;; idx++ )
663 int x = block_idx_x[idx];
664 int y = block_idx_y[idx];
665 uint8_t *p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
666 uint8_t *p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
667 int i_best = COST_MAX;
668 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
670 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
672 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
673 /* emulate missing topright samples */
674 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
676 if( b_merged_satd && i_max >= 6 )
679 h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
680 if( i_pred_mode < 3 )
681 satd[i_pred_mode] -= 3 * a->i_lambda;
682 for( i=2; i>=0; i-- )
683 COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
684 a->i_predict4x4[idx], i );
690 for( ; i<i_max; i++ )
693 int i_mode = predict_mode[i];
695 h->predict_4x4[i_mode]( p_dst_by );
697 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
698 p_src_by, FENC_STRIDE )
699 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
701 COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
705 if( i_cost > i_satd_thresh || idx == 15 )
708 /* we need to encode this block now (for next ones) */
709 h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
710 x264_mb_encode_i4x4( h, idx, a->i_qp );
712 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
715 a->i_satd_i4x4 = i_cost;
717 a->i_satd_i4x4 = COST_MAX;
721 static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
723 if( a->i_satd_i16x16 <= i_satd_thresh )
725 h->mb.i_type = I_16x16;
726 x264_analyse_update_cache( h, a );
727 a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
730 a->i_satd_i16x16 = COST_MAX;
732 if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
734 h->mb.i_type = I_4x4;
735 x264_analyse_update_cache( h, a );
736 a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
739 a->i_satd_i4x4 = COST_MAX;
741 if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
743 h->mb.i_type = I_8x8;
744 x264_analyse_update_cache( h, a );
745 a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
748 a->i_satd_i8x8 = COST_MAX;
751 static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
753 uint8_t *p_src = h->mb.pic.p_fenc[0];
754 uint8_t *p_dst = h->mb.pic.p_fdec[0];
757 int i_max, i_satd, i_best, i_mode, i_thresh;
761 if( h->mb.i_type == I_16x16 )
763 int old_pred_mode = a->i_predict16x16;
764 i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
765 i_best = a->i_satd_i16x16;
766 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
767 for( i = 0; i < i_max; i++ )
769 int i_mode = predict_mode[i];
770 if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
772 h->mb.i_intra16x16_pred_mode = i_mode;
773 i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
774 COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
777 else if( h->mb.i_type == I_4x4 )
779 uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
781 for( idx = 0; idx < 16; idx++ )
787 i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
788 x = block_idx_x[idx];
789 y = block_idx_y[idx];
791 p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
792 p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
793 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
795 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
796 /* emulate missing topright samples */
797 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
799 for( i = 0; i < i_max; i++ )
801 i_mode = predict_mode[i];
802 h->predict_4x4[i_mode]( p_dst_by );
803 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
805 if( i_best > i_satd )
807 a->i_predict4x4[idx] = i_mode;
809 pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
810 pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
811 pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
812 pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
813 i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
817 *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
818 *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
819 *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
820 *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
821 h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
823 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
826 else if( h->mb.i_type == I_8x8 )
828 DECLARE_ALIGNED( uint8_t, edge[33], 8 );
829 for( idx = 0; idx < 4; idx++ )
837 i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
840 i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
844 p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
845 p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
846 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
847 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
849 for( i = 0; i < i_max; i++ )
851 i_mode = predict_mode[i];
852 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
854 h->predict_8x8[i_mode]( p_dst_by, edge );
855 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
857 if( i_best > i_satd )
859 a->i_predict8x8[idx] = i_mode;
862 pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
865 pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
867 i_nnz[j] = h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]];
871 *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
874 p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
876 h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]] = i_nnz[j];
878 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
882 /* RD selection for chroma prediction */
883 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
886 i_thresh = a->i_satd_i8x8chroma * 5/4;
888 for( i = j = 0; i < i_max; i++ )
889 if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
890 predict_mode[i] != a->i_predict8x8chroma )
892 predict_mode[j++] = predict_mode[i];
898 int i_chroma_lambda = i_qp0_cost2_table[h->mb.i_chroma_qp];
899 /* the previous thing encoded was x264_intra_rd(), so the pixels and
900 * coefs for the current chroma mode are still around, so we only
901 * have to recount the bits. */
902 i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
903 for( i = 0; i < i_max; i++ )
905 i_mode = predict_mode[i];
906 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
907 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
908 /* if we've already found a mode that needs no residual, then
909 * probably any mode with a residual will be worse.
910 * so avoid dct on the remaining modes to improve speed. */
911 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
912 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode );
914 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
919 #define LOAD_FENC( m, src, xoff, yoff) \
920 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
921 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
922 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
923 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
924 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
926 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
927 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
928 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
929 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
930 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
931 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
932 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
933 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
935 #define REF_COST(list, ref) \
936 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
938 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
942 int mvc[7][2], i_mvc;
943 int i_halfpel_thresh = INT_MAX;
944 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
946 /* 16x16 Search on all ref frame */
947 m.i_pixel = PIXEL_16x16;
948 m.p_cost_mv = a->p_cost_mv;
949 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
951 a->l0.me16x16.cost = INT_MAX;
952 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
954 const int i_ref_cost = REF_COST( 0, i_ref );
955 i_halfpel_thresh -= i_ref_cost;
956 m.i_ref_cost = i_ref_cost;
959 /* search with ref */
960 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
961 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
962 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
963 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
966 * SSD threshold would probably be better than SATD */
969 && m.cost-m.cost_mv < 300*a->i_lambda
970 && abs(m.mv[0]-h->mb.cache.pskip_mv[0])
971 + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
972 && x264_macroblock_probe_pskip( h ) )
974 h->mb.i_type = P_SKIP;
975 x264_analyse_update_cache( h, a );
976 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
980 m.cost += i_ref_cost;
981 i_halfpel_thresh += i_ref_cost;
983 if( m.cost < a->l0.me16x16.cost )
986 /* save mv for predicting neighbors */
987 a->l0.mvc[i_ref][0][0] =
988 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
989 a->l0.mvc[i_ref][0][1] =
990 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
993 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
994 assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
997 if( a->b_mbrd && a->l0.i_ref == 0
998 && a->l0.me16x16.mv[0] == h->mb.cache.pskip_mv[0]
999 && a->l0.me16x16.mv[1] == h->mb.cache.pskip_mv[1] )
1001 h->mb.i_partition = D_16x16;
1002 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
1003 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1007 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
1011 uint8_t **p_fenc = h->mb.pic.p_fenc;
1012 int i_halfpel_thresh = INT_MAX;
1013 int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
1015 int i_maxref = h->mb.pic.i_fref[0]-1;
1017 h->mb.i_partition = D_8x8;
1019 /* early termination: if 16x16 chose ref 0, then evalute no refs older
1020 * than those used by the neighbors */
1021 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
1022 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
1025 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
1026 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
1027 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
1028 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
1029 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
1030 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
1033 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1035 a->l0.mvc[i_ref][0][0] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0];
1036 a->l0.mvc[i_ref][0][1] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1];
1039 for( i = 0; i < 4; i++ )
1041 x264_me_t *l0m = &a->l0.me8x8[i];
1045 m.i_pixel = PIXEL_8x8;
1046 m.p_cost_mv = a->p_cost_mv;
1048 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
1049 l0m->cost = INT_MAX;
1050 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1052 const int i_ref_cost = REF_COST( 0, i_ref );
1053 i_halfpel_thresh -= i_ref_cost;
1054 m.i_ref_cost = i_ref_cost;
1057 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
1058 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
1059 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1060 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
1062 m.cost += i_ref_cost;
1063 i_halfpel_thresh += i_ref_cost;
1064 *(uint64_t*)a->l0.mvc[i_ref][i+1] = *(uint64_t*)m.mv;
1066 if( m.cost < l0m->cost )
1069 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv[0], l0m->mv[1] );
1070 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1073 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1076 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1077 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1078 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1079 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1082 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1084 const int i_ref = a->l0.me16x16.i_ref;
1085 const int i_ref_cost = REF_COST( 0, i_ref );
1086 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
1087 uint8_t **p_fenc = h->mb.pic.p_fenc;
1089 int (*mvc)[2] = a->l0.mvc[i_ref];
1092 /* XXX Needed for x264_mb_predict_mv */
1093 h->mb.i_partition = D_8x8;
1096 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.me16x16.mv;
1098 for( i = 0; i < 4; i++ )
1100 x264_me_t *m = &a->l0.me8x8[i];
1104 m->i_pixel = PIXEL_8x8;
1105 m->p_cost_mv = a->p_cost_mv;
1106 m->i_ref_cost = i_ref_cost;
1109 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1110 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1111 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1112 x264_me_search( h, m, mvc, i_mvc );
1114 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
1116 *(uint64_t*)mvc[i_mvc] = *(uint64_t*)m->mv;
1120 m->cost += i_ref_cost;
1121 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1124 /* theoretically this should include 4*ref_cost,
1125 * but 3 seems a better approximation of cabac. */
1126 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1127 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
1128 REF_COST( 0, a->l0.me16x16.i_ref );
1129 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1130 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1133 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
1136 uint8_t **p_fenc = h->mb.pic.p_fenc;
1140 /* XXX Needed for x264_mb_predict_mv */
1141 h->mb.i_partition = D_16x8;
1143 for( i = 0; i < 2; i++ )
1145 x264_me_t *l0m = &a->l0.me16x8[i];
1146 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1147 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1149 m.i_pixel = PIXEL_16x8;
1150 m.p_cost_mv = a->p_cost_mv;
1152 LOAD_FENC( &m, p_fenc, 0, 8*i );
1153 l0m->cost = INT_MAX;
1154 for( j = 0; j < i_ref8s; j++ )
1156 const int i_ref = ref8[j];
1157 const int i_ref_cost = REF_COST( 0, i_ref );
1158 m.i_ref_cost = i_ref_cost;
1161 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1162 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1163 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][2*i+1];
1164 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][2*i+2];
1166 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1167 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1168 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1169 x264_me_search( h, &m, mvc, 3 );
1171 m.cost += i_ref_cost;
1173 if( m.cost < l0m->cost )
1176 x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, l0m->mv[0], l0m->mv[1] );
1177 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1180 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1183 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1186 uint8_t **p_fenc = h->mb.pic.p_fenc;
1190 /* XXX Needed for x264_mb_predict_mv */
1191 h->mb.i_partition = D_8x16;
1193 for( i = 0; i < 2; i++ )
1195 x264_me_t *l0m = &a->l0.me8x16[i];
1196 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1197 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1199 m.i_pixel = PIXEL_8x16;
1200 m.p_cost_mv = a->p_cost_mv;
1202 LOAD_FENC( &m, p_fenc, 8*i, 0 );
1203 l0m->cost = INT_MAX;
1204 for( j = 0; j < i_ref8s; j++ )
1206 const int i_ref = ref8[j];
1207 const int i_ref_cost = REF_COST( 0, i_ref );
1208 m.i_ref_cost = i_ref_cost;
1211 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1212 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][i+1];
1213 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][i+3];
1215 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1216 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1217 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1218 x264_me_search( h, &m, mvc, 3 );
1220 m.cost += i_ref_cost;
1222 if( m.cost < l0m->cost )
1225 x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, l0m->mv[0], l0m->mv[1] );
1226 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1229 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1232 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1234 DECLARE_ALIGNED( uint8_t, pix1[16*8], 8 );
1235 uint8_t *pix2 = pix1+8;
1236 const int i_stride = h->mb.pic.i_stride[1];
1237 const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1238 const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1240 #define CHROMA4x4MC( width, height, me, x, y ) \
1241 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 ); \
1242 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 );
1244 if( pixel == PIXEL_4x4 )
1246 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1247 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 0,2 );
1248 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 2,0 );
1249 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1251 else if( pixel == PIXEL_8x4 )
1253 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1254 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1258 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1259 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1262 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1263 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1266 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1268 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1269 uint8_t **p_fenc = h->mb.pic.p_fenc;
1270 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1273 /* XXX Needed for x264_mb_predict_mv */
1274 h->mb.i_partition = D_8x8;
1276 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1278 const int idx = 4*i8x8 + i4x4;
1279 const int x4 = block_idx_x[idx];
1280 const int y4 = block_idx_y[idx];
1281 const int i_mvc = (i4x4 == 0);
1283 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1285 m->i_pixel = PIXEL_4x4;
1286 m->p_cost_mv = a->p_cost_mv;
1288 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1289 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1291 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1292 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1294 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
1296 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1297 a->l0.me4x4[i8x8][1].cost +
1298 a->l0.me4x4[i8x8][2].cost +
1299 a->l0.me4x4[i8x8][3].cost +
1300 REF_COST( 0, i_ref ) +
1301 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1302 if( h->mb.b_chroma_me )
1303 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1306 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1308 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1309 uint8_t **p_fenc = h->mb.pic.p_fenc;
1310 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1313 /* XXX Needed for x264_mb_predict_mv */
1314 h->mb.i_partition = D_8x8;
1316 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1318 const int idx = 4*i8x8 + 2*i8x4;
1319 const int x4 = block_idx_x[idx];
1320 const int y4 = block_idx_y[idx];
1321 const int i_mvc = (i8x4 == 0);
1323 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1325 m->i_pixel = PIXEL_8x4;
1326 m->p_cost_mv = a->p_cost_mv;
1328 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1329 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1331 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1332 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1334 x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
1336 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1337 REF_COST( 0, i_ref ) +
1338 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1339 if( h->mb.b_chroma_me )
1340 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1343 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1345 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1346 uint8_t **p_fenc = h->mb.pic.p_fenc;
1347 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1350 /* XXX Needed for x264_mb_predict_mv */
1351 h->mb.i_partition = D_8x8;
1353 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1355 const int idx = 4*i8x8 + i4x8;
1356 const int x4 = block_idx_x[idx];
1357 const int y4 = block_idx_y[idx];
1358 const int i_mvc = (i4x8 == 0);
1360 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1362 m->i_pixel = PIXEL_4x8;
1363 m->p_cost_mv = a->p_cost_mv;
1365 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1366 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1368 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1369 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1371 x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
1373 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1374 REF_COST( 0, i_ref ) +
1375 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1376 if( h->mb.b_chroma_me )
1377 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1380 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1382 /* Assumes that fdec still contains the results of
1383 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1385 uint8_t **p_fenc = h->mb.pic.p_fenc;
1386 uint8_t **p_fdec = h->mb.pic.p_fdec;
1389 a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1390 for( i = 0; i < 4; i++ )
1392 const int x = (i&1)*8;
1393 const int y = (i>>1)*8;
1394 a->i_cost16x16direct +=
1395 a->i_cost8x8direct[i] =
1396 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1399 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1403 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1405 if( h->param.analyse.b_weighted_bipred ) \
1406 h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1407 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1409 h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1412 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1414 uint8_t pix1[16*16], pix2[16*16];
1421 int mvc[8][2], i_mvc;
1422 int i_halfpel_thresh = INT_MAX;
1423 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1425 /* 16x16 Search on all ref frame */
1426 m.i_pixel = PIXEL_16x16;
1427 m.p_cost_mv = a->p_cost_mv;
1428 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1431 a->l0.me16x16.cost = INT_MAX;
1432 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1434 /* search with ref */
1435 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1436 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1437 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1438 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1441 m.cost += REF_COST( 0, i_ref );
1443 if( m.cost < a->l0.me16x16.cost )
1445 a->l0.i_ref = i_ref;
1449 /* save mv for predicting neighbors */
1450 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1451 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1453 /* subtract ref cost, so we don't have to add it for the other MB types */
1454 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1457 i_halfpel_thresh = INT_MAX;
1458 p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1459 a->l1.me16x16.cost = INT_MAX;
1460 for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1462 /* search with ref */
1463 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1464 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1465 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1466 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1469 m.cost += REF_COST( 1, i_ref );
1471 if( m.cost < a->l1.me16x16.cost )
1473 a->l1.i_ref = i_ref;
1477 /* save mv for predicting neighbors */
1478 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1479 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1481 /* subtract ref cost, so we don't have to add it for the other MB types */
1482 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1484 /* Set global ref, needed for other modes? */
1485 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1486 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1488 /* get cost of BI mode */
1489 weight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1490 if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
1492 /* l0 reference is halfpel, so get_ref on it will make it faster */
1493 src2 = h->mc.get_ref( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1495 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1497 h->mc.mc_luma( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1499 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1501 weight = 64 - weight;
1505 /* if l0 was qpel, we'll use get_ref on l1 instead */
1506 h->mc.mc_luma( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1508 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1510 src2 = h->mc.get_ref( 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( m->p_fref, m->i_stride[0], pix[l], 8,
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( m->p_fref, m->i_stride[0], pix[l], 16,
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( m->p_fref, m->i_stride[0], pix[l], 8,
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 h->mb.cache.b_transform_8x8_allowed =
1988 h->pps->b_transform_8x8_mode
1989 && !IS_INTRA( h->mb.i_type ) && x264_mb_transform_8x8_allowed( h );
1991 if( h->mb.cache.b_transform_8x8_allowed && h->param.analyse.b_transform_8x8 )
1993 int i_cost4, i_cost8;
1994 /* FIXME only luma mc is needed */
1997 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
1998 h->mb.pic.p_fdec[0], FDEC_STRIDE );
1999 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2000 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2002 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2006 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2008 h->mb.cache.b_transform_8x8_allowed =
2009 h->pps->b_transform_8x8_mode && x264_mb_transform_8x8_allowed( h );
2011 if( h->mb.cache.b_transform_8x8_allowed && h->param.analyse.b_transform_8x8 )
2014 x264_analyse_update_cache( h, a );
2015 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2016 /* FIXME only luma is needed, but the score for comparison already includes chroma */
2017 i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2019 if( *i_rd >= i_rd8 )
2022 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2023 /* prevent a rare division by zero in estimated intra cost */
2030 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2035 /*****************************************************************************
2036 * x264_macroblock_analyse:
2037 *****************************************************************************/
2038 void x264_macroblock_analyse( x264_t *h )
2040 x264_mb_analysis_t analysis;
2041 int i_cost = COST_MAX;
2045 x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
2047 /*--------------------------- Do the analysis ---------------------------*/
2048 if( h->sh.i_type == SLICE_TYPE_I )
2050 x264_mb_analyse_intra( h, &analysis, COST_MAX );
2051 if( analysis.b_mbrd )
2052 x264_intra_rd( h, &analysis, COST_MAX );
2054 i_cost = analysis.i_satd_i16x16;
2055 h->mb.i_type = I_16x16;
2056 if( analysis.i_satd_i4x4 < i_cost )
2058 i_cost = analysis.i_satd_i4x4;
2059 h->mb.i_type = I_4x4;
2061 if( analysis.i_satd_i8x8 < i_cost )
2062 h->mb.i_type = I_8x8;
2064 if( h->mb.i_subpel_refine >= 7 )
2065 x264_intra_rd_refine( h, &analysis );
2067 else if( h->sh.i_type == SLICE_TYPE_P )
2070 int i_intra_cost, i_intra_type;
2072 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2074 /* Fast P_SKIP detection */
2075 analysis.b_try_pskip = 0;
2076 if( h->param.analyse.b_fast_pskip )
2078 if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2079 // FIXME don't need to check this if the reference frame is done
2081 else if( h->param.analyse.i_subpel_refine >= 3 )
2082 analysis.b_try_pskip = 1;
2083 else if( h->mb.i_mb_type_left == P_SKIP ||
2084 h->mb.i_mb_type_top == P_SKIP ||
2085 h->mb.i_mb_type_topleft == P_SKIP ||
2086 h->mb.i_mb_type_topright == P_SKIP )
2087 b_skip = x264_macroblock_probe_pskip( h );
2090 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2094 h->mb.i_type = P_SKIP;
2095 h->mb.i_partition = D_16x16;
2096 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2100 const unsigned int flags = h->param.analyse.inter;
2104 int i_satd_inter, i_satd_intra;
2106 x264_mb_analyse_load_costs( h, &analysis );
2108 x264_mb_analyse_inter_p16x16( h, &analysis );
2110 if( h->mb.i_type == P_SKIP )
2113 if( flags & X264_ANALYSE_PSUB16x16 )
2115 if( h->param.analyse.b_mixed_references )
2116 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2118 x264_mb_analyse_inter_p8x8( h, &analysis );
2121 /* Select best inter mode */
2123 i_partition = D_16x16;
2124 i_cost = analysis.l0.me16x16.cost;
2126 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2127 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2130 i_partition = D_8x8;
2131 i_cost = analysis.l0.i_cost8x8;
2134 if( flags & X264_ANALYSE_PSUB8x8 )
2136 for( i = 0; i < 4; i++ )
2138 x264_mb_analyse_inter_p4x4( h, &analysis, i );
2139 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2141 int i_cost8x8 = analysis.l0.i_cost4x4[i];
2142 h->mb.i_sub_partition[i] = D_L0_4x4;
2144 x264_mb_analyse_inter_p8x4( h, &analysis, i );
2145 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2146 h->mb.i_sub_partition[i], D_L0_8x4 );
2148 x264_mb_analyse_inter_p4x8( h, &analysis, i );
2149 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2150 h->mb.i_sub_partition[i], D_L0_4x8 );
2152 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2154 x264_mb_cache_mv_p8x8( h, &analysis, i );
2156 analysis.l0.i_cost8x8 = i_cost;
2160 /* Now do 16x8/8x16 */
2161 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2162 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2163 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2165 x264_mb_analyse_inter_p16x8( h, &analysis );
2166 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2168 x264_mb_analyse_inter_p8x16( h, &analysis );
2169 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2172 h->mb.i_partition = i_partition;
2175 //FIXME mb_type costs?
2176 if( analysis.b_mbrd )
2180 else if( i_partition == D_16x16 )
2182 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2183 i_cost = analysis.l0.me16x16.cost;
2185 else if( i_partition == D_16x8 )
2187 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2188 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2189 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2191 else if( i_partition == D_8x16 )
2193 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2194 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2195 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2197 else if( i_partition == D_8x8 )
2201 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2203 switch( h->mb.i_sub_partition[i8x8] )
2206 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2207 i_cost += analysis.l0.me8x8[i8x8].cost;
2210 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2211 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2212 i_cost += analysis.l0.me8x4[i8x8][0].cost +
2213 analysis.l0.me8x4[i8x8][1].cost;
2216 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2217 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2218 i_cost += analysis.l0.me4x8[i8x8][0].cost +
2219 analysis.l0.me4x8[i8x8][1].cost;
2223 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2224 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2225 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2226 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2227 i_cost += analysis.l0.me4x4[i8x8][0].cost +
2228 analysis.l0.me4x4[i8x8][1].cost +
2229 analysis.l0.me4x4[i8x8][2].cost +
2230 analysis.l0.me4x4[i8x8][3].cost;
2233 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2239 if( h->mb.b_chroma_me )
2241 x264_mb_analyse_intra_chroma( h, &analysis );
2242 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2243 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2244 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2245 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2248 x264_mb_analyse_intra( h, &analysis, i_cost );
2250 i_satd_inter = i_cost;
2251 i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2252 analysis.i_satd_i8x8,
2253 analysis.i_satd_i4x4 );
2255 if( analysis.b_mbrd )
2257 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2259 i_partition = D_16x16;
2260 i_cost = analysis.l0.me16x16.cost;
2261 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2262 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2263 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2264 h->mb.i_type = i_type;
2265 h->mb.i_partition = i_partition;
2266 if( i_cost < COST_MAX )
2267 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2268 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2271 i_intra_type = I_16x16;
2272 i_intra_cost = analysis.i_satd_i16x16;
2273 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
2274 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
2275 COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
2277 if( i_intra_cost == COST_MAX )
2278 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
2280 h->mb.i_type = i_type;
2281 h->stat.frame.i_intra_cost += i_intra_cost;
2282 h->stat.frame.i_inter_cost += i_cost;
2283 h->stat.frame.i_mbs_analysed++;
2285 if( h->mb.i_subpel_refine >= 7 )
2287 if( IS_INTRA( h->mb.i_type ) )
2289 x264_intra_rd_refine( h, &analysis );
2291 else if( i_partition == D_16x16 )
2293 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2294 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
2296 else if( i_partition == D_16x8 )
2298 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2299 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2300 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
2301 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
2303 else if( i_partition == D_8x16 )
2305 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2306 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2307 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
2308 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
2310 else if( i_partition == D_8x8 )
2313 x264_analyse_update_cache( h, &analysis );
2314 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2315 if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2316 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
2321 else if( h->sh.i_type == SLICE_TYPE_B )
2323 int i_bskip_cost = COST_MAX;
2326 h->mb.i_type = B_SKIP;
2327 if( h->mb.b_direct_auto_write )
2329 /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2330 for( i = 0; i < 2; i++ )
2333 h->sh.b_direct_spatial_mv_pred ^= 1;
2334 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2335 if( analysis.b_direct_available )
2340 b_skip = x264_macroblock_probe_bskip( h );
2342 h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2349 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2351 if( analysis.b_direct_available )
2353 if( !h->mb.b_direct_auto_write )
2355 if( h->mb.b_lossless )
2357 /* chance of skip is too small to bother */
2359 else if( analysis.b_mbrd )
2361 i_bskip_cost = ssd_mb( h );
2363 /* 6 = minimum cavlc cost of a non-skipped MB */
2364 if( i_bskip_cost <= 6 * analysis.i_lambda2 )
2366 h->mb.i_type = B_SKIP;
2367 x264_analyse_update_cache( h, &analysis );
2371 else if( !h->mb.b_direct_auto_write )
2373 /* Conditioning the probe on neighboring block types
2374 * doesn't seem to help speed or quality. */
2375 b_skip = x264_macroblock_probe_bskip( h );
2381 const unsigned int flags = h->param.analyse.inter;
2385 x264_mb_analyse_load_costs( h, &analysis );
2387 /* select best inter mode */
2388 /* direct must be first */
2389 if( analysis.b_direct_available )
2390 x264_mb_analyse_inter_direct( h, &analysis );
2392 x264_mb_analyse_inter_b16x16( h, &analysis );
2395 i_partition = D_16x16;
2396 i_cost = analysis.l0.me16x16.cost;
2397 COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2398 COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2399 COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2401 if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2403 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2404 if( i_bskip_cost < analysis.i_rd16x16direct &&
2405 i_bskip_cost < analysis.i_rd16x16bi &&
2406 i_bskip_cost < analysis.l0.i_rd16x16 &&
2407 i_bskip_cost < analysis.l1.i_rd16x16 )
2409 h->mb.i_type = B_SKIP;
2410 x264_analyse_update_cache( h, &analysis );
2415 if( flags & X264_ANALYSE_BSUB16x16 )
2417 x264_mb_analyse_inter_b8x8( h, &analysis );
2418 if( analysis.i_cost8x8bi < i_cost )
2421 i_partition = D_8x8;
2422 i_cost = analysis.i_cost8x8bi;
2424 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2425 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2427 x264_mb_analyse_inter_b16x8( h, &analysis );
2428 COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2429 i_type, analysis.i_mb_type16x8,
2430 i_partition, D_16x8 );
2432 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2433 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2435 x264_mb_analyse_inter_b8x16( h, &analysis );
2436 COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2437 i_type, analysis.i_mb_type8x16,
2438 i_partition, D_8x16 );
2443 if( analysis.b_mbrd )
2448 else if( i_partition == D_16x16 )
2450 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2451 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2452 if( i_type == B_L0_L0 )
2454 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2455 i_cost = analysis.l0.me16x16.cost
2456 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2458 else if( i_type == B_L1_L1 )
2460 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2461 i_cost = analysis.l1.me16x16.cost
2462 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2464 else if( i_type == B_BI_BI )
2466 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2467 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2470 else if( i_partition == D_16x8 )
2472 for( i=0; i<2; i++ )
2474 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2475 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2476 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2477 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2480 else if( i_partition == D_8x16 )
2482 for( i=0; i<2; i++ )
2484 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2485 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2486 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2487 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2490 else if( i_partition == D_8x8 )
2492 for( i=0; i<4; i++ )
2495 int i_part_cost_old;
2497 int i_part_type = h->mb.i_sub_partition[i];
2498 int b_bidir = (i_part_type == D_BI_8x8);
2500 if( i_part_type == D_DIRECT_8x8 )
2502 if( x264_mb_partition_listX_table[0][i_part_type] )
2504 m = &analysis.l0.me8x8[i];
2505 i_part_cost_old = m->cost;
2506 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2507 m->cost -= i_type_cost;
2508 x264_me_refine_qpel( h, m );
2510 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2512 if( x264_mb_partition_listX_table[1][i_part_type] )
2514 m = &analysis.l1.me8x8[i];
2515 i_part_cost_old = m->cost;
2516 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2517 m->cost -= i_type_cost;
2518 x264_me_refine_qpel( h, m );
2520 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2522 /* TODO: update mvp? */
2526 x264_mb_analyse_intra( h, &analysis, i_cost );
2528 if( analysis.b_mbrd )
2530 int i_satd_inter = i_cost;
2531 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2533 i_cost = i_bskip_cost;
2534 i_partition = D_16x16;
2535 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2536 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2537 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2538 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2539 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2540 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2541 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2543 h->mb.i_type = i_type;
2544 h->mb.i_partition = i_partition;
2545 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2546 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2549 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2550 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2551 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2553 h->mb.i_type = i_type;
2554 h->mb.i_partition = i_partition;
2556 if( h->param.analyse.b_bidir_me )
2557 refine_bidir( h, &analysis );
2561 x264_analyse_update_cache( h, &analysis );
2563 if( !analysis.b_mbrd )
2564 x264_mb_analyse_transform( h );
2566 h->mb.b_trellis = h->param.analyse.i_trellis;
2567 h->mb.b_noise_reduction = h->param.analyse.i_noise_reduction;
2570 /*-------------------- Update MB from the analysis ----------------------*/
2571 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2575 switch( h->mb.i_type )
2578 for( i = 0; i < 16; i++ )
2579 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2581 x264_mb_analyse_intra_chroma( h, a );
2584 for( i = 0; i < 4; i++ )
2585 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2587 x264_mb_analyse_intra_chroma( h, a );
2590 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2591 x264_mb_analyse_intra_chroma( h, a );
2595 switch( h->mb.i_partition )
2598 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2599 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2603 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2604 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2605 x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv[0], a->l0.me16x8[0].mv[1] );
2606 x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv[0], a->l0.me16x8[1].mv[1] );
2610 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2611 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2612 x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv[0], a->l0.me8x16[0].mv[1] );
2613 x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv[0], a->l0.me8x16[1].mv[1] );
2617 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2623 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2624 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2625 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2626 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2627 for( i = 0; i < 4; i++ )
2628 x264_mb_cache_mv_p8x8( h, a, i );
2633 h->mb.i_partition = D_16x16;
2634 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2635 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv[0],
2636 h->mb.cache.pskip_mv[1] );
2642 x264_mb_load_mv_direct8x8( h, 0 );
2643 x264_mb_load_mv_direct8x8( h, 1 );
2644 x264_mb_load_mv_direct8x8( h, 2 );
2645 x264_mb_load_mv_direct8x8( h, 3 );
2649 /* optimize: cache might not need to be rewritten */
2650 for( i = 0; i < 4; i++ )
2651 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2654 default: /* the rest of the B types */
2655 switch( h->mb.i_partition )
2658 switch( h->mb.i_type )
2661 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2662 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2664 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2665 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0, 0 );
2666 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0, 0 );
2669 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2670 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0, 0 );
2671 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0, 0 );
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] );
2677 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2678 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2680 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2681 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2686 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2687 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2690 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2691 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2694 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2700 if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2703 for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2706 int ref = h->mb.cache.ref[l][x264_scan8[0]];
2709 completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2710 if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2712 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2713 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2714 h->mb.cache.mv[l][x264_scan8[15]][0],
2715 h->mb.cache.mv[l][x264_scan8[15]][1] );
2716 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2717 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2718 fprintf(stderr, "completed: %d \n", completed );
2726 #include "slicetype.c"