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 uint16_t *x264_cost_mv_fpel[52][4];
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];
178 if( !p_cost_mv[a->i_qp] )
180 /* could be faster, but isn't called many times */
181 /* 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 );
190 a->p_cost_mv = p_cost_mv[a->i_qp];
192 /* FIXME is this useful for all me methods? */
193 if( h->param.analyse.i_me_method >= X264_ME_ESA && !x264_cost_mv_fpel[a->i_qp][0] )
197 x264_cost_mv_fpel[a->i_qp][j] = x264_malloc( (4*2048 + 1) * sizeof(int16_t) );
198 x264_cost_mv_fpel[a->i_qp][j] += 2*2048;
199 for( i = -2*2048; i < 2*2048; i++ )
200 x264_cost_mv_fpel[a->i_qp][j][i] = p_cost_mv[a->i_qp][i*4+j];
205 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
207 memset( a, 0, sizeof( x264_mb_analysis_t ) );
209 /* conduct the analysis using this lamda and QP */
210 a->i_qp = h->mb.i_qp = i_qp;
211 h->mb.i_chroma_qp = i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )];
212 a->i_lambda = i_qp0_cost_table[i_qp];
213 a->i_lambda2 = i_qp0_cost2_table[i_qp];
214 a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
215 ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
217 h->mb.i_me_method = h->param.analyse.i_me_method;
218 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
219 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
220 && h->mb.i_subpel_refine >= 5;
221 h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
222 h->mb.b_transform_8x8 = 0;
223 h->mb.b_noise_reduction = 0;
229 a->i_satd_i8x8chroma = COST_MAX;
233 /* II: Inter part P/B frame */
234 if( h->sh.i_type != SLICE_TYPE_I )
237 int i_fmv_range = 4 * h->param.analyse.i_mv_range;
238 // limit motion search to a slightly smaller range than the theoretical limit,
239 // since the search may go a few iterations past its given range
240 int i_fpel_border = 5; // umh unconditional radius
241 int i_spel_border = 8; // 1.5 for subpel_satd, 1.5 for subpel_rd, 2 for bime, round up
243 /* Calculate max allowed MV range */
244 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range-1 )
245 h->mb.mv_min[0] = 4*( -16*h->mb.i_mb_x - 24 );
246 h->mb.mv_max[0] = 4*( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 24 );
247 h->mb.mv_min_spel[0] = CLIP_FMV( h->mb.mv_min[0] );
248 h->mb.mv_max_spel[0] = CLIP_FMV( h->mb.mv_max[0] );
249 h->mb.mv_min_fpel[0] = (h->mb.mv_min_spel[0]>>2) + i_fpel_border;
250 h->mb.mv_max_fpel[0] = (h->mb.mv_max_spel[0]>>2) - i_fpel_border;
251 if( h->mb.i_mb_x == 0)
253 int mb_y = h->mb.i_mb_y >> h->sh.b_mbaff;
254 int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff;
255 int thread_mvy_range = i_fmv_range;
257 if( h->param.i_threads > 1 )
259 int pix_y = (h->mb.i_mb_y | h->mb.b_interlaced) * 16;
260 int thresh = pix_y + h->param.analyse.i_mv_range_thread;
261 for( i = (h->sh.i_type == SLICE_TYPE_B); i >= 0; i-- )
263 x264_frame_t **fref = i ? h->fref1 : h->fref0;
264 int i_ref = i ? h->i_ref1 : h->i_ref0;
265 for( j=0; j<i_ref; j++ )
267 x264_frame_cond_wait( fref[j], thresh );
268 thread_mvy_range = X264_MIN( thread_mvy_range, fref[j]->i_lines_completed - pix_y );
271 if( h->param.b_deterministic )
272 thread_mvy_range = h->param.analyse.i_mv_range_thread;
273 if( h->mb.b_interlaced )
274 thread_mvy_range >>= 1;
277 h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
278 h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
279 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 );
280 h->mb.mv_max_spel[1] = CLIP_FMV( h->mb.mv_max[1] );
281 h->mb.mv_max_spel[1] = X264_MIN( h->mb.mv_max_spel[1], thread_mvy_range*4 );
282 h->mb.mv_min_fpel[1] = (h->mb.mv_min_spel[1]>>2) + i_fpel_border;
283 h->mb.mv_max_fpel[1] = (h->mb.mv_max_spel[1]>>2) - i_fpel_border;
289 a->l0.i_cost8x8 = COST_MAX;
291 for( i = 0; i < 4; i++ )
295 a->l0.i_cost4x8[i] = COST_MAX;
299 a->l0.i_cost8x16 = COST_MAX;
300 if( h->sh.i_type == SLICE_TYPE_B )
304 a->l1.i_cost8x8 = COST_MAX;
306 for( i = 0; i < 4; i++ )
311 a->i_cost8x8direct[i] = COST_MAX;
322 a->i_cost16x16direct =
325 a->i_cost8x16bi = COST_MAX;
328 /* Fast intra decision */
329 if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
331 if( IS_INTRA( h->mb.i_mb_type_left )
332 || IS_INTRA( h->mb.i_mb_type_top )
333 || IS_INTRA( h->mb.i_mb_type_topleft )
334 || IS_INTRA( h->mb.i_mb_type_topright )
335 || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
336 || (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])) )
337 { /* intra is likely */ }
352 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
354 if( i_neighbour & MB_TOPLEFT )
356 /* top and left available */
357 *mode++ = I_PRED_16x16_V;
358 *mode++ = I_PRED_16x16_H;
359 *mode++ = I_PRED_16x16_DC;
360 *mode++ = I_PRED_16x16_P;
363 else if( i_neighbour & MB_LEFT )
366 *mode++ = I_PRED_16x16_DC_LEFT;
367 *mode++ = I_PRED_16x16_H;
370 else if( i_neighbour & MB_TOP )
373 *mode++ = I_PRED_16x16_DC_TOP;
374 *mode++ = I_PRED_16x16_V;
380 *mode = I_PRED_16x16_DC_128;
386 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
388 if( i_neighbour & MB_TOPLEFT )
390 /* top and left available */
391 *mode++ = I_PRED_CHROMA_V;
392 *mode++ = I_PRED_CHROMA_H;
393 *mode++ = I_PRED_CHROMA_DC;
394 *mode++ = I_PRED_CHROMA_P;
397 else if( i_neighbour & MB_LEFT )
400 *mode++ = I_PRED_CHROMA_DC_LEFT;
401 *mode++ = I_PRED_CHROMA_H;
404 else if( i_neighbour & MB_TOP )
407 *mode++ = I_PRED_CHROMA_DC_TOP;
408 *mode++ = I_PRED_CHROMA_V;
414 *mode = I_PRED_CHROMA_DC_128;
420 static void predict_4x4_mode_available( unsigned int i_neighbour,
421 int *mode, int *pi_count )
423 int b_l = i_neighbour & MB_LEFT;
424 int b_t = i_neighbour & MB_TOP;
429 *mode++ = I_PRED_4x4_DC;
430 *mode++ = I_PRED_4x4_H;
431 *mode++ = I_PRED_4x4_V;
432 *mode++ = I_PRED_4x4_DDL;
433 if( i_neighbour & MB_TOPLEFT )
435 *mode++ = I_PRED_4x4_DDR;
436 *mode++ = I_PRED_4x4_VR;
437 *mode++ = I_PRED_4x4_HD;
440 *mode++ = I_PRED_4x4_VL;
441 *mode++ = I_PRED_4x4_HU;
445 *mode++ = I_PRED_4x4_DC_LEFT;
446 *mode++ = I_PRED_4x4_H;
447 *mode++ = I_PRED_4x4_HU;
452 *mode++ = I_PRED_4x4_DC_TOP;
453 *mode++ = I_PRED_4x4_V;
454 *mode++ = I_PRED_4x4_DDL;
455 *mode++ = I_PRED_4x4_VL;
460 *mode++ = I_PRED_4x4_DC_128;
465 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
472 uint8_t *p_dstc[2], *p_srcc[2];
474 if( a->i_satd_i8x8chroma < COST_MAX )
477 /* 8x8 prediction selection for chroma */
478 p_dstc[0] = h->mb.pic.p_fdec[1];
479 p_dstc[1] = h->mb.pic.p_fdec[2];
480 p_srcc[0] = h->mb.pic.p_fenc[1];
481 p_srcc[1] = h->mb.pic.p_fenc[2];
483 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
484 a->i_satd_i8x8chroma = COST_MAX;
485 if( i_max == 4 && h->pixf.intra_satd_x3_8x8c && h->pixf.mbcmp[0] == h->pixf.satd[0] )
487 int satdu[4], satdv[4];
488 h->pixf.intra_satd_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
489 h->pixf.intra_satd_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
490 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
491 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
492 satdu[I_PRED_CHROMA_P] =
493 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
494 satdv[I_PRED_CHROMA_P] =
495 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
497 for( i=0; i<i_max; i++ )
499 int i_mode = predict_mode[i];
500 int i_satd = satdu[i_mode] + satdv[i_mode]
501 + a->i_lambda * bs_size_ue(i_mode);
503 a->i_satd_i8x8chroma_dir[i] = i_satd;
504 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
509 for( i=0; i<i_max; i++ )
512 int i_mode = predict_mode[i];
514 /* we do the prediction */
515 h->predict_8x8c[i_mode]( p_dstc[0] );
516 h->predict_8x8c[i_mode]( p_dstc[1] );
518 /* we calculate the cost */
519 i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
520 p_srcc[0], FENC_STRIDE ) +
521 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
522 p_srcc[1], FENC_STRIDE ) +
523 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
525 a->i_satd_i8x8chroma_dir[i] = i_satd;
526 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
530 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
533 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
535 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
536 uint8_t *p_src = h->mb.pic.p_fenc[0];
537 uint8_t *p_dst = h->mb.pic.p_fdec[0];
542 int b_merged_satd = h->pixf.intra_satd_x3_16x16 && h->pixf.mbcmp[0] == h->pixf.satd[0];
544 /*---------------- Try all mode and calculate their score ---------------*/
546 /* 16x16 prediction selection */
547 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
549 if( b_merged_satd && i_max == 4 )
551 h->pixf.intra_satd_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
552 h->predict_16x16[I_PRED_16x16_P]( p_dst );
553 a->i_satd_i16x16_dir[I_PRED_16x16_P] =
554 h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
557 int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
558 COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
563 for( i = 0; i < i_max; i++ )
566 int i_mode = predict_mode[i];
567 h->predict_16x16[i_mode]( p_dst );
569 i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
570 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
571 COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
572 a->i_satd_i16x16_dir[i_mode] = i_satd;
576 if( h->sh.i_type == SLICE_TYPE_B )
577 /* cavlc mb type prefix */
578 a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
579 if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
582 /* 8x8 prediction selection */
583 if( flags & X264_ANALYSE_I8x8 )
585 DECLARE_ALIGNED( uint8_t, edge[33], 8 );
586 x264_pixel_cmp_t sa8d = (*h->pixf.mbcmp == *h->pixf.sad) ? h->pixf.sad[PIXEL_8x8] : h->pixf.sa8d[PIXEL_8x8];
587 int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
589 b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
591 // FIXME some bias like in i4x4?
592 if( h->sh.i_type == SLICE_TYPE_B )
593 i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
595 for( idx = 0;; idx++ )
599 uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
600 uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
601 int i_best = COST_MAX;
602 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
604 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
605 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
607 if( b_merged_satd && i_max == 9 )
610 h->pixf.intra_sa8d_x3_8x8( p_src_by, edge, satd );
611 if( i_pred_mode < 3 )
612 satd[i_pred_mode] -= 3 * a->i_lambda;
613 for( i=2; i>=0; i-- )
615 int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
616 COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
623 for( ; i<i_max; i++ )
626 int i_mode = predict_mode[i];
628 h->predict_8x8[i_mode]( p_dst_by, edge );
630 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
631 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
633 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
634 a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
638 if( idx == 3 || i_cost > i_satd_thresh )
641 /* we need to encode this block now (for next ones) */
642 h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
643 x264_mb_encode_i8x8( h, idx, a->i_qp );
645 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
649 a->i_satd_i8x8 = i_cost;
652 a->i_satd_i8x8 = COST_MAX;
653 i_cost = i_cost * 4/(idx+1);
655 if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 )
659 /* 4x4 prediction selection */
660 if( flags & X264_ANALYSE_I4x4 )
663 int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
664 b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
666 i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
668 i_cost = a->i_lambda * 24; /* from JVT (SATD0) */
669 if( h->sh.i_type == SLICE_TYPE_B )
670 i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];
672 for( idx = 0;; idx++ )
674 int x = block_idx_x[idx];
675 int y = block_idx_y[idx];
676 uint8_t *p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
677 uint8_t *p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
678 int i_best = COST_MAX;
679 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
681 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
683 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
684 /* emulate missing topright samples */
685 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
687 if( b_merged_satd && i_max >= 6 )
690 h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
691 if( i_pred_mode < 3 )
692 satd[i_pred_mode] -= 3 * a->i_lambda;
693 for( i=2; i>=0; i-- )
694 COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
695 a->i_predict4x4[idx], i );
701 for( ; i<i_max; i++ )
704 int i_mode = predict_mode[i];
706 h->predict_4x4[i_mode]( p_dst_by );
708 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
709 p_src_by, FENC_STRIDE )
710 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
712 COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
716 if( i_cost > i_satd_thresh || idx == 15 )
719 /* we need to encode this block now (for next ones) */
720 h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
721 x264_mb_encode_i4x4( h, idx, a->i_qp );
723 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
726 a->i_satd_i4x4 = i_cost;
728 a->i_satd_i4x4 = COST_MAX;
732 static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
734 if( a->i_satd_i16x16 <= i_satd_thresh )
736 h->mb.i_type = I_16x16;
737 x264_analyse_update_cache( h, a );
738 a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
741 a->i_satd_i16x16 = COST_MAX;
743 if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
745 h->mb.i_type = I_4x4;
746 x264_analyse_update_cache( h, a );
747 a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
750 a->i_satd_i4x4 = COST_MAX;
752 if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
754 h->mb.i_type = I_8x8;
755 x264_analyse_update_cache( h, a );
756 a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
759 a->i_satd_i8x8 = COST_MAX;
762 static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
764 uint8_t *p_src = h->mb.pic.p_fenc[0];
765 uint8_t *p_dst = h->mb.pic.p_fdec[0];
768 int i_max, i_satd, i_best, i_mode, i_thresh;
772 if( h->mb.i_type == I_16x16 )
774 int old_pred_mode = a->i_predict16x16;
775 i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
776 i_best = a->i_satd_i16x16;
777 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
778 for( i = 0; i < i_max; i++ )
780 int i_mode = predict_mode[i];
781 if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
783 h->mb.i_intra16x16_pred_mode = i_mode;
784 i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
785 COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
788 else if( h->mb.i_type == I_4x4 )
790 uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
792 for( idx = 0; idx < 16; idx++ )
798 i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
799 x = block_idx_x[idx];
800 y = block_idx_y[idx];
802 p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
803 p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
804 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
806 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
807 /* emulate missing topright samples */
808 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
810 for( i = 0; i < i_max; i++ )
812 i_mode = predict_mode[i];
813 h->predict_4x4[i_mode]( p_dst_by );
814 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
816 if( i_best > i_satd )
818 a->i_predict4x4[idx] = i_mode;
820 pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
821 pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
822 pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
823 pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
824 i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
828 *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
829 *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
830 *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
831 *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
832 h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
834 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
837 else if( h->mb.i_type == I_8x8 )
839 DECLARE_ALIGNED( uint8_t, edge[33], 8 );
840 for( idx = 0; idx < 4; idx++ )
848 i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
851 i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
855 p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
856 p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
857 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
858 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
860 for( i = 0; i < i_max; i++ )
862 i_mode = predict_mode[i];
863 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
865 h->predict_8x8[i_mode]( p_dst_by, edge );
866 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
868 if( i_best > i_satd )
870 a->i_predict8x8[idx] = i_mode;
873 pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
876 pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
878 i_nnz[j] = h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]];
882 *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
885 p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
887 h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]] = i_nnz[j];
889 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
893 /* RD selection for chroma prediction */
894 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
897 i_thresh = a->i_satd_i8x8chroma * 5/4;
899 for( i = j = 0; i < i_max; i++ )
900 if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
901 predict_mode[i] != a->i_predict8x8chroma )
903 predict_mode[j++] = predict_mode[i];
909 int i_chroma_lambda = i_qp0_cost2_table[h->mb.i_chroma_qp];
910 /* the previous thing encoded was x264_intra_rd(), so the pixels and
911 * coefs for the current chroma mode are still around, so we only
912 * have to recount the bits. */
913 i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
914 for( i = 0; i < i_max; i++ )
916 i_mode = predict_mode[i];
917 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
918 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
919 /* if we've already found a mode that needs no residual, then
920 * probably any mode with a residual will be worse.
921 * so avoid dct on the remaining modes to improve speed. */
922 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
923 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode );
925 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
930 #define LOAD_FENC( m, src, xoff, yoff) \
931 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
932 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
933 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
934 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
935 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
937 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
938 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
939 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
940 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
941 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
942 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
943 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
944 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
946 #define REF_COST(list, ref) \
947 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
949 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
953 int mvc[7][2], i_mvc;
954 int i_halfpel_thresh = INT_MAX;
955 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
957 /* 16x16 Search on all ref frame */
958 m.i_pixel = PIXEL_16x16;
959 m.p_cost_mv = a->p_cost_mv;
960 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
962 a->l0.me16x16.cost = INT_MAX;
963 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
965 const int i_ref_cost = REF_COST( 0, i_ref );
966 i_halfpel_thresh -= i_ref_cost;
967 m.i_ref_cost = i_ref_cost;
970 /* search with ref */
971 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
972 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
973 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
974 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
977 * SSD threshold would probably be better than SATD */
980 && m.cost-m.cost_mv < 300*a->i_lambda
981 && abs(m.mv[0]-h->mb.cache.pskip_mv[0])
982 + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
983 && x264_macroblock_probe_pskip( h ) )
985 h->mb.i_type = P_SKIP;
986 x264_analyse_update_cache( h, a );
987 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
991 m.cost += i_ref_cost;
992 i_halfpel_thresh += i_ref_cost;
994 if( m.cost < a->l0.me16x16.cost )
997 /* save mv for predicting neighbors */
998 a->l0.mvc[i_ref][0][0] =
999 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1000 a->l0.mvc[i_ref][0][1] =
1001 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1004 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
1005 assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1007 h->mb.i_type = P_L0;
1008 if( a->b_mbrd && a->l0.i_ref == 0
1009 && a->l0.me16x16.mv[0] == h->mb.cache.pskip_mv[0]
1010 && a->l0.me16x16.mv[1] == h->mb.cache.pskip_mv[1] )
1012 h->mb.i_partition = D_16x16;
1013 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
1014 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1018 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
1022 uint8_t **p_fenc = h->mb.pic.p_fenc;
1023 int i_halfpel_thresh = INT_MAX;
1024 int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
1026 int i_maxref = h->mb.pic.i_fref[0]-1;
1028 h->mb.i_partition = D_8x8;
1030 /* early termination: if 16x16 chose ref 0, then evalute no refs older
1031 * than those used by the neighbors */
1032 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
1033 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
1036 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
1037 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
1038 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
1039 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
1040 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
1041 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
1044 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1046 a->l0.mvc[i_ref][0][0] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0];
1047 a->l0.mvc[i_ref][0][1] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1];
1050 for( i = 0; i < 4; i++ )
1052 x264_me_t *l0m = &a->l0.me8x8[i];
1056 m.i_pixel = PIXEL_8x8;
1057 m.p_cost_mv = a->p_cost_mv;
1059 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
1060 l0m->cost = INT_MAX;
1061 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1063 const int i_ref_cost = REF_COST( 0, i_ref );
1064 i_halfpel_thresh -= i_ref_cost;
1065 m.i_ref_cost = i_ref_cost;
1068 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
1069 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
1070 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1071 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
1073 m.cost += i_ref_cost;
1074 i_halfpel_thresh += i_ref_cost;
1075 *(uint64_t*)a->l0.mvc[i_ref][i+1] = *(uint64_t*)m.mv;
1077 if( m.cost < l0m->cost )
1080 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv[0], l0m->mv[1] );
1081 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1084 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1087 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1088 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1089 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1090 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1093 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1095 const int i_ref = a->l0.me16x16.i_ref;
1096 const int i_ref_cost = REF_COST( 0, i_ref );
1097 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
1098 uint8_t **p_fenc = h->mb.pic.p_fenc;
1100 int (*mvc)[2] = a->l0.mvc[i_ref];
1103 /* XXX Needed for x264_mb_predict_mv */
1104 h->mb.i_partition = D_8x8;
1107 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.me16x16.mv;
1109 for( i = 0; i < 4; i++ )
1111 x264_me_t *m = &a->l0.me8x8[i];
1115 m->i_pixel = PIXEL_8x8;
1116 m->p_cost_mv = a->p_cost_mv;
1117 m->i_ref_cost = i_ref_cost;
1120 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1121 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1122 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1123 x264_me_search( h, m, mvc, i_mvc );
1125 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
1127 *(uint64_t*)mvc[i_mvc] = *(uint64_t*)m->mv;
1131 m->cost += i_ref_cost;
1132 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1135 /* theoretically this should include 4*ref_cost,
1136 * but 3 seems a better approximation of cabac. */
1137 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1138 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
1139 REF_COST( 0, a->l0.me16x16.i_ref );
1140 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1141 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1144 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
1147 uint8_t **p_fenc = h->mb.pic.p_fenc;
1151 /* XXX Needed for x264_mb_predict_mv */
1152 h->mb.i_partition = D_16x8;
1154 for( i = 0; i < 2; i++ )
1156 x264_me_t *l0m = &a->l0.me16x8[i];
1157 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1158 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1160 m.i_pixel = PIXEL_16x8;
1161 m.p_cost_mv = a->p_cost_mv;
1163 LOAD_FENC( &m, p_fenc, 0, 8*i );
1164 l0m->cost = INT_MAX;
1165 for( j = 0; j < i_ref8s; j++ )
1167 const int i_ref = ref8[j];
1168 const int i_ref_cost = REF_COST( 0, i_ref );
1169 m.i_ref_cost = i_ref_cost;
1172 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1173 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1174 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][2*i+1];
1175 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][2*i+2];
1177 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1178 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1179 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1180 x264_me_search( h, &m, mvc, 3 );
1182 m.cost += i_ref_cost;
1184 if( m.cost < l0m->cost )
1187 x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, l0m->mv[0], l0m->mv[1] );
1188 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1191 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1194 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1197 uint8_t **p_fenc = h->mb.pic.p_fenc;
1201 /* XXX Needed for x264_mb_predict_mv */
1202 h->mb.i_partition = D_8x16;
1204 for( i = 0; i < 2; i++ )
1206 x264_me_t *l0m = &a->l0.me8x16[i];
1207 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1208 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1210 m.i_pixel = PIXEL_8x16;
1211 m.p_cost_mv = a->p_cost_mv;
1213 LOAD_FENC( &m, p_fenc, 8*i, 0 );
1214 l0m->cost = INT_MAX;
1215 for( j = 0; j < i_ref8s; j++ )
1217 const int i_ref = ref8[j];
1218 const int i_ref_cost = REF_COST( 0, i_ref );
1219 m.i_ref_cost = i_ref_cost;
1222 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1223 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][i+1];
1224 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][i+3];
1226 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1227 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1228 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1229 x264_me_search( h, &m, mvc, 3 );
1231 m.cost += i_ref_cost;
1233 if( m.cost < l0m->cost )
1236 x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, l0m->mv[0], l0m->mv[1] );
1237 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1240 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1243 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1245 DECLARE_ALIGNED( uint8_t, pix1[16*8], 8 );
1246 uint8_t *pix2 = pix1+8;
1247 const int i_stride = h->mb.pic.i_stride[1];
1248 const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1249 const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1251 #define CHROMA4x4MC( width, height, me, x, y ) \
1252 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 ); \
1253 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 );
1255 if( pixel == PIXEL_4x4 )
1257 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1258 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 2,0 );
1259 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 0,2 );
1260 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1262 else if( pixel == PIXEL_8x4 )
1264 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1265 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1269 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1270 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1273 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1274 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1277 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1279 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1280 uint8_t **p_fenc = h->mb.pic.p_fenc;
1281 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1284 /* XXX Needed for x264_mb_predict_mv */
1285 h->mb.i_partition = D_8x8;
1287 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1289 const int idx = 4*i8x8 + i4x4;
1290 const int x4 = block_idx_x[idx];
1291 const int y4 = block_idx_y[idx];
1292 const int i_mvc = (i4x4 == 0);
1294 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1296 m->i_pixel = PIXEL_4x4;
1297 m->p_cost_mv = a->p_cost_mv;
1299 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1300 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1302 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1303 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1305 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
1307 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1308 a->l0.me4x4[i8x8][1].cost +
1309 a->l0.me4x4[i8x8][2].cost +
1310 a->l0.me4x4[i8x8][3].cost +
1311 REF_COST( 0, i_ref ) +
1312 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1313 if( h->mb.b_chroma_me )
1314 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1317 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1319 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1320 uint8_t **p_fenc = h->mb.pic.p_fenc;
1321 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1324 /* XXX Needed for x264_mb_predict_mv */
1325 h->mb.i_partition = D_8x8;
1327 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1329 const int idx = 4*i8x8 + 2*i8x4;
1330 const int x4 = block_idx_x[idx];
1331 const int y4 = block_idx_y[idx];
1332 const int i_mvc = (i8x4 == 0);
1334 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1336 m->i_pixel = PIXEL_8x4;
1337 m->p_cost_mv = a->p_cost_mv;
1339 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1340 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1342 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1343 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1345 x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
1347 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1348 REF_COST( 0, i_ref ) +
1349 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1350 if( h->mb.b_chroma_me )
1351 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1354 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1356 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1357 uint8_t **p_fenc = h->mb.pic.p_fenc;
1358 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1361 /* XXX Needed for x264_mb_predict_mv */
1362 h->mb.i_partition = D_8x8;
1364 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1366 const int idx = 4*i8x8 + i4x8;
1367 const int x4 = block_idx_x[idx];
1368 const int y4 = block_idx_y[idx];
1369 const int i_mvc = (i4x8 == 0);
1371 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1373 m->i_pixel = PIXEL_4x8;
1374 m->p_cost_mv = a->p_cost_mv;
1376 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1377 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1379 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1380 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1382 x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
1384 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1385 REF_COST( 0, i_ref ) +
1386 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1387 if( h->mb.b_chroma_me )
1388 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1391 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1393 /* Assumes that fdec still contains the results of
1394 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1396 uint8_t **p_fenc = h->mb.pic.p_fenc;
1397 uint8_t **p_fdec = h->mb.pic.p_fdec;
1400 a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1401 for( i = 0; i < 4; i++ )
1403 const int x = (i&1)*8;
1404 const int y = (i>>1)*8;
1405 a->i_cost16x16direct +=
1406 a->i_cost8x8direct[i] =
1407 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1410 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1414 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1416 if( h->param.analyse.b_weighted_bipred ) \
1417 h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1418 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1420 h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1423 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1425 uint8_t pix1[16*16], pix2[16*16];
1432 int mvc[8][2], i_mvc;
1433 int i_halfpel_thresh = INT_MAX;
1434 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1436 /* 16x16 Search on all ref frame */
1437 m.i_pixel = PIXEL_16x16;
1438 m.p_cost_mv = a->p_cost_mv;
1439 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1442 a->l0.me16x16.cost = INT_MAX;
1443 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1445 /* search with ref */
1446 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1447 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1448 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1449 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1452 m.cost += REF_COST( 0, i_ref );
1454 if( m.cost < a->l0.me16x16.cost )
1456 a->l0.i_ref = i_ref;
1460 /* save mv for predicting neighbors */
1461 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1462 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1464 /* subtract ref cost, so we don't have to add it for the other MB types */
1465 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1468 i_halfpel_thresh = INT_MAX;
1469 p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1470 a->l1.me16x16.cost = INT_MAX;
1471 for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1473 /* search with ref */
1474 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1475 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1476 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1477 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1480 m.cost += REF_COST( 1, i_ref );
1482 if( m.cost < a->l1.me16x16.cost )
1484 a->l1.i_ref = i_ref;
1488 /* save mv for predicting neighbors */
1489 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1490 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1492 /* subtract ref cost, so we don't have to add it for the other MB types */
1493 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1495 /* Set global ref, needed for other modes? */
1496 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1497 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1499 /* get cost of BI mode */
1500 weight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1501 if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
1503 /* l0 reference is halfpel, so get_ref on it will make it faster */
1505 h->mc.get_ref( pix2, &stride2,
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],
1509 h->mc.mc_luma( pix1, 16,
1510 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1511 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1513 weight = 64 - weight;
1517 /* if l0 was qpel, we'll use get_ref on l1 instead */
1518 h->mc.mc_luma( pix1, 16,
1519 h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1520 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1523 h->mc.get_ref( pix2, &stride2,
1524 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1525 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1529 if( h->param.analyse.b_weighted_bipred )
1530 h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2, weight );
1532 h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1534 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix1, 16 )
1535 + REF_COST( 0, a->l0.i_ref )
1536 + REF_COST( 1, a->l1.i_ref )
1537 + a->l0.me16x16.cost_mv
1538 + a->l1.me16x16.cost_mv;
1541 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1542 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1543 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1546 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1548 const int x = 2*(i%2);
1549 const int y = 2*(i/2);
1551 switch( h->mb.i_sub_partition[i] )
1554 x264_macroblock_cache_mv( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv[0], a->l0.me8x8[i].mv[1] );
1557 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] );
1558 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] );
1561 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] );
1562 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] );
1565 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] );
1566 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] );
1567 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] );
1568 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] );
1571 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1576 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1577 if( x264_mb_partition_listX_table[0][part] ) \
1579 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1580 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
1584 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1585 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0, 0 ); \
1587 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
1589 if( x264_mb_partition_listX_table[1][part] ) \
1591 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1592 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
1596 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1597 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0, 0 ); \
1599 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
1602 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1606 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1608 x264_mb_load_mv_direct8x8( h, i );
1611 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0, 0 );
1612 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0, 0 );
1613 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1618 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1621 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1623 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1625 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1627 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1631 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1633 uint8_t **p_fref[2] =
1634 { h->mb.pic.p_fref[0][a->l0.i_ref],
1635 h->mb.pic.p_fref[1][a->l1.i_ref] };
1636 uint8_t pix[2][8*8];
1639 /* XXX Needed for x264_mb_predict_mv */
1640 h->mb.i_partition = D_8x8;
1644 for( i = 0; i < 4; i++ )
1649 int i_part_cost_bi = 0;
1651 for( l = 0; l < 2; l++ )
1653 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1654 x264_me_t *m = &lX->me8x8[i];
1656 m->i_pixel = PIXEL_8x8;
1657 m->p_cost_mv = a->p_cost_mv;
1659 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1660 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1662 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1663 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1665 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
1668 h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
1669 m->mv[0], m->mv[1], 8, 8 );
1670 i_part_cost_bi += m->cost_mv;
1671 /* FIXME: ref cost */
1674 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1675 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1676 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1677 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1678 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1680 i_part_cost = a->l0.me8x8[i].cost;
1681 h->mb.i_sub_partition[i] = D_L0_8x8;
1682 COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1683 COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1684 COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1685 a->i_cost8x8bi += i_part_cost;
1687 /* XXX Needed for x264_mb_predict_mv */
1688 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1692 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1695 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1697 uint8_t **p_fref[2] =
1698 { h->mb.pic.p_fref[0][a->l0.i_ref],
1699 h->mb.pic.p_fref[1][a->l1.i_ref] };
1700 DECLARE_ALIGNED( uint8_t, pix[2][16*8], 16 );
1704 h->mb.i_partition = D_16x8;
1705 a->i_cost16x8bi = 0;
1707 for( i = 0; i < 2; i++ )
1710 int i_part_cost_bi = 0;
1712 /* TODO: check only the list(s) that were used in b8x8? */
1713 for( l = 0; l < 2; l++ )
1715 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1716 x264_me_t *m = &lX->me16x8[i];
1718 m->i_pixel = PIXEL_16x8;
1719 m->p_cost_mv = a->p_cost_mv;
1721 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1722 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1724 mvc[0][0] = lX->me8x8[2*i].mv[0];
1725 mvc[0][1] = lX->me8x8[2*i].mv[1];
1726 mvc[1][0] = lX->me8x8[2*i+1].mv[0];
1727 mvc[1][1] = lX->me8x8[2*i+1].mv[1];
1729 x264_mb_predict_mv( h, l, 8*i, 2, m->mvp );
1730 x264_me_search( h, m, mvc, 2 );
1733 h->mc.mc_luma( pix[l], 16, m->p_fref, m->i_stride[0],
1734 m->mv[0], m->mv[1], 16, 8 );
1735 /* FIXME: ref cost */
1736 i_part_cost_bi += m->cost_mv;
1739 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1740 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1742 i_part_cost = a->l0.me16x8[i].cost;
1743 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1744 if( a->l1.me16x8[i].cost < i_part_cost )
1746 i_part_cost = a->l1.me16x8[i].cost;
1747 a->i_mb_partition16x8[i] = D_L1_8x8;
1749 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1751 i_part_cost = i_part_cost_bi;
1752 a->i_mb_partition16x8[i] = D_BI_8x8;
1754 a->i_cost16x8bi += i_part_cost;
1756 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1760 a->i_mb_type16x8 = B_L0_L0
1761 + (a->i_mb_partition16x8[0]>>2) * 3
1762 + (a->i_mb_partition16x8[1]>>2);
1763 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1766 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1768 uint8_t **p_fref[2] =
1769 { h->mb.pic.p_fref[0][a->l0.i_ref],
1770 h->mb.pic.p_fref[1][a->l1.i_ref] };
1771 uint8_t pix[2][8*16];
1775 h->mb.i_partition = D_8x16;
1776 a->i_cost8x16bi = 0;
1778 for( i = 0; i < 2; i++ )
1781 int i_part_cost_bi = 0;
1783 for( l = 0; l < 2; l++ )
1785 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1786 x264_me_t *m = &lX->me8x16[i];
1788 m->i_pixel = PIXEL_8x16;
1789 m->p_cost_mv = a->p_cost_mv;
1791 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1792 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1794 mvc[0][0] = lX->me8x8[i].mv[0];
1795 mvc[0][1] = lX->me8x8[i].mv[1];
1796 mvc[1][0] = lX->me8x8[i+2].mv[0];
1797 mvc[1][1] = lX->me8x8[i+2].mv[1];
1799 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1800 x264_me_search( h, m, mvc, 2 );
1803 h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
1804 m->mv[0], m->mv[1], 8, 16 );
1805 /* FIXME: ref cost */
1806 i_part_cost_bi += m->cost_mv;
1809 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1810 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1812 i_part_cost = a->l0.me8x16[i].cost;
1813 a->i_mb_partition8x16[i] = D_L0_8x8;
1814 if( a->l1.me8x16[i].cost < i_part_cost )
1816 i_part_cost = a->l1.me8x16[i].cost;
1817 a->i_mb_partition8x16[i] = D_L1_8x8;
1819 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1821 i_part_cost = i_part_cost_bi;
1822 a->i_mb_partition8x16[i] = D_BI_8x8;
1824 a->i_cost8x16bi += i_part_cost;
1826 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1830 a->i_mb_type8x16 = B_L0_L0
1831 + (a->i_mb_partition8x16[0]>>2) * 3
1832 + (a->i_mb_partition8x16[1]>>2);
1833 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1836 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1838 int thresh = i_satd * 5/4;
1840 h->mb.i_type = P_L0;
1841 if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1843 h->mb.i_partition = D_16x16;
1844 x264_analyse_update_cache( h, a );
1845 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1847 a->l0.me16x16.cost = a->l0.i_rd16x16;
1849 if( a->l0.i_cost16x8 <= thresh )
1851 h->mb.i_partition = D_16x8;
1852 x264_analyse_update_cache( h, a );
1853 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1856 a->l0.i_cost16x8 = COST_MAX;
1858 if( a->l0.i_cost8x16 <= thresh )
1860 h->mb.i_partition = D_8x16;
1861 x264_analyse_update_cache( h, a );
1862 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1865 a->l0.i_cost8x16 = COST_MAX;
1867 if( a->l0.i_cost8x8 <= thresh )
1869 h->mb.i_type = P_8x8;
1870 x264_analyse_update_cache( h, a );
1871 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1873 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1875 /* FIXME: RD per subpartition */
1879 for( i=0; i<4; i++ )
1881 part_bak[i] = h->mb.i_sub_partition[i];
1882 b_sub8x8 |= (part_bak[i] != D_L0_8x8);
1886 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1887 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1888 i_cost = x264_rd_cost_mb( h, a->i_lambda2 );
1889 if( a->l0.i_cost8x8 < i_cost )
1891 for( i=0; i<4; i++ )
1892 h->mb.i_sub_partition[i] = part_bak[i];
1895 a->l0.i_cost8x8 = i_cost;
1900 a->l0.i_cost8x8 = COST_MAX;
1903 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
1905 int thresh = i_satd_inter * 17/16;
1907 if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
1909 h->mb.i_type = B_DIRECT;
1910 x264_analyse_update_cache( h, a );
1911 a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
1914 //FIXME not all the update_cache calls are needed
1915 h->mb.i_partition = D_16x16;
1917 if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
1919 h->mb.i_type = B_L0_L0;
1920 x264_analyse_update_cache( h, a );
1921 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1925 if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
1927 h->mb.i_type = B_L1_L1;
1928 x264_analyse_update_cache( h, a );
1929 a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1933 if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
1935 h->mb.i_type = B_BI_BI;
1936 x264_analyse_update_cache( h, a );
1937 a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1941 if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
1943 h->mb.i_type = B_8x8;
1944 h->mb.i_partition = D_8x8;
1945 x264_analyse_update_cache( h, a );
1946 a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1947 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1951 if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
1953 h->mb.i_type = a->i_mb_type16x8;
1954 h->mb.i_partition = D_16x8;
1955 x264_analyse_update_cache( h, a );
1956 a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1960 if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
1962 h->mb.i_type = a->i_mb_type8x16;
1963 h->mb.i_partition = D_8x16;
1964 x264_analyse_update_cache( h, a );
1965 a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1969 static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
1971 const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1974 switch( h->mb.i_partition )
1977 if( h->mb.i_type == B_BI_BI )
1978 x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
1981 for( i=0; i<2; i++ )
1982 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
1983 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
1986 for( i=0; i<2; i++ )
1987 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
1988 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
1991 for( i=0; i<4; i++ )
1992 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
1993 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
1998 static inline void x264_mb_analyse_transform( x264_t *h )
2000 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2002 int i_cost4, i_cost8;
2003 /* FIXME only luma mc is needed */
2006 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2007 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2008 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2009 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2011 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2015 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2017 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2020 x264_analyse_update_cache( h, a );
2021 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2022 /* FIXME only luma is needed, but the score for comparison already includes chroma */
2023 i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2025 if( *i_rd >= i_rd8 )
2028 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2029 /* prevent a rare division by zero in estimated intra cost */
2036 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2041 /*****************************************************************************
2042 * x264_macroblock_analyse:
2043 *****************************************************************************/
2044 void x264_macroblock_analyse( x264_t *h )
2046 x264_mb_analysis_t analysis;
2047 int i_cost = COST_MAX;
2051 x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
2053 /*--------------------------- Do the analysis ---------------------------*/
2054 if( h->sh.i_type == SLICE_TYPE_I )
2056 x264_mb_analyse_intra( h, &analysis, COST_MAX );
2057 if( analysis.b_mbrd )
2058 x264_intra_rd( h, &analysis, COST_MAX );
2060 i_cost = analysis.i_satd_i16x16;
2061 h->mb.i_type = I_16x16;
2062 if( analysis.i_satd_i4x4 < i_cost )
2064 i_cost = analysis.i_satd_i4x4;
2065 h->mb.i_type = I_4x4;
2067 if( analysis.i_satd_i8x8 < i_cost )
2068 h->mb.i_type = I_8x8;
2070 if( h->mb.i_subpel_refine >= 7 )
2071 x264_intra_rd_refine( h, &analysis );
2073 else if( h->sh.i_type == SLICE_TYPE_P )
2076 int i_intra_cost, i_intra_type;
2078 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2080 /* Fast P_SKIP detection */
2081 analysis.b_try_pskip = 0;
2082 if( h->param.analyse.b_fast_pskip )
2084 if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2085 // FIXME don't need to check this if the reference frame is done
2087 else if( h->param.analyse.i_subpel_refine >= 3 )
2088 analysis.b_try_pskip = 1;
2089 else if( h->mb.i_mb_type_left == P_SKIP ||
2090 h->mb.i_mb_type_top == P_SKIP ||
2091 h->mb.i_mb_type_topleft == P_SKIP ||
2092 h->mb.i_mb_type_topright == P_SKIP )
2093 b_skip = x264_macroblock_probe_pskip( h );
2096 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2100 h->mb.i_type = P_SKIP;
2101 h->mb.i_partition = D_16x16;
2102 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2106 const unsigned int flags = h->param.analyse.inter;
2110 int i_satd_inter, i_satd_intra;
2112 x264_mb_analyse_load_costs( h, &analysis );
2114 x264_mb_analyse_inter_p16x16( h, &analysis );
2116 if( h->mb.i_type == P_SKIP )
2119 if( flags & X264_ANALYSE_PSUB16x16 )
2121 if( h->param.analyse.b_mixed_references )
2122 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2124 x264_mb_analyse_inter_p8x8( h, &analysis );
2127 /* Select best inter mode */
2129 i_partition = D_16x16;
2130 i_cost = analysis.l0.me16x16.cost;
2132 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2133 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2136 i_partition = D_8x8;
2137 i_cost = analysis.l0.i_cost8x8;
2140 if( flags & X264_ANALYSE_PSUB8x8 )
2142 for( i = 0; i < 4; i++ )
2144 x264_mb_analyse_inter_p4x4( h, &analysis, i );
2145 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2147 int i_cost8x8 = analysis.l0.i_cost4x4[i];
2148 h->mb.i_sub_partition[i] = D_L0_4x4;
2150 x264_mb_analyse_inter_p8x4( h, &analysis, i );
2151 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2152 h->mb.i_sub_partition[i], D_L0_8x4 );
2154 x264_mb_analyse_inter_p4x8( h, &analysis, i );
2155 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2156 h->mb.i_sub_partition[i], D_L0_4x8 );
2158 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2160 x264_mb_cache_mv_p8x8( h, &analysis, i );
2162 analysis.l0.i_cost8x8 = i_cost;
2166 /* Now do 16x8/8x16 */
2167 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2168 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2169 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2171 x264_mb_analyse_inter_p16x8( h, &analysis );
2172 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2174 x264_mb_analyse_inter_p8x16( h, &analysis );
2175 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2178 h->mb.i_partition = i_partition;
2181 //FIXME mb_type costs?
2182 if( analysis.b_mbrd )
2186 else if( i_partition == D_16x16 )
2188 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2189 i_cost = analysis.l0.me16x16.cost;
2191 else if( i_partition == D_16x8 )
2193 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2194 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2195 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2197 else if( i_partition == D_8x16 )
2199 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2200 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2201 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2203 else if( i_partition == D_8x8 )
2207 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2209 switch( h->mb.i_sub_partition[i8x8] )
2212 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2213 i_cost += analysis.l0.me8x8[i8x8].cost;
2216 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2217 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2218 i_cost += analysis.l0.me8x4[i8x8][0].cost +
2219 analysis.l0.me8x4[i8x8][1].cost;
2222 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2223 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2224 i_cost += analysis.l0.me4x8[i8x8][0].cost +
2225 analysis.l0.me4x8[i8x8][1].cost;
2229 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2230 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2231 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2232 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2233 i_cost += analysis.l0.me4x4[i8x8][0].cost +
2234 analysis.l0.me4x4[i8x8][1].cost +
2235 analysis.l0.me4x4[i8x8][2].cost +
2236 analysis.l0.me4x4[i8x8][3].cost;
2239 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2245 if( h->mb.b_chroma_me )
2247 x264_mb_analyse_intra_chroma( h, &analysis );
2248 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2249 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2250 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2251 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2254 x264_mb_analyse_intra( h, &analysis, i_cost );
2256 i_satd_inter = i_cost;
2257 i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2258 analysis.i_satd_i8x8,
2259 analysis.i_satd_i4x4 );
2261 if( analysis.b_mbrd )
2263 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2265 i_partition = D_16x16;
2266 i_cost = analysis.l0.me16x16.cost;
2267 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2268 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2269 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2270 h->mb.i_type = i_type;
2271 h->mb.i_partition = i_partition;
2272 if( i_cost < COST_MAX )
2273 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2274 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2277 i_intra_type = I_16x16;
2278 i_intra_cost = analysis.i_satd_i16x16;
2279 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
2280 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
2281 COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
2283 if( i_intra_cost == COST_MAX )
2284 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
2286 h->mb.i_type = i_type;
2287 h->stat.frame.i_intra_cost += i_intra_cost;
2288 h->stat.frame.i_inter_cost += i_cost;
2289 h->stat.frame.i_mbs_analysed++;
2291 if( h->mb.i_subpel_refine >= 7 )
2293 if( IS_INTRA( h->mb.i_type ) )
2295 x264_intra_rd_refine( h, &analysis );
2297 else if( i_partition == D_16x16 )
2299 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2300 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
2302 else if( i_partition == D_16x8 )
2304 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2305 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2306 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
2307 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
2309 else if( i_partition == D_8x16 )
2311 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2312 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2313 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
2314 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
2316 else if( i_partition == D_8x8 )
2319 x264_analyse_update_cache( h, &analysis );
2320 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2321 if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2322 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
2327 else if( h->sh.i_type == SLICE_TYPE_B )
2329 int i_bskip_cost = COST_MAX;
2332 h->mb.i_type = B_SKIP;
2333 if( h->mb.b_direct_auto_write )
2335 /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2336 for( i = 0; i < 2; i++ )
2339 h->sh.b_direct_spatial_mv_pred ^= 1;
2340 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2341 if( analysis.b_direct_available )
2346 b_skip = x264_macroblock_probe_bskip( h );
2348 h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2355 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2357 if( analysis.b_direct_available )
2359 if( !h->mb.b_direct_auto_write )
2361 if( h->mb.b_lossless )
2363 /* chance of skip is too small to bother */
2365 else if( analysis.b_mbrd )
2367 i_bskip_cost = ssd_mb( h );
2369 /* 6 = minimum cavlc cost of a non-skipped MB */
2370 if( i_bskip_cost <= 6 * analysis.i_lambda2 )
2372 h->mb.i_type = B_SKIP;
2373 x264_analyse_update_cache( h, &analysis );
2377 else if( !h->mb.b_direct_auto_write )
2379 /* Conditioning the probe on neighboring block types
2380 * doesn't seem to help speed or quality. */
2381 b_skip = x264_macroblock_probe_bskip( h );
2387 const unsigned int flags = h->param.analyse.inter;
2391 x264_mb_analyse_load_costs( h, &analysis );
2393 /* select best inter mode */
2394 /* direct must be first */
2395 if( analysis.b_direct_available )
2396 x264_mb_analyse_inter_direct( h, &analysis );
2398 x264_mb_analyse_inter_b16x16( h, &analysis );
2401 i_partition = D_16x16;
2402 i_cost = analysis.l0.me16x16.cost;
2403 COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2404 COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2405 COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2407 if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2409 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2410 if( i_bskip_cost < analysis.i_rd16x16direct &&
2411 i_bskip_cost < analysis.i_rd16x16bi &&
2412 i_bskip_cost < analysis.l0.i_rd16x16 &&
2413 i_bskip_cost < analysis.l1.i_rd16x16 )
2415 h->mb.i_type = B_SKIP;
2416 x264_analyse_update_cache( h, &analysis );
2421 if( flags & X264_ANALYSE_BSUB16x16 )
2423 x264_mb_analyse_inter_b8x8( h, &analysis );
2424 if( analysis.i_cost8x8bi < i_cost )
2427 i_partition = D_8x8;
2428 i_cost = analysis.i_cost8x8bi;
2430 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2431 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2433 x264_mb_analyse_inter_b16x8( h, &analysis );
2434 COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2435 i_type, analysis.i_mb_type16x8,
2436 i_partition, D_16x8 );
2438 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2439 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2441 x264_mb_analyse_inter_b8x16( h, &analysis );
2442 COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2443 i_type, analysis.i_mb_type8x16,
2444 i_partition, D_8x16 );
2449 if( analysis.b_mbrd )
2454 else if( i_partition == D_16x16 )
2456 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2457 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2458 if( i_type == B_L0_L0 )
2460 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2461 i_cost = analysis.l0.me16x16.cost
2462 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2464 else if( i_type == B_L1_L1 )
2466 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2467 i_cost = analysis.l1.me16x16.cost
2468 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2470 else if( i_type == B_BI_BI )
2472 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2473 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2476 else if( i_partition == D_16x8 )
2478 for( i=0; i<2; i++ )
2480 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2481 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2482 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2483 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2486 else if( i_partition == D_8x16 )
2488 for( i=0; i<2; i++ )
2490 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2491 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2492 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2493 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2496 else if( i_partition == D_8x8 )
2498 for( i=0; i<4; i++ )
2501 int i_part_cost_old;
2503 int i_part_type = h->mb.i_sub_partition[i];
2504 int b_bidir = (i_part_type == D_BI_8x8);
2506 if( i_part_type == D_DIRECT_8x8 )
2508 if( x264_mb_partition_listX_table[0][i_part_type] )
2510 m = &analysis.l0.me8x8[i];
2511 i_part_cost_old = m->cost;
2512 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2513 m->cost -= i_type_cost;
2514 x264_me_refine_qpel( h, m );
2516 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2518 if( x264_mb_partition_listX_table[1][i_part_type] )
2520 m = &analysis.l1.me8x8[i];
2521 i_part_cost_old = m->cost;
2522 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2523 m->cost -= i_type_cost;
2524 x264_me_refine_qpel( h, m );
2526 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2528 /* TODO: update mvp? */
2532 x264_mb_analyse_intra( h, &analysis, i_cost );
2534 if( analysis.b_mbrd )
2536 int i_satd_inter = i_cost;
2537 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2539 i_cost = i_bskip_cost;
2540 i_partition = D_16x16;
2541 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2542 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2543 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2544 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2545 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2546 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2547 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2549 h->mb.i_type = i_type;
2550 h->mb.i_partition = i_partition;
2551 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2552 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2555 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2556 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2557 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2559 h->mb.i_type = i_type;
2560 h->mb.i_partition = i_partition;
2562 if( h->param.analyse.b_bidir_me )
2563 refine_bidir( h, &analysis );
2567 x264_analyse_update_cache( h, &analysis );
2569 if( !analysis.b_mbrd )
2570 x264_mb_analyse_transform( h );
2572 h->mb.b_trellis = h->param.analyse.i_trellis;
2573 h->mb.b_noise_reduction = h->param.analyse.i_noise_reduction;
2576 /*-------------------- Update MB from the analysis ----------------------*/
2577 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2581 switch( h->mb.i_type )
2584 for( i = 0; i < 16; i++ )
2585 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2587 x264_mb_analyse_intra_chroma( h, a );
2590 for( i = 0; i < 4; i++ )
2591 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2593 x264_mb_analyse_intra_chroma( h, a );
2596 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2597 x264_mb_analyse_intra_chroma( h, a );
2601 switch( h->mb.i_partition )
2604 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2605 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2609 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2610 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2611 x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv[0], a->l0.me16x8[0].mv[1] );
2612 x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv[0], a->l0.me16x8[1].mv[1] );
2616 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2617 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2618 x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv[0], a->l0.me8x16[0].mv[1] );
2619 x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv[0], a->l0.me8x16[1].mv[1] );
2623 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2629 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2630 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2631 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2632 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2633 for( i = 0; i < 4; i++ )
2634 x264_mb_cache_mv_p8x8( h, a, i );
2639 h->mb.i_partition = D_16x16;
2640 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2641 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv[0],
2642 h->mb.cache.pskip_mv[1] );
2648 x264_mb_load_mv_direct8x8( h, 0 );
2649 x264_mb_load_mv_direct8x8( h, 1 );
2650 x264_mb_load_mv_direct8x8( h, 2 );
2651 x264_mb_load_mv_direct8x8( h, 3 );
2655 /* optimize: cache might not need to be rewritten */
2656 for( i = 0; i < 4; i++ )
2657 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2660 default: /* the rest of the B types */
2661 switch( h->mb.i_partition )
2664 switch( h->mb.i_type )
2667 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2668 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2670 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2671 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0, 0 );
2672 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0, 0 );
2675 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2676 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0, 0 );
2677 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0, 0 );
2679 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2680 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2683 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2684 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2686 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2687 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2692 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2693 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2696 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2697 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2700 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2706 if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2709 for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2712 int ref = h->mb.cache.ref[l][x264_scan8[0]];
2715 completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2716 if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2718 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2719 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2720 h->mb.cache.mv[l][x264_scan8[15]][0],
2721 h->mb.cache.mv[l][x264_scan8[15]][1] );
2722 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2723 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2724 fprintf(stderr, "completed: %d \n", completed );
2732 #include "slicetype.c"