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 /* [ref][0] is 16x16 mv, [ref][1..4] are 8x8 mv from partition [0..3] */
48 DECLARE_ALIGNED_4( int16_t mvc[32][5][2] );
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 /* lambda2 = pow(lambda,2) * .9 * 256 */
142 static const int i_qp0_cost2_table[52] = {
143 14, 18, 22, 28, 36, 45, 57, 72, /* 0 - 7 */
144 91, 115, 145, 182, 230, 290, 365, 460, /* 8 - 15 */
145 580, 731, 921, 1161, 1462, 1843, 2322, 2925, /* 16 - 23 */
146 3686, 4644, 5851, 7372, 9289, 11703, 14745, 18578, /* 24 - 31 */
147 23407, 29491, 37156, 46814, 58982, 74313, 93628, 117964, /* 32 - 39 */
148 148626, 187257, 235929, 297252, 374514, 471859, 594505, 749029, /* 40 - 47 */
149 943718, 1189010, 1498059, 1887436 /* 48 - 51 */
152 /* TODO: calculate CABAC costs */
153 static const int i_mb_b_cost_table[X264_MBTYPE_MAX] = {
154 9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
156 static const int i_mb_b16x8_cost_table[17] = {
157 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
159 static const int i_sub_mb_b_cost_table[13] = {
160 7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
162 static const int i_sub_mb_p_cost_table[4] = {
166 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
168 uint16_t *x264_cost_mv_fpel[52][4];
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];
176 if( !p_cost_mv[a->i_qp] )
178 /* could be faster, but isn't called many times */
179 /* 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 );
188 a->p_cost_mv = p_cost_mv[a->i_qp];
190 /* FIXME is this useful for all me methods? */
191 if( h->param.analyse.i_me_method >= X264_ME_ESA && !x264_cost_mv_fpel[a->i_qp][0] )
195 x264_cost_mv_fpel[a->i_qp][j] = x264_malloc( (4*2048 + 1) * sizeof(int16_t) );
196 x264_cost_mv_fpel[a->i_qp][j] += 2*2048;
197 for( i = -2*2048; i < 2*2048; i++ )
198 x264_cost_mv_fpel[a->i_qp][j][i] = p_cost_mv[a->i_qp][i*4+j];
203 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
205 /* conduct the analysis using this lamda and QP */
206 a->i_qp = h->mb.i_qp = i_qp;
207 h->mb.i_chroma_qp = i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )];
208 a->i_lambda = i_qp0_cost_table[i_qp];
209 a->i_lambda2 = i_qp0_cost2_table[i_qp];
210 a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
211 ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
213 h->mb.i_me_method = h->param.analyse.i_me_method;
214 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
215 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
216 && h->mb.i_subpel_refine >= 5;
217 h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
218 h->mb.b_transform_8x8 = 0;
219 h->mb.b_noise_reduction = 0;
225 a->i_satd_i8x8chroma = COST_MAX;
229 h->mb.b_lossless ? 0 :
231 !h->param.analyse.i_trellis && !h->param.analyse.i_noise_reduction;
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 */ }
353 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
355 if( i_neighbour & MB_TOPLEFT )
357 /* top and left available */
358 *mode++ = I_PRED_16x16_V;
359 *mode++ = I_PRED_16x16_H;
360 *mode++ = I_PRED_16x16_DC;
361 *mode++ = I_PRED_16x16_P;
364 else if( i_neighbour & MB_LEFT )
367 *mode++ = I_PRED_16x16_DC_LEFT;
368 *mode++ = I_PRED_16x16_H;
371 else if( i_neighbour & MB_TOP )
374 *mode++ = I_PRED_16x16_DC_TOP;
375 *mode++ = I_PRED_16x16_V;
381 *mode = I_PRED_16x16_DC_128;
387 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
389 if( i_neighbour & MB_TOPLEFT )
391 /* top and left available */
392 *mode++ = I_PRED_CHROMA_V;
393 *mode++ = I_PRED_CHROMA_H;
394 *mode++ = I_PRED_CHROMA_DC;
395 *mode++ = I_PRED_CHROMA_P;
398 else if( i_neighbour & MB_LEFT )
401 *mode++ = I_PRED_CHROMA_DC_LEFT;
402 *mode++ = I_PRED_CHROMA_H;
405 else if( i_neighbour & MB_TOP )
408 *mode++ = I_PRED_CHROMA_DC_TOP;
409 *mode++ = I_PRED_CHROMA_V;
415 *mode = I_PRED_CHROMA_DC_128;
421 static void predict_4x4_mode_available( unsigned int i_neighbour,
422 int *mode, int *pi_count )
424 int b_l = i_neighbour & MB_LEFT;
425 int b_t = i_neighbour & MB_TOP;
430 *mode++ = I_PRED_4x4_DC;
431 *mode++ = I_PRED_4x4_H;
432 *mode++ = I_PRED_4x4_V;
433 *mode++ = I_PRED_4x4_DDL;
434 if( i_neighbour & MB_TOPLEFT )
436 *mode++ = I_PRED_4x4_DDR;
437 *mode++ = I_PRED_4x4_VR;
438 *mode++ = I_PRED_4x4_HD;
441 *mode++ = I_PRED_4x4_VL;
442 *mode++ = I_PRED_4x4_HU;
446 *mode++ = I_PRED_4x4_DC_LEFT;
447 *mode++ = I_PRED_4x4_H;
448 *mode++ = I_PRED_4x4_HU;
453 *mode++ = I_PRED_4x4_DC_TOP;
454 *mode++ = I_PRED_4x4_V;
455 *mode++ = I_PRED_4x4_DDL;
456 *mode++ = I_PRED_4x4_VL;
461 *mode++ = I_PRED_4x4_DC_128;
466 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
473 uint8_t *p_dstc[2], *p_srcc[2];
475 if( a->i_satd_i8x8chroma < COST_MAX )
478 /* 8x8 prediction selection for chroma */
479 p_dstc[0] = h->mb.pic.p_fdec[1];
480 p_dstc[1] = h->mb.pic.p_fdec[2];
481 p_srcc[0] = h->mb.pic.p_fenc[1];
482 p_srcc[1] = h->mb.pic.p_fenc[2];
484 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
485 a->i_satd_i8x8chroma = COST_MAX;
486 if( i_max == 4 && h->pixf.intra_satd_x3_8x8c && h->pixf.mbcmp[0] == h->pixf.satd[0] )
488 int satdu[4], satdv[4];
489 h->pixf.intra_satd_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
490 h->pixf.intra_satd_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
491 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
492 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
493 satdu[I_PRED_CHROMA_P] =
494 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
495 satdv[I_PRED_CHROMA_P] =
496 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
498 for( i=0; i<i_max; i++ )
500 int i_mode = predict_mode[i];
501 int i_satd = satdu[i_mode] + satdv[i_mode]
502 + a->i_lambda * bs_size_ue(i_mode);
504 a->i_satd_i8x8chroma_dir[i] = i_satd;
505 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
510 for( i=0; i<i_max; i++ )
513 int i_mode = predict_mode[i];
515 /* we do the prediction */
516 h->predict_8x8c[i_mode]( p_dstc[0] );
517 h->predict_8x8c[i_mode]( p_dstc[1] );
519 /* we calculate the cost */
520 i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
521 p_srcc[0], FENC_STRIDE ) +
522 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
523 p_srcc[1], FENC_STRIDE ) +
524 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
526 a->i_satd_i8x8chroma_dir[i] = i_satd;
527 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
531 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
534 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
536 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
537 uint8_t *p_src = h->mb.pic.p_fenc[0];
538 uint8_t *p_dst = h->mb.pic.p_fdec[0];
543 int b_merged_satd = h->pixf.intra_satd_x3_16x16 && h->pixf.mbcmp[0] == h->pixf.satd[0];
545 /*---------------- Try all mode and calculate their score ---------------*/
547 /* 16x16 prediction selection */
548 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
550 if( b_merged_satd && i_max == 4 )
552 h->pixf.intra_satd_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
553 h->predict_16x16[I_PRED_16x16_P]( p_dst );
554 a->i_satd_i16x16_dir[I_PRED_16x16_P] =
555 h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
558 int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
559 COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
564 for( i = 0; i < i_max; i++ )
567 int i_mode = predict_mode[i];
568 h->predict_16x16[i_mode]( p_dst );
570 i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
571 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
572 COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
573 a->i_satd_i16x16_dir[i_mode] = i_satd;
577 if( h->sh.i_type == SLICE_TYPE_B )
578 /* cavlc mb type prefix */
579 a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
580 if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
583 /* 8x8 prediction selection */
584 if( flags & X264_ANALYSE_I8x8 )
586 DECLARE_ALIGNED_16( uint8_t edge[33] );
587 x264_pixel_cmp_t sa8d = (*h->pixf.mbcmp == *h->pixf.sad) ? h->pixf.sad[PIXEL_8x8] : h->pixf.sa8d[PIXEL_8x8];
588 int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
590 b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
592 // FIXME some bias like in i4x4?
593 if( h->sh.i_type == SLICE_TYPE_B )
594 i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
596 for( idx = 0;; idx++ )
600 uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
601 uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
602 int i_best = COST_MAX;
603 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
605 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
606 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
608 if( b_merged_satd && i_max == 9 )
611 h->pixf.intra_sa8d_x3_8x8( p_src_by, edge, satd );
612 if( i_pred_mode < 3 )
613 satd[i_pred_mode] -= 3 * a->i_lambda;
614 for( i=2; i>=0; i-- )
616 int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
617 COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
624 for( ; i<i_max; i++ )
627 int i_mode = predict_mode[i];
629 h->predict_8x8[i_mode]( p_dst_by, edge );
631 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
632 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
634 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
635 a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
639 if( idx == 3 || i_cost > i_satd_thresh )
642 /* we need to encode this block now (for next ones) */
643 h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
644 x264_mb_encode_i8x8( h, idx, a->i_qp );
646 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
651 a->i_satd_i8x8 = i_cost;
652 if( h->mb.i_skip_intra )
654 h->mc.copy[PIXEL_16x16]( h->mb.pic.i8x8_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
655 if( h->mb.i_skip_intra == 2 )
656 h->mc.memcpy_aligned( h->mb.pic.i8x8_dct_buf, h->dct.luma8x8, sizeof(h->mb.pic.i8x8_dct_buf) );
661 a->i_satd_i8x8 = COST_MAX;
662 i_cost = i_cost * 4/(idx+1);
664 if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 )
668 /* 4x4 prediction selection */
669 if( flags & X264_ANALYSE_I4x4 )
672 int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
673 b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
675 i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
677 i_cost = a->i_lambda * 24; /* from JVT (SATD0) */
678 if( h->sh.i_type == SLICE_TYPE_B )
679 i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];
681 for( idx = 0;; idx++ )
683 int x = block_idx_x[idx];
684 int y = block_idx_y[idx];
685 uint8_t *p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
686 uint8_t *p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
687 int i_best = COST_MAX;
688 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
690 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
692 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
693 /* emulate missing topright samples */
694 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
696 if( b_merged_satd && i_max >= 6 )
699 h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
700 if( i_pred_mode < 3 )
701 satd[i_pred_mode] -= 3 * a->i_lambda;
702 for( i=2; i>=0; i-- )
703 COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
704 a->i_predict4x4[idx], i );
710 for( ; i<i_max; i++ )
713 int i_mode = predict_mode[i];
715 h->predict_4x4[i_mode]( p_dst_by );
717 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
718 p_src_by, FENC_STRIDE )
719 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
721 COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
725 if( i_cost > i_satd_thresh || idx == 15 )
728 /* we need to encode this block now (for next ones) */
729 h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
730 x264_mb_encode_i4x4( h, idx, a->i_qp );
732 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
736 a->i_satd_i4x4 = i_cost;
737 if( h->mb.i_skip_intra )
739 h->mc.copy[PIXEL_16x16]( h->mb.pic.i4x4_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
740 if( h->mb.i_skip_intra == 2 )
741 h->mc.memcpy_aligned( h->mb.pic.i4x4_dct_buf, h->dct.luma4x4, sizeof(h->mb.pic.i4x4_dct_buf) );
745 a->i_satd_i4x4 = COST_MAX;
749 static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
751 if( a->i_satd_i16x16 <= i_satd_thresh )
753 h->mb.i_type = I_16x16;
754 x264_analyse_update_cache( h, a );
755 a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
758 a->i_satd_i16x16 = COST_MAX;
760 if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
762 h->mb.i_type = I_4x4;
763 x264_analyse_update_cache( h, a );
764 a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
767 a->i_satd_i4x4 = COST_MAX;
769 if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
771 h->mb.i_type = I_8x8;
772 x264_analyse_update_cache( h, a );
773 a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
776 a->i_satd_i8x8 = COST_MAX;
779 static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
781 uint8_t *p_src = h->mb.pic.p_fenc[0];
782 uint8_t *p_dst = h->mb.pic.p_fdec[0];
785 int i_max, i_satd, i_best, i_mode, i_thresh;
788 h->mb.i_skip_intra = 0;
790 if( h->mb.i_type == I_16x16 )
792 int old_pred_mode = a->i_predict16x16;
793 i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
794 i_best = a->i_satd_i16x16;
795 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
796 for( i = 0; i < i_max; i++ )
798 int i_mode = predict_mode[i];
799 if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
801 h->mb.i_intra16x16_pred_mode = i_mode;
802 i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
803 COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
806 else if( h->mb.i_type == I_4x4 )
808 uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
810 for( idx = 0; idx < 16; idx++ )
816 i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
817 x = block_idx_x[idx];
818 y = block_idx_y[idx];
820 p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
821 p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
822 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
824 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
825 /* emulate missing topright samples */
826 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
828 for( i = 0; i < i_max; i++ )
830 i_mode = predict_mode[i];
831 h->predict_4x4[i_mode]( p_dst_by );
832 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
834 if( i_best > i_satd )
836 a->i_predict4x4[idx] = i_mode;
838 pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
839 pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
840 pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
841 pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
842 i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
846 *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
847 *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
848 *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
849 *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
850 h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
852 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
855 else if( h->mb.i_type == I_8x8 )
857 DECLARE_ALIGNED_16( uint8_t edge[33] );
858 for( idx = 0; idx < 4; idx++ )
866 i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
869 i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
873 p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
874 p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
875 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
876 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
878 for( i = 0; i < i_max; i++ )
880 i_mode = predict_mode[i];
881 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
883 h->predict_8x8[i_mode]( p_dst_by, edge );
884 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
886 if( i_best > i_satd )
888 a->i_predict8x8[idx] = i_mode;
891 pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
894 pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
896 i_nnz[j] = h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]];
900 *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
903 p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
905 h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]] = i_nnz[j];
907 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
911 /* RD selection for chroma prediction */
912 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
915 i_thresh = a->i_satd_i8x8chroma * 5/4;
917 for( i = j = 0; i < i_max; i++ )
918 if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
919 predict_mode[i] != a->i_predict8x8chroma )
921 predict_mode[j++] = predict_mode[i];
927 int i_chroma_lambda = i_qp0_cost2_table[h->mb.i_chroma_qp];
928 /* the previous thing encoded was x264_intra_rd(), so the pixels and
929 * coefs for the current chroma mode are still around, so we only
930 * have to recount the bits. */
931 i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
932 for( i = 0; i < i_max; i++ )
934 i_mode = predict_mode[i];
935 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
936 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
937 /* if we've already found a mode that needs no residual, then
938 * probably any mode with a residual will be worse.
939 * so avoid dct on the remaining modes to improve speed. */
940 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
941 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode );
943 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
948 #define LOAD_FENC( m, src, xoff, yoff) \
949 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
950 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
951 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
952 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
953 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
955 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
956 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
957 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
958 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
959 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
960 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
961 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
962 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
964 #define REF_COST(list, ref) \
965 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
967 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
971 DECLARE_ALIGNED_4( int16_t mvc[7][2] );
972 int i_halfpel_thresh = INT_MAX;
973 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
975 /* 16x16 Search on all ref frame */
976 m.i_pixel = PIXEL_16x16;
977 m.p_cost_mv = a->p_cost_mv;
978 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
980 a->l0.me16x16.cost = INT_MAX;
981 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
983 const int i_ref_cost = REF_COST( 0, i_ref );
984 i_halfpel_thresh -= i_ref_cost;
985 m.i_ref_cost = i_ref_cost;
988 /* search with ref */
989 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
990 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
991 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
992 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
995 * SSD threshold would probably be better than SATD */
998 && m.cost-m.cost_mv < 300*a->i_lambda
999 && abs(m.mv[0]-h->mb.cache.pskip_mv[0])
1000 + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
1001 && x264_macroblock_probe_pskip( h ) )
1003 h->mb.i_type = P_SKIP;
1004 x264_analyse_update_cache( h, a );
1005 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1009 m.cost += i_ref_cost;
1010 i_halfpel_thresh += i_ref_cost;
1012 if( m.cost < a->l0.me16x16.cost )
1013 h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1015 /* save mv for predicting neighbors */
1016 *(uint32_t*)a->l0.mvc[i_ref][0] =
1017 *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1020 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
1021 assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1023 h->mb.i_type = P_L0;
1024 if( a->b_mbrd && a->l0.me16x16.i_ref == 0
1025 && *(uint32_t*)a->l0.me16x16.mv == *(uint32_t*)h->mb.cache.pskip_mv )
1027 h->mb.i_partition = D_16x16;
1028 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
1029 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1033 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
1037 uint8_t **p_fenc = h->mb.pic.p_fenc;
1038 int i_halfpel_thresh = INT_MAX;
1039 int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
1041 int i_maxref = h->mb.pic.i_fref[0]-1;
1043 h->mb.i_partition = D_8x8;
1045 /* early termination: if 16x16 chose ref 0, then evalute no refs older
1046 * than those used by the neighbors */
1047 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
1048 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
1051 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
1052 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
1053 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
1054 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
1055 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
1056 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
1059 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1060 *(uint32_t*)a->l0.mvc[i_ref][0] = *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy];
1062 for( i = 0; i < 4; i++ )
1064 x264_me_t *l0m = &a->l0.me8x8[i];
1068 m.i_pixel = PIXEL_8x8;
1069 m.p_cost_mv = a->p_cost_mv;
1071 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
1072 l0m->cost = INT_MAX;
1073 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1075 const int i_ref_cost = REF_COST( 0, i_ref );
1076 i_halfpel_thresh -= i_ref_cost;
1077 m.i_ref_cost = i_ref_cost;
1080 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
1081 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
1082 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1083 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
1085 m.cost += i_ref_cost;
1086 i_halfpel_thresh += i_ref_cost;
1087 *(uint32_t*)a->l0.mvc[i_ref][i+1] = *(uint32_t*)m.mv;
1089 if( m.cost < l0m->cost )
1090 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1092 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv );
1093 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1096 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1099 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1100 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1101 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1102 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1105 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1107 const int i_ref = a->l0.me16x16.i_ref;
1108 const int i_ref_cost = REF_COST( 0, i_ref );
1109 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
1110 uint8_t **p_fenc = h->mb.pic.p_fenc;
1112 int16_t (*mvc)[2] = a->l0.mvc[i_ref];
1115 /* XXX Needed for x264_mb_predict_mv */
1116 h->mb.i_partition = D_8x8;
1119 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.me16x16.mv;
1121 for( i = 0; i < 4; i++ )
1123 x264_me_t *m = &a->l0.me8x8[i];
1127 m->i_pixel = PIXEL_8x8;
1128 m->p_cost_mv = a->p_cost_mv;
1129 m->i_ref_cost = i_ref_cost;
1132 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1133 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1134 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1135 x264_me_search( h, m, mvc, i_mvc );
1137 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, m->mv );
1139 *(uint32_t*)mvc[i_mvc] = *(uint32_t*)m->mv;
1143 m->cost += i_ref_cost;
1144 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1147 /* theoretically this should include 4*ref_cost,
1148 * but 3 seems a better approximation of cabac. */
1149 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1150 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
1151 REF_COST( 0, a->l0.me16x16.i_ref );
1152 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1153 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1156 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
1159 uint8_t **p_fenc = h->mb.pic.p_fenc;
1160 DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1163 /* XXX Needed for x264_mb_predict_mv */
1164 h->mb.i_partition = D_16x8;
1166 for( i = 0; i < 2; i++ )
1168 x264_me_t *l0m = &a->l0.me16x8[i];
1169 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1170 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1172 m.i_pixel = PIXEL_16x8;
1173 m.p_cost_mv = a->p_cost_mv;
1175 LOAD_FENC( &m, p_fenc, 0, 8*i );
1176 l0m->cost = INT_MAX;
1177 for( j = 0; j < i_ref8s; j++ )
1179 const int i_ref = ref8[j];
1180 const int i_ref_cost = REF_COST( 0, i_ref );
1181 m.i_ref_cost = i_ref_cost;
1184 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1185 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1186 *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][2*i+1];
1187 *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][2*i+2];
1189 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1190 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1191 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1192 x264_me_search( h, &m, mvc, 3 );
1194 m.cost += i_ref_cost;
1196 if( m.cost < l0m->cost )
1197 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1199 x264_macroblock_cache_mv_ptr( h, 0, 2*i, 4, 2, 0, l0m->mv );
1200 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1203 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1206 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1209 uint8_t **p_fenc = h->mb.pic.p_fenc;
1210 DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1213 /* XXX Needed for x264_mb_predict_mv */
1214 h->mb.i_partition = D_8x16;
1216 for( i = 0; i < 2; i++ )
1218 x264_me_t *l0m = &a->l0.me8x16[i];
1219 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1220 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1222 m.i_pixel = PIXEL_8x16;
1223 m.p_cost_mv = a->p_cost_mv;
1225 LOAD_FENC( &m, p_fenc, 8*i, 0 );
1226 l0m->cost = INT_MAX;
1227 for( j = 0; j < i_ref8s; j++ )
1229 const int i_ref = ref8[j];
1230 const int i_ref_cost = REF_COST( 0, i_ref );
1231 m.i_ref_cost = i_ref_cost;
1234 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1235 *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][i+1];
1236 *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][i+3];
1238 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1239 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1240 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1241 x264_me_search( h, &m, mvc, 3 );
1243 m.cost += i_ref_cost;
1245 if( m.cost < l0m->cost )
1246 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1248 x264_macroblock_cache_mv_ptr( h, 2*i, 0, 2, 4, 0, l0m->mv );
1249 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1252 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1255 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1257 DECLARE_ALIGNED_8( uint8_t pix1[16*8] );
1258 uint8_t *pix2 = pix1+8;
1259 const int i_stride = h->mb.pic.i_stride[1];
1260 const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1261 const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1263 #define CHROMA4x4MC( width, height, me, x, y ) \
1264 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 ); \
1265 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 );
1267 if( pixel == PIXEL_4x4 )
1269 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1270 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 2,0 );
1271 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 0,2 );
1272 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1274 else if( pixel == PIXEL_8x4 )
1276 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1277 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1281 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1282 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1285 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1286 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1289 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1291 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1292 uint8_t **p_fenc = h->mb.pic.p_fenc;
1293 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1296 /* XXX Needed for x264_mb_predict_mv */
1297 h->mb.i_partition = D_8x8;
1299 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1301 const int idx = 4*i8x8 + i4x4;
1302 const int x4 = block_idx_x[idx];
1303 const int y4 = block_idx_y[idx];
1304 const int i_mvc = (i4x4 == 0);
1306 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1308 m->i_pixel = PIXEL_4x4;
1309 m->p_cost_mv = a->p_cost_mv;
1311 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1312 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1314 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1315 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1317 x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 1, 0, m->mv );
1319 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1320 a->l0.me4x4[i8x8][1].cost +
1321 a->l0.me4x4[i8x8][2].cost +
1322 a->l0.me4x4[i8x8][3].cost +
1323 REF_COST( 0, i_ref ) +
1324 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1325 if( h->mb.b_chroma_me )
1326 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1329 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1331 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1332 uint8_t **p_fenc = h->mb.pic.p_fenc;
1333 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1336 /* XXX Needed for x264_mb_predict_mv */
1337 h->mb.i_partition = D_8x8;
1339 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1341 const int idx = 4*i8x8 + 2*i8x4;
1342 const int x4 = block_idx_x[idx];
1343 const int y4 = block_idx_y[idx];
1344 const int i_mvc = (i8x4 == 0);
1346 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1348 m->i_pixel = PIXEL_8x4;
1349 m->p_cost_mv = a->p_cost_mv;
1351 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1352 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1354 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1355 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1357 x264_macroblock_cache_mv_ptr( h, x4, y4, 2, 1, 0, m->mv );
1359 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1360 REF_COST( 0, i_ref ) +
1361 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1362 if( h->mb.b_chroma_me )
1363 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1366 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1368 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1369 uint8_t **p_fenc = h->mb.pic.p_fenc;
1370 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1373 /* XXX Needed for x264_mb_predict_mv */
1374 h->mb.i_partition = D_8x8;
1376 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1378 const int idx = 4*i8x8 + i4x8;
1379 const int x4 = block_idx_x[idx];
1380 const int y4 = block_idx_y[idx];
1381 const int i_mvc = (i4x8 == 0);
1383 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1385 m->i_pixel = PIXEL_4x8;
1386 m->p_cost_mv = a->p_cost_mv;
1388 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1389 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1391 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1392 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1394 x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 2, 0, m->mv );
1396 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1397 REF_COST( 0, i_ref ) +
1398 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1399 if( h->mb.b_chroma_me )
1400 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1403 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1405 /* Assumes that fdec still contains the results of
1406 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1408 uint8_t **p_fenc = h->mb.pic.p_fenc;
1409 uint8_t **p_fdec = h->mb.pic.p_fdec;
1412 a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1413 for( i = 0; i < 4; i++ )
1415 const int x = (i&1)*8;
1416 const int y = (i>>1)*8;
1417 a->i_cost16x16direct +=
1418 a->i_cost8x8direct[i] =
1419 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1422 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1426 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1428 if( h->param.analyse.b_weighted_bipred ) \
1429 h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1430 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1432 h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1435 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1437 DECLARE_ALIGNED_16( uint8_t pix1[16*16] );
1438 DECLARE_ALIGNED_16( uint8_t pix2[16*16] );
1445 DECLARE_ALIGNED_4( int16_t mvc[8][2] );
1446 int i_halfpel_thresh = INT_MAX;
1447 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1449 /* 16x16 Search on all ref frame */
1450 m.i_pixel = PIXEL_16x16;
1451 m.p_cost_mv = a->p_cost_mv;
1452 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1455 a->l0.me16x16.cost = INT_MAX;
1456 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1458 /* search with ref */
1459 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1460 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1461 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1462 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1465 m.cost += REF_COST( 0, i_ref );
1467 if( m.cost < a->l0.me16x16.cost )
1469 a->l0.i_ref = i_ref;
1470 h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1473 /* save mv for predicting neighbors */
1474 *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1476 /* subtract ref cost, so we don't have to add it for the other MB types */
1477 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1480 i_halfpel_thresh = INT_MAX;
1481 p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1482 a->l1.me16x16.cost = INT_MAX;
1483 for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1485 /* search with ref */
1486 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1487 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1488 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1489 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1492 m.cost += REF_COST( 1, i_ref );
1494 if( m.cost < a->l1.me16x16.cost )
1496 a->l1.i_ref = i_ref;
1497 h->mc.memcpy_aligned( &a->l1.me16x16, &m, sizeof(x264_me_t) );
1500 /* save mv for predicting neighbors */
1501 *(uint32_t*)h->mb.mvr[1][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1503 /* subtract ref cost, so we don't have to add it for the other MB types */
1504 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1506 /* Set global ref, needed for other modes? */
1507 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1508 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1510 /* get cost of BI mode */
1511 weight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1512 if ( (*(uint32_t*)a->l0.me16x16.mv & 0x10001) == 0 )
1514 /* l0 reference is halfpel, so get_ref on it will make it faster */
1516 h->mc.get_ref( pix2, &stride2,
1517 h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1518 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1520 h->mc.mc_luma( pix1, 16,
1521 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1522 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1524 weight = 64 - weight;
1528 /* if l0 was qpel, we'll use get_ref on l1 instead */
1529 h->mc.mc_luma( pix1, 16,
1530 h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1531 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1534 h->mc.get_ref( pix2, &stride2,
1535 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1536 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1540 if( h->param.analyse.b_weighted_bipred )
1541 h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2, weight );
1543 h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1545 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix1, 16 )
1546 + REF_COST( 0, a->l0.i_ref )
1547 + REF_COST( 1, a->l1.i_ref )
1548 + a->l0.me16x16.cost_mv
1549 + a->l1.me16x16.cost_mv;
1552 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1553 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1554 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1557 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1559 const int x = 2*(i%2);
1560 const int y = 2*(i/2);
1562 switch( h->mb.i_sub_partition[i] )
1565 x264_macroblock_cache_mv_ptr( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv );
1568 x264_macroblock_cache_mv_ptr( h, x, y+0, 2, 1, 0, a->l0.me8x4[i][0].mv );
1569 x264_macroblock_cache_mv_ptr( h, x, y+1, 2, 1, 0, a->l0.me8x4[i][1].mv );
1572 x264_macroblock_cache_mv_ptr( h, x+0, y, 1, 2, 0, a->l0.me4x8[i][0].mv );
1573 x264_macroblock_cache_mv_ptr( h, x+1, y, 1, 2, 0, a->l0.me4x8[i][1].mv );
1576 x264_macroblock_cache_mv_ptr( h, x+0, y+0, 1, 1, 0, a->l0.me4x4[i][0].mv );
1577 x264_macroblock_cache_mv_ptr( h, x+1, y+0, 1, 1, 0, a->l0.me4x4[i][1].mv );
1578 x264_macroblock_cache_mv_ptr( h, x+0, y+1, 1, 1, 0, a->l0.me4x4[i][2].mv );
1579 x264_macroblock_cache_mv_ptr( h, x+1, y+1, 1, 1, 0, a->l0.me4x4[i][3].mv );
1582 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1587 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1588 if( x264_mb_partition_listX_table[0][part] ) \
1590 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1591 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 0, me0.mv ); \
1595 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1596 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0 ); \
1598 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0 ); \
1600 if( x264_mb_partition_listX_table[1][part] ) \
1602 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1603 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 1, me1.mv ); \
1607 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1608 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0 ); \
1610 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0 ); \
1613 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1617 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1619 x264_mb_load_mv_direct8x8( h, i );
1622 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0 );
1623 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0 );
1624 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1629 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1632 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1634 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1636 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1638 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1642 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1644 uint8_t **p_fref[2] =
1645 { h->mb.pic.p_fref[0][a->l0.i_ref],
1646 h->mb.pic.p_fref[1][a->l1.i_ref] };
1647 DECLARE_ALIGNED_8( uint8_t pix[2][8*8] );
1650 /* XXX Needed for x264_mb_predict_mv */
1651 h->mb.i_partition = D_8x8;
1655 for( i = 0; i < 4; i++ )
1660 int i_part_cost_bi = 0;
1662 for( l = 0; l < 2; l++ )
1664 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1665 x264_me_t *m = &lX->me8x8[i];
1667 m->i_pixel = PIXEL_8x8;
1668 m->p_cost_mv = a->p_cost_mv;
1670 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1671 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1673 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1674 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1676 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, l, m->mv );
1679 h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
1680 m->mv[0], m->mv[1], 8, 8 );
1681 i_part_cost_bi += m->cost_mv;
1682 /* FIXME: ref cost */
1685 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1686 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1687 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1688 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1689 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1691 i_part_cost = a->l0.me8x8[i].cost;
1692 h->mb.i_sub_partition[i] = D_L0_8x8;
1693 COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1694 COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1695 COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1696 a->i_cost8x8bi += i_part_cost;
1698 /* XXX Needed for x264_mb_predict_mv */
1699 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1703 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1706 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1708 uint8_t **p_fref[2] =
1709 { h->mb.pic.p_fref[0][a->l0.i_ref],
1710 h->mb.pic.p_fref[1][a->l1.i_ref] };
1711 DECLARE_ALIGNED_16( uint8_t pix[2][16*8] );
1712 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1715 h->mb.i_partition = D_16x8;
1716 a->i_cost16x8bi = 0;
1718 for( i = 0; i < 2; i++ )
1721 int i_part_cost_bi = 0;
1723 /* TODO: check only the list(s) that were used in b8x8? */
1724 for( l = 0; l < 2; l++ )
1726 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1727 x264_me_t *m = &lX->me16x8[i];
1729 m->i_pixel = PIXEL_16x8;
1730 m->p_cost_mv = a->p_cost_mv;
1732 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1733 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1735 *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[2*i].mv;
1736 *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[2*i+1].mv;
1738 x264_mb_predict_mv( h, l, 8*i, 2, m->mvp );
1739 x264_me_search( h, m, mvc, 2 );
1742 h->mc.mc_luma( pix[l], 16, m->p_fref, m->i_stride[0],
1743 m->mv[0], m->mv[1], 16, 8 );
1744 /* FIXME: ref cost */
1745 i_part_cost_bi += m->cost_mv;
1748 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1749 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1751 i_part_cost = a->l0.me16x8[i].cost;
1752 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1753 if( a->l1.me16x8[i].cost < i_part_cost )
1755 i_part_cost = a->l1.me16x8[i].cost;
1756 a->i_mb_partition16x8[i] = D_L1_8x8;
1758 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1760 i_part_cost = i_part_cost_bi;
1761 a->i_mb_partition16x8[i] = D_BI_8x8;
1763 a->i_cost16x8bi += i_part_cost;
1765 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1769 a->i_mb_type16x8 = B_L0_L0
1770 + (a->i_mb_partition16x8[0]>>2) * 3
1771 + (a->i_mb_partition16x8[1]>>2);
1772 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1775 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1777 uint8_t **p_fref[2] =
1778 { h->mb.pic.p_fref[0][a->l0.i_ref],
1779 h->mb.pic.p_fref[1][a->l1.i_ref] };
1780 DECLARE_ALIGNED_8( uint8_t pix[2][8*16] );
1781 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1784 h->mb.i_partition = D_8x16;
1785 a->i_cost8x16bi = 0;
1787 for( i = 0; i < 2; i++ )
1790 int i_part_cost_bi = 0;
1792 for( l = 0; l < 2; l++ )
1794 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1795 x264_me_t *m = &lX->me8x16[i];
1797 m->i_pixel = PIXEL_8x16;
1798 m->p_cost_mv = a->p_cost_mv;
1800 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1801 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1803 *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[i].mv;
1804 *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[i+2].mv;
1806 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1807 x264_me_search( h, m, mvc, 2 );
1810 h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
1811 m->mv[0], m->mv[1], 8, 16 );
1812 /* FIXME: ref cost */
1813 i_part_cost_bi += m->cost_mv;
1816 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1817 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1819 i_part_cost = a->l0.me8x16[i].cost;
1820 a->i_mb_partition8x16[i] = D_L0_8x8;
1821 if( a->l1.me8x16[i].cost < i_part_cost )
1823 i_part_cost = a->l1.me8x16[i].cost;
1824 a->i_mb_partition8x16[i] = D_L1_8x8;
1826 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1828 i_part_cost = i_part_cost_bi;
1829 a->i_mb_partition8x16[i] = D_BI_8x8;
1831 a->i_cost8x16bi += i_part_cost;
1833 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1837 a->i_mb_type8x16 = B_L0_L0
1838 + (a->i_mb_partition8x16[0]>>2) * 3
1839 + (a->i_mb_partition8x16[1]>>2);
1840 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1843 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1845 int thresh = i_satd * 5/4;
1847 h->mb.i_type = P_L0;
1848 if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1850 h->mb.i_partition = D_16x16;
1851 x264_analyse_update_cache( h, a );
1852 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1854 a->l0.me16x16.cost = a->l0.i_rd16x16;
1856 if( a->l0.i_cost16x8 <= thresh )
1858 h->mb.i_partition = D_16x8;
1859 x264_analyse_update_cache( h, a );
1860 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1863 a->l0.i_cost16x8 = COST_MAX;
1865 if( a->l0.i_cost8x16 <= thresh )
1867 h->mb.i_partition = D_8x16;
1868 x264_analyse_update_cache( h, a );
1869 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1872 a->l0.i_cost8x16 = COST_MAX;
1874 if( a->l0.i_cost8x8 <= thresh )
1876 h->mb.i_type = P_8x8;
1877 h->mb.i_partition = D_8x8;
1878 x264_analyse_update_cache( h, a );
1879 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1881 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1883 /* FIXME: RD per subpartition */
1887 for( i=0; i<4; i++ )
1889 part_bak[i] = h->mb.i_sub_partition[i];
1890 b_sub8x8 |= (part_bak[i] != D_L0_8x8);
1894 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1895 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1896 x264_analyse_update_cache( h, a );
1897 i_cost = x264_rd_cost_mb( h, a->i_lambda2 );
1898 if( a->l0.i_cost8x8 < i_cost )
1900 for( i=0; i<4; i++ )
1901 h->mb.i_sub_partition[i] = part_bak[i];
1904 a->l0.i_cost8x8 = i_cost;
1909 a->l0.i_cost8x8 = COST_MAX;
1912 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
1914 int thresh = i_satd_inter * 17/16;
1916 if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
1918 h->mb.i_type = B_DIRECT;
1919 /* Assumes direct/skip MC is still in fdec */
1920 /* Requires b-rdo to be done before intra analysis */
1921 h->mb.b_skip_mc = 1;
1922 x264_analyse_update_cache( h, a );
1923 a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
1924 h->mb.b_skip_mc = 0;
1927 //FIXME not all the update_cache calls are needed
1928 h->mb.i_partition = D_16x16;
1930 if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
1932 h->mb.i_type = B_L0_L0;
1933 x264_analyse_update_cache( h, a );
1934 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1938 if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
1940 h->mb.i_type = B_L1_L1;
1941 x264_analyse_update_cache( h, a );
1942 a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1946 if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
1948 h->mb.i_type = B_BI_BI;
1949 x264_analyse_update_cache( h, a );
1950 a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1954 if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
1956 h->mb.i_type = B_8x8;
1957 h->mb.i_partition = D_8x8;
1958 x264_analyse_update_cache( h, a );
1959 a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1960 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1964 if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
1966 h->mb.i_type = a->i_mb_type16x8;
1967 h->mb.i_partition = D_16x8;
1968 x264_analyse_update_cache( h, a );
1969 a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1973 if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
1975 h->mb.i_type = a->i_mb_type8x16;
1976 h->mb.i_partition = D_8x16;
1977 x264_analyse_update_cache( h, a );
1978 a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1982 static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
1984 const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1987 switch( h->mb.i_partition )
1990 if( h->mb.i_type == B_BI_BI )
1991 x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
1994 for( i=0; i<2; i++ )
1995 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
1996 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
1999 for( i=0; i<2; i++ )
2000 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
2001 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
2004 for( i=0; i<4; i++ )
2005 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
2006 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
2011 static inline void x264_mb_analyse_transform( x264_t *h )
2013 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2015 int i_cost4, i_cost8;
2016 /* Only luma MC is really needed, but the full MC is re-used in macroblock_encode. */
2019 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2020 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2021 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2022 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2024 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2025 h->mb.b_skip_mc = 1;
2029 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2031 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2034 x264_analyse_update_cache( h, a );
2035 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2036 /* FIXME only luma is needed, but the score for comparison already includes chroma */
2037 i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2039 if( *i_rd >= i_rd8 )
2042 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2043 /* prevent a rare division by zero in estimated intra cost */
2050 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2055 /*****************************************************************************
2056 * x264_macroblock_analyse:
2057 *****************************************************************************/
2058 void x264_macroblock_analyse( x264_t *h )
2060 x264_mb_analysis_t analysis;
2061 int i_cost = COST_MAX;
2064 h->mb.i_qp = x264_ratecontrol_qp( h );
2065 if( h->param.rc.i_aq_mode )
2066 x264_adaptive_quant( h );
2068 x264_mb_analyse_init( h, &analysis, h->mb.i_qp );
2070 /*--------------------------- Do the analysis ---------------------------*/
2071 if( h->sh.i_type == SLICE_TYPE_I )
2073 x264_mb_analyse_intra( h, &analysis, COST_MAX );
2074 if( analysis.b_mbrd )
2075 x264_intra_rd( h, &analysis, COST_MAX );
2077 i_cost = analysis.i_satd_i16x16;
2078 h->mb.i_type = I_16x16;
2079 if( analysis.i_satd_i4x4 < i_cost )
2081 i_cost = analysis.i_satd_i4x4;
2082 h->mb.i_type = I_4x4;
2084 if( analysis.i_satd_i8x8 < i_cost )
2085 h->mb.i_type = I_8x8;
2087 if( h->mb.i_subpel_refine >= 7 )
2088 x264_intra_rd_refine( h, &analysis );
2090 else if( h->sh.i_type == SLICE_TYPE_P )
2093 int i_intra_cost, i_intra_type;
2095 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2097 /* Fast P_SKIP detection */
2098 analysis.b_try_pskip = 0;
2099 if( h->param.analyse.b_fast_pskip )
2101 if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2102 // FIXME don't need to check this if the reference frame is done
2104 else if( h->param.analyse.i_subpel_refine >= 3 )
2105 analysis.b_try_pskip = 1;
2106 else if( h->mb.i_mb_type_left == P_SKIP ||
2107 h->mb.i_mb_type_top == P_SKIP ||
2108 h->mb.i_mb_type_topleft == P_SKIP ||
2109 h->mb.i_mb_type_topright == P_SKIP )
2110 b_skip = x264_macroblock_probe_pskip( h );
2113 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2117 h->mb.i_type = P_SKIP;
2118 h->mb.i_partition = D_16x16;
2119 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2123 const unsigned int flags = h->param.analyse.inter;
2127 int i_satd_inter, i_satd_intra;
2129 x264_mb_analyse_load_costs( h, &analysis );
2131 x264_mb_analyse_inter_p16x16( h, &analysis );
2133 if( h->mb.i_type == P_SKIP )
2136 if( flags & X264_ANALYSE_PSUB16x16 )
2138 if( h->param.analyse.b_mixed_references )
2139 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2141 x264_mb_analyse_inter_p8x8( h, &analysis );
2144 /* Select best inter mode */
2146 i_partition = D_16x16;
2147 i_cost = analysis.l0.me16x16.cost;
2149 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2150 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2153 i_partition = D_8x8;
2154 i_cost = analysis.l0.i_cost8x8;
2157 if( flags & X264_ANALYSE_PSUB8x8 )
2159 for( i = 0; i < 4; i++ )
2161 x264_mb_analyse_inter_p4x4( h, &analysis, i );
2162 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2164 int i_cost8x8 = analysis.l0.i_cost4x4[i];
2165 h->mb.i_sub_partition[i] = D_L0_4x4;
2167 x264_mb_analyse_inter_p8x4( h, &analysis, i );
2168 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2169 h->mb.i_sub_partition[i], D_L0_8x4 );
2171 x264_mb_analyse_inter_p4x8( h, &analysis, i );
2172 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2173 h->mb.i_sub_partition[i], D_L0_4x8 );
2175 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2177 x264_mb_cache_mv_p8x8( h, &analysis, i );
2179 analysis.l0.i_cost8x8 = i_cost;
2183 /* Now do 16x8/8x16 */
2184 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2185 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2186 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2188 x264_mb_analyse_inter_p16x8( h, &analysis );
2189 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2191 x264_mb_analyse_inter_p8x16( h, &analysis );
2192 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2195 h->mb.i_partition = i_partition;
2198 //FIXME mb_type costs?
2199 if( analysis.b_mbrd )
2203 else if( i_partition == D_16x16 )
2205 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2206 i_cost = analysis.l0.me16x16.cost;
2208 else if( i_partition == D_16x8 )
2210 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2211 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2212 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2214 else if( i_partition == D_8x16 )
2216 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2217 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2218 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2220 else if( i_partition == D_8x8 )
2224 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2226 switch( h->mb.i_sub_partition[i8x8] )
2229 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2230 i_cost += analysis.l0.me8x8[i8x8].cost;
2233 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2234 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2235 i_cost += analysis.l0.me8x4[i8x8][0].cost +
2236 analysis.l0.me8x4[i8x8][1].cost;
2239 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2240 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2241 i_cost += analysis.l0.me4x8[i8x8][0].cost +
2242 analysis.l0.me4x8[i8x8][1].cost;
2246 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2247 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2248 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2249 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2250 i_cost += analysis.l0.me4x4[i8x8][0].cost +
2251 analysis.l0.me4x4[i8x8][1].cost +
2252 analysis.l0.me4x4[i8x8][2].cost +
2253 analysis.l0.me4x4[i8x8][3].cost;
2256 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2262 if( h->mb.b_chroma_me )
2264 x264_mb_analyse_intra_chroma( h, &analysis );
2265 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2266 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2267 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2268 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2271 x264_mb_analyse_intra( h, &analysis, i_cost );
2273 i_satd_inter = i_cost;
2274 i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2275 analysis.i_satd_i8x8,
2276 analysis.i_satd_i4x4 );
2278 if( analysis.b_mbrd )
2280 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2282 i_partition = D_16x16;
2283 i_cost = analysis.l0.me16x16.cost;
2284 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2285 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2286 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2287 h->mb.i_type = i_type;
2288 h->mb.i_partition = i_partition;
2289 if( i_cost < COST_MAX )
2290 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2291 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2294 i_intra_type = I_16x16;
2295 i_intra_cost = analysis.i_satd_i16x16;
2296 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
2297 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
2298 COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
2300 if( i_intra_cost == COST_MAX )
2301 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
2303 h->mb.i_type = i_type;
2304 h->stat.frame.i_intra_cost += i_intra_cost;
2305 h->stat.frame.i_inter_cost += i_cost;
2306 h->stat.frame.i_mbs_analysed++;
2308 if( h->mb.i_subpel_refine >= 7 )
2310 if( IS_INTRA( h->mb.i_type ) )
2312 x264_intra_rd_refine( h, &analysis );
2314 else if( i_partition == D_16x16 )
2316 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2317 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
2319 else if( i_partition == D_16x8 )
2321 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2322 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2323 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2324 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2325 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
2326 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
2328 else if( i_partition == D_8x16 )
2330 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2331 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2332 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2333 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2334 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
2335 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
2337 else if( i_partition == D_8x8 )
2340 x264_analyse_update_cache( h, &analysis );
2341 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2342 if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2343 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
2348 else if( h->sh.i_type == SLICE_TYPE_B )
2350 int i_bskip_cost = COST_MAX;
2353 h->mb.i_type = B_SKIP;
2354 if( h->mb.b_direct_auto_write )
2356 /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2357 for( i = 0; i < 2; i++ )
2360 h->sh.b_direct_spatial_mv_pred ^= 1;
2361 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2362 if( analysis.b_direct_available )
2367 b_skip = x264_macroblock_probe_bskip( h );
2369 h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2376 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2378 if( analysis.b_direct_available )
2380 if( !h->mb.b_direct_auto_write )
2382 if( h->mb.b_lossless )
2384 /* chance of skip is too small to bother */
2386 else if( analysis.b_mbrd )
2388 i_bskip_cost = ssd_mb( h );
2389 /* 6 = minimum cavlc cost of a non-skipped MB */
2390 b_skip = h->mb.b_skip_mc = i_bskip_cost <= ((6 * analysis.i_lambda2 + 128) >> 8);
2392 else if( !h->mb.b_direct_auto_write )
2394 /* Conditioning the probe on neighboring block types
2395 * doesn't seem to help speed or quality. */
2396 b_skip = x264_macroblock_probe_bskip( h );
2402 const unsigned int flags = h->param.analyse.inter;
2405 int i_satd_inter = 0; // shut up uninitialized warning
2406 h->mb.b_skip_mc = 0;
2408 x264_mb_analyse_load_costs( h, &analysis );
2410 /* select best inter mode */
2411 /* direct must be first */
2412 if( analysis.b_direct_available )
2413 x264_mb_analyse_inter_direct( h, &analysis );
2415 x264_mb_analyse_inter_b16x16( h, &analysis );
2418 i_partition = D_16x16;
2419 i_cost = analysis.l0.me16x16.cost;
2420 COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2421 COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2422 COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2424 if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2426 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2427 if( i_bskip_cost < analysis.i_rd16x16direct &&
2428 i_bskip_cost < analysis.i_rd16x16bi &&
2429 i_bskip_cost < analysis.l0.i_rd16x16 &&
2430 i_bskip_cost < analysis.l1.i_rd16x16 )
2432 h->mb.i_type = B_SKIP;
2433 x264_analyse_update_cache( h, &analysis );
2438 if( flags & X264_ANALYSE_BSUB16x16 )
2440 x264_mb_analyse_inter_b8x8( h, &analysis );
2441 if( analysis.i_cost8x8bi < i_cost )
2444 i_partition = D_8x8;
2445 i_cost = analysis.i_cost8x8bi;
2447 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2448 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2450 x264_mb_analyse_inter_b16x8( h, &analysis );
2451 COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2452 i_type, analysis.i_mb_type16x8,
2453 i_partition, D_16x8 );
2455 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2456 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2458 x264_mb_analyse_inter_b8x16( h, &analysis );
2459 COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2460 i_type, analysis.i_mb_type8x16,
2461 i_partition, D_8x16 );
2466 if( analysis.b_mbrd )
2471 else if( i_partition == D_16x16 )
2473 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2474 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2475 if( i_type == B_L0_L0 )
2477 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2478 i_cost = analysis.l0.me16x16.cost
2479 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2481 else if( i_type == B_L1_L1 )
2483 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2484 i_cost = analysis.l1.me16x16.cost
2485 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2487 else if( i_type == B_BI_BI )
2489 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2490 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2493 else if( i_partition == D_16x8 )
2495 for( i=0; i<2; i++ )
2497 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2498 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2499 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2500 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2503 else if( i_partition == D_8x16 )
2505 for( i=0; i<2; i++ )
2507 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2508 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2509 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2510 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2513 else if( i_partition == D_8x8 )
2515 for( i=0; i<4; i++ )
2518 int i_part_cost_old;
2520 int i_part_type = h->mb.i_sub_partition[i];
2521 int b_bidir = (i_part_type == D_BI_8x8);
2523 if( i_part_type == D_DIRECT_8x8 )
2525 if( x264_mb_partition_listX_table[0][i_part_type] )
2527 m = &analysis.l0.me8x8[i];
2528 i_part_cost_old = m->cost;
2529 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2530 m->cost -= i_type_cost;
2531 x264_me_refine_qpel( h, m );
2533 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2535 if( x264_mb_partition_listX_table[1][i_part_type] )
2537 m = &analysis.l1.me8x8[i];
2538 i_part_cost_old = m->cost;
2539 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2540 m->cost -= i_type_cost;
2541 x264_me_refine_qpel( h, m );
2543 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2545 /* TODO: update mvp? */
2549 if( analysis.b_mbrd )
2551 i_satd_inter = i_cost;
2552 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2554 i_cost = i_bskip_cost;
2555 i_partition = D_16x16;
2556 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2557 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2558 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2559 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2560 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2561 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2562 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2564 h->mb.i_type = i_type;
2565 h->mb.i_partition = i_partition;
2568 x264_mb_analyse_intra( h, &analysis, i_satd_inter );
2570 if( analysis.b_mbrd )
2572 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2573 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2576 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2577 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2578 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2580 h->mb.i_type = i_type;
2581 h->mb.i_partition = i_partition;
2583 if( analysis.b_mbrd && h->mb.i_subpel_refine >= 7 && IS_INTRA( i_type ) )
2584 x264_intra_rd_refine( h, &analysis );
2585 else if( h->param.analyse.b_bidir_me )
2586 refine_bidir( h, &analysis );
2590 x264_analyse_update_cache( h, &analysis );
2592 if( !analysis.b_mbrd )
2593 x264_mb_analyse_transform( h );
2595 h->mb.b_trellis = h->param.analyse.i_trellis;
2596 h->mb.b_noise_reduction = h->param.analyse.i_noise_reduction;
2597 if( h->mb.b_trellis == 1 || h->mb.b_noise_reduction )
2598 h->mb.i_skip_intra = 0;
2601 /*-------------------- Update MB from the analysis ----------------------*/
2602 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2606 switch( h->mb.i_type )
2609 for( i = 0; i < 16; i++ )
2610 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2612 x264_mb_analyse_intra_chroma( h, a );
2615 for( i = 0; i < 4; i++ )
2616 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2618 x264_mb_analyse_intra_chroma( h, a );
2621 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2622 x264_mb_analyse_intra_chroma( h, a );
2626 switch( h->mb.i_partition )
2629 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2630 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2634 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2635 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2636 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv );
2637 x264_macroblock_cache_mv_ptr( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv );
2641 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2642 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2643 x264_macroblock_cache_mv_ptr( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv );
2644 x264_macroblock_cache_mv_ptr( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv );
2648 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2654 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2655 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2656 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2657 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2658 for( i = 0; i < 4; i++ )
2659 x264_mb_cache_mv_p8x8( h, a, i );
2664 h->mb.i_partition = D_16x16;
2665 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2666 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv );
2672 x264_mb_load_mv_direct8x8( h, 0 );
2673 x264_mb_load_mv_direct8x8( h, 1 );
2674 x264_mb_load_mv_direct8x8( h, 2 );
2675 x264_mb_load_mv_direct8x8( h, 3 );
2679 /* optimize: cache might not need to be rewritten */
2680 for( i = 0; i < 4; i++ )
2681 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2684 default: /* the rest of the B types */
2685 switch( h->mb.i_partition )
2688 switch( h->mb.i_type )
2691 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2692 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2694 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2695 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0 );
2696 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0 );
2699 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2700 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0 );
2701 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0 );
2703 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2704 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2707 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2708 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2710 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2711 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2716 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2717 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2720 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2721 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2724 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2730 if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2733 for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2736 int ref = h->mb.cache.ref[l][x264_scan8[0]];
2739 completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2740 if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2742 x264_log( h, X264_LOG_WARNING, "internal error (MV out of thread range)\n");
2743 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2744 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2745 h->mb.cache.mv[l][x264_scan8[15]][0],
2746 h->mb.cache.mv[l][x264_scan8[15]][1] );
2747 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2748 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2749 fprintf(stderr, "completed: %d \n", completed );
2750 x264_log( h, X264_LOG_WARNING, "recovering by using intra mode\n");
2751 x264_mb_analyse_intra( h, a, COST_MAX );
2752 h->mb.i_type = I_16x16;
2753 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2754 x264_mb_analyse_intra_chroma( h, a );
2761 #include "slicetype.c"