1 /*****************************************************************************
2 * analyse.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
8 * Fiona Glaser <fiona@x264.com>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #define _ISOC99_SOURCE
32 #include "common/common.h"
33 #include "common/cpu.h"
34 #include "macroblock.h"
36 #include "ratecontrol.h"
49 /* [ref][0] is 16x16 mv, [ref][1..4] are 8x8 mv from partition [0..3] */
50 DECLARE_ALIGNED_4( int16_t mvc[32][5][2] );
54 int i_cost4x4[4]; /* cost per 8x8 partition */
55 x264_me_t me4x4[4][4];
58 int i_cost8x4[4]; /* cost per 8x8 partition */
59 x264_me_t me8x4[4][2];
62 int i_cost4x8[4]; /* cost per 8x8 partition */
63 x264_me_t me4x8[4][2];
73 } x264_mb_analysis_list_t;
77 /* conduct the analysis using this lamda and QP */
82 uint16_t *p_cost_ref0;
83 uint16_t *p_cost_ref1;
88 /* Take some shortcuts in intra search if intra is deemed unlikely */
94 int i_satd_i16x16_dir[7];
99 int i_satd_i8x8_dir[12][4];
103 int i_predict4x4[16];
108 int i_satd_i8x8chroma;
109 int i_satd_i8x8chroma_dir[4];
110 int i_predict8x8chroma;
112 /* II: Inter part P/B frame */
113 x264_mb_analysis_list_t l0;
114 x264_mb_analysis_list_t l1;
116 int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
117 int i_cost16x16direct;
119 int i_cost8x8direct[4];
128 int i_mb_partition16x8[2]; /* mb_partition_e */
129 int i_mb_partition8x16[2];
130 int i_mb_type16x8; /* mb_class_e */
133 int b_direct_available;
135 } x264_mb_analysis_t;
137 /* lambda = pow(2,qp/6-2) */
138 const int x264_lambda_tab[52] = {
139 1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
140 1, 1, 1, 1, /* 8-11 */
141 1, 1, 1, 1, 2, 2, 2, 2, /* 12-19 */
142 3, 3, 3, 4, 4, 4, 5, 6, /* 20-27 */
143 6, 7, 8, 9,10,11,13,14, /* 28-35 */
144 16,18,20,23,25,29,32,36, /* 36-43 */
145 40,45,51,57,64,72,81,91 /* 44-51 */
148 /* lambda2 = pow(lambda,2) * .9 * 256 */
149 const int x264_lambda2_tab[52] = {
150 14, 18, 22, 28, 36, 45, 57, 72, /* 0 - 7 */
151 91, 115, 145, 182, 230, 290, 365, 460, /* 8 - 15 */
152 580, 731, 921, 1161, 1462, 1843, 2322, 2925, /* 16 - 23 */
153 3686, 4644, 5851, 7372, 9289, 11703, 14745, 18578, /* 24 - 31 */
154 23407, 29491, 37156, 46814, 58982, 74313, 93628, 117964, /* 32 - 39 */
155 148626, 187257, 235929, 297252, 374514, 471859, 594505, 749029, /* 40 - 47 */
156 943718, 1189010, 1498059, 1887436 /* 48 - 51 */
159 /* TODO: calculate CABAC costs */
160 static const int i_mb_b_cost_table[X264_MBTYPE_MAX] = {
161 9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
163 static const int i_mb_b16x8_cost_table[17] = {
164 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
166 static const int i_sub_mb_b_cost_table[13] = {
167 7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
169 static const int i_sub_mb_p_cost_table[4] = {
173 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
175 uint16_t *x264_cost_mv_fpel[52][4];
176 uint16_t x264_cost_ref[52][3][33];
178 /* initialize an array of lambda*nbits for all possible mvs */
179 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
181 static int16_t *p_cost_mv[52];
184 if( !p_cost_mv[a->i_qp] )
187 /* could be faster, but isn't called many times */
188 /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
189 p_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) );
190 p_cost_mv[a->i_qp] += 2*4*2048;
191 for( i = 0; i <= 2*4*2048; i++ )
193 p_cost_mv[a->i_qp][-i] =
194 p_cost_mv[a->i_qp][i] = a->i_lambda * (log2f(i+1)*2 + 0.718f + !!i) + .5f;
196 for( i = 0; i < 3; i++ )
197 for( j = 0; j < 33; j++ )
198 x264_cost_ref[a->i_qp][i][j] = i ? a->i_lambda * bs_size_te( i, j ) : 0;
200 a->p_cost_mv = p_cost_mv[a->i_qp];
201 a->p_cost_ref0 = x264_cost_ref[a->i_qp][x264_clip3(h->sh.i_num_ref_idx_l0_active-1,0,2)];
202 a->p_cost_ref1 = x264_cost_ref[a->i_qp][x264_clip3(h->sh.i_num_ref_idx_l1_active-1,0,2)];
204 /* FIXME is this useful for all me methods? */
205 if( h->param.analyse.i_me_method >= X264_ME_ESA && !x264_cost_mv_fpel[a->i_qp][0] )
209 x264_cost_mv_fpel[a->i_qp][j] = x264_malloc( (4*2048 + 1) * sizeof(int16_t) );
210 x264_cost_mv_fpel[a->i_qp][j] += 2*2048;
211 for( i = -2*2048; i < 2*2048; i++ )
212 x264_cost_mv_fpel[a->i_qp][j][i] = p_cost_mv[a->i_qp][i*4+j];
217 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
219 int i = h->param.analyse.i_subpel_refine - (h->sh.i_type == SLICE_TYPE_B);
220 /* mbrd == 1 -> RD mode decision */
221 /* mbrd == 2 -> RD refinement */
222 a->i_mbrd = (i>=6) + (i>=8);
223 /* conduct the analysis using this lamda and QP */
224 a->i_qp = h->mb.i_qp = i_qp;
225 h->mb.i_chroma_qp = h->chroma_qp_table[i_qp];
226 a->i_lambda = x264_lambda_tab[i_qp];
227 a->i_lambda2 = x264_lambda2_tab[i_qp];
228 h->mb.i_me_method = h->param.analyse.i_me_method;
229 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
230 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
231 && h->mb.i_subpel_refine >= 5;
232 h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->i_mbrd;
233 h->mb.b_transform_8x8 = 0;
234 h->mb.b_noise_reduction = 0;
240 a->i_satd_i8x8chroma = COST_MAX;
242 /* non-RD PCM decision is inaccurate (as is psy-rd), so don't do it */
243 a->i_satd_pcm = !h->mb.i_psy_rd && a->i_mbrd ? ((uint64_t)X264_PCM_COST*a->i_lambda2 + 128) >> 8 : COST_MAX;
247 h->mb.b_lossless ? 0 :
249 !h->param.analyse.i_trellis && !h->param.analyse.i_noise_reduction;
251 /* II: Inter part P/B frame */
252 if( h->sh.i_type != SLICE_TYPE_I )
255 int i_fmv_range = 4 * h->param.analyse.i_mv_range;
256 // limit motion search to a slightly smaller range than the theoretical limit,
257 // since the search may go a few iterations past its given range
258 int i_fpel_border = 5; // umh unconditional radius
259 int i_spel_border = 8; // 1.5 for subpel_satd, 1.5 for subpel_rd, 2 for bime, round up
261 /* Calculate max allowed MV range */
262 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range-1 )
263 h->mb.mv_min[0] = 4*( -16*h->mb.i_mb_x - 24 );
264 h->mb.mv_max[0] = 4*( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 24 );
265 h->mb.mv_min_spel[0] = CLIP_FMV( h->mb.mv_min[0] );
266 h->mb.mv_max_spel[0] = CLIP_FMV( h->mb.mv_max[0] );
267 h->mb.mv_min_fpel[0] = (h->mb.mv_min_spel[0]>>2) + i_fpel_border;
268 h->mb.mv_max_fpel[0] = (h->mb.mv_max_spel[0]>>2) - i_fpel_border;
269 if( h->mb.i_mb_x == 0)
271 int mb_y = h->mb.i_mb_y >> h->sh.b_mbaff;
272 int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff;
273 int thread_mvy_range = i_fmv_range;
275 if( h->param.i_threads > 1 )
277 int pix_y = (h->mb.i_mb_y | h->mb.b_interlaced) * 16;
278 int thresh = pix_y + h->param.analyse.i_mv_range_thread;
279 for( i = (h->sh.i_type == SLICE_TYPE_B); i >= 0; i-- )
281 x264_frame_t **fref = i ? h->fref1 : h->fref0;
282 int i_ref = i ? h->i_ref1 : h->i_ref0;
283 for( j=0; j<i_ref; j++ )
285 x264_frame_cond_wait( fref[j], thresh );
286 thread_mvy_range = X264_MIN( thread_mvy_range, fref[j]->i_lines_completed - pix_y );
289 if( h->param.b_deterministic )
290 thread_mvy_range = h->param.analyse.i_mv_range_thread;
291 if( h->mb.b_interlaced )
292 thread_mvy_range >>= 1;
295 h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
296 h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
297 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 );
298 h->mb.mv_max_spel[1] = CLIP_FMV( h->mb.mv_max[1] );
299 h->mb.mv_max_spel[1] = X264_MIN( h->mb.mv_max_spel[1], thread_mvy_range*4 );
300 h->mb.mv_min_fpel[1] = (h->mb.mv_min_spel[1]>>2) + i_fpel_border;
301 h->mb.mv_max_fpel[1] = (h->mb.mv_max_spel[1]>>2) - i_fpel_border;
307 a->l0.i_cost8x8 = COST_MAX;
309 for( i = 0; i < 4; i++ )
313 a->l0.i_cost4x8[i] = COST_MAX;
317 a->l0.i_cost8x16 = COST_MAX;
318 if( h->sh.i_type == SLICE_TYPE_B )
322 a->l1.i_cost8x8 = COST_MAX;
324 for( i = 0; i < 4; i++ )
329 a->i_cost8x8direct[i] = COST_MAX;
340 a->i_cost16x16direct =
343 a->i_cost8x16bi = COST_MAX;
346 /* Fast intra decision */
347 if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
349 if( IS_INTRA( h->mb.i_mb_type_left )
350 || IS_INTRA( h->mb.i_mb_type_top )
351 || IS_INTRA( h->mb.i_mb_type_topleft )
352 || IS_INTRA( h->mb.i_mb_type_topright )
353 || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
354 || (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])) )
355 { /* intra is likely */ }
371 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
373 if( i_neighbour & MB_TOPLEFT )
375 /* top and left available */
376 *mode++ = I_PRED_16x16_V;
377 *mode++ = I_PRED_16x16_H;
378 *mode++ = I_PRED_16x16_DC;
379 *mode++ = I_PRED_16x16_P;
382 else if( i_neighbour & MB_LEFT )
385 *mode++ = I_PRED_16x16_DC_LEFT;
386 *mode++ = I_PRED_16x16_H;
389 else if( i_neighbour & MB_TOP )
392 *mode++ = I_PRED_16x16_DC_TOP;
393 *mode++ = I_PRED_16x16_V;
399 *mode = I_PRED_16x16_DC_128;
405 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
407 if( i_neighbour & MB_TOPLEFT )
409 /* top and left available */
410 *mode++ = I_PRED_CHROMA_V;
411 *mode++ = I_PRED_CHROMA_H;
412 *mode++ = I_PRED_CHROMA_DC;
413 *mode++ = I_PRED_CHROMA_P;
416 else if( i_neighbour & MB_LEFT )
419 *mode++ = I_PRED_CHROMA_DC_LEFT;
420 *mode++ = I_PRED_CHROMA_H;
423 else if( i_neighbour & MB_TOP )
426 *mode++ = I_PRED_CHROMA_DC_TOP;
427 *mode++ = I_PRED_CHROMA_V;
433 *mode = I_PRED_CHROMA_DC_128;
439 static void predict_4x4_mode_available( unsigned int i_neighbour,
440 int *mode, int *pi_count )
442 int b_l = i_neighbour & MB_LEFT;
443 int b_t = i_neighbour & MB_TOP;
448 *mode++ = I_PRED_4x4_DC;
449 *mode++ = I_PRED_4x4_H;
450 *mode++ = I_PRED_4x4_V;
451 *mode++ = I_PRED_4x4_DDL;
452 if( i_neighbour & MB_TOPLEFT )
454 *mode++ = I_PRED_4x4_DDR;
455 *mode++ = I_PRED_4x4_VR;
456 *mode++ = I_PRED_4x4_HD;
459 *mode++ = I_PRED_4x4_VL;
460 *mode++ = I_PRED_4x4_HU;
464 *mode++ = I_PRED_4x4_DC_LEFT;
465 *mode++ = I_PRED_4x4_H;
466 *mode++ = I_PRED_4x4_HU;
471 *mode++ = I_PRED_4x4_DC_TOP;
472 *mode++ = I_PRED_4x4_V;
473 *mode++ = I_PRED_4x4_DDL;
474 *mode++ = I_PRED_4x4_VL;
479 *mode++ = I_PRED_4x4_DC_128;
484 /* For trellis=2, we need to do this for both sizes of DCT, for trellis=1 we only need to use it on the chosen mode. */
485 static void inline x264_psy_trellis_init( x264_t *h, int do_both_dct )
487 DECLARE_ALIGNED_16( int16_t dct8x8[4][8][8] );
488 DECLARE_ALIGNED_16( int16_t dct4x4[16][4][4] );
489 DECLARE_ALIGNED_16( static uint8_t zero[16*FDEC_STRIDE] ) = {0};
492 if( do_both_dct || h->mb.b_transform_8x8 )
494 h->dctf.sub16x16_dct8( dct8x8, h->mb.pic.p_fenc[0], zero );
495 for( i = 0; i < 4; i++ )
496 h->zigzagf.scan_8x8( h->mb.pic.fenc_dct8[i], dct8x8[i] );
498 if( do_both_dct || !h->mb.b_transform_8x8 )
500 h->dctf.sub16x16_dct( dct4x4, h->mb.pic.p_fenc[0], zero );
501 for( i = 0; i < 16; i++ )
502 h->zigzagf.scan_4x4( h->mb.pic.fenc_dct4[i], dct4x4[i] );
506 /* Pre-calculate fenc satd scores for psy RD, minus DC coefficients */
507 static inline void x264_mb_cache_fenc_satd( x264_t *h )
509 DECLARE_ALIGNED_16( static uint8_t zero[16] ) = {0};
511 int x, y, satd_sum = 0, sa8d_sum = 0;
512 if( h->param.analyse.i_trellis == 2 && h->mb.i_psy_trellis )
513 x264_psy_trellis_init( h, h->param.analyse.b_transform_8x8 );
514 if( !h->mb.i_psy_rd )
516 for( y = 0; y < 4; y++ )
517 for( x = 0; x < 4; x++ )
519 fenc = h->mb.pic.p_fenc[0]+x*4+y*4*FENC_STRIDE;
520 h->mb.pic.fenc_satd[y][x] = h->pixf.satd[PIXEL_4x4]( zero, 0, fenc, FENC_STRIDE )
521 - (h->pixf.sad[PIXEL_4x4]( zero, 0, fenc, FENC_STRIDE )>>1);
522 satd_sum += h->mb.pic.fenc_satd[y][x];
524 for( y = 0; y < 2; y++ )
525 for( x = 0; x < 2; x++ )
527 fenc = h->mb.pic.p_fenc[0]+x*8+y*8*FENC_STRIDE;
528 h->mb.pic.fenc_sa8d[y][x] = h->pixf.sa8d[PIXEL_8x8]( zero, 0, fenc, FENC_STRIDE )
529 - (h->pixf.sad[PIXEL_8x8]( zero, 0, fenc, FENC_STRIDE )>>2);
530 sa8d_sum += h->mb.pic.fenc_sa8d[y][x];
532 h->mb.pic.fenc_satd_sum = satd_sum;
533 h->mb.pic.fenc_sa8d_sum = sa8d_sum;
536 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
542 int b_merged_satd = !!h->pixf.intra_mbcmp_x3_8x8c && !h->mb.b_lossless;
544 uint8_t *p_dstc[2], *p_srcc[2];
546 if( a->i_satd_i8x8chroma < COST_MAX )
549 /* 8x8 prediction selection for chroma */
550 p_dstc[0] = h->mb.pic.p_fdec[1];
551 p_dstc[1] = h->mb.pic.p_fdec[2];
552 p_srcc[0] = h->mb.pic.p_fenc[1];
553 p_srcc[1] = h->mb.pic.p_fenc[2];
555 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
556 a->i_satd_i8x8chroma = COST_MAX;
557 if( i_max == 4 && b_merged_satd )
559 int satdu[4], satdv[4];
560 h->pixf.intra_mbcmp_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
561 h->pixf.intra_mbcmp_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
562 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
563 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
564 satdu[I_PRED_CHROMA_P] =
565 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
566 satdv[I_PRED_CHROMA_P] =
567 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
569 for( i=0; i<i_max; i++ )
571 int i_mode = predict_mode[i];
572 int i_satd = satdu[i_mode] + satdv[i_mode]
573 + a->i_lambda * bs_size_ue(i_mode);
575 a->i_satd_i8x8chroma_dir[i] = i_satd;
576 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
581 for( i=0; i<i_max; i++ )
584 int i_mode = predict_mode[i];
586 /* we do the prediction */
587 if( h->mb.b_lossless )
588 x264_predict_lossless_8x8_chroma( h, i_mode );
591 h->predict_8x8c[i_mode]( p_dstc[0] );
592 h->predict_8x8c[i_mode]( p_dstc[1] );
595 /* we calculate the cost */
596 i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
597 p_srcc[0], FENC_STRIDE ) +
598 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
599 p_srcc[1], FENC_STRIDE ) +
600 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
602 a->i_satd_i8x8chroma_dir[i] = i_satd;
603 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
607 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
610 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
612 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
613 uint8_t *p_src = h->mb.pic.p_fenc[0];
614 uint8_t *p_dst = h->mb.pic.p_fdec[0];
619 int b_merged_satd = !!h->pixf.intra_mbcmp_x3_16x16 && !h->mb.b_lossless;
621 /*---------------- Try all mode and calculate their score ---------------*/
623 /* 16x16 prediction selection */
624 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
626 if( b_merged_satd && i_max == 4 )
628 h->pixf.intra_mbcmp_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
629 h->predict_16x16[I_PRED_16x16_P]( p_dst );
630 a->i_satd_i16x16_dir[I_PRED_16x16_P] =
631 h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
634 int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
635 COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
640 for( i = 0; i < i_max; i++ )
643 int i_mode = predict_mode[i];
645 if( h->mb.b_lossless )
646 x264_predict_lossless_16x16( h, i_mode );
648 h->predict_16x16[i_mode]( p_dst );
650 i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
651 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
652 COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
653 a->i_satd_i16x16_dir[i_mode] = i_satd;
657 if( h->sh.i_type == SLICE_TYPE_B )
658 /* cavlc mb type prefix */
659 a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
660 if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
663 /* 8x8 prediction selection */
664 if( flags & X264_ANALYSE_I8x8 )
666 DECLARE_ALIGNED_16( uint8_t edge[33] );
667 x264_pixel_cmp_t sa8d = (h->pixf.mbcmp[0] == h->pixf.satd[0]) ? h->pixf.sa8d[PIXEL_8x8] : h->pixf.mbcmp[PIXEL_8x8];
668 int i_satd_thresh = a->i_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
670 h->mb.i_cbp_luma = 0;
671 b_merged_satd = h->pixf.intra_mbcmp_x3_8x8 && !h->mb.b_lossless;
673 // FIXME some bias like in i4x4?
674 if( h->sh.i_type == SLICE_TYPE_B )
675 i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
677 for( idx = 0;; idx++ )
681 uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
682 uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
683 int i_best = COST_MAX;
684 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
686 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
687 h->predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
689 if( b_merged_satd && i_max == 9 )
692 h->pixf.intra_mbcmp_x3_8x8( p_src_by, edge, satd );
693 satd[i_pred_mode] -= 3 * a->i_lambda;
694 for( i=2; i>=0; i-- )
696 int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
697 COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
704 for( ; i<i_max; i++ )
707 int i_mode = predict_mode[i];
709 if( h->mb.b_lossless )
710 x264_predict_lossless_8x8( h, p_dst_by, idx, i_mode, edge );
712 h->predict_8x8[i_mode]( p_dst_by, edge );
714 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
715 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
717 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
718 a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
722 if( idx == 3 || i_cost > i_satd_thresh )
725 /* we need to encode this block now (for next ones) */
726 h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
727 x264_mb_encode_i8x8( h, idx, a->i_qp );
729 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
734 a->i_satd_i8x8 = i_cost;
735 if( h->mb.i_skip_intra )
737 h->mc.copy[PIXEL_16x16]( h->mb.pic.i8x8_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
738 h->mb.pic.i8x8_nnz_buf[0] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[ 0]];
739 h->mb.pic.i8x8_nnz_buf[1] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[ 2]];
740 h->mb.pic.i8x8_nnz_buf[2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[ 8]];
741 h->mb.pic.i8x8_nnz_buf[3] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[10]];
742 h->mb.pic.i8x8_cbp = h->mb.i_cbp_luma;
743 if( h->mb.i_skip_intra == 2 )
744 h->mc.memcpy_aligned( h->mb.pic.i8x8_dct_buf, h->dct.luma8x8, sizeof(h->mb.pic.i8x8_dct_buf) );
749 static const uint16_t cost_div_fix8[3] = {1024,512,341};
750 a->i_satd_i8x8 = COST_MAX;
751 i_cost = (i_cost * cost_div_fix8[idx]) >> 8;
753 if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+!!a->i_mbrd)/4 )
757 /* 4x4 prediction selection */
758 if( flags & X264_ANALYSE_I4x4 )
761 int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
762 h->mb.i_cbp_luma = 0;
763 b_merged_satd = h->pixf.intra_mbcmp_x3_4x4 && !h->mb.b_lossless;
765 i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
767 i_cost = a->i_lambda * 24; /* from JVT (SATD0) */
768 if( h->sh.i_type == SLICE_TYPE_B )
769 i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];
771 for( idx = 0;; idx++ )
773 uint8_t *p_src_by = p_src + block_idx_xy_fenc[idx];
774 uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
775 int i_best = COST_MAX;
776 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
778 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
780 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
781 /* emulate missing topright samples */
782 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
784 if( b_merged_satd && i_max >= 6 )
787 h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
788 satd[i_pred_mode] -= 3 * a->i_lambda;
789 for( i=2; i>=0; i-- )
790 COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
791 a->i_predict4x4[idx], i );
797 for( ; i<i_max; i++ )
800 int i_mode = predict_mode[i];
801 if( h->mb.b_lossless )
802 x264_predict_lossless_4x4( h, p_dst_by, idx, i_mode );
804 h->predict_4x4[i_mode]( p_dst_by );
806 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
807 p_src_by, FENC_STRIDE )
808 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
810 COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
814 if( i_cost > i_satd_thresh || idx == 15 )
817 /* we need to encode this block now (for next ones) */
818 h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
819 x264_mb_encode_i4x4( h, idx, a->i_qp );
821 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
825 a->i_satd_i4x4 = i_cost;
826 if( h->mb.i_skip_intra )
828 h->mc.copy[PIXEL_16x16]( h->mb.pic.i4x4_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
829 h->mb.pic.i4x4_nnz_buf[0] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[ 0]];
830 h->mb.pic.i4x4_nnz_buf[1] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[ 2]];
831 h->mb.pic.i4x4_nnz_buf[2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[ 8]];
832 h->mb.pic.i4x4_nnz_buf[3] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[10]];
833 h->mb.pic.i4x4_cbp = h->mb.i_cbp_luma;
834 if( h->mb.i_skip_intra == 2 )
835 h->mc.memcpy_aligned( h->mb.pic.i4x4_dct_buf, h->dct.luma4x4, sizeof(h->mb.pic.i4x4_dct_buf) );
839 a->i_satd_i4x4 = COST_MAX;
843 static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
845 if( a->i_satd_i16x16 <= i_satd_thresh )
847 h->mb.i_type = I_16x16;
848 x264_analyse_update_cache( h, a );
849 a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
852 a->i_satd_i16x16 = COST_MAX;
854 if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
856 h->mb.i_type = I_4x4;
857 x264_analyse_update_cache( h, a );
858 a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
861 a->i_satd_i4x4 = COST_MAX;
863 if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
865 h->mb.i_type = I_8x8;
866 x264_analyse_update_cache( h, a );
867 a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
868 a->i_cbp_i8x8_luma = h->mb.i_cbp_luma;
871 a->i_satd_i8x8 = COST_MAX;
874 static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
876 uint8_t *p_dst = h->mb.pic.p_fdec[0];
879 int i_max, i_mode, i_thresh;
880 uint64_t i_satd, i_best;
882 h->mb.i_skip_intra = 0;
884 if( h->mb.i_type == I_16x16 )
886 int old_pred_mode = a->i_predict16x16;
887 i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
888 i_best = a->i_satd_i16x16;
889 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
890 for( i = 0; i < i_max; i++ )
892 int i_mode = predict_mode[i];
893 if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
895 h->mb.i_intra16x16_pred_mode = i_mode;
896 i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
897 COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
901 /* RD selection for chroma prediction */
902 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
905 i_thresh = a->i_satd_i8x8chroma * 5/4;
907 for( i = j = 0; i < i_max; i++ )
908 if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
909 predict_mode[i] != a->i_predict8x8chroma )
911 predict_mode[j++] = predict_mode[i];
917 int i_cbp_chroma_best = h->mb.i_cbp_chroma;
918 int i_chroma_lambda = x264_lambda2_tab[h->mb.i_chroma_qp];
919 /* the previous thing encoded was x264_intra_rd(), so the pixels and
920 * coefs for the current chroma mode are still around, so we only
921 * have to recount the bits. */
922 i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
923 for( i = 0; i < i_max; i++ )
925 i_mode = predict_mode[i];
926 if( h->mb.b_lossless )
927 x264_predict_lossless_8x8_chroma( h, i_mode );
930 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
931 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
933 /* if we've already found a mode that needs no residual, then
934 * probably any mode with a residual will be worse.
935 * so avoid dct on the remaining modes to improve speed. */
936 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
937 COPY3_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode, i_cbp_chroma_best, h->mb.i_cbp_chroma );
939 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
940 h->mb.i_cbp_chroma = i_cbp_chroma_best;
944 if( h->mb.i_type == I_4x4 )
946 uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
948 for( idx = 0; idx < 16; idx++ )
950 uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
953 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
955 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
956 /* emulate missing topright samples */
957 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
959 for( i = 0; i < i_max; i++ )
961 i_mode = predict_mode[i];
962 if( h->mb.b_lossless )
963 x264_predict_lossless_4x4( h, p_dst_by, idx, i_mode );
965 h->predict_4x4[i_mode]( p_dst_by );
966 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
968 if( i_best > i_satd )
970 a->i_predict4x4[idx] = i_mode;
972 pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
973 pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
974 pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
975 pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
976 i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
980 *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
981 *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
982 *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
983 *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
984 h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
986 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
989 else if( h->mb.i_type == I_8x8 )
991 DECLARE_ALIGNED_16( uint8_t edge[33] );
992 for( idx = 0; idx < 4; idx++ )
999 int cbp_luma_new = 0;
1000 i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
1002 i_best = COST_MAX64;
1006 p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
1007 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
1008 h->predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
1010 for( i = 0; i < i_max; i++ )
1012 i_mode = predict_mode[i];
1013 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
1015 if( h->mb.b_lossless )
1016 x264_predict_lossless_8x8( h, p_dst_by, idx, i_mode, edge );
1018 h->predict_8x8[i_mode]( p_dst_by, edge );
1019 h->mb.i_cbp_luma = a->i_cbp_i8x8_luma;
1020 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
1022 if( i_best > i_satd )
1024 a->i_predict8x8[idx] = i_mode;
1025 cbp_luma_new = h->mb.i_cbp_luma;
1028 pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
1030 for( j=0; j<7; j++ )
1031 pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
1032 i_nnz[0] = *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[4*idx+0]];
1033 i_nnz[1] = *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[4*idx+2]];
1036 a->i_cbp_i8x8_luma = cbp_luma_new;
1037 *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
1039 for( j=0; j<7; j++ )
1040 p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
1041 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[4*idx+0]] = i_nnz[0];
1042 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[4*idx+2]] = i_nnz[1];
1044 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
1049 #define LOAD_FENC( m, src, xoff, yoff) \
1050 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
1051 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
1052 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
1053 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
1054 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
1056 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
1057 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
1058 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
1059 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
1060 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
1061 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
1062 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
1063 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
1065 #define REF_COST(list, ref) \
1066 (a->p_cost_ref##list[ref])
1068 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
1072 DECLARE_ALIGNED_4( int16_t mvc[8][2] );
1073 int i_halfpel_thresh = INT_MAX;
1074 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1076 /* 16x16 Search on all ref frame */
1077 m.i_pixel = PIXEL_16x16;
1078 m.p_cost_mv = a->p_cost_mv;
1079 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1081 a->l0.me16x16.cost = INT_MAX;
1082 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1084 const int i_ref_cost = REF_COST( 0, i_ref );
1085 i_halfpel_thresh -= i_ref_cost;
1086 m.i_ref_cost = i_ref_cost;
1089 /* search with ref */
1090 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1091 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1092 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1093 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1095 /* early termination
1096 * SSD threshold would probably be better than SATD */
1099 && m.cost-m.cost_mv < 300*a->i_lambda
1100 && abs(m.mv[0]-h->mb.cache.pskip_mv[0])
1101 + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
1102 && x264_macroblock_probe_pskip( h ) )
1104 h->mb.i_type = P_SKIP;
1105 x264_analyse_update_cache( h, a );
1106 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1110 m.cost += i_ref_cost;
1111 i_halfpel_thresh += i_ref_cost;
1113 if( m.cost < a->l0.me16x16.cost )
1114 h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1116 /* save mv for predicting neighbors */
1117 *(uint32_t*)a->l0.mvc[i_ref][0] =
1118 *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1121 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
1122 assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1124 h->mb.i_type = P_L0;
1127 x264_mb_cache_fenc_satd( h );
1128 if( a->l0.me16x16.i_ref == 0 && *(uint32_t*)a->l0.me16x16.mv == *(uint32_t*)h->mb.cache.pskip_mv )
1130 h->mb.i_partition = D_16x16;
1131 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
1132 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1137 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
1141 uint8_t **p_fenc = h->mb.pic.p_fenc;
1142 int i_halfpel_thresh = INT_MAX;
1143 int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
1145 int i_maxref = h->mb.pic.i_fref[0]-1;
1147 h->mb.i_partition = D_8x8;
1149 /* early termination: if 16x16 chose ref 0, then evalute no refs older
1150 * than those used by the neighbors */
1151 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
1152 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
1155 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
1156 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
1157 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
1158 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
1159 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
1160 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
1163 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1164 *(uint32_t*)a->l0.mvc[i_ref][0] = *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy];
1166 for( i = 0; i < 4; i++ )
1168 x264_me_t *l0m = &a->l0.me8x8[i];
1172 m.i_pixel = PIXEL_8x8;
1173 m.p_cost_mv = a->p_cost_mv;
1175 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
1176 l0m->cost = INT_MAX;
1177 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1179 const int i_ref_cost = REF_COST( 0, i_ref );
1180 i_halfpel_thresh -= i_ref_cost;
1181 m.i_ref_cost = i_ref_cost;
1184 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
1185 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
1186 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1187 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
1189 m.cost += i_ref_cost;
1190 i_halfpel_thresh += i_ref_cost;
1191 *(uint32_t*)a->l0.mvc[i_ref][i+1] = *(uint32_t*)m.mv;
1193 if( m.cost < l0m->cost )
1194 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1196 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv );
1197 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1200 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1203 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1204 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1205 /* P_8x8 ref0 has no ref cost */
1206 if( !h->param.b_cabac && !(a->l0.me8x8[0].i_ref | a->l0.me8x8[1].i_ref |
1207 a->l0.me8x8[2].i_ref | a->l0.me8x8[3].i_ref) )
1208 a->l0.i_cost8x8 -= REF_COST( 0, 0 ) * 4;
1209 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1210 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1213 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1215 const int i_ref = a->l0.me16x16.i_ref;
1216 const int i_ref_cost = h->param.b_cabac || i_ref ? REF_COST( 0, i_ref ) : 0;
1217 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
1218 uint8_t **p_fenc = h->mb.pic.p_fenc;
1220 int16_t (*mvc)[2] = a->l0.mvc[i_ref];
1223 /* XXX Needed for x264_mb_predict_mv */
1224 h->mb.i_partition = D_8x8;
1227 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.me16x16.mv;
1229 for( i = 0; i < 4; i++ )
1231 x264_me_t *m = &a->l0.me8x8[i];
1235 m->i_pixel = PIXEL_8x8;
1236 m->p_cost_mv = a->p_cost_mv;
1237 m->i_ref_cost = i_ref_cost;
1240 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1241 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1242 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1243 x264_me_search( h, m, mvc, i_mvc );
1245 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, m->mv );
1247 *(uint32_t*)mvc[i_mvc] = *(uint32_t*)m->mv;
1251 m->cost += i_ref_cost;
1252 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1255 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1256 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1257 /* theoretically this should include 4*ref_cost,
1258 * but 3 seems a better approximation of cabac. */
1259 if( h->param.b_cabac )
1260 a->l0.i_cost8x8 -= i_ref_cost;
1261 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1262 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1265 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
1268 uint8_t **p_fenc = h->mb.pic.p_fenc;
1269 DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1272 /* XXX Needed for x264_mb_predict_mv */
1273 h->mb.i_partition = D_16x8;
1275 for( i = 0; i < 2; i++ )
1277 x264_me_t *l0m = &a->l0.me16x8[i];
1278 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1279 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1281 m.i_pixel = PIXEL_16x8;
1282 m.p_cost_mv = a->p_cost_mv;
1284 LOAD_FENC( &m, p_fenc, 0, 8*i );
1285 l0m->cost = INT_MAX;
1286 for( j = 0; j < i_ref8s; j++ )
1288 const int i_ref = ref8[j];
1289 const int i_ref_cost = REF_COST( 0, i_ref );
1290 m.i_ref_cost = i_ref_cost;
1293 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1294 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1295 *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][2*i+1];
1296 *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][2*i+2];
1298 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1299 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1300 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1301 x264_me_search( h, &m, mvc, 3 );
1303 m.cost += i_ref_cost;
1305 if( m.cost < l0m->cost )
1306 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1308 x264_macroblock_cache_mv_ptr( h, 0, 2*i, 4, 2, 0, l0m->mv );
1309 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1312 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1315 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1318 uint8_t **p_fenc = h->mb.pic.p_fenc;
1319 DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1322 /* XXX Needed for x264_mb_predict_mv */
1323 h->mb.i_partition = D_8x16;
1325 for( i = 0; i < 2; i++ )
1327 x264_me_t *l0m = &a->l0.me8x16[i];
1328 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1329 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1331 m.i_pixel = PIXEL_8x16;
1332 m.p_cost_mv = a->p_cost_mv;
1334 LOAD_FENC( &m, p_fenc, 8*i, 0 );
1335 l0m->cost = INT_MAX;
1336 for( j = 0; j < i_ref8s; j++ )
1338 const int i_ref = ref8[j];
1339 const int i_ref_cost = REF_COST( 0, i_ref );
1340 m.i_ref_cost = i_ref_cost;
1343 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1344 *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][i+1];
1345 *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][i+3];
1347 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1348 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1349 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1350 x264_me_search( h, &m, mvc, 3 );
1352 m.cost += i_ref_cost;
1354 if( m.cost < l0m->cost )
1355 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1357 x264_macroblock_cache_mv_ptr( h, 2*i, 0, 2, 4, 0, l0m->mv );
1358 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1361 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1364 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1366 DECLARE_ALIGNED_8( uint8_t pix1[16*8] );
1367 uint8_t *pix2 = pix1+8;
1368 const int i_stride = h->mb.pic.i_stride[1];
1369 const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1370 const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1372 #define CHROMA4x4MC( width, height, me, x, y ) \
1373 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 ); \
1374 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 );
1376 if( pixel == PIXEL_4x4 )
1378 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1379 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 2,0 );
1380 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 0,2 );
1381 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1383 else if( pixel == PIXEL_8x4 )
1385 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1386 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1390 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1391 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1394 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1395 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1398 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1400 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1401 uint8_t **p_fenc = h->mb.pic.p_fenc;
1402 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1405 /* XXX Needed for x264_mb_predict_mv */
1406 h->mb.i_partition = D_8x8;
1408 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1410 const int idx = 4*i8x8 + i4x4;
1411 const int x4 = block_idx_x[idx];
1412 const int y4 = block_idx_y[idx];
1413 const int i_mvc = (i4x4 == 0);
1415 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1417 m->i_pixel = PIXEL_4x4;
1418 m->p_cost_mv = a->p_cost_mv;
1420 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1421 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1423 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1424 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1426 x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 1, 0, m->mv );
1428 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1429 a->l0.me4x4[i8x8][1].cost +
1430 a->l0.me4x4[i8x8][2].cost +
1431 a->l0.me4x4[i8x8][3].cost +
1432 REF_COST( 0, i_ref ) +
1433 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1434 if( h->mb.b_chroma_me )
1435 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1438 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1440 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1441 uint8_t **p_fenc = h->mb.pic.p_fenc;
1442 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1445 /* XXX Needed for x264_mb_predict_mv */
1446 h->mb.i_partition = D_8x8;
1448 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1450 const int idx = 4*i8x8 + 2*i8x4;
1451 const int x4 = block_idx_x[idx];
1452 const int y4 = block_idx_y[idx];
1453 const int i_mvc = (i8x4 == 0);
1455 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1457 m->i_pixel = PIXEL_8x4;
1458 m->p_cost_mv = a->p_cost_mv;
1460 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1461 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1463 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1464 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1466 x264_macroblock_cache_mv_ptr( h, x4, y4, 2, 1, 0, m->mv );
1468 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1469 REF_COST( 0, i_ref ) +
1470 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1471 if( h->mb.b_chroma_me )
1472 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1475 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1477 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1478 uint8_t **p_fenc = h->mb.pic.p_fenc;
1479 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1482 /* XXX Needed for x264_mb_predict_mv */
1483 h->mb.i_partition = D_8x8;
1485 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1487 const int idx = 4*i8x8 + i4x8;
1488 const int x4 = block_idx_x[idx];
1489 const int y4 = block_idx_y[idx];
1490 const int i_mvc = (i4x8 == 0);
1492 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1494 m->i_pixel = PIXEL_4x8;
1495 m->p_cost_mv = a->p_cost_mv;
1497 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1498 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1500 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1501 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1503 x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 2, 0, m->mv );
1505 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1506 REF_COST( 0, i_ref ) +
1507 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1508 if( h->mb.b_chroma_me )
1509 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1512 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1514 /* Assumes that fdec still contains the results of
1515 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1517 uint8_t **p_fenc = h->mb.pic.p_fenc;
1518 uint8_t **p_fdec = h->mb.pic.p_fdec;
1521 a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1522 for( i = 0; i < 4; i++ )
1524 const int x = (i&1)*8;
1525 const int y = (i>>1)*8;
1526 a->i_cost16x16direct +=
1527 a->i_cost8x8direct[i] =
1528 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1531 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1535 #define WEIGHTED_AVG( size, pix, stride, src1, stride1, src2, stride2 ) \
1537 h->mc.avg[size]( pix, stride, src1, stride1, src2, stride2, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1540 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1542 DECLARE_ALIGNED_16( uint8_t pix0[16*16] );
1543 DECLARE_ALIGNED_16( uint8_t pix1[16*16] );
1544 uint8_t *src0, *src1;
1545 int stride0 = 16, stride1 = 16;
1549 DECLARE_ALIGNED_4( int16_t mvc[9][2] );
1550 int i_halfpel_thresh = INT_MAX;
1551 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1553 /* 16x16 Search on all ref frame */
1554 m.i_pixel = PIXEL_16x16;
1555 m.p_cost_mv = a->p_cost_mv;
1556 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1559 a->l0.me16x16.cost = INT_MAX;
1560 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1562 /* search with ref */
1563 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1564 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1565 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1566 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1569 m.cost += REF_COST( 0, i_ref );
1571 if( m.cost < a->l0.me16x16.cost )
1573 a->l0.i_ref = i_ref;
1574 h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1577 /* save mv for predicting neighbors */
1578 *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1580 /* subtract ref cost, so we don't have to add it for the other MB types */
1581 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1584 i_halfpel_thresh = INT_MAX;
1585 p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1586 a->l1.me16x16.cost = INT_MAX;
1587 for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1589 /* search with ref */
1590 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1591 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1592 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1593 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1596 m.cost += REF_COST( 1, i_ref );
1598 if( m.cost < a->l1.me16x16.cost )
1600 a->l1.i_ref = i_ref;
1601 h->mc.memcpy_aligned( &a->l1.me16x16, &m, sizeof(x264_me_t) );
1604 /* save mv for predicting neighbors */
1605 *(uint32_t*)h->mb.mvr[1][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1607 /* subtract ref cost, so we don't have to add it for the other MB types */
1608 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1610 /* Set global ref, needed for other modes? */
1611 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1612 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1614 /* get cost of BI mode */
1615 src0 = h->mc.get_ref( pix0, &stride0,
1616 h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1617 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1], 16, 16 );
1618 src1 = h->mc.get_ref( pix1, &stride1,
1619 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1620 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1], 16, 16 );
1622 h->mc.avg[PIXEL_16x16]( pix0, 16, src0, stride0, src1, stride1, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
1624 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix0, 16 )
1625 + REF_COST( 0, a->l0.i_ref )
1626 + REF_COST( 1, a->l1.i_ref )
1627 + a->l0.me16x16.cost_mv
1628 + a->l1.me16x16.cost_mv;
1631 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1632 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1633 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1636 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1638 const int x = 2*(i%2);
1639 const int y = 2*(i/2);
1641 switch( h->mb.i_sub_partition[i] )
1644 x264_macroblock_cache_mv_ptr( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv );
1647 x264_macroblock_cache_mv_ptr( h, x, y+0, 2, 1, 0, a->l0.me8x4[i][0].mv );
1648 x264_macroblock_cache_mv_ptr( h, x, y+1, 2, 1, 0, a->l0.me8x4[i][1].mv );
1651 x264_macroblock_cache_mv_ptr( h, x+0, y, 1, 2, 0, a->l0.me4x8[i][0].mv );
1652 x264_macroblock_cache_mv_ptr( h, x+1, y, 1, 2, 0, a->l0.me4x8[i][1].mv );
1655 x264_macroblock_cache_mv_ptr( h, x+0, y+0, 1, 1, 0, a->l0.me4x4[i][0].mv );
1656 x264_macroblock_cache_mv_ptr( h, x+1, y+0, 1, 1, 0, a->l0.me4x4[i][1].mv );
1657 x264_macroblock_cache_mv_ptr( h, x+0, y+1, 1, 1, 0, a->l0.me4x4[i][2].mv );
1658 x264_macroblock_cache_mv_ptr( h, x+1, y+1, 1, 1, 0, a->l0.me4x4[i][3].mv );
1661 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1666 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1667 if( x264_mb_partition_listX_table[0][part] ) \
1669 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1670 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 0, me0.mv ); \
1674 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1675 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0 ); \
1677 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0 ); \
1679 if( x264_mb_partition_listX_table[1][part] ) \
1681 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1682 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 1, me1.mv ); \
1686 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1687 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0 ); \
1689 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0 ); \
1692 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1696 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1698 x264_mb_load_mv_direct8x8( h, i );
1701 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0 );
1702 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0 );
1703 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1708 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1711 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1713 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1715 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1717 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1721 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1723 uint8_t **p_fref[2] =
1724 { h->mb.pic.p_fref[0][a->l0.i_ref],
1725 h->mb.pic.p_fref[1][a->l1.i_ref] };
1726 DECLARE_ALIGNED_8( uint8_t pix[2][8*8] );
1729 /* XXX Needed for x264_mb_predict_mv */
1730 h->mb.i_partition = D_8x8;
1734 for( i = 0; i < 4; i++ )
1739 int i_part_cost_bi = 0;
1740 int stride[2] = {8,8};
1743 for( l = 0; l < 2; l++ )
1745 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1746 x264_me_t *m = &lX->me8x8[i];
1748 m->i_pixel = PIXEL_8x8;
1749 m->p_cost_mv = a->p_cost_mv;
1751 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1752 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1754 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1755 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1757 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, l, m->mv );
1760 src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1761 m->mv[0], m->mv[1], 8, 8 );
1762 i_part_cost_bi += m->cost_mv;
1763 /* FIXME: ref cost */
1765 h->mc.avg[PIXEL_8x8]( pix[0], 8, src[0], stride[0], src[1], stride[1], h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
1766 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1767 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1768 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1769 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1771 i_part_cost = a->l0.me8x8[i].cost;
1772 h->mb.i_sub_partition[i] = D_L0_8x8;
1773 COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1774 COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1775 COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1776 a->i_cost8x8bi += i_part_cost;
1778 /* XXX Needed for x264_mb_predict_mv */
1779 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1783 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1786 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1788 uint8_t **p_fref[2] =
1789 { h->mb.pic.p_fref[0][a->l0.i_ref],
1790 h->mb.pic.p_fref[1][a->l1.i_ref] };
1791 DECLARE_ALIGNED_16( uint8_t pix[2][16*8] );
1792 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1795 h->mb.i_partition = D_16x8;
1796 a->i_cost16x8bi = 0;
1798 for( i = 0; i < 2; i++ )
1801 int i_part_cost_bi = 0;
1802 int stride[2] = {16,16};
1805 /* TODO: check only the list(s) that were used in b8x8? */
1806 for( l = 0; l < 2; l++ )
1808 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1809 x264_me_t *m = &lX->me16x8[i];
1811 m->i_pixel = PIXEL_16x8;
1812 m->p_cost_mv = a->p_cost_mv;
1814 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1815 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1817 *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[2*i].mv;
1818 *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[2*i+1].mv;
1820 x264_mb_predict_mv( h, l, 8*i, 2, m->mvp );
1821 x264_me_search( h, m, mvc, 2 );
1824 src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1825 m->mv[0], m->mv[1], 16, 8 );
1826 /* FIXME: ref cost */
1827 i_part_cost_bi += m->cost_mv;
1829 h->mc.avg[PIXEL_16x8]( pix[0], 16, src[0], stride[0], src[1], stride[1], h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
1830 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1832 i_part_cost = a->l0.me16x8[i].cost;
1833 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1834 if( a->l1.me16x8[i].cost < i_part_cost )
1836 i_part_cost = a->l1.me16x8[i].cost;
1837 a->i_mb_partition16x8[i] = D_L1_8x8;
1839 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1841 i_part_cost = i_part_cost_bi;
1842 a->i_mb_partition16x8[i] = D_BI_8x8;
1844 a->i_cost16x8bi += i_part_cost;
1846 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1850 a->i_mb_type16x8 = B_L0_L0
1851 + (a->i_mb_partition16x8[0]>>2) * 3
1852 + (a->i_mb_partition16x8[1]>>2);
1853 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1856 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1858 uint8_t **p_fref[2] =
1859 { h->mb.pic.p_fref[0][a->l0.i_ref],
1860 h->mb.pic.p_fref[1][a->l1.i_ref] };
1861 DECLARE_ALIGNED_8( uint8_t pix[2][8*16] );
1862 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1865 h->mb.i_partition = D_8x16;
1866 a->i_cost8x16bi = 0;
1868 for( i = 0; i < 2; i++ )
1871 int i_part_cost_bi = 0;
1872 int stride[2] = {8,8};
1875 for( l = 0; l < 2; l++ )
1877 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1878 x264_me_t *m = &lX->me8x16[i];
1880 m->i_pixel = PIXEL_8x16;
1881 m->p_cost_mv = a->p_cost_mv;
1883 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1884 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1886 *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[i].mv;
1887 *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[i+2].mv;
1889 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1890 x264_me_search( h, m, mvc, 2 );
1893 src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1894 m->mv[0], m->mv[1], 8, 16 );
1895 /* FIXME: ref cost */
1896 i_part_cost_bi += m->cost_mv;
1899 h->mc.avg[PIXEL_8x16]( pix[0], 8, src[0], stride[0], src[1], stride[1], h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
1900 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1902 i_part_cost = a->l0.me8x16[i].cost;
1903 a->i_mb_partition8x16[i] = D_L0_8x8;
1904 if( a->l1.me8x16[i].cost < i_part_cost )
1906 i_part_cost = a->l1.me8x16[i].cost;
1907 a->i_mb_partition8x16[i] = D_L1_8x8;
1909 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1911 i_part_cost = i_part_cost_bi;
1912 a->i_mb_partition8x16[i] = D_BI_8x8;
1914 a->i_cost8x16bi += i_part_cost;
1916 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1920 a->i_mb_type8x16 = B_L0_L0
1921 + (a->i_mb_partition8x16[0]>>2) * 3
1922 + (a->i_mb_partition8x16[1]>>2);
1923 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1926 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1928 int thresh = i_satd * 5/4;
1930 h->mb.i_type = P_L0;
1931 if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1933 h->mb.i_partition = D_16x16;
1934 x264_analyse_update_cache( h, a );
1935 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1937 a->l0.me16x16.cost = a->l0.i_rd16x16;
1939 if( a->l0.i_cost16x8 <= thresh )
1941 h->mb.i_partition = D_16x8;
1942 x264_analyse_update_cache( h, a );
1943 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1946 a->l0.i_cost16x8 = COST_MAX;
1948 if( a->l0.i_cost8x16 <= thresh )
1950 h->mb.i_partition = D_8x16;
1951 x264_analyse_update_cache( h, a );
1952 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1955 a->l0.i_cost8x16 = COST_MAX;
1957 if( a->l0.i_cost8x8 <= thresh )
1959 h->mb.i_type = P_8x8;
1960 h->mb.i_partition = D_8x8;
1961 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1964 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
1965 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
1966 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
1967 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
1968 /* FIXME: In the 8x8 blocks where RDO isn't run, the NNZ values used for context selection
1969 * for future blocks are those left over from previous RDO calls. */
1970 for( i = 0; i < 4; i++ )
1972 int costs[4] = {a->l0.i_cost4x4[i], a->l0.i_cost8x4[i], a->l0.i_cost4x8[i], a->l0.me8x8[i].cost};
1973 int thresh = X264_MIN4( costs[0], costs[1], costs[2], costs[3] ) * 5 / 4;
1974 int subtype, btype = D_L0_8x8;
1975 uint64_t bcost = COST_MAX64;
1976 for( subtype = D_L0_4x4; subtype <= D_L0_8x8; subtype++ )
1979 if( costs[subtype] > thresh || (subtype == D_L0_8x8 && bcost == COST_MAX64) )
1981 h->mb.i_sub_partition[i] = subtype;
1982 x264_mb_cache_mv_p8x8( h, a, i );
1983 cost = x264_rd_cost_part( h, a->i_lambda2, i<<2, PIXEL_8x8 );
1984 COPY2_IF_LT( bcost, cost, btype, subtype );
1986 h->mb.i_sub_partition[i] = btype;
1987 x264_mb_cache_mv_p8x8( h, a, i );
1991 x264_analyse_update_cache( h, a );
1992 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1995 a->l0.i_cost8x8 = COST_MAX;
1998 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
2000 int thresh = i_satd_inter * (17 + (!!h->mb.i_psy_rd))/16;
2002 if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
2004 h->mb.i_type = B_DIRECT;
2005 /* Assumes direct/skip MC is still in fdec */
2006 /* Requires b-rdo to be done before intra analysis */
2007 h->mb.b_skip_mc = 1;
2008 x264_analyse_update_cache( h, a );
2009 a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
2010 h->mb.b_skip_mc = 0;
2013 //FIXME not all the update_cache calls are needed
2014 h->mb.i_partition = D_16x16;
2016 if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
2018 h->mb.i_type = B_L0_L0;
2019 x264_analyse_update_cache( h, a );
2020 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
2024 if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
2026 h->mb.i_type = B_L1_L1;
2027 x264_analyse_update_cache( h, a );
2028 a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
2032 if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
2034 h->mb.i_type = B_BI_BI;
2035 x264_analyse_update_cache( h, a );
2036 a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
2040 if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
2042 h->mb.i_type = B_8x8;
2043 h->mb.i_partition = D_8x8;
2044 x264_analyse_update_cache( h, a );
2045 a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
2046 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
2050 if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
2052 h->mb.i_type = a->i_mb_type16x8;
2053 h->mb.i_partition = D_16x8;
2054 x264_analyse_update_cache( h, a );
2055 a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
2059 if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
2061 h->mb.i_type = a->i_mb_type8x16;
2062 h->mb.i_partition = D_8x16;
2063 x264_analyse_update_cache( h, a );
2064 a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
2068 static void x264_refine_bidir( x264_t *h, x264_mb_analysis_t *a )
2070 const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
2073 if( IS_INTRA(h->mb.i_type) )
2076 switch( h->mb.i_partition )
2079 if( h->mb.i_type == B_BI_BI )
2080 x264_me_refine_bidir_satd( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
2083 for( i=0; i<2; i++ )
2084 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
2085 x264_me_refine_bidir_satd( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
2088 for( i=0; i<2; i++ )
2089 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
2090 x264_me_refine_bidir_satd( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
2093 for( i=0; i<4; i++ )
2094 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
2095 x264_me_refine_bidir_satd( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
2100 static inline void x264_mb_analyse_transform( x264_t *h )
2102 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 && !h->mb.b_lossless )
2104 int i_cost4, i_cost8;
2105 /* Only luma MC is really needed, but the full MC is re-used in macroblock_encode. */
2108 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2109 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2110 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2111 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2113 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2114 h->mb.b_skip_mc = 1;
2118 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2120 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2123 x264_analyse_update_cache( h, a );
2124 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2125 /* FIXME only luma is needed, but the score for comparison already includes chroma */
2126 i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2128 if( *i_rd >= i_rd8 )
2131 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2135 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2140 /*****************************************************************************
2141 * x264_macroblock_analyse:
2142 *****************************************************************************/
2143 void x264_macroblock_analyse( x264_t *h )
2145 x264_mb_analysis_t analysis;
2146 int i_cost = COST_MAX;
2149 h->mb.i_qp = x264_ratecontrol_qp( h );
2150 if( h->param.rc.i_aq_mode )
2151 x264_adaptive_quant( h );
2153 x264_mb_analyse_init( h, &analysis, h->mb.i_qp );
2155 /*--------------------------- Do the analysis ---------------------------*/
2156 if( h->sh.i_type == SLICE_TYPE_I )
2158 if( analysis.i_mbrd )
2159 x264_mb_cache_fenc_satd( h );
2160 x264_mb_analyse_intra( h, &analysis, COST_MAX );
2161 if( analysis.i_mbrd )
2162 x264_intra_rd( h, &analysis, COST_MAX );
2164 i_cost = analysis.i_satd_i16x16;
2165 h->mb.i_type = I_16x16;
2166 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, h->mb.i_type, I_4x4 );
2167 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, h->mb.i_type, I_8x8 );
2168 if( analysis.i_satd_pcm < i_cost )
2169 h->mb.i_type = I_PCM;
2171 else if( analysis.i_mbrd >= 2 )
2172 x264_intra_rd_refine( h, &analysis );
2174 else if( h->sh.i_type == SLICE_TYPE_P )
2178 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2180 /* Fast P_SKIP detection */
2181 analysis.b_try_pskip = 0;
2182 if( h->param.analyse.b_fast_pskip )
2184 if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2185 // FIXME don't need to check this if the reference frame is done
2187 else if( h->param.analyse.i_subpel_refine >= 3 )
2188 analysis.b_try_pskip = 1;
2189 else if( h->mb.i_mb_type_left == P_SKIP ||
2190 h->mb.i_mb_type_top == P_SKIP ||
2191 h->mb.i_mb_type_topleft == P_SKIP ||
2192 h->mb.i_mb_type_topright == P_SKIP )
2193 b_skip = x264_macroblock_probe_pskip( h );
2196 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2200 h->mb.i_type = P_SKIP;
2201 h->mb.i_partition = D_16x16;
2202 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2206 const unsigned int flags = h->param.analyse.inter;
2210 int i_satd_inter, i_satd_intra;
2212 x264_mb_analyse_load_costs( h, &analysis );
2214 x264_mb_analyse_inter_p16x16( h, &analysis );
2216 if( h->mb.i_type == P_SKIP )
2219 if( flags & X264_ANALYSE_PSUB16x16 )
2221 if( h->param.analyse.b_mixed_references )
2222 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2224 x264_mb_analyse_inter_p8x8( h, &analysis );
2227 /* Select best inter mode */
2229 i_partition = D_16x16;
2230 i_cost = analysis.l0.me16x16.cost;
2232 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2233 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2236 i_partition = D_8x8;
2237 i_cost = analysis.l0.i_cost8x8;
2240 if( flags & X264_ANALYSE_PSUB8x8 )
2242 for( i = 0; i < 4; i++ )
2244 x264_mb_analyse_inter_p4x4( h, &analysis, i );
2245 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2247 int i_cost8x8 = analysis.l0.i_cost4x4[i];
2248 h->mb.i_sub_partition[i] = D_L0_4x4;
2250 x264_mb_analyse_inter_p8x4( h, &analysis, i );
2251 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2252 h->mb.i_sub_partition[i], D_L0_8x4 );
2254 x264_mb_analyse_inter_p4x8( h, &analysis, i );
2255 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2256 h->mb.i_sub_partition[i], D_L0_4x8 );
2258 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2260 x264_mb_cache_mv_p8x8( h, &analysis, i );
2262 analysis.l0.i_cost8x8 = i_cost;
2266 /* Now do 16x8/8x16 */
2267 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2268 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2269 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2271 x264_mb_analyse_inter_p16x8( h, &analysis );
2272 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2274 x264_mb_analyse_inter_p8x16( h, &analysis );
2275 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2278 h->mb.i_partition = i_partition;
2281 //FIXME mb_type costs?
2282 if( analysis.i_mbrd )
2286 else if( i_partition == D_16x16 )
2288 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2289 i_cost = analysis.l0.me16x16.cost;
2291 else if( i_partition == D_16x8 )
2293 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2294 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2295 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2297 else if( i_partition == D_8x16 )
2299 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2300 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2301 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2303 else if( i_partition == D_8x8 )
2307 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2309 switch( h->mb.i_sub_partition[i8x8] )
2312 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2313 i_cost += analysis.l0.me8x8[i8x8].cost;
2316 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2317 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2318 i_cost += analysis.l0.me8x4[i8x8][0].cost +
2319 analysis.l0.me8x4[i8x8][1].cost;
2322 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2323 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2324 i_cost += analysis.l0.me4x8[i8x8][0].cost +
2325 analysis.l0.me4x8[i8x8][1].cost;
2329 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2330 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2331 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2332 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2333 i_cost += analysis.l0.me4x4[i8x8][0].cost +
2334 analysis.l0.me4x4[i8x8][1].cost +
2335 analysis.l0.me4x4[i8x8][2].cost +
2336 analysis.l0.me4x4[i8x8][3].cost;
2339 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2345 if( h->mb.b_chroma_me )
2347 x264_mb_analyse_intra_chroma( h, &analysis );
2348 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2349 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2350 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2351 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2354 x264_mb_analyse_intra( h, &analysis, i_cost );
2356 i_satd_inter = i_cost;
2357 i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2358 analysis.i_satd_i8x8,
2359 analysis.i_satd_i4x4 );
2361 if( analysis.i_mbrd )
2363 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2365 i_partition = D_16x16;
2366 i_cost = analysis.l0.me16x16.cost;
2367 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2368 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2369 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2370 h->mb.i_type = i_type;
2371 h->mb.i_partition = i_partition;
2372 if( i_cost < COST_MAX )
2373 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2374 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2377 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2378 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2379 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2380 COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
2382 h->mb.i_type = i_type;
2384 if( analysis.i_mbrd >= 2 && h->mb.i_type != I_PCM )
2386 if( IS_INTRA( h->mb.i_type ) )
2388 x264_intra_rd_refine( h, &analysis );
2390 else if( i_partition == D_16x16 )
2392 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2393 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 );
2395 else if( i_partition == D_16x8 )
2397 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2398 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2399 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2400 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2401 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0, 0 );
2402 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 8, 0 );
2404 else if( i_partition == D_8x16 )
2406 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2407 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2408 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2409 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2410 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0, 0 );
2411 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 4, 0 );
2413 else if( i_partition == D_8x8 )
2416 x264_analyse_update_cache( h, &analysis );
2417 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2419 if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2421 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8*4, 0 );
2423 else if( h->mb.i_sub_partition[i8x8] == D_L0_8x4 )
2425 x264_me_refine_qpel_rd( h, &analysis.l0.me8x4[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
2426 x264_me_refine_qpel_rd( h, &analysis.l0.me8x4[i8x8][1], analysis.i_lambda2, i8x8*4+2, 0 );
2428 else if( h->mb.i_sub_partition[i8x8] == D_L0_4x8 )
2430 x264_me_refine_qpel_rd( h, &analysis.l0.me4x8[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
2431 x264_me_refine_qpel_rd( h, &analysis.l0.me4x8[i8x8][1], analysis.i_lambda2, i8x8*4+1, 0 );
2433 else if( h->mb.i_sub_partition[i8x8] == D_L0_4x4 )
2435 x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
2436 x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][1], analysis.i_lambda2, i8x8*4+1, 0 );
2437 x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][2], analysis.i_lambda2, i8x8*4+2, 0 );
2438 x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][3], analysis.i_lambda2, i8x8*4+3, 0 );
2445 else if( h->sh.i_type == SLICE_TYPE_B )
2447 int i_bskip_cost = COST_MAX;
2450 if( analysis.i_mbrd )
2451 x264_mb_cache_fenc_satd( h );
2453 h->mb.i_type = B_SKIP;
2454 if( h->mb.b_direct_auto_write )
2456 /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2457 for( i = 0; i < 2; i++ )
2460 h->sh.b_direct_spatial_mv_pred ^= 1;
2461 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2462 if( analysis.b_direct_available )
2467 b_skip = x264_macroblock_probe_bskip( h );
2469 h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2476 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2478 if( analysis.b_direct_available )
2480 if( !h->mb.b_direct_auto_write )
2482 if( analysis.i_mbrd )
2484 i_bskip_cost = ssd_mb( h );
2485 /* 6 = minimum cavlc cost of a non-skipped MB */
2486 b_skip = h->mb.b_skip_mc = i_bskip_cost <= ((6 * analysis.i_lambda2 + 128) >> 8);
2488 else if( !h->mb.b_direct_auto_write )
2490 /* Conditioning the probe on neighboring block types
2491 * doesn't seem to help speed or quality. */
2492 b_skip = x264_macroblock_probe_bskip( h );
2498 const unsigned int flags = h->param.analyse.inter;
2501 int i_satd_inter = 0; // shut up uninitialized warning
2502 h->mb.b_skip_mc = 0;
2504 x264_mb_analyse_load_costs( h, &analysis );
2506 /* select best inter mode */
2507 /* direct must be first */
2508 if( analysis.b_direct_available )
2509 x264_mb_analyse_inter_direct( h, &analysis );
2511 x264_mb_analyse_inter_b16x16( h, &analysis );
2514 i_partition = D_16x16;
2515 i_cost = analysis.l0.me16x16.cost;
2516 COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2517 COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2518 COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2520 if( analysis.i_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2522 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2523 if( i_bskip_cost < analysis.i_rd16x16direct &&
2524 i_bskip_cost < analysis.i_rd16x16bi &&
2525 i_bskip_cost < analysis.l0.i_rd16x16 &&
2526 i_bskip_cost < analysis.l1.i_rd16x16 )
2528 h->mb.i_type = B_SKIP;
2529 x264_analyse_update_cache( h, &analysis );
2534 if( flags & X264_ANALYSE_BSUB16x16 )
2536 x264_mb_analyse_inter_b8x8( h, &analysis );
2537 if( analysis.i_cost8x8bi < i_cost )
2540 i_partition = D_8x8;
2541 i_cost = analysis.i_cost8x8bi;
2543 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2544 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2546 x264_mb_analyse_inter_b16x8( h, &analysis );
2547 COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2548 i_type, analysis.i_mb_type16x8,
2549 i_partition, D_16x8 );
2551 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2552 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2554 x264_mb_analyse_inter_b8x16( h, &analysis );
2555 COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2556 i_type, analysis.i_mb_type8x16,
2557 i_partition, D_8x16 );
2562 if( analysis.i_mbrd )
2567 else if( i_partition == D_16x16 )
2569 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2570 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2571 if( i_type == B_L0_L0 )
2573 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2574 i_cost = analysis.l0.me16x16.cost
2575 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2577 else if( i_type == B_L1_L1 )
2579 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2580 i_cost = analysis.l1.me16x16.cost
2581 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2583 else if( i_type == B_BI_BI )
2585 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2586 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2589 else if( i_partition == D_16x8 )
2591 for( i=0; i<2; i++ )
2593 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2594 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2595 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2596 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2599 else if( i_partition == D_8x16 )
2601 for( i=0; i<2; i++ )
2603 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2604 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2605 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2606 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2609 else if( i_partition == D_8x8 )
2611 for( i=0; i<4; i++ )
2614 int i_part_cost_old;
2616 int i_part_type = h->mb.i_sub_partition[i];
2617 int b_bidir = (i_part_type == D_BI_8x8);
2619 if( i_part_type == D_DIRECT_8x8 )
2621 if( x264_mb_partition_listX_table[0][i_part_type] )
2623 m = &analysis.l0.me8x8[i];
2624 i_part_cost_old = m->cost;
2625 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2626 m->cost -= i_type_cost;
2627 x264_me_refine_qpel( h, m );
2629 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2631 if( x264_mb_partition_listX_table[1][i_part_type] )
2633 m = &analysis.l1.me8x8[i];
2634 i_part_cost_old = m->cost;
2635 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2636 m->cost -= i_type_cost;
2637 x264_me_refine_qpel( h, m );
2639 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2641 /* TODO: update mvp? */
2645 if( analysis.i_mbrd )
2647 i_satd_inter = i_cost;
2648 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2650 i_cost = i_bskip_cost;
2651 i_partition = D_16x16;
2652 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2653 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2654 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2655 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2656 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2657 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2658 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2660 h->mb.i_type = i_type;
2661 h->mb.i_partition = i_partition;
2664 x264_mb_analyse_intra( h, &analysis, i_satd_inter );
2666 if( analysis.i_mbrd )
2668 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2669 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2672 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2673 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2674 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2675 COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
2677 h->mb.i_type = i_type;
2678 h->mb.i_partition = i_partition;
2680 if( analysis.i_mbrd >= 2 && IS_INTRA( i_type ) && i_type != I_PCM )
2681 x264_intra_rd_refine( h, &analysis );
2682 if( h->mb.i_subpel_refine >= 5 )
2683 x264_refine_bidir( h, &analysis );
2685 if( analysis.i_mbrd >= 2 && i_type > B_DIRECT && i_type < B_SKIP )
2687 const int i_biweight = h->mb.bipred_weight[analysis.l0.i_ref][analysis.l1.i_ref];
2688 x264_analyse_update_cache( h, &analysis );
2690 if( i_partition == D_16x16 )
2692 if( i_type == B_L0_L0 )
2693 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 );
2694 else if( i_type == B_L1_L1 )
2695 x264_me_refine_qpel_rd( h, &analysis.l1.me16x16, analysis.i_lambda2, 0, 1 );
2696 else if( i_type == B_BI_BI )
2697 x264_me_refine_bidir_rd( h, &analysis.l0.me16x16, &analysis.l1.me16x16, i_biweight, 0, analysis.i_lambda2 );
2699 else if( i_partition == D_16x8 )
2701 for( i = 0; i < 2; i++ )
2703 h->mb.i_sub_partition[i*2] = h->mb.i_sub_partition[i*2+1] = analysis.i_mb_partition16x8[i];
2704 if( analysis.i_mb_partition16x8[i] == D_L0_8x8 )
2705 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[i], analysis.i_lambda2, i*8, 0 );
2706 else if( analysis.i_mb_partition16x8[i] == D_L1_8x8 )
2707 x264_me_refine_qpel_rd( h, &analysis.l1.me16x8[i], analysis.i_lambda2, i*8, 1 );
2708 else if( analysis.i_mb_partition16x8[i] == D_BI_8x8 )
2709 x264_me_refine_bidir_rd( h, &analysis.l0.me16x8[i], &analysis.l1.me16x8[i], i_biweight, i*2, analysis.i_lambda2 );
2712 else if( i_partition == D_8x16 )
2714 for( i = 0; i < 2; i++ )
2716 h->mb.i_sub_partition[i] = h->mb.i_sub_partition[i+2] = analysis.i_mb_partition8x16[i];
2717 if( analysis.i_mb_partition8x16[i] == D_L0_8x8 )
2718 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[i], analysis.i_lambda2, i*4, 0 );
2719 else if( analysis.i_mb_partition8x16[i] == D_L1_8x8 )
2720 x264_me_refine_qpel_rd( h, &analysis.l1.me8x16[i], analysis.i_lambda2, i*4, 1 );
2721 else if( analysis.i_mb_partition8x16[i] == D_BI_8x8 )
2722 x264_me_refine_bidir_rd( h, &analysis.l0.me8x16[i], &analysis.l1.me8x16[i], i_biweight, i, analysis.i_lambda2 );
2725 else if( i_partition == D_8x8 )
2727 for( i = 0; i < 4; i++ )
2729 if( h->mb.i_sub_partition[i] == D_L0_8x8 )
2730 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i], analysis.i_lambda2, i*4, 0 );
2731 else if( h->mb.i_sub_partition[i] == D_L1_8x8 )
2732 x264_me_refine_qpel_rd( h, &analysis.l1.me8x8[i], analysis.i_lambda2, i*4, 1 );
2733 else if( h->mb.i_sub_partition[i] == D_BI_8x8 )
2734 x264_me_refine_bidir_rd( h, &analysis.l0.me8x8[i], &analysis.l1.me8x8[i], i_biweight, i, analysis.i_lambda2 );
2741 x264_analyse_update_cache( h, &analysis );
2743 if( !analysis.i_mbrd )
2744 x264_mb_analyse_transform( h );
2746 h->mb.b_trellis = h->param.analyse.i_trellis;
2747 h->mb.b_noise_reduction = !!h->param.analyse.i_noise_reduction;
2748 if( !IS_SKIP(h->mb.i_type) && h->mb.i_psy_trellis && h->param.analyse.i_trellis == 1 )
2749 x264_psy_trellis_init( h, 0 );
2750 if( h->mb.b_trellis == 1 || h->mb.b_noise_reduction )
2751 h->mb.i_skip_intra = 0;
2754 /*-------------------- Update MB from the analysis ----------------------*/
2755 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2759 switch( h->mb.i_type )
2762 for( i = 0; i < 16; i++ )
2763 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2765 x264_mb_analyse_intra_chroma( h, a );
2768 for( i = 0; i < 4; i++ )
2769 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2771 x264_mb_analyse_intra_chroma( h, a );
2774 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2775 x264_mb_analyse_intra_chroma( h, a );
2782 switch( h->mb.i_partition )
2785 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2786 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2790 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2791 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2792 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv );
2793 x264_macroblock_cache_mv_ptr( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv );
2797 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2798 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2799 x264_macroblock_cache_mv_ptr( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv );
2800 x264_macroblock_cache_mv_ptr( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv );
2804 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2810 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2811 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2812 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2813 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2814 for( i = 0; i < 4; i++ )
2815 x264_mb_cache_mv_p8x8( h, a, i );
2820 h->mb.i_partition = D_16x16;
2821 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2822 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv );
2828 x264_mb_load_mv_direct8x8( h, 0 );
2829 x264_mb_load_mv_direct8x8( h, 1 );
2830 x264_mb_load_mv_direct8x8( h, 2 );
2831 x264_mb_load_mv_direct8x8( h, 3 );
2835 /* optimize: cache might not need to be rewritten */
2836 for( i = 0; i < 4; i++ )
2837 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2840 default: /* the rest of the B types */
2841 switch( h->mb.i_partition )
2844 switch( h->mb.i_type )
2847 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2848 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2850 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2851 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0 );
2852 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0 );
2855 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2856 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0 );
2857 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0 );
2859 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2860 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2863 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2864 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2866 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2867 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2872 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2873 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2876 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2877 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2880 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2886 if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2889 for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2892 int ref = h->mb.cache.ref[l][x264_scan8[0]];
2895 completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2896 if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2898 x264_log( h, X264_LOG_WARNING, "internal error (MV out of thread range)\n");
2899 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2900 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2901 h->mb.cache.mv[l][x264_scan8[15]][0],
2902 h->mb.cache.mv[l][x264_scan8[15]][1] );
2903 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2904 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2905 fprintf(stderr, "completed: %d \n", completed );
2906 x264_log( h, X264_LOG_WARNING, "recovering by using intra mode\n");
2907 x264_mb_analyse_intra( h, a, COST_MAX );
2908 h->mb.i_type = I_16x16;
2909 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2910 x264_mb_analyse_intra_chroma( h, a );
2917 #include "slicetype.c"