1 /*****************************************************************************
2 * analyse.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003 x264 project
5 * $Id: analyse.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Loren Merritt <lorenm@u.washington.edu>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
31 #include "common/common.h"
32 #include "macroblock.h"
34 #include "ratecontrol.h"
47 int mvc[32][5][2]; /* [ref][0] is 16x16 mv,
48 [ref][1..4] are 8x8 mv from partition [0..3] */
52 int i_cost4x4[4]; /* cost per 8x8 partition */
53 x264_me_t me4x4[4][4];
56 int i_cost8x4[4]; /* cost per 8x8 partition */
57 x264_me_t me8x4[4][2];
60 int i_cost4x8[4]; /* cost per 8x8 partition */
61 x264_me_t me4x8[4][2];
71 } x264_mb_analysis_list_t;
75 /* conduct the analysis using this lamda and QP */
84 /* Take some shortcuts in intra search if intra is deemed unlikely */
90 int i_satd_i16x16_dir[7];
94 int i_satd_i8x8_dir[12][4];
101 int i_satd_i8x8chroma;
102 int i_satd_i8x8chroma_dir[4];
103 int i_predict8x8chroma;
105 /* II: Inter part P/B frame */
106 x264_mb_analysis_list_t l0;
107 x264_mb_analysis_list_t l1;
109 int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
110 int i_cost16x16direct;
112 int i_cost8x8direct[4];
121 int i_mb_partition16x8[2]; /* mb_partition_e */
122 int i_mb_partition8x16[2];
123 int i_mb_type16x8; /* mb_class_e */
126 int b_direct_available;
128 } x264_mb_analysis_t;
130 /* lambda = pow(2,qp/6-2) */
131 static const int i_qp0_cost_table[52] = {
132 1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
133 1, 1, 1, 1, /* 8-11 */
134 1, 1, 1, 1, 2, 2, 2, 2, /* 12-19 */
135 3, 3, 3, 4, 4, 4, 5, 6, /* 20-27 */
136 6, 7, 8, 9,10,11,13,14, /* 28-35 */
137 16,18,20,23,25,29,32,36, /* 36-43 */
138 40,45,51,57,64,72,81,91 /* 44-51 */
141 /* pow(lambda,2) * .9 */
142 static const int i_qp0_cost2_table[52] = {
143 1, 1, 1, 1, 1, 1, /* 0-5 */
144 1, 1, 1, 1, 1, 1, /* 6-11 */
145 1, 1, 1, 2, 2, 3, /* 12-17 */
146 4, 5, 6, 7, 9, 11, /* 18-23 */
147 14, 18, 23, 29, 36, 46, /* 24-29 */
148 58, 73, 91, 115, 145, 183, /* 30-35 */
149 230, 290, 366, 461, 581, 731, /* 36-41 */
150 922,1161,1463,1843,2322,2926, /* 42-47 */
154 /* TODO: calculate CABAC costs */
155 static const int i_mb_b_cost_table[X264_MBTYPE_MAX] = {
156 9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
158 static const int i_mb_b16x8_cost_table[17] = {
159 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
161 static const int i_sub_mb_b_cost_table[13] = {
162 7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
164 static const int i_sub_mb_p_cost_table[4] = {
168 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
170 /* initialize an array of lambda*nbits for all possible mvs */
171 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
173 static int16_t *p_cost_mv[52];
175 if( !p_cost_mv[a->i_qp] )
177 /* could be faster, but isn't called many times */
178 /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
180 p_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) );
181 p_cost_mv[a->i_qp] += 2*4*2048;
182 for( i = 0; i <= 2*4*2048; i++ )
184 p_cost_mv[a->i_qp][-i] =
185 p_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i );
189 a->p_cost_mv = p_cost_mv[a->i_qp];
192 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
194 memset( a, 0, sizeof( x264_mb_analysis_t ) );
196 /* conduct the analysis using this lamda and QP */
197 a->i_qp = h->mb.i_qp = i_qp;
198 h->mb.i_chroma_qp = i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )];
199 a->i_lambda = i_qp0_cost_table[i_qp];
200 a->i_lambda2 = i_qp0_cost2_table[i_qp];
201 a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
202 ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
204 h->mb.i_me_method = h->param.analyse.i_me_method;
205 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
206 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
207 && h->mb.i_subpel_refine >= 5;
208 h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
209 h->mb.b_transform_8x8 = 0;
210 h->mb.b_noise_reduction = 0;
216 a->i_satd_i8x8chroma = COST_MAX;
220 /* II: Inter part P/B frame */
221 if( h->sh.i_type != SLICE_TYPE_I )
224 int i_fmv_range = 4 * h->param.analyse.i_mv_range;
225 // limit motion search to a slightly smaller range than the theoretical limit,
226 // since the search may go a few iterations past its given range
227 int i_fpel_border = 5; // umh unconditional radius
228 int i_spel_border = 8; // 1.5 for subpel_satd, 1.5 for subpel_rd, 2 for bime, round up
230 /* Calculate max allowed MV range */
231 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range )
232 h->mb.mv_min[0] = 4*( -16*h->mb.i_mb_x - 24 );
233 h->mb.mv_max[0] = 4*( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 24 );
234 h->mb.mv_min_spel[0] = CLIP_FMV( h->mb.mv_min[0] );
235 h->mb.mv_max_spel[0] = CLIP_FMV( h->mb.mv_max[0] );
236 h->mb.mv_min_fpel[0] = (h->mb.mv_min_spel[0]>>2) + i_fpel_border;
237 h->mb.mv_max_fpel[0] = (h->mb.mv_max_spel[0]>>2) - i_fpel_border;
238 if( h->mb.i_mb_x == 0)
240 int mb_y = h->mb.i_mb_y >> h->sh.b_mbaff;
241 int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff;
242 int thread_mvy_range = i_fmv_range;
244 if( h->param.i_threads > 1 )
246 int pix_y = (h->mb.i_mb_y | h->mb.b_interlaced) * 16;
247 int thresh = pix_y + h->param.analyse.i_mv_range_thread;
248 for( i = (h->sh.i_type == SLICE_TYPE_B); i >= 0; i-- )
250 x264_frame_t **fref = i ? h->fref1 : h->fref0;
251 int i_ref = i ? h->i_ref1 : h->i_ref0;
252 for( j=0; j<i_ref; j++ )
254 x264_frame_cond_wait( fref[j], thresh );
255 thread_mvy_range = X264_MIN( thread_mvy_range, fref[j]->i_lines_completed - pix_y );
258 if( h->param.b_deterministic )
259 thread_mvy_range = h->param.analyse.i_mv_range_thread;
260 if( h->mb.b_interlaced )
261 thread_mvy_range >>= 1;
264 h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
265 h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
266 h->mb.mv_min_spel[1] = x264_clip3( h->mb.mv_min[1], X264_MAX(4*(-512+i_spel_border), -i_fmv_range), i_fmv_range );
267 h->mb.mv_max_spel[1] = CLIP_FMV( h->mb.mv_max[1] );
268 h->mb.mv_max_spel[1] = X264_MIN( h->mb.mv_max_spel[1], thread_mvy_range*4 );
269 h->mb.mv_min_fpel[1] = (h->mb.mv_min_spel[1]>>2) + i_fpel_border;
270 h->mb.mv_max_fpel[1] = (h->mb.mv_max_spel[1]>>2) - i_fpel_border;
276 a->l0.i_cost8x8 = COST_MAX;
278 for( i = 0; i < 4; i++ )
282 a->l0.i_cost4x8[i] = COST_MAX;
286 a->l0.i_cost8x16 = COST_MAX;
287 if( h->sh.i_type == SLICE_TYPE_B )
291 a->l1.i_cost8x8 = COST_MAX;
293 for( i = 0; i < 4; i++ )
298 a->i_cost8x8direct[i] = COST_MAX;
309 a->i_cost16x16direct =
312 a->i_cost8x16bi = COST_MAX;
315 /* Fast intra decision */
316 if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
318 if( IS_INTRA( h->mb.i_mb_type_left )
319 || IS_INTRA( h->mb.i_mb_type_top )
320 || IS_INTRA( h->mb.i_mb_type_topleft )
321 || IS_INTRA( h->mb.i_mb_type_topright )
322 || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
323 || (h->mb.i_mb_xy - h->sh.i_first_mb < 3*(h->stat.frame.i_mb_count[I_4x4] + h->stat.frame.i_mb_count[I_8x8] + h->stat.frame.i_mb_count[I_16x16])) )
324 { /* intra is likely */ }
339 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
341 if( i_neighbour & MB_TOPLEFT )
343 /* top and left available */
344 *mode++ = I_PRED_16x16_V;
345 *mode++ = I_PRED_16x16_H;
346 *mode++ = I_PRED_16x16_DC;
347 *mode++ = I_PRED_16x16_P;
350 else if( i_neighbour & MB_LEFT )
353 *mode++ = I_PRED_16x16_DC_LEFT;
354 *mode++ = I_PRED_16x16_H;
357 else if( i_neighbour & MB_TOP )
360 *mode++ = I_PRED_16x16_DC_TOP;
361 *mode++ = I_PRED_16x16_V;
367 *mode = I_PRED_16x16_DC_128;
373 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
375 if( i_neighbour & MB_TOPLEFT )
377 /* top and left available */
378 *mode++ = I_PRED_CHROMA_V;
379 *mode++ = I_PRED_CHROMA_H;
380 *mode++ = I_PRED_CHROMA_DC;
381 *mode++ = I_PRED_CHROMA_P;
384 else if( i_neighbour & MB_LEFT )
387 *mode++ = I_PRED_CHROMA_DC_LEFT;
388 *mode++ = I_PRED_CHROMA_H;
391 else if( i_neighbour & MB_TOP )
394 *mode++ = I_PRED_CHROMA_DC_TOP;
395 *mode++ = I_PRED_CHROMA_V;
401 *mode = I_PRED_CHROMA_DC_128;
407 static void predict_4x4_mode_available( unsigned int i_neighbour,
408 int *mode, int *pi_count )
410 int b_l = i_neighbour & MB_LEFT;
411 int b_t = i_neighbour & MB_TOP;
416 *mode++ = I_PRED_4x4_DC;
417 *mode++ = I_PRED_4x4_H;
418 *mode++ = I_PRED_4x4_V;
419 *mode++ = I_PRED_4x4_DDL;
420 if( i_neighbour & MB_TOPLEFT )
422 *mode++ = I_PRED_4x4_DDR;
423 *mode++ = I_PRED_4x4_VR;
424 *mode++ = I_PRED_4x4_HD;
427 *mode++ = I_PRED_4x4_VL;
428 *mode++ = I_PRED_4x4_HU;
432 *mode++ = I_PRED_4x4_DC_LEFT;
433 *mode++ = I_PRED_4x4_H;
434 *mode++ = I_PRED_4x4_HU;
439 *mode++ = I_PRED_4x4_DC_TOP;
440 *mode++ = I_PRED_4x4_V;
441 *mode++ = I_PRED_4x4_DDL;
442 *mode++ = I_PRED_4x4_VL;
447 *mode++ = I_PRED_4x4_DC_128;
452 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
459 uint8_t *p_dstc[2], *p_srcc[2];
461 if( a->i_satd_i8x8chroma < COST_MAX )
464 /* 8x8 prediction selection for chroma */
465 p_dstc[0] = h->mb.pic.p_fdec[1];
466 p_dstc[1] = h->mb.pic.p_fdec[2];
467 p_srcc[0] = h->mb.pic.p_fenc[1];
468 p_srcc[1] = h->mb.pic.p_fenc[2];
470 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
471 a->i_satd_i8x8chroma = COST_MAX;
472 if( i_max == 4 && h->pixf.intra_satd_x3_8x8c && h->pixf.mbcmp[0] == h->pixf.satd[0] )
474 int satdu[4], satdv[4];
475 h->pixf.intra_satd_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
476 h->pixf.intra_satd_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
477 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
478 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
479 satdu[I_PRED_CHROMA_P] =
480 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
481 satdv[I_PRED_CHROMA_P] =
482 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
484 for( i=0; i<i_max; i++ )
486 int i_mode = predict_mode[i];
487 int i_satd = satdu[i_mode] + satdv[i_mode]
488 + a->i_lambda * bs_size_ue(i_mode);
490 a->i_satd_i8x8chroma_dir[i] = i_satd;
491 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
496 for( i=0; i<i_max; i++ )
499 int i_mode = predict_mode[i];
501 /* we do the prediction */
502 h->predict_8x8c[i_mode]( p_dstc[0] );
503 h->predict_8x8c[i_mode]( p_dstc[1] );
505 /* we calculate the cost */
506 i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
507 p_srcc[0], FENC_STRIDE ) +
508 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
509 p_srcc[1], FENC_STRIDE ) +
510 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
512 a->i_satd_i8x8chroma_dir[i] = i_satd;
513 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
517 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
520 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
522 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
523 uint8_t *p_src = h->mb.pic.p_fenc[0];
524 uint8_t *p_dst = h->mb.pic.p_fdec[0];
529 int b_merged_satd = h->pixf.intra_satd_x3_16x16 && h->pixf.mbcmp[0] == h->pixf.satd[0];
531 /*---------------- Try all mode and calculate their score ---------------*/
533 /* 16x16 prediction selection */
534 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
536 if( b_merged_satd && i_max == 4 )
538 h->pixf.intra_satd_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
539 h->predict_16x16[I_PRED_16x16_P]( p_dst );
540 a->i_satd_i16x16_dir[I_PRED_16x16_P] =
541 h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
544 int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
545 COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
550 for( i = 0; i < i_max; i++ )
553 int i_mode = predict_mode[i];
554 h->predict_16x16[i_mode]( p_dst );
556 i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
557 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
558 COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
559 a->i_satd_i16x16_dir[i_mode] = i_satd;
563 if( h->sh.i_type == SLICE_TYPE_B )
564 /* cavlc mb type prefix */
565 a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
566 if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
569 /* 8x8 prediction selection */
570 if( flags & X264_ANALYSE_I8x8 )
572 DECLARE_ALIGNED( uint8_t, edge[33], 8 );
573 x264_pixel_cmp_t sa8d = (*h->pixf.mbcmp == *h->pixf.sad) ? h->pixf.sad[PIXEL_8x8] : h->pixf.sa8d[PIXEL_8x8];
574 int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
576 b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
578 // FIXME some bias like in i4x4?
579 if( h->sh.i_type == SLICE_TYPE_B )
580 i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
582 for( idx = 0;; idx++ )
586 uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
587 uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
588 int i_best = COST_MAX;
589 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
591 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
592 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
594 if( b_merged_satd && i_max == 9 )
597 h->pixf.intra_sa8d_x3_8x8( p_src_by, edge, satd );
598 if( i_pred_mode < 3 )
599 satd[i_pred_mode] -= 3 * a->i_lambda;
600 for( i=2; i>=0; i-- )
602 int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
603 COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
610 for( ; i<i_max; i++ )
613 int i_mode = predict_mode[i];
615 h->predict_8x8[i_mode]( p_dst_by, edge );
617 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
618 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
620 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
621 a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
625 if( idx == 3 || i_cost > i_satd_thresh )
628 /* we need to encode this block now (for next ones) */
629 h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
630 x264_mb_encode_i8x8( h, idx, a->i_qp );
632 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
636 a->i_satd_i8x8 = i_cost;
639 a->i_satd_i8x8 = COST_MAX;
640 i_cost = i_cost * 4/(idx+1);
642 if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 )
646 /* 4x4 prediction selection */
647 if( flags & X264_ANALYSE_I4x4 )
650 int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
651 b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
653 i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
655 i_cost = a->i_lambda * 24; /* from JVT (SATD0) */
656 if( h->sh.i_type == SLICE_TYPE_B )
657 i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];
659 for( idx = 0;; idx++ )
661 int x = block_idx_x[idx];
662 int y = block_idx_y[idx];
663 uint8_t *p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
664 uint8_t *p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
665 int i_best = COST_MAX;
666 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
668 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
670 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
671 /* emulate missing topright samples */
672 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
674 if( b_merged_satd && i_max >= 6 )
677 h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
678 if( i_pred_mode < 3 )
679 satd[i_pred_mode] -= 3 * a->i_lambda;
680 for( i=2; i>=0; i-- )
681 COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
682 a->i_predict4x4[idx], i );
688 for( ; i<i_max; i++ )
691 int i_mode = predict_mode[i];
693 h->predict_4x4[i_mode]( p_dst_by );
695 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
696 p_src_by, FENC_STRIDE )
697 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
699 COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
703 if( i_cost > i_satd_thresh || idx == 15 )
706 /* we need to encode this block now (for next ones) */
707 h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
708 x264_mb_encode_i4x4( h, idx, a->i_qp );
710 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
713 a->i_satd_i4x4 = i_cost;
715 a->i_satd_i4x4 = COST_MAX;
719 static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
721 if( a->i_satd_i16x16 <= i_satd_thresh )
723 h->mb.i_type = I_16x16;
724 x264_analyse_update_cache( h, a );
725 a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
728 a->i_satd_i16x16 = COST_MAX;
730 if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
732 h->mb.i_type = I_4x4;
733 x264_analyse_update_cache( h, a );
734 a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
737 a->i_satd_i4x4 = COST_MAX;
739 if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
741 h->mb.i_type = I_8x8;
742 x264_analyse_update_cache( h, a );
743 a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
746 a->i_satd_i8x8 = COST_MAX;
749 static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
751 uint8_t *p_src = h->mb.pic.p_fenc[0];
752 uint8_t *p_dst = h->mb.pic.p_fdec[0];
755 int i_max, i_satd, i_best, i_mode, i_thresh;
759 if( h->mb.i_type == I_16x16 )
761 int old_pred_mode = a->i_predict16x16;
762 i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
763 i_best = a->i_satd_i16x16;
764 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
765 for( i = 0; i < i_max; i++ )
767 int i_mode = predict_mode[i];
768 if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
770 h->mb.i_intra16x16_pred_mode = i_mode;
771 i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
772 COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
775 else if( h->mb.i_type == I_4x4 )
777 uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
779 for( idx = 0; idx < 16; idx++ )
785 i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
786 x = block_idx_x[idx];
787 y = block_idx_y[idx];
789 p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
790 p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
791 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
793 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
794 /* emulate missing topright samples */
795 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
797 for( i = 0; i < i_max; i++ )
799 i_mode = predict_mode[i];
800 h->predict_4x4[i_mode]( p_dst_by );
801 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
803 if( i_best > i_satd )
805 a->i_predict4x4[idx] = i_mode;
807 pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
808 pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
809 pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
810 pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
811 i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
815 *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
816 *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
817 *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
818 *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
819 h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
821 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
824 else if( h->mb.i_type == I_8x8 )
826 DECLARE_ALIGNED( uint8_t, edge[33], 8 );
827 for( idx = 0; idx < 4; idx++ )
835 i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
838 i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
842 p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
843 p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
844 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
845 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
847 for( i = 0; i < i_max; i++ )
849 i_mode = predict_mode[i];
850 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
852 h->predict_8x8[i_mode]( p_dst_by, edge );
853 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
855 if( i_best > i_satd )
857 a->i_predict8x8[idx] = i_mode;
860 pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
863 pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
865 i_nnz[j] = h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]];
869 *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
872 p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
874 h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]] = i_nnz[j];
876 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
880 /* RD selection for chroma prediction */
881 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
884 i_thresh = a->i_satd_i8x8chroma * 5/4;
886 for( i = j = 0; i < i_max; i++ )
887 if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
888 predict_mode[i] != a->i_predict8x8chroma )
890 predict_mode[j++] = predict_mode[i];
896 int i_chroma_lambda = i_qp0_cost2_table[h->mb.i_chroma_qp];
897 /* the previous thing encoded was x264_intra_rd(), so the pixels and
898 * coefs for the current chroma mode are still around, so we only
899 * have to recount the bits. */
900 i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
901 for( i = 0; i < i_max; i++ )
903 i_mode = predict_mode[i];
904 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
905 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
906 /* if we've already found a mode that needs no residual, then
907 * probably any mode with a residual will be worse.
908 * so avoid dct on the remaining modes to improve speed. */
909 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
910 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode );
912 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
917 #define LOAD_FENC( m, src, xoff, yoff) \
918 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
919 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
920 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
921 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
922 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
924 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
925 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
926 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
927 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
928 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
929 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
930 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
931 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
933 #define REF_COST(list, ref) \
934 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
936 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
940 int mvc[7][2], i_mvc;
941 int i_halfpel_thresh = INT_MAX;
942 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
944 /* 16x16 Search on all ref frame */
945 m.i_pixel = PIXEL_16x16;
946 m.p_cost_mv = a->p_cost_mv;
947 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
949 a->l0.me16x16.cost = INT_MAX;
950 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
952 const int i_ref_cost = REF_COST( 0, i_ref );
953 i_halfpel_thresh -= i_ref_cost;
954 m.i_ref_cost = i_ref_cost;
957 /* search with ref */
958 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
959 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
960 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
961 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
964 * SSD threshold would probably be better than SATD */
967 && m.cost-m.cost_mv < 300*a->i_lambda
968 && abs(m.mv[0]-h->mb.cache.pskip_mv[0])
969 + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
970 && x264_macroblock_probe_pskip( h ) )
972 h->mb.i_type = P_SKIP;
973 x264_analyse_update_cache( h, a );
974 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
978 m.cost += i_ref_cost;
979 i_halfpel_thresh += i_ref_cost;
981 if( m.cost < a->l0.me16x16.cost )
984 /* save mv for predicting neighbors */
985 a->l0.mvc[i_ref][0][0] =
986 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
987 a->l0.mvc[i_ref][0][1] =
988 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
991 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
992 assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
995 if( a->b_mbrd && a->l0.i_ref == 0
996 && a->l0.me16x16.mv[0] == h->mb.cache.pskip_mv[0]
997 && a->l0.me16x16.mv[1] == h->mb.cache.pskip_mv[1] )
999 h->mb.i_partition = D_16x16;
1000 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
1001 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1005 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
1009 uint8_t **p_fenc = h->mb.pic.p_fenc;
1010 int i_halfpel_thresh = INT_MAX;
1011 int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
1013 int i_maxref = h->mb.pic.i_fref[0]-1;
1015 h->mb.i_partition = D_8x8;
1017 /* early termination: if 16x16 chose ref 0, then evalute no refs older
1018 * than those used by the neighbors */
1019 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
1020 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
1023 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
1024 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
1025 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
1026 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
1027 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
1028 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
1031 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1033 a->l0.mvc[i_ref][0][0] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0];
1034 a->l0.mvc[i_ref][0][1] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1];
1037 for( i = 0; i < 4; i++ )
1039 x264_me_t *l0m = &a->l0.me8x8[i];
1043 m.i_pixel = PIXEL_8x8;
1044 m.p_cost_mv = a->p_cost_mv;
1046 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
1047 l0m->cost = INT_MAX;
1048 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1050 const int i_ref_cost = REF_COST( 0, i_ref );
1051 i_halfpel_thresh -= i_ref_cost;
1052 m.i_ref_cost = i_ref_cost;
1055 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
1056 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
1057 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1058 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
1060 m.cost += i_ref_cost;
1061 i_halfpel_thresh += i_ref_cost;
1062 *(uint64_t*)a->l0.mvc[i_ref][i+1] = *(uint64_t*)m.mv;
1064 if( m.cost < l0m->cost )
1067 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv[0], l0m->mv[1] );
1068 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1071 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1074 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1075 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1076 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1077 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1080 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1082 const int i_ref = a->l0.me16x16.i_ref;
1083 const int i_ref_cost = REF_COST( 0, i_ref );
1084 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
1085 uint8_t **p_fenc = h->mb.pic.p_fenc;
1087 int (*mvc)[2] = a->l0.mvc[i_ref];
1090 /* XXX Needed for x264_mb_predict_mv */
1091 h->mb.i_partition = D_8x8;
1094 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.me16x16.mv;
1096 for( i = 0; i < 4; i++ )
1098 x264_me_t *m = &a->l0.me8x8[i];
1102 m->i_pixel = PIXEL_8x8;
1103 m->p_cost_mv = a->p_cost_mv;
1104 m->i_ref_cost = i_ref_cost;
1107 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1108 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1109 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1110 x264_me_search( h, m, mvc, i_mvc );
1112 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
1114 *(uint64_t*)mvc[i_mvc] = *(uint64_t*)m->mv;
1118 m->cost += i_ref_cost;
1119 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1122 /* theoretically this should include 4*ref_cost,
1123 * but 3 seems a better approximation of cabac. */
1124 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1125 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
1126 REF_COST( 0, a->l0.me16x16.i_ref );
1127 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1128 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1131 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
1134 uint8_t **p_fenc = h->mb.pic.p_fenc;
1138 /* XXX Needed for x264_mb_predict_mv */
1139 h->mb.i_partition = D_16x8;
1141 for( i = 0; i < 2; i++ )
1143 x264_me_t *l0m = &a->l0.me16x8[i];
1144 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1145 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1147 m.i_pixel = PIXEL_16x8;
1148 m.p_cost_mv = a->p_cost_mv;
1150 LOAD_FENC( &m, p_fenc, 0, 8*i );
1151 l0m->cost = INT_MAX;
1152 for( j = 0; j < i_ref8s; j++ )
1154 const int i_ref = ref8[j];
1155 const int i_ref_cost = REF_COST( 0, i_ref );
1156 m.i_ref_cost = i_ref_cost;
1159 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1160 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1161 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][2*i+1];
1162 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][2*i+2];
1164 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1165 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1166 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1167 x264_me_search( h, &m, mvc, 3 );
1169 m.cost += i_ref_cost;
1171 if( m.cost < l0m->cost )
1174 x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, l0m->mv[0], l0m->mv[1] );
1175 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1178 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1181 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1184 uint8_t **p_fenc = h->mb.pic.p_fenc;
1188 /* XXX Needed for x264_mb_predict_mv */
1189 h->mb.i_partition = D_8x16;
1191 for( i = 0; i < 2; i++ )
1193 x264_me_t *l0m = &a->l0.me8x16[i];
1194 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1195 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1197 m.i_pixel = PIXEL_8x16;
1198 m.p_cost_mv = a->p_cost_mv;
1200 LOAD_FENC( &m, p_fenc, 8*i, 0 );
1201 l0m->cost = INT_MAX;
1202 for( j = 0; j < i_ref8s; j++ )
1204 const int i_ref = ref8[j];
1205 const int i_ref_cost = REF_COST( 0, i_ref );
1206 m.i_ref_cost = i_ref_cost;
1209 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1210 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][i+1];
1211 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][i+3];
1213 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1214 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1215 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1216 x264_me_search( h, &m, mvc, 3 );
1218 m.cost += i_ref_cost;
1220 if( m.cost < l0m->cost )
1223 x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, l0m->mv[0], l0m->mv[1] );
1224 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1227 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1230 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1232 DECLARE_ALIGNED( uint8_t, pix1[16*8], 8 );
1233 uint8_t *pix2 = pix1+8;
1234 const int i_stride = h->mb.pic.i_stride[1];
1235 const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1236 const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1238 #define CHROMA4x4MC( width, height, me, x, y ) \
1239 h->mc.mc_chroma( &p_fref[4][or+x+y*i_stride], i_stride, &pix1[x+y*16], 16, (me).mv[0], (me).mv[1], width, height ); \
1240 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 );
1242 if( pixel == PIXEL_4x4 )
1244 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1245 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 0,2 );
1246 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 2,0 );
1247 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1249 else if( pixel == PIXEL_8x4 )
1251 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1252 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1256 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1257 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1260 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1261 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1264 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1266 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1267 uint8_t **p_fenc = h->mb.pic.p_fenc;
1268 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1271 /* XXX Needed for x264_mb_predict_mv */
1272 h->mb.i_partition = D_8x8;
1274 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1276 const int idx = 4*i8x8 + i4x4;
1277 const int x4 = block_idx_x[idx];
1278 const int y4 = block_idx_y[idx];
1279 const int i_mvc = (i4x4 == 0);
1281 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1283 m->i_pixel = PIXEL_4x4;
1284 m->p_cost_mv = a->p_cost_mv;
1286 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1287 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1289 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1290 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1292 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
1294 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1295 a->l0.me4x4[i8x8][1].cost +
1296 a->l0.me4x4[i8x8][2].cost +
1297 a->l0.me4x4[i8x8][3].cost +
1298 REF_COST( 0, i_ref ) +
1299 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1300 if( h->mb.b_chroma_me )
1301 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1304 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1306 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1307 uint8_t **p_fenc = h->mb.pic.p_fenc;
1308 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1311 /* XXX Needed for x264_mb_predict_mv */
1312 h->mb.i_partition = D_8x8;
1314 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1316 const int idx = 4*i8x8 + 2*i8x4;
1317 const int x4 = block_idx_x[idx];
1318 const int y4 = block_idx_y[idx];
1319 const int i_mvc = (i8x4 == 0);
1321 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1323 m->i_pixel = PIXEL_8x4;
1324 m->p_cost_mv = a->p_cost_mv;
1326 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1327 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1329 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1330 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1332 x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
1334 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1335 REF_COST( 0, i_ref ) +
1336 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1337 if( h->mb.b_chroma_me )
1338 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1341 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1343 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1344 uint8_t **p_fenc = h->mb.pic.p_fenc;
1345 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1348 /* XXX Needed for x264_mb_predict_mv */
1349 h->mb.i_partition = D_8x8;
1351 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1353 const int idx = 4*i8x8 + i4x8;
1354 const int x4 = block_idx_x[idx];
1355 const int y4 = block_idx_y[idx];
1356 const int i_mvc = (i4x8 == 0);
1358 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1360 m->i_pixel = PIXEL_4x8;
1361 m->p_cost_mv = a->p_cost_mv;
1363 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1364 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1366 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1367 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1369 x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
1371 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1372 REF_COST( 0, i_ref ) +
1373 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1374 if( h->mb.b_chroma_me )
1375 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1378 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1380 /* Assumes that fdec still contains the results of
1381 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1383 uint8_t **p_fenc = h->mb.pic.p_fenc;
1384 uint8_t **p_fdec = h->mb.pic.p_fdec;
1387 a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1388 for( i = 0; i < 4; i++ )
1390 const int x = (i&1)*8;
1391 const int y = (i>>1)*8;
1392 a->i_cost16x16direct +=
1393 a->i_cost8x8direct[i] =
1394 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1397 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1401 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1403 if( h->param.analyse.b_weighted_bipred ) \
1404 h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1405 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1407 h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1410 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1412 uint8_t pix1[16*16], pix2[16*16];
1419 int mvc[8][2], i_mvc;
1420 int i_halfpel_thresh = INT_MAX;
1421 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1423 /* 16x16 Search on all ref frame */
1424 m.i_pixel = PIXEL_16x16;
1425 m.p_cost_mv = a->p_cost_mv;
1426 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1429 a->l0.me16x16.cost = INT_MAX;
1430 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1432 /* search with ref */
1433 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1434 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1435 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1436 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1439 m.cost += REF_COST( 0, i_ref );
1441 if( m.cost < a->l0.me16x16.cost )
1443 a->l0.i_ref = i_ref;
1447 /* save mv for predicting neighbors */
1448 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1449 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1451 /* subtract ref cost, so we don't have to add it for the other MB types */
1452 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1455 i_halfpel_thresh = INT_MAX;
1456 p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1457 a->l1.me16x16.cost = INT_MAX;
1458 for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1460 /* search with ref */
1461 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1462 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1463 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1464 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1467 m.cost += REF_COST( 1, i_ref );
1469 if( m.cost < a->l1.me16x16.cost )
1471 a->l1.i_ref = i_ref;
1475 /* save mv for predicting neighbors */
1476 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1477 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1479 /* subtract ref cost, so we don't have to add it for the other MB types */
1480 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1482 /* Set global ref, needed for other modes? */
1483 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1484 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1486 /* get cost of BI mode */
1487 weight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1488 if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
1490 /* l0 reference is halfpel, so get_ref on it will make it faster */
1491 src2 = h->mc.get_ref( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1493 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1495 h->mc.mc_luma( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1497 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1499 weight = 64 - weight;
1503 /* if l0 was qpel, we'll use get_ref on l1 instead */
1504 h->mc.mc_luma( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1506 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1508 src2 = h->mc.get_ref( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1510 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1514 if( h->param.analyse.b_weighted_bipred )
1515 h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2, weight );
1517 h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1519 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix1, 16 )
1520 + REF_COST( 0, a->l0.i_ref )
1521 + REF_COST( 1, a->l1.i_ref )
1522 + a->l0.me16x16.cost_mv
1523 + a->l1.me16x16.cost_mv;
1526 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1527 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1528 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1531 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1533 const int x = 2*(i%2);
1534 const int y = 2*(i/2);
1536 switch( h->mb.i_sub_partition[i] )
1539 x264_macroblock_cache_mv( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv[0], a->l0.me8x8[i].mv[1] );
1542 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] );
1543 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] );
1546 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] );
1547 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] );
1550 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] );
1551 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] );
1552 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] );
1553 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] );
1556 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1561 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1562 if( x264_mb_partition_listX_table[0][part] ) \
1564 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1565 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
1569 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1570 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0, 0 ); \
1572 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
1574 if( x264_mb_partition_listX_table[1][part] ) \
1576 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1577 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
1581 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1582 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0, 0 ); \
1584 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
1587 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1591 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1593 x264_mb_load_mv_direct8x8( h, i );
1596 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0, 0 );
1597 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0, 0 );
1598 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1603 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1606 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1608 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1610 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1612 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1616 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1618 uint8_t **p_fref[2] =
1619 { h->mb.pic.p_fref[0][a->l0.i_ref],
1620 h->mb.pic.p_fref[1][a->l1.i_ref] };
1621 uint8_t pix[2][8*8];
1624 /* XXX Needed for x264_mb_predict_mv */
1625 h->mb.i_partition = D_8x8;
1629 for( i = 0; i < 4; i++ )
1634 int i_part_cost_bi = 0;
1636 for( l = 0; l < 2; l++ )
1638 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1639 x264_me_t *m = &lX->me8x8[i];
1641 m->i_pixel = PIXEL_8x8;
1642 m->p_cost_mv = a->p_cost_mv;
1644 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1645 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1647 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1648 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1650 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
1653 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1654 m->mv[0], m->mv[1], 8, 8 );
1655 i_part_cost_bi += m->cost_mv;
1656 /* FIXME: ref cost */
1659 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1660 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1661 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1662 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1663 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1665 i_part_cost = a->l0.me8x8[i].cost;
1666 h->mb.i_sub_partition[i] = D_L0_8x8;
1667 COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1668 COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1669 COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1670 a->i_cost8x8bi += i_part_cost;
1672 /* XXX Needed for x264_mb_predict_mv */
1673 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1677 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1680 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1682 uint8_t **p_fref[2] =
1683 { h->mb.pic.p_fref[0][a->l0.i_ref],
1684 h->mb.pic.p_fref[1][a->l1.i_ref] };
1685 DECLARE_ALIGNED( uint8_t, pix[2][16*8], 16 );
1689 h->mb.i_partition = D_16x8;
1690 a->i_cost16x8bi = 0;
1692 for( i = 0; i < 2; i++ )
1695 int i_part_cost_bi = 0;
1697 /* TODO: check only the list(s) that were used in b8x8? */
1698 for( l = 0; l < 2; l++ )
1700 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1701 x264_me_t *m = &lX->me16x8[i];
1703 m->i_pixel = PIXEL_16x8;
1704 m->p_cost_mv = a->p_cost_mv;
1706 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1707 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1709 mvc[0][0] = lX->me8x8[2*i].mv[0];
1710 mvc[0][1] = lX->me8x8[2*i].mv[1];
1711 mvc[1][0] = lX->me8x8[2*i+1].mv[0];
1712 mvc[1][1] = lX->me8x8[2*i+1].mv[1];
1714 x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp );
1715 x264_me_search( h, m, mvc, 2 );
1718 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 16,
1719 m->mv[0], m->mv[1], 16, 8 );
1720 /* FIXME: ref cost */
1721 i_part_cost_bi += m->cost_mv;
1724 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1725 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1727 i_part_cost = a->l0.me16x8[i].cost;
1728 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1729 if( a->l1.me16x8[i].cost < i_part_cost )
1731 i_part_cost = a->l1.me16x8[i].cost;
1732 a->i_mb_partition16x8[i] = D_L1_8x8;
1734 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1736 i_part_cost = i_part_cost_bi;
1737 a->i_mb_partition16x8[i] = D_BI_8x8;
1739 a->i_cost16x8bi += i_part_cost;
1741 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1745 a->i_mb_type16x8 = B_L0_L0
1746 + (a->i_mb_partition16x8[0]>>2) * 3
1747 + (a->i_mb_partition16x8[1]>>2);
1748 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1751 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1753 uint8_t **p_fref[2] =
1754 { h->mb.pic.p_fref[0][a->l0.i_ref],
1755 h->mb.pic.p_fref[1][a->l1.i_ref] };
1756 uint8_t pix[2][8*16];
1760 h->mb.i_partition = D_8x16;
1761 a->i_cost8x16bi = 0;
1763 for( i = 0; i < 2; i++ )
1766 int i_part_cost_bi = 0;
1768 for( l = 0; l < 2; l++ )
1770 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1771 x264_me_t *m = &lX->me8x16[i];
1773 m->i_pixel = PIXEL_8x16;
1774 m->p_cost_mv = a->p_cost_mv;
1776 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1777 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1779 mvc[0][0] = lX->me8x8[i].mv[0];
1780 mvc[0][1] = lX->me8x8[i].mv[1];
1781 mvc[1][0] = lX->me8x8[i+2].mv[0];
1782 mvc[1][1] = lX->me8x8[i+2].mv[1];
1784 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1785 x264_me_search( h, m, mvc, 2 );
1788 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1789 m->mv[0], m->mv[1], 8, 16 );
1790 /* FIXME: ref cost */
1791 i_part_cost_bi += m->cost_mv;
1794 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1795 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1797 i_part_cost = a->l0.me8x16[i].cost;
1798 a->i_mb_partition8x16[i] = D_L0_8x8;
1799 if( a->l1.me8x16[i].cost < i_part_cost )
1801 i_part_cost = a->l1.me8x16[i].cost;
1802 a->i_mb_partition8x16[i] = D_L1_8x8;
1804 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1806 i_part_cost = i_part_cost_bi;
1807 a->i_mb_partition8x16[i] = D_BI_8x8;
1809 a->i_cost8x16bi += i_part_cost;
1811 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1815 a->i_mb_type8x16 = B_L0_L0
1816 + (a->i_mb_partition8x16[0]>>2) * 3
1817 + (a->i_mb_partition8x16[1]>>2);
1818 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1821 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1823 int thresh = i_satd * 5/4;
1825 h->mb.i_type = P_L0;
1826 if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1828 h->mb.i_partition = D_16x16;
1829 x264_analyse_update_cache( h, a );
1830 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1832 a->l0.me16x16.cost = a->l0.i_rd16x16;
1834 if( a->l0.i_cost16x8 <= thresh )
1836 h->mb.i_partition = D_16x8;
1837 x264_analyse_update_cache( h, a );
1838 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1841 a->l0.i_cost16x8 = COST_MAX;
1843 if( a->l0.i_cost8x16 <= thresh )
1845 h->mb.i_partition = D_8x16;
1846 x264_analyse_update_cache( h, a );
1847 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1850 a->l0.i_cost8x16 = COST_MAX;
1852 if( a->l0.i_cost8x8 <= thresh )
1854 h->mb.i_type = P_8x8;
1855 x264_analyse_update_cache( h, a );
1856 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1858 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1860 /* FIXME: RD per subpartition */
1864 for( i=0; i<4; i++ )
1866 part_bak[i] = h->mb.i_sub_partition[i];
1867 b_sub8x8 |= (part_bak[i] != D_L0_8x8);
1871 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1872 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1873 i_cost = x264_rd_cost_mb( h, a->i_lambda2 );
1874 if( a->l0.i_cost8x8 < i_cost )
1876 for( i=0; i<4; i++ )
1877 h->mb.i_sub_partition[i] = part_bak[i];
1880 a->l0.i_cost8x8 = i_cost;
1885 a->l0.i_cost8x8 = COST_MAX;
1888 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
1890 int thresh = i_satd_inter * 17/16;
1892 if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
1894 h->mb.i_type = B_DIRECT;
1895 x264_analyse_update_cache( h, a );
1896 a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
1899 //FIXME not all the update_cache calls are needed
1900 h->mb.i_partition = D_16x16;
1902 if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
1904 h->mb.i_type = B_L0_L0;
1905 x264_analyse_update_cache( h, a );
1906 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1910 if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
1912 h->mb.i_type = B_L1_L1;
1913 x264_analyse_update_cache( h, a );
1914 a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1918 if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
1920 h->mb.i_type = B_BI_BI;
1921 x264_analyse_update_cache( h, a );
1922 a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1926 if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
1928 h->mb.i_type = B_8x8;
1929 h->mb.i_partition = D_8x8;
1930 x264_analyse_update_cache( h, a );
1931 a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1932 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1936 if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
1938 h->mb.i_type = a->i_mb_type16x8;
1939 h->mb.i_partition = D_16x8;
1940 x264_analyse_update_cache( h, a );
1941 a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1945 if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
1947 h->mb.i_type = a->i_mb_type8x16;
1948 h->mb.i_partition = D_8x16;
1949 x264_analyse_update_cache( h, a );
1950 a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1954 static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
1956 const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1959 switch( h->mb.i_partition )
1962 if( h->mb.i_type == B_BI_BI )
1963 x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
1966 for( i=0; i<2; i++ )
1967 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
1968 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
1971 for( i=0; i<2; i++ )
1972 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
1973 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
1976 for( i=0; i<4; i++ )
1977 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
1978 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
1983 static inline void x264_mb_analyse_transform( x264_t *h )
1985 h->mb.cache.b_transform_8x8_allowed =
1986 h->pps->b_transform_8x8_mode
1987 && !IS_INTRA( h->mb.i_type ) && x264_mb_transform_8x8_allowed( h );
1989 if( h->mb.cache.b_transform_8x8_allowed && h->param.analyse.b_transform_8x8 )
1991 int i_cost4, i_cost8;
1992 /* FIXME only luma mc is needed */
1995 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
1996 h->mb.pic.p_fdec[0], FDEC_STRIDE );
1997 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
1998 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2000 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2004 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2006 h->mb.cache.b_transform_8x8_allowed =
2007 h->pps->b_transform_8x8_mode && x264_mb_transform_8x8_allowed( h );
2009 if( h->mb.cache.b_transform_8x8_allowed && h->param.analyse.b_transform_8x8 )
2012 x264_analyse_update_cache( h, a );
2013 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2014 /* FIXME only luma is needed, but the score for comparison already includes chroma */
2015 i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2017 if( *i_rd >= i_rd8 )
2020 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2021 /* prevent a rare division by zero in estimated intra cost */
2028 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2033 /*****************************************************************************
2034 * x264_macroblock_analyse:
2035 *****************************************************************************/
2036 void x264_macroblock_analyse( x264_t *h )
2038 x264_mb_analysis_t analysis;
2039 int i_cost = COST_MAX;
2043 x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
2045 /*--------------------------- Do the analysis ---------------------------*/
2046 if( h->sh.i_type == SLICE_TYPE_I )
2048 x264_mb_analyse_intra( h, &analysis, COST_MAX );
2049 if( analysis.b_mbrd )
2050 x264_intra_rd( h, &analysis, COST_MAX );
2052 i_cost = analysis.i_satd_i16x16;
2053 h->mb.i_type = I_16x16;
2054 if( analysis.i_satd_i4x4 < i_cost )
2056 i_cost = analysis.i_satd_i4x4;
2057 h->mb.i_type = I_4x4;
2059 if( analysis.i_satd_i8x8 < i_cost )
2060 h->mb.i_type = I_8x8;
2062 if( h->mb.i_subpel_refine >= 7 )
2063 x264_intra_rd_refine( h, &analysis );
2065 else if( h->sh.i_type == SLICE_TYPE_P )
2068 int i_intra_cost, i_intra_type;
2070 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2072 /* Fast P_SKIP detection */
2073 analysis.b_try_pskip = 0;
2074 if( h->param.analyse.b_fast_pskip )
2076 if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2077 // FIXME don't need to check this if the reference frame is done
2079 else if( h->param.analyse.i_subpel_refine >= 3 )
2080 analysis.b_try_pskip = 1;
2081 else if( h->mb.i_mb_type_left == P_SKIP ||
2082 h->mb.i_mb_type_top == P_SKIP ||
2083 h->mb.i_mb_type_topleft == P_SKIP ||
2084 h->mb.i_mb_type_topright == P_SKIP )
2085 b_skip = x264_macroblock_probe_pskip( h );
2088 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2092 h->mb.i_type = P_SKIP;
2093 h->mb.i_partition = D_16x16;
2094 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2098 const unsigned int flags = h->param.analyse.inter;
2102 int i_satd_inter, i_satd_intra;
2104 x264_mb_analyse_load_costs( h, &analysis );
2106 x264_mb_analyse_inter_p16x16( h, &analysis );
2108 if( h->mb.i_type == P_SKIP )
2111 if( flags & X264_ANALYSE_PSUB16x16 )
2113 if( h->param.analyse.b_mixed_references )
2114 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2116 x264_mb_analyse_inter_p8x8( h, &analysis );
2119 /* Select best inter mode */
2121 i_partition = D_16x16;
2122 i_cost = analysis.l0.me16x16.cost;
2124 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2125 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2128 i_partition = D_8x8;
2129 i_cost = analysis.l0.i_cost8x8;
2132 if( flags & X264_ANALYSE_PSUB8x8 )
2134 for( i = 0; i < 4; i++ )
2136 x264_mb_analyse_inter_p4x4( h, &analysis, i );
2137 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2139 int i_cost8x8 = analysis.l0.i_cost4x4[i];
2140 h->mb.i_sub_partition[i] = D_L0_4x4;
2142 x264_mb_analyse_inter_p8x4( h, &analysis, i );
2143 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2144 h->mb.i_sub_partition[i], D_L0_8x4 );
2146 x264_mb_analyse_inter_p4x8( h, &analysis, i );
2147 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2148 h->mb.i_sub_partition[i], D_L0_4x8 );
2150 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2152 x264_mb_cache_mv_p8x8( h, &analysis, i );
2154 analysis.l0.i_cost8x8 = i_cost;
2158 /* Now do 16x8/8x16 */
2159 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2160 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2161 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2163 x264_mb_analyse_inter_p16x8( h, &analysis );
2164 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2166 x264_mb_analyse_inter_p8x16( h, &analysis );
2167 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2170 h->mb.i_partition = i_partition;
2173 //FIXME mb_type costs?
2174 if( analysis.b_mbrd )
2178 else if( i_partition == D_16x16 )
2180 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2181 i_cost = analysis.l0.me16x16.cost;
2183 else if( i_partition == D_16x8 )
2185 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2186 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2187 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2189 else if( i_partition == D_8x16 )
2191 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2192 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2193 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2195 else if( i_partition == D_8x8 )
2199 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2201 switch( h->mb.i_sub_partition[i8x8] )
2204 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2205 i_cost += analysis.l0.me8x8[i8x8].cost;
2208 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2209 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2210 i_cost += analysis.l0.me8x4[i8x8][0].cost +
2211 analysis.l0.me8x4[i8x8][1].cost;
2214 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2215 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2216 i_cost += analysis.l0.me4x8[i8x8][0].cost +
2217 analysis.l0.me4x8[i8x8][1].cost;
2221 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2222 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2223 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2224 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2225 i_cost += analysis.l0.me4x4[i8x8][0].cost +
2226 analysis.l0.me4x4[i8x8][1].cost +
2227 analysis.l0.me4x4[i8x8][2].cost +
2228 analysis.l0.me4x4[i8x8][3].cost;
2231 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2237 if( h->mb.b_chroma_me )
2239 x264_mb_analyse_intra_chroma( h, &analysis );
2240 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2241 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2242 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2243 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2246 x264_mb_analyse_intra( h, &analysis, i_cost );
2248 i_satd_inter = i_cost;
2249 i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2250 analysis.i_satd_i8x8,
2251 analysis.i_satd_i4x4 );
2253 if( analysis.b_mbrd )
2255 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2257 i_partition = D_16x16;
2258 i_cost = analysis.l0.me16x16.cost;
2259 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2260 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2261 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2262 h->mb.i_type = i_type;
2263 h->mb.i_partition = i_partition;
2264 if( i_cost < COST_MAX )
2265 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2266 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2269 i_intra_type = I_16x16;
2270 i_intra_cost = analysis.i_satd_i16x16;
2271 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
2272 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
2273 COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
2275 if( i_intra_cost == COST_MAX )
2276 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
2278 h->mb.i_type = i_type;
2279 h->stat.frame.i_intra_cost += i_intra_cost;
2280 h->stat.frame.i_inter_cost += i_cost;
2281 h->stat.frame.i_mbs_analysed++;
2283 if( h->mb.i_subpel_refine >= 7 )
2285 if( IS_INTRA( h->mb.i_type ) )
2287 x264_intra_rd_refine( h, &analysis );
2289 else if( i_partition == D_16x16 )
2291 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2292 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
2294 else if( i_partition == D_16x8 )
2296 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2297 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2298 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
2299 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
2301 else if( i_partition == D_8x16 )
2303 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2304 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2305 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
2306 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
2308 else if( i_partition == D_8x8 )
2311 x264_analyse_update_cache( h, &analysis );
2312 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2313 if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2314 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
2319 else if( h->sh.i_type == SLICE_TYPE_B )
2321 int i_bskip_cost = COST_MAX;
2324 h->mb.i_type = B_SKIP;
2325 if( h->mb.b_direct_auto_write )
2327 /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2328 for( i = 0; i < 2; i++ )
2331 h->sh.b_direct_spatial_mv_pred ^= 1;
2332 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2333 if( analysis.b_direct_available )
2338 b_skip = x264_macroblock_probe_bskip( h );
2340 h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2347 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2349 if( analysis.b_direct_available )
2351 if( !h->mb.b_direct_auto_write )
2353 if( h->mb.b_lossless )
2355 /* chance of skip is too small to bother */
2357 else if( analysis.b_mbrd )
2359 i_bskip_cost = ssd_mb( h );
2361 /* 6 = minimum cavlc cost of a non-skipped MB */
2362 if( i_bskip_cost <= 6 * analysis.i_lambda2 )
2364 h->mb.i_type = B_SKIP;
2365 x264_analyse_update_cache( h, &analysis );
2369 else if( !h->mb.b_direct_auto_write )
2371 /* Conditioning the probe on neighboring block types
2372 * doesn't seem to help speed or quality. */
2373 b_skip = x264_macroblock_probe_bskip( h );
2379 const unsigned int flags = h->param.analyse.inter;
2383 x264_mb_analyse_load_costs( h, &analysis );
2385 /* select best inter mode */
2386 /* direct must be first */
2387 if( analysis.b_direct_available )
2388 x264_mb_analyse_inter_direct( h, &analysis );
2390 x264_mb_analyse_inter_b16x16( h, &analysis );
2393 i_partition = D_16x16;
2394 i_cost = analysis.l0.me16x16.cost;
2395 COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2396 COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2397 COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2399 if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2401 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2402 if( i_bskip_cost < analysis.i_rd16x16direct &&
2403 i_bskip_cost < analysis.i_rd16x16bi &&
2404 i_bskip_cost < analysis.l0.i_rd16x16 &&
2405 i_bskip_cost < analysis.l1.i_rd16x16 )
2407 h->mb.i_type = B_SKIP;
2408 x264_analyse_update_cache( h, &analysis );
2413 if( flags & X264_ANALYSE_BSUB16x16 )
2415 x264_mb_analyse_inter_b8x8( h, &analysis );
2416 if( analysis.i_cost8x8bi < i_cost )
2419 i_partition = D_8x8;
2420 i_cost = analysis.i_cost8x8bi;
2422 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2423 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2425 x264_mb_analyse_inter_b16x8( h, &analysis );
2426 COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2427 i_type, analysis.i_mb_type16x8,
2428 i_partition, D_16x8 );
2430 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2431 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2433 x264_mb_analyse_inter_b8x16( h, &analysis );
2434 COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2435 i_type, analysis.i_mb_type8x16,
2436 i_partition, D_8x16 );
2441 if( analysis.b_mbrd )
2446 else if( i_partition == D_16x16 )
2448 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2449 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2450 if( i_type == B_L0_L0 )
2452 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2453 i_cost = analysis.l0.me16x16.cost
2454 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2456 else if( i_type == B_L1_L1 )
2458 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2459 i_cost = analysis.l1.me16x16.cost
2460 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2462 else if( i_type == B_BI_BI )
2464 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2465 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2468 else if( i_partition == D_16x8 )
2470 for( i=0; i<2; i++ )
2472 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2473 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2474 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2475 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2478 else if( i_partition == D_8x16 )
2480 for( i=0; i<2; i++ )
2482 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2483 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2484 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2485 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2488 else if( i_partition == D_8x8 )
2490 for( i=0; i<4; i++ )
2493 int i_part_cost_old;
2495 int i_part_type = h->mb.i_sub_partition[i];
2496 int b_bidir = (i_part_type == D_BI_8x8);
2498 if( i_part_type == D_DIRECT_8x8 )
2500 if( x264_mb_partition_listX_table[0][i_part_type] )
2502 m = &analysis.l0.me8x8[i];
2503 i_part_cost_old = m->cost;
2504 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2505 m->cost -= i_type_cost;
2506 x264_me_refine_qpel( h, m );
2508 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2510 if( x264_mb_partition_listX_table[1][i_part_type] )
2512 m = &analysis.l1.me8x8[i];
2513 i_part_cost_old = m->cost;
2514 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2515 m->cost -= i_type_cost;
2516 x264_me_refine_qpel( h, m );
2518 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2520 /* TODO: update mvp? */
2524 x264_mb_analyse_intra( h, &analysis, i_cost );
2526 if( analysis.b_mbrd )
2528 int i_satd_inter = i_cost;
2529 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2531 i_cost = i_bskip_cost;
2532 i_partition = D_16x16;
2533 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2534 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2535 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2536 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2537 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2538 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2539 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2541 h->mb.i_type = i_type;
2542 h->mb.i_partition = i_partition;
2543 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2544 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2547 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2548 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2549 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2551 h->mb.i_type = i_type;
2552 h->mb.i_partition = i_partition;
2554 if( h->param.analyse.b_bidir_me )
2555 refine_bidir( h, &analysis );
2559 x264_analyse_update_cache( h, &analysis );
2561 if( !analysis.b_mbrd )
2562 x264_mb_analyse_transform( h );
2564 h->mb.b_trellis = h->param.analyse.i_trellis;
2565 h->mb.b_noise_reduction = h->param.analyse.i_noise_reduction;
2568 /*-------------------- Update MB from the analysis ----------------------*/
2569 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2573 switch( h->mb.i_type )
2576 for( i = 0; i < 16; i++ )
2577 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2579 x264_mb_analyse_intra_chroma( h, a );
2582 for( i = 0; i < 4; i++ )
2583 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2585 x264_mb_analyse_intra_chroma( h, a );
2588 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2589 x264_mb_analyse_intra_chroma( h, a );
2593 switch( h->mb.i_partition )
2596 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2597 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2601 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2602 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2603 x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv[0], a->l0.me16x8[0].mv[1] );
2604 x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv[0], a->l0.me16x8[1].mv[1] );
2608 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2609 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2610 x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv[0], a->l0.me8x16[0].mv[1] );
2611 x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv[0], a->l0.me8x16[1].mv[1] );
2615 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2621 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2622 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2623 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2624 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2625 for( i = 0; i < 4; i++ )
2626 x264_mb_cache_mv_p8x8( h, a, i );
2631 h->mb.i_partition = D_16x16;
2632 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2633 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv[0],
2634 h->mb.cache.pskip_mv[1] );
2640 x264_mb_load_mv_direct8x8( h, 0 );
2641 x264_mb_load_mv_direct8x8( h, 1 );
2642 x264_mb_load_mv_direct8x8( h, 2 );
2643 x264_mb_load_mv_direct8x8( h, 3 );
2647 /* optimize: cache might not need to be rewritten */
2648 for( i = 0; i < 4; i++ )
2649 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2652 default: /* the rest of the B types */
2653 switch( h->mb.i_partition )
2656 switch( h->mb.i_type )
2659 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2660 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2662 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2663 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0, 0 );
2664 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0, 0 );
2667 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2668 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0, 0 );
2669 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0, 0 );
2671 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2672 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2675 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2676 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2678 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2679 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2684 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2685 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2688 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2689 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2692 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2698 if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2701 for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2704 int ref = h->mb.cache.ref[l][x264_scan8[0]];
2707 completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2708 if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2710 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2711 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2712 h->mb.cache.mv[l][x264_scan8[15]][0],
2713 h->mb.cache.mv[l][x264_scan8[15]][1] );
2714 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2715 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2716 fprintf(stderr, "completed: %d \n", completed );
2724 #include "slicetype.c"