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_src = h->mb.pic.p_fenc[0];
877 uint8_t *p_dst = h->mb.pic.p_fdec[0];
880 int i_max, i_mode, i_thresh;
881 uint64_t i_satd, i_best;
884 h->mb.i_skip_intra = 0;
886 if( h->mb.i_type == I_16x16 )
888 int old_pred_mode = a->i_predict16x16;
889 i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
890 i_best = a->i_satd_i16x16;
891 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
892 for( i = 0; i < i_max; i++ )
894 int i_mode = predict_mode[i];
895 if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
897 h->mb.i_intra16x16_pred_mode = i_mode;
898 i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
899 COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
903 /* RD selection for chroma prediction */
904 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
907 i_thresh = a->i_satd_i8x8chroma * 5/4;
909 for( i = j = 0; i < i_max; i++ )
910 if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
911 predict_mode[i] != a->i_predict8x8chroma )
913 predict_mode[j++] = predict_mode[i];
919 int i_cbp_chroma_best = h->mb.i_cbp_chroma;
920 int i_chroma_lambda = x264_lambda2_tab[h->mb.i_chroma_qp];
921 /* the previous thing encoded was x264_intra_rd(), so the pixels and
922 * coefs for the current chroma mode are still around, so we only
923 * have to recount the bits. */
924 i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
925 for( i = 0; i < i_max; i++ )
927 i_mode = predict_mode[i];
928 if( h->mb.b_lossless )
929 x264_predict_lossless_8x8_chroma( h, i_mode );
932 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
933 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
935 /* if we've already found a mode that needs no residual, then
936 * probably any mode with a residual will be worse.
937 * so avoid dct on the remaining modes to improve speed. */
938 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
939 COPY3_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode, i_cbp_chroma_best, h->mb.i_cbp_chroma );
941 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
942 h->mb.i_cbp_chroma = i_cbp_chroma_best;
946 if( h->mb.i_type == I_4x4 )
948 uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
950 for( idx = 0; idx < 16; idx++ )
952 uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
955 i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
957 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
959 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
960 /* emulate missing topright samples */
961 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
963 for( i = 0; i < i_max; i++ )
965 i_mode = predict_mode[i];
966 if( h->mb.b_lossless )
967 x264_predict_lossless_4x4( h, p_dst_by, idx, i_mode );
969 h->predict_4x4[i_mode]( p_dst_by );
970 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
972 if( i_best > i_satd )
974 a->i_predict4x4[idx] = i_mode;
976 pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
977 pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
978 pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
979 pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
980 i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
984 *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
985 *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
986 *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
987 *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
988 h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
990 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
993 else if( h->mb.i_type == I_8x8 )
995 DECLARE_ALIGNED_16( uint8_t edge[33] );
996 for( idx = 0; idx < 4; idx++ )
1004 int cbp_luma_new = 0;
1005 i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
1007 i_best = COST_MAX64;
1008 i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
1012 p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
1013 p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
1014 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
1015 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
1017 for( i = 0; i < i_max; i++ )
1019 i_mode = predict_mode[i];
1020 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
1022 if( h->mb.b_lossless )
1023 x264_predict_lossless_8x8( h, p_dst_by, idx, i_mode, edge );
1025 h->predict_8x8[i_mode]( p_dst_by, edge );
1026 h->mb.i_cbp_luma = a->i_cbp_i8x8_luma;
1027 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
1029 if( i_best > i_satd )
1031 a->i_predict8x8[idx] = i_mode;
1032 cbp_luma_new = h->mb.i_cbp_luma;
1035 pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
1037 for( j=0; j<7; j++ )
1038 pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
1039 i_nnz[0] = *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[4*idx+0]];
1040 i_nnz[1] = *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[4*idx+2]];
1043 a->i_cbp_i8x8_luma = cbp_luma_new;
1044 *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
1046 for( j=0; j<7; j++ )
1047 p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
1048 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[4*idx+0]] = i_nnz[0];
1049 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[4*idx+2]] = i_nnz[1];
1051 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
1056 #define LOAD_FENC( m, src, xoff, yoff) \
1057 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
1058 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
1059 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
1060 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
1061 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
1063 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
1064 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
1065 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
1066 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
1067 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
1068 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
1069 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
1070 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
1072 #define REF_COST(list, ref) \
1073 (a->p_cost_ref##list[ref])
1075 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
1079 DECLARE_ALIGNED_4( int16_t mvc[8][2] );
1080 int i_halfpel_thresh = INT_MAX;
1081 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1083 /* 16x16 Search on all ref frame */
1084 m.i_pixel = PIXEL_16x16;
1085 m.p_cost_mv = a->p_cost_mv;
1086 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1088 a->l0.me16x16.cost = INT_MAX;
1089 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1091 const int i_ref_cost = REF_COST( 0, i_ref );
1092 i_halfpel_thresh -= i_ref_cost;
1093 m.i_ref_cost = i_ref_cost;
1096 /* search with ref */
1097 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1098 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1099 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1100 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1102 /* early termination
1103 * SSD threshold would probably be better than SATD */
1106 && m.cost-m.cost_mv < 300*a->i_lambda
1107 && abs(m.mv[0]-h->mb.cache.pskip_mv[0])
1108 + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
1109 && x264_macroblock_probe_pskip( h ) )
1111 h->mb.i_type = P_SKIP;
1112 x264_analyse_update_cache( h, a );
1113 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1117 m.cost += i_ref_cost;
1118 i_halfpel_thresh += i_ref_cost;
1120 if( m.cost < a->l0.me16x16.cost )
1121 h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1123 /* save mv for predicting neighbors */
1124 *(uint32_t*)a->l0.mvc[i_ref][0] =
1125 *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1128 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
1129 assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1131 h->mb.i_type = P_L0;
1134 x264_mb_cache_fenc_satd( h );
1135 if( a->l0.me16x16.i_ref == 0 && *(uint32_t*)a->l0.me16x16.mv == *(uint32_t*)h->mb.cache.pskip_mv )
1137 h->mb.i_partition = D_16x16;
1138 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
1139 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1144 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
1148 uint8_t **p_fenc = h->mb.pic.p_fenc;
1149 int i_halfpel_thresh = INT_MAX;
1150 int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
1152 int i_maxref = h->mb.pic.i_fref[0]-1;
1154 h->mb.i_partition = D_8x8;
1156 /* early termination: if 16x16 chose ref 0, then evalute no refs older
1157 * than those used by the neighbors */
1158 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
1159 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
1162 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
1163 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
1164 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
1165 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
1166 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
1167 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
1170 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1171 *(uint32_t*)a->l0.mvc[i_ref][0] = *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy];
1173 for( i = 0; i < 4; i++ )
1175 x264_me_t *l0m = &a->l0.me8x8[i];
1179 m.i_pixel = PIXEL_8x8;
1180 m.p_cost_mv = a->p_cost_mv;
1182 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
1183 l0m->cost = INT_MAX;
1184 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1186 const int i_ref_cost = REF_COST( 0, i_ref );
1187 i_halfpel_thresh -= i_ref_cost;
1188 m.i_ref_cost = i_ref_cost;
1191 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
1192 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
1193 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1194 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
1196 m.cost += i_ref_cost;
1197 i_halfpel_thresh += i_ref_cost;
1198 *(uint32_t*)a->l0.mvc[i_ref][i+1] = *(uint32_t*)m.mv;
1200 if( m.cost < l0m->cost )
1201 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1203 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv );
1204 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1207 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1210 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1211 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1212 /* P_8x8 ref0 has no ref cost */
1213 if( !h->param.b_cabac && !(a->l0.me8x8[0].i_ref | a->l0.me8x8[1].i_ref |
1214 a->l0.me8x8[2].i_ref | a->l0.me8x8[3].i_ref) )
1215 a->l0.i_cost8x8 -= REF_COST( 0, 0 ) * 4;
1216 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1217 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1220 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1222 const int i_ref = a->l0.me16x16.i_ref;
1223 const int i_ref_cost = h->param.b_cabac || i_ref ? REF_COST( 0, i_ref ) : 0;
1224 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
1225 uint8_t **p_fenc = h->mb.pic.p_fenc;
1227 int16_t (*mvc)[2] = a->l0.mvc[i_ref];
1230 /* XXX Needed for x264_mb_predict_mv */
1231 h->mb.i_partition = D_8x8;
1234 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.me16x16.mv;
1236 for( i = 0; i < 4; i++ )
1238 x264_me_t *m = &a->l0.me8x8[i];
1242 m->i_pixel = PIXEL_8x8;
1243 m->p_cost_mv = a->p_cost_mv;
1244 m->i_ref_cost = i_ref_cost;
1247 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1248 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1249 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1250 x264_me_search( h, m, mvc, i_mvc );
1252 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, m->mv );
1254 *(uint32_t*)mvc[i_mvc] = *(uint32_t*)m->mv;
1258 m->cost += i_ref_cost;
1259 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1262 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1263 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1264 /* theoretically this should include 4*ref_cost,
1265 * but 3 seems a better approximation of cabac. */
1266 if( h->param.b_cabac )
1267 a->l0.i_cost8x8 -= i_ref_cost;
1268 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1269 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1272 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
1275 uint8_t **p_fenc = h->mb.pic.p_fenc;
1276 DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1279 /* XXX Needed for x264_mb_predict_mv */
1280 h->mb.i_partition = D_16x8;
1282 for( i = 0; i < 2; i++ )
1284 x264_me_t *l0m = &a->l0.me16x8[i];
1285 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1286 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1288 m.i_pixel = PIXEL_16x8;
1289 m.p_cost_mv = a->p_cost_mv;
1291 LOAD_FENC( &m, p_fenc, 0, 8*i );
1292 l0m->cost = INT_MAX;
1293 for( j = 0; j < i_ref8s; j++ )
1295 const int i_ref = ref8[j];
1296 const int i_ref_cost = REF_COST( 0, i_ref );
1297 m.i_ref_cost = i_ref_cost;
1300 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1301 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1302 *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][2*i+1];
1303 *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][2*i+2];
1305 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1306 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1307 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1308 x264_me_search( h, &m, mvc, 3 );
1310 m.cost += i_ref_cost;
1312 if( m.cost < l0m->cost )
1313 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1315 x264_macroblock_cache_mv_ptr( h, 0, 2*i, 4, 2, 0, l0m->mv );
1316 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1319 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1322 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1325 uint8_t **p_fenc = h->mb.pic.p_fenc;
1326 DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1329 /* XXX Needed for x264_mb_predict_mv */
1330 h->mb.i_partition = D_8x16;
1332 for( i = 0; i < 2; i++ )
1334 x264_me_t *l0m = &a->l0.me8x16[i];
1335 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1336 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1338 m.i_pixel = PIXEL_8x16;
1339 m.p_cost_mv = a->p_cost_mv;
1341 LOAD_FENC( &m, p_fenc, 8*i, 0 );
1342 l0m->cost = INT_MAX;
1343 for( j = 0; j < i_ref8s; j++ )
1345 const int i_ref = ref8[j];
1346 const int i_ref_cost = REF_COST( 0, i_ref );
1347 m.i_ref_cost = i_ref_cost;
1350 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1351 *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][i+1];
1352 *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][i+3];
1354 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1355 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1356 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1357 x264_me_search( h, &m, mvc, 3 );
1359 m.cost += i_ref_cost;
1361 if( m.cost < l0m->cost )
1362 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1364 x264_macroblock_cache_mv_ptr( h, 2*i, 0, 2, 4, 0, l0m->mv );
1365 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1368 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1371 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1373 DECLARE_ALIGNED_8( uint8_t pix1[16*8] );
1374 uint8_t *pix2 = pix1+8;
1375 const int i_stride = h->mb.pic.i_stride[1];
1376 const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1377 const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1379 #define CHROMA4x4MC( width, height, me, x, y ) \
1380 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 ); \
1381 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 );
1383 if( pixel == PIXEL_4x4 )
1385 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1386 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 2,0 );
1387 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 0,2 );
1388 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1390 else if( pixel == PIXEL_8x4 )
1392 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1393 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1397 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1398 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1401 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1402 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1405 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1407 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1408 uint8_t **p_fenc = h->mb.pic.p_fenc;
1409 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1412 /* XXX Needed for x264_mb_predict_mv */
1413 h->mb.i_partition = D_8x8;
1415 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1417 const int idx = 4*i8x8 + i4x4;
1418 const int x4 = block_idx_x[idx];
1419 const int y4 = block_idx_y[idx];
1420 const int i_mvc = (i4x4 == 0);
1422 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1424 m->i_pixel = PIXEL_4x4;
1425 m->p_cost_mv = a->p_cost_mv;
1427 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1428 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1430 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1431 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1433 x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 1, 0, m->mv );
1435 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1436 a->l0.me4x4[i8x8][1].cost +
1437 a->l0.me4x4[i8x8][2].cost +
1438 a->l0.me4x4[i8x8][3].cost +
1439 REF_COST( 0, i_ref ) +
1440 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1441 if( h->mb.b_chroma_me )
1442 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1445 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1447 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1448 uint8_t **p_fenc = h->mb.pic.p_fenc;
1449 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1452 /* XXX Needed for x264_mb_predict_mv */
1453 h->mb.i_partition = D_8x8;
1455 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1457 const int idx = 4*i8x8 + 2*i8x4;
1458 const int x4 = block_idx_x[idx];
1459 const int y4 = block_idx_y[idx];
1460 const int i_mvc = (i8x4 == 0);
1462 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1464 m->i_pixel = PIXEL_8x4;
1465 m->p_cost_mv = a->p_cost_mv;
1467 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1468 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1470 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1471 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1473 x264_macroblock_cache_mv_ptr( h, x4, y4, 2, 1, 0, m->mv );
1475 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1476 REF_COST( 0, i_ref ) +
1477 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1478 if( h->mb.b_chroma_me )
1479 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1482 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1484 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1485 uint8_t **p_fenc = h->mb.pic.p_fenc;
1486 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1489 /* XXX Needed for x264_mb_predict_mv */
1490 h->mb.i_partition = D_8x8;
1492 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1494 const int idx = 4*i8x8 + i4x8;
1495 const int x4 = block_idx_x[idx];
1496 const int y4 = block_idx_y[idx];
1497 const int i_mvc = (i4x8 == 0);
1499 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1501 m->i_pixel = PIXEL_4x8;
1502 m->p_cost_mv = a->p_cost_mv;
1504 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1505 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1507 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1508 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1510 x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 2, 0, m->mv );
1512 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1513 REF_COST( 0, i_ref ) +
1514 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1515 if( h->mb.b_chroma_me )
1516 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1519 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1521 /* Assumes that fdec still contains the results of
1522 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1524 uint8_t **p_fenc = h->mb.pic.p_fenc;
1525 uint8_t **p_fdec = h->mb.pic.p_fdec;
1528 a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1529 for( i = 0; i < 4; i++ )
1531 const int x = (i&1)*8;
1532 const int y = (i>>1)*8;
1533 a->i_cost16x16direct +=
1534 a->i_cost8x8direct[i] =
1535 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1538 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1542 #define WEIGHTED_AVG( size, pix, stride, src1, stride1, src2, stride2 ) \
1544 h->mc.avg[size]( pix, stride, src1, stride1, src2, stride2, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1547 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1549 DECLARE_ALIGNED_16( uint8_t pix0[16*16] );
1550 DECLARE_ALIGNED_16( uint8_t pix1[16*16] );
1551 uint8_t *src0, *src1;
1552 int stride0 = 16, stride1 = 16;
1556 DECLARE_ALIGNED_4( int16_t mvc[9][2] );
1557 int i_halfpel_thresh = INT_MAX;
1558 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1560 /* 16x16 Search on all ref frame */
1561 m.i_pixel = PIXEL_16x16;
1562 m.p_cost_mv = a->p_cost_mv;
1563 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1566 a->l0.me16x16.cost = INT_MAX;
1567 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1569 /* search with ref */
1570 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1571 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1572 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1573 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1576 m.cost += REF_COST( 0, i_ref );
1578 if( m.cost < a->l0.me16x16.cost )
1580 a->l0.i_ref = i_ref;
1581 h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1584 /* save mv for predicting neighbors */
1585 *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1587 /* subtract ref cost, so we don't have to add it for the other MB types */
1588 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1591 i_halfpel_thresh = INT_MAX;
1592 p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1593 a->l1.me16x16.cost = INT_MAX;
1594 for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1596 /* search with ref */
1597 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1598 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1599 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1600 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1603 m.cost += REF_COST( 1, i_ref );
1605 if( m.cost < a->l1.me16x16.cost )
1607 a->l1.i_ref = i_ref;
1608 h->mc.memcpy_aligned( &a->l1.me16x16, &m, sizeof(x264_me_t) );
1611 /* save mv for predicting neighbors */
1612 *(uint32_t*)h->mb.mvr[1][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1614 /* subtract ref cost, so we don't have to add it for the other MB types */
1615 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1617 /* Set global ref, needed for other modes? */
1618 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1619 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1621 /* get cost of BI mode */
1622 src0 = h->mc.get_ref( pix0, &stride0,
1623 h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1624 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1], 16, 16 );
1625 src1 = h->mc.get_ref( pix1, &stride1,
1626 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1627 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1], 16, 16 );
1629 h->mc.avg[PIXEL_16x16]( pix0, 16, src0, stride0, src1, stride1, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
1631 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix0, 16 )
1632 + REF_COST( 0, a->l0.i_ref )
1633 + REF_COST( 1, a->l1.i_ref )
1634 + a->l0.me16x16.cost_mv
1635 + a->l1.me16x16.cost_mv;
1638 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1639 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1640 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1643 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1645 const int x = 2*(i%2);
1646 const int y = 2*(i/2);
1648 switch( h->mb.i_sub_partition[i] )
1651 x264_macroblock_cache_mv_ptr( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv );
1654 x264_macroblock_cache_mv_ptr( h, x, y+0, 2, 1, 0, a->l0.me8x4[i][0].mv );
1655 x264_macroblock_cache_mv_ptr( h, x, y+1, 2, 1, 0, a->l0.me8x4[i][1].mv );
1658 x264_macroblock_cache_mv_ptr( h, x+0, y, 1, 2, 0, a->l0.me4x8[i][0].mv );
1659 x264_macroblock_cache_mv_ptr( h, x+1, y, 1, 2, 0, a->l0.me4x8[i][1].mv );
1662 x264_macroblock_cache_mv_ptr( h, x+0, y+0, 1, 1, 0, a->l0.me4x4[i][0].mv );
1663 x264_macroblock_cache_mv_ptr( h, x+1, y+0, 1, 1, 0, a->l0.me4x4[i][1].mv );
1664 x264_macroblock_cache_mv_ptr( h, x+0, y+1, 1, 1, 0, a->l0.me4x4[i][2].mv );
1665 x264_macroblock_cache_mv_ptr( h, x+1, y+1, 1, 1, 0, a->l0.me4x4[i][3].mv );
1668 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1673 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1674 if( x264_mb_partition_listX_table[0][part] ) \
1676 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1677 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 0, me0.mv ); \
1681 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1682 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0 ); \
1684 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0 ); \
1686 if( x264_mb_partition_listX_table[1][part] ) \
1688 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1689 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 1, me1.mv ); \
1693 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1694 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0 ); \
1696 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0 ); \
1699 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1703 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1705 x264_mb_load_mv_direct8x8( h, i );
1708 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0 );
1709 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0 );
1710 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1715 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1718 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1720 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1722 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1724 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1728 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1730 uint8_t **p_fref[2] =
1731 { h->mb.pic.p_fref[0][a->l0.i_ref],
1732 h->mb.pic.p_fref[1][a->l1.i_ref] };
1733 DECLARE_ALIGNED_8( uint8_t pix[2][8*8] );
1736 /* XXX Needed for x264_mb_predict_mv */
1737 h->mb.i_partition = D_8x8;
1741 for( i = 0; i < 4; i++ )
1746 int i_part_cost_bi = 0;
1747 int stride[2] = {8,8};
1750 for( l = 0; l < 2; l++ )
1752 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1753 x264_me_t *m = &lX->me8x8[i];
1755 m->i_pixel = PIXEL_8x8;
1756 m->p_cost_mv = a->p_cost_mv;
1758 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1759 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1761 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1762 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1764 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, l, m->mv );
1767 src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1768 m->mv[0], m->mv[1], 8, 8 );
1769 i_part_cost_bi += m->cost_mv;
1770 /* FIXME: ref cost */
1772 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] );
1773 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1774 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1775 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1776 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1778 i_part_cost = a->l0.me8x8[i].cost;
1779 h->mb.i_sub_partition[i] = D_L0_8x8;
1780 COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1781 COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1782 COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1783 a->i_cost8x8bi += i_part_cost;
1785 /* XXX Needed for x264_mb_predict_mv */
1786 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1790 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1793 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1795 uint8_t **p_fref[2] =
1796 { h->mb.pic.p_fref[0][a->l0.i_ref],
1797 h->mb.pic.p_fref[1][a->l1.i_ref] };
1798 DECLARE_ALIGNED_16( uint8_t pix[2][16*8] );
1799 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1802 h->mb.i_partition = D_16x8;
1803 a->i_cost16x8bi = 0;
1805 for( i = 0; i < 2; i++ )
1808 int i_part_cost_bi = 0;
1809 int stride[2] = {16,16};
1812 /* TODO: check only the list(s) that were used in b8x8? */
1813 for( l = 0; l < 2; l++ )
1815 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1816 x264_me_t *m = &lX->me16x8[i];
1818 m->i_pixel = PIXEL_16x8;
1819 m->p_cost_mv = a->p_cost_mv;
1821 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1822 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1824 *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[2*i].mv;
1825 *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[2*i+1].mv;
1827 x264_mb_predict_mv( h, l, 8*i, 2, m->mvp );
1828 x264_me_search( h, m, mvc, 2 );
1831 src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1832 m->mv[0], m->mv[1], 16, 8 );
1833 /* FIXME: ref cost */
1834 i_part_cost_bi += m->cost_mv;
1836 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] );
1837 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1839 i_part_cost = a->l0.me16x8[i].cost;
1840 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1841 if( a->l1.me16x8[i].cost < i_part_cost )
1843 i_part_cost = a->l1.me16x8[i].cost;
1844 a->i_mb_partition16x8[i] = D_L1_8x8;
1846 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1848 i_part_cost = i_part_cost_bi;
1849 a->i_mb_partition16x8[i] = D_BI_8x8;
1851 a->i_cost16x8bi += i_part_cost;
1853 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1857 a->i_mb_type16x8 = B_L0_L0
1858 + (a->i_mb_partition16x8[0]>>2) * 3
1859 + (a->i_mb_partition16x8[1]>>2);
1860 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1863 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1865 uint8_t **p_fref[2] =
1866 { h->mb.pic.p_fref[0][a->l0.i_ref],
1867 h->mb.pic.p_fref[1][a->l1.i_ref] };
1868 DECLARE_ALIGNED_8( uint8_t pix[2][8*16] );
1869 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1872 h->mb.i_partition = D_8x16;
1873 a->i_cost8x16bi = 0;
1875 for( i = 0; i < 2; i++ )
1878 int i_part_cost_bi = 0;
1879 int stride[2] = {8,8};
1882 for( l = 0; l < 2; l++ )
1884 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1885 x264_me_t *m = &lX->me8x16[i];
1887 m->i_pixel = PIXEL_8x16;
1888 m->p_cost_mv = a->p_cost_mv;
1890 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1891 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1893 *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[i].mv;
1894 *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[i+2].mv;
1896 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1897 x264_me_search( h, m, mvc, 2 );
1900 src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1901 m->mv[0], m->mv[1], 8, 16 );
1902 /* FIXME: ref cost */
1903 i_part_cost_bi += m->cost_mv;
1906 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] );
1907 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1909 i_part_cost = a->l0.me8x16[i].cost;
1910 a->i_mb_partition8x16[i] = D_L0_8x8;
1911 if( a->l1.me8x16[i].cost < i_part_cost )
1913 i_part_cost = a->l1.me8x16[i].cost;
1914 a->i_mb_partition8x16[i] = D_L1_8x8;
1916 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1918 i_part_cost = i_part_cost_bi;
1919 a->i_mb_partition8x16[i] = D_BI_8x8;
1921 a->i_cost8x16bi += i_part_cost;
1923 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1927 a->i_mb_type8x16 = B_L0_L0
1928 + (a->i_mb_partition8x16[0]>>2) * 3
1929 + (a->i_mb_partition8x16[1]>>2);
1930 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1933 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1935 int thresh = i_satd * 5/4;
1937 h->mb.i_type = P_L0;
1938 if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1940 h->mb.i_partition = D_16x16;
1941 x264_analyse_update_cache( h, a );
1942 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1944 a->l0.me16x16.cost = a->l0.i_rd16x16;
1946 if( a->l0.i_cost16x8 <= thresh )
1948 h->mb.i_partition = D_16x8;
1949 x264_analyse_update_cache( h, a );
1950 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1953 a->l0.i_cost16x8 = COST_MAX;
1955 if( a->l0.i_cost8x16 <= thresh )
1957 h->mb.i_partition = D_8x16;
1958 x264_analyse_update_cache( h, a );
1959 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1962 a->l0.i_cost8x16 = COST_MAX;
1964 if( a->l0.i_cost8x8 <= thresh )
1966 h->mb.i_type = P_8x8;
1967 h->mb.i_partition = D_8x8;
1968 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1971 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
1972 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
1973 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
1974 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
1975 /* FIXME: In the 8x8 blocks where RDO isn't run, the NNZ values used for context selection
1976 * for future blocks are those left over from previous RDO calls. */
1977 for( i = 0; i < 4; i++ )
1979 int costs[4] = {a->l0.i_cost4x4[i], a->l0.i_cost8x4[i], a->l0.i_cost4x8[i], a->l0.me8x8[i].cost};
1980 int thresh = X264_MIN4( costs[0], costs[1], costs[2], costs[3] ) * 5 / 4;
1981 int subtype, btype = D_L0_8x8;
1982 uint64_t bcost = COST_MAX64;
1983 for( subtype = D_L0_4x4; subtype <= D_L0_8x8; subtype++ )
1986 if( costs[subtype] > thresh || (subtype == D_L0_8x8 && bcost == COST_MAX64) )
1988 h->mb.i_sub_partition[i] = subtype;
1989 x264_mb_cache_mv_p8x8( h, a, i );
1990 cost = x264_rd_cost_part( h, a->i_lambda2, i<<2, PIXEL_8x8 );
1991 COPY2_IF_LT( bcost, cost, btype, subtype );
1993 h->mb.i_sub_partition[i] = btype;
1994 x264_mb_cache_mv_p8x8( h, a, i );
1998 x264_analyse_update_cache( h, a );
1999 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
2002 a->l0.i_cost8x8 = COST_MAX;
2005 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
2007 int thresh = i_satd_inter * (17 + (!!h->mb.i_psy_rd))/16;
2009 if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
2011 h->mb.i_type = B_DIRECT;
2012 /* Assumes direct/skip MC is still in fdec */
2013 /* Requires b-rdo to be done before intra analysis */
2014 h->mb.b_skip_mc = 1;
2015 x264_analyse_update_cache( h, a );
2016 a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
2017 h->mb.b_skip_mc = 0;
2020 //FIXME not all the update_cache calls are needed
2021 h->mb.i_partition = D_16x16;
2023 if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
2025 h->mb.i_type = B_L0_L0;
2026 x264_analyse_update_cache( h, a );
2027 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
2031 if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
2033 h->mb.i_type = B_L1_L1;
2034 x264_analyse_update_cache( h, a );
2035 a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
2039 if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
2041 h->mb.i_type = B_BI_BI;
2042 x264_analyse_update_cache( h, a );
2043 a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
2047 if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
2049 h->mb.i_type = B_8x8;
2050 h->mb.i_partition = D_8x8;
2051 x264_analyse_update_cache( h, a );
2052 a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
2053 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
2057 if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
2059 h->mb.i_type = a->i_mb_type16x8;
2060 h->mb.i_partition = D_16x8;
2061 x264_analyse_update_cache( h, a );
2062 a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
2066 if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
2068 h->mb.i_type = a->i_mb_type8x16;
2069 h->mb.i_partition = D_8x16;
2070 x264_analyse_update_cache( h, a );
2071 a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
2075 static void x264_refine_bidir( x264_t *h, x264_mb_analysis_t *a )
2077 const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
2080 if( IS_INTRA(h->mb.i_type) )
2083 switch( h->mb.i_partition )
2086 if( h->mb.i_type == B_BI_BI )
2087 x264_me_refine_bidir_satd( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
2090 for( i=0; i<2; i++ )
2091 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
2092 x264_me_refine_bidir_satd( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
2095 for( i=0; i<2; i++ )
2096 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
2097 x264_me_refine_bidir_satd( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
2100 for( i=0; i<4; i++ )
2101 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
2102 x264_me_refine_bidir_satd( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
2107 static inline void x264_mb_analyse_transform( x264_t *h )
2109 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 && !h->mb.b_lossless )
2111 int i_cost4, i_cost8;
2112 /* Only luma MC is really needed, but the full MC is re-used in macroblock_encode. */
2115 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2116 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2117 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2118 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2120 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2121 h->mb.b_skip_mc = 1;
2125 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2127 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2130 x264_analyse_update_cache( h, a );
2131 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2132 /* FIXME only luma is needed, but the score for comparison already includes chroma */
2133 i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2135 if( *i_rd >= i_rd8 )
2138 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2142 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2147 /*****************************************************************************
2148 * x264_macroblock_analyse:
2149 *****************************************************************************/
2150 void x264_macroblock_analyse( x264_t *h )
2152 x264_mb_analysis_t analysis;
2153 int i_cost = COST_MAX;
2156 h->mb.i_qp = x264_ratecontrol_qp( h );
2157 if( h->param.rc.i_aq_mode )
2158 x264_adaptive_quant( h );
2160 x264_mb_analyse_init( h, &analysis, h->mb.i_qp );
2162 /*--------------------------- Do the analysis ---------------------------*/
2163 if( h->sh.i_type == SLICE_TYPE_I )
2165 if( analysis.i_mbrd )
2166 x264_mb_cache_fenc_satd( h );
2167 x264_mb_analyse_intra( h, &analysis, COST_MAX );
2168 if( analysis.i_mbrd )
2169 x264_intra_rd( h, &analysis, COST_MAX );
2171 i_cost = analysis.i_satd_i16x16;
2172 h->mb.i_type = I_16x16;
2173 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, h->mb.i_type, I_4x4 );
2174 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, h->mb.i_type, I_8x8 );
2175 if( analysis.i_satd_pcm < i_cost )
2176 h->mb.i_type = I_PCM;
2178 else if( analysis.i_mbrd >= 2 )
2179 x264_intra_rd_refine( h, &analysis );
2181 else if( h->sh.i_type == SLICE_TYPE_P )
2185 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2187 /* Fast P_SKIP detection */
2188 analysis.b_try_pskip = 0;
2189 if( h->param.analyse.b_fast_pskip )
2191 if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2192 // FIXME don't need to check this if the reference frame is done
2194 else if( h->param.analyse.i_subpel_refine >= 3 )
2195 analysis.b_try_pskip = 1;
2196 else if( h->mb.i_mb_type_left == P_SKIP ||
2197 h->mb.i_mb_type_top == P_SKIP ||
2198 h->mb.i_mb_type_topleft == P_SKIP ||
2199 h->mb.i_mb_type_topright == P_SKIP )
2200 b_skip = x264_macroblock_probe_pskip( h );
2203 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2207 h->mb.i_type = P_SKIP;
2208 h->mb.i_partition = D_16x16;
2209 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2213 const unsigned int flags = h->param.analyse.inter;
2217 int i_satd_inter, i_satd_intra;
2219 x264_mb_analyse_load_costs( h, &analysis );
2221 x264_mb_analyse_inter_p16x16( h, &analysis );
2223 if( h->mb.i_type == P_SKIP )
2226 if( flags & X264_ANALYSE_PSUB16x16 )
2228 if( h->param.analyse.b_mixed_references )
2229 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2231 x264_mb_analyse_inter_p8x8( h, &analysis );
2234 /* Select best inter mode */
2236 i_partition = D_16x16;
2237 i_cost = analysis.l0.me16x16.cost;
2239 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2240 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2243 i_partition = D_8x8;
2244 i_cost = analysis.l0.i_cost8x8;
2247 if( flags & X264_ANALYSE_PSUB8x8 )
2249 for( i = 0; i < 4; i++ )
2251 x264_mb_analyse_inter_p4x4( h, &analysis, i );
2252 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2254 int i_cost8x8 = analysis.l0.i_cost4x4[i];
2255 h->mb.i_sub_partition[i] = D_L0_4x4;
2257 x264_mb_analyse_inter_p8x4( h, &analysis, i );
2258 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2259 h->mb.i_sub_partition[i], D_L0_8x4 );
2261 x264_mb_analyse_inter_p4x8( h, &analysis, i );
2262 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2263 h->mb.i_sub_partition[i], D_L0_4x8 );
2265 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2267 x264_mb_cache_mv_p8x8( h, &analysis, i );
2269 analysis.l0.i_cost8x8 = i_cost;
2273 /* Now do 16x8/8x16 */
2274 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2275 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2276 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2278 x264_mb_analyse_inter_p16x8( h, &analysis );
2279 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2281 x264_mb_analyse_inter_p8x16( h, &analysis );
2282 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2285 h->mb.i_partition = i_partition;
2288 //FIXME mb_type costs?
2289 if( analysis.i_mbrd )
2293 else if( i_partition == D_16x16 )
2295 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2296 i_cost = analysis.l0.me16x16.cost;
2298 else if( i_partition == D_16x8 )
2300 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2301 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2302 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2304 else if( i_partition == D_8x16 )
2306 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2307 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2308 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2310 else if( i_partition == D_8x8 )
2314 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2316 switch( h->mb.i_sub_partition[i8x8] )
2319 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2320 i_cost += analysis.l0.me8x8[i8x8].cost;
2323 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2324 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2325 i_cost += analysis.l0.me8x4[i8x8][0].cost +
2326 analysis.l0.me8x4[i8x8][1].cost;
2329 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2330 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2331 i_cost += analysis.l0.me4x8[i8x8][0].cost +
2332 analysis.l0.me4x8[i8x8][1].cost;
2336 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2337 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2338 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2339 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2340 i_cost += analysis.l0.me4x4[i8x8][0].cost +
2341 analysis.l0.me4x4[i8x8][1].cost +
2342 analysis.l0.me4x4[i8x8][2].cost +
2343 analysis.l0.me4x4[i8x8][3].cost;
2346 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2352 if( h->mb.b_chroma_me )
2354 x264_mb_analyse_intra_chroma( h, &analysis );
2355 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2356 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2357 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2358 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2361 x264_mb_analyse_intra( h, &analysis, i_cost );
2363 i_satd_inter = i_cost;
2364 i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2365 analysis.i_satd_i8x8,
2366 analysis.i_satd_i4x4 );
2368 if( analysis.i_mbrd )
2370 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2372 i_partition = D_16x16;
2373 i_cost = analysis.l0.me16x16.cost;
2374 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2375 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2376 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2377 h->mb.i_type = i_type;
2378 h->mb.i_partition = i_partition;
2379 if( i_cost < COST_MAX )
2380 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2381 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2384 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2385 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2386 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2387 COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
2389 h->mb.i_type = i_type;
2391 if( analysis.i_mbrd >= 2 && h->mb.i_type != I_PCM )
2393 if( IS_INTRA( h->mb.i_type ) )
2395 x264_intra_rd_refine( h, &analysis );
2397 else if( i_partition == D_16x16 )
2399 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2400 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 );
2402 else if( i_partition == D_16x8 )
2404 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2405 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2406 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2407 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2408 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0, 0 );
2409 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 8, 0 );
2411 else if( i_partition == D_8x16 )
2413 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2414 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2415 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2416 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2417 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0, 0 );
2418 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 4, 0 );
2420 else if( i_partition == D_8x8 )
2423 x264_analyse_update_cache( h, &analysis );
2424 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2426 if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2428 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8*4, 0 );
2430 else if( h->mb.i_sub_partition[i8x8] == D_L0_8x4 )
2432 x264_me_refine_qpel_rd( h, &analysis.l0.me8x4[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
2433 x264_me_refine_qpel_rd( h, &analysis.l0.me8x4[i8x8][1], analysis.i_lambda2, i8x8*4+2, 0 );
2435 else if( h->mb.i_sub_partition[i8x8] == D_L0_4x8 )
2437 x264_me_refine_qpel_rd( h, &analysis.l0.me4x8[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
2438 x264_me_refine_qpel_rd( h, &analysis.l0.me4x8[i8x8][1], analysis.i_lambda2, i8x8*4+1, 0 );
2440 else if( h->mb.i_sub_partition[i8x8] == D_L0_4x4 )
2442 x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
2443 x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][1], analysis.i_lambda2, i8x8*4+1, 0 );
2444 x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][2], analysis.i_lambda2, i8x8*4+2, 0 );
2445 x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][3], analysis.i_lambda2, i8x8*4+3, 0 );
2452 else if( h->sh.i_type == SLICE_TYPE_B )
2454 int i_bskip_cost = COST_MAX;
2457 if( analysis.i_mbrd )
2458 x264_mb_cache_fenc_satd( h );
2460 h->mb.i_type = B_SKIP;
2461 if( h->mb.b_direct_auto_write )
2463 /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2464 for( i = 0; i < 2; i++ )
2467 h->sh.b_direct_spatial_mv_pred ^= 1;
2468 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2469 if( analysis.b_direct_available )
2474 b_skip = x264_macroblock_probe_bskip( h );
2476 h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2483 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2485 if( analysis.b_direct_available )
2487 if( !h->mb.b_direct_auto_write )
2489 if( analysis.i_mbrd )
2491 i_bskip_cost = ssd_mb( h );
2492 /* 6 = minimum cavlc cost of a non-skipped MB */
2493 b_skip = h->mb.b_skip_mc = i_bskip_cost <= ((6 * analysis.i_lambda2 + 128) >> 8);
2495 else if( !h->mb.b_direct_auto_write )
2497 /* Conditioning the probe on neighboring block types
2498 * doesn't seem to help speed or quality. */
2499 b_skip = x264_macroblock_probe_bskip( h );
2505 const unsigned int flags = h->param.analyse.inter;
2508 int i_satd_inter = 0; // shut up uninitialized warning
2509 h->mb.b_skip_mc = 0;
2511 x264_mb_analyse_load_costs( h, &analysis );
2513 /* select best inter mode */
2514 /* direct must be first */
2515 if( analysis.b_direct_available )
2516 x264_mb_analyse_inter_direct( h, &analysis );
2518 x264_mb_analyse_inter_b16x16( h, &analysis );
2521 i_partition = D_16x16;
2522 i_cost = analysis.l0.me16x16.cost;
2523 COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2524 COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2525 COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2527 if( analysis.i_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2529 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2530 if( i_bskip_cost < analysis.i_rd16x16direct &&
2531 i_bskip_cost < analysis.i_rd16x16bi &&
2532 i_bskip_cost < analysis.l0.i_rd16x16 &&
2533 i_bskip_cost < analysis.l1.i_rd16x16 )
2535 h->mb.i_type = B_SKIP;
2536 x264_analyse_update_cache( h, &analysis );
2541 if( flags & X264_ANALYSE_BSUB16x16 )
2543 x264_mb_analyse_inter_b8x8( h, &analysis );
2544 if( analysis.i_cost8x8bi < i_cost )
2547 i_partition = D_8x8;
2548 i_cost = analysis.i_cost8x8bi;
2550 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2551 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2553 x264_mb_analyse_inter_b16x8( h, &analysis );
2554 COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2555 i_type, analysis.i_mb_type16x8,
2556 i_partition, D_16x8 );
2558 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2559 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2561 x264_mb_analyse_inter_b8x16( h, &analysis );
2562 COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2563 i_type, analysis.i_mb_type8x16,
2564 i_partition, D_8x16 );
2569 if( analysis.i_mbrd )
2574 else if( i_partition == D_16x16 )
2576 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2577 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2578 if( i_type == B_L0_L0 )
2580 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2581 i_cost = analysis.l0.me16x16.cost
2582 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2584 else if( i_type == B_L1_L1 )
2586 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2587 i_cost = analysis.l1.me16x16.cost
2588 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2590 else if( i_type == B_BI_BI )
2592 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2593 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2596 else if( i_partition == D_16x8 )
2598 for( i=0; i<2; i++ )
2600 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2601 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2602 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2603 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2606 else if( i_partition == D_8x16 )
2608 for( i=0; i<2; i++ )
2610 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2611 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2612 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2613 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2616 else if( i_partition == D_8x8 )
2618 for( i=0; i<4; i++ )
2621 int i_part_cost_old;
2623 int i_part_type = h->mb.i_sub_partition[i];
2624 int b_bidir = (i_part_type == D_BI_8x8);
2626 if( i_part_type == D_DIRECT_8x8 )
2628 if( x264_mb_partition_listX_table[0][i_part_type] )
2630 m = &analysis.l0.me8x8[i];
2631 i_part_cost_old = m->cost;
2632 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2633 m->cost -= i_type_cost;
2634 x264_me_refine_qpel( h, m );
2636 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2638 if( x264_mb_partition_listX_table[1][i_part_type] )
2640 m = &analysis.l1.me8x8[i];
2641 i_part_cost_old = m->cost;
2642 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2643 m->cost -= i_type_cost;
2644 x264_me_refine_qpel( h, m );
2646 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2648 /* TODO: update mvp? */
2652 if( analysis.i_mbrd )
2654 i_satd_inter = i_cost;
2655 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2657 i_cost = i_bskip_cost;
2658 i_partition = D_16x16;
2659 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2660 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2661 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2662 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2663 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2664 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2665 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2667 h->mb.i_type = i_type;
2668 h->mb.i_partition = i_partition;
2671 x264_mb_analyse_intra( h, &analysis, i_satd_inter );
2673 if( analysis.i_mbrd )
2675 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2676 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2679 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2680 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2681 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2682 COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
2684 h->mb.i_type = i_type;
2685 h->mb.i_partition = i_partition;
2687 if( analysis.i_mbrd >= 2 && IS_INTRA( i_type ) && i_type != I_PCM )
2688 x264_intra_rd_refine( h, &analysis );
2689 if( h->mb.i_subpel_refine >= 5 )
2690 x264_refine_bidir( h, &analysis );
2692 if( analysis.i_mbrd >= 2 && i_type > B_DIRECT && i_type < B_SKIP )
2694 const int i_biweight = h->mb.bipred_weight[analysis.l0.i_ref][analysis.l1.i_ref];
2695 x264_analyse_update_cache( h, &analysis );
2697 if( i_partition == D_16x16 )
2699 if( i_type == B_L0_L0 )
2700 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 );
2701 else if( i_type == B_L1_L1 )
2702 x264_me_refine_qpel_rd( h, &analysis.l1.me16x16, analysis.i_lambda2, 0, 1 );
2703 else if( i_type == B_BI_BI )
2704 x264_me_refine_bidir_rd( h, &analysis.l0.me16x16, &analysis.l1.me16x16, i_biweight, 0, analysis.i_lambda2 );
2706 else if( i_partition == D_16x8 )
2708 for( i = 0; i < 2; i++ )
2710 h->mb.i_sub_partition[i*2] = h->mb.i_sub_partition[i*2+1] = analysis.i_mb_partition16x8[i];
2711 if( analysis.i_mb_partition16x8[i] == D_L0_8x8 )
2712 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[i], analysis.i_lambda2, i*8, 0 );
2713 else if( analysis.i_mb_partition16x8[i] == D_L1_8x8 )
2714 x264_me_refine_qpel_rd( h, &analysis.l1.me16x8[i], analysis.i_lambda2, i*8, 1 );
2715 else if( analysis.i_mb_partition16x8[i] == D_BI_8x8 )
2716 x264_me_refine_bidir_rd( h, &analysis.l0.me16x8[i], &analysis.l1.me16x8[i], i_biweight, i*2, analysis.i_lambda2 );
2719 else if( i_partition == D_8x16 )
2721 for( i = 0; i < 2; i++ )
2723 h->mb.i_sub_partition[i] = h->mb.i_sub_partition[i+2] = analysis.i_mb_partition8x16[i];
2724 if( analysis.i_mb_partition8x16[i] == D_L0_8x8 )
2725 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[i], analysis.i_lambda2, i*4, 0 );
2726 else if( analysis.i_mb_partition8x16[i] == D_L1_8x8 )
2727 x264_me_refine_qpel_rd( h, &analysis.l1.me8x16[i], analysis.i_lambda2, i*4, 1 );
2728 else if( analysis.i_mb_partition8x16[i] == D_BI_8x8 )
2729 x264_me_refine_bidir_rd( h, &analysis.l0.me8x16[i], &analysis.l1.me8x16[i], i_biweight, i, analysis.i_lambda2 );
2732 else if( i_partition == D_8x8 )
2734 for( i = 0; i < 4; i++ )
2736 if( h->mb.i_sub_partition[i] == D_L0_8x8 )
2737 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i], analysis.i_lambda2, i*4, 0 );
2738 else if( h->mb.i_sub_partition[i] == D_L1_8x8 )
2739 x264_me_refine_qpel_rd( h, &analysis.l1.me8x8[i], analysis.i_lambda2, i*4, 1 );
2740 else if( h->mb.i_sub_partition[i] == D_BI_8x8 )
2741 x264_me_refine_bidir_rd( h, &analysis.l0.me8x8[i], &analysis.l1.me8x8[i], i_biweight, i, analysis.i_lambda2 );
2748 x264_analyse_update_cache( h, &analysis );
2750 if( !analysis.i_mbrd )
2751 x264_mb_analyse_transform( h );
2753 h->mb.b_trellis = h->param.analyse.i_trellis;
2754 h->mb.b_noise_reduction = !!h->param.analyse.i_noise_reduction;
2755 if( !IS_SKIP(h->mb.i_type) && h->mb.i_psy_trellis && h->param.analyse.i_trellis == 1 )
2756 x264_psy_trellis_init( h, 0 );
2757 if( h->mb.b_trellis == 1 || h->mb.b_noise_reduction )
2758 h->mb.i_skip_intra = 0;
2761 /*-------------------- Update MB from the analysis ----------------------*/
2762 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2766 switch( h->mb.i_type )
2769 for( i = 0; i < 16; i++ )
2770 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2772 x264_mb_analyse_intra_chroma( h, a );
2775 for( i = 0; i < 4; i++ )
2776 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2778 x264_mb_analyse_intra_chroma( h, a );
2781 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2782 x264_mb_analyse_intra_chroma( h, a );
2789 switch( h->mb.i_partition )
2792 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2793 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2797 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2798 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2799 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv );
2800 x264_macroblock_cache_mv_ptr( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv );
2804 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2805 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2806 x264_macroblock_cache_mv_ptr( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv );
2807 x264_macroblock_cache_mv_ptr( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv );
2811 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2817 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2818 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2819 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2820 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2821 for( i = 0; i < 4; i++ )
2822 x264_mb_cache_mv_p8x8( h, a, i );
2827 h->mb.i_partition = D_16x16;
2828 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2829 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv );
2835 x264_mb_load_mv_direct8x8( h, 0 );
2836 x264_mb_load_mv_direct8x8( h, 1 );
2837 x264_mb_load_mv_direct8x8( h, 2 );
2838 x264_mb_load_mv_direct8x8( h, 3 );
2842 /* optimize: cache might not need to be rewritten */
2843 for( i = 0; i < 4; i++ )
2844 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2847 default: /* the rest of the B types */
2848 switch( h->mb.i_partition )
2851 switch( h->mb.i_type )
2854 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2855 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2857 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2858 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0 );
2859 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0 );
2862 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2863 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0 );
2864 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0 );
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 );
2870 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2871 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2873 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2874 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2879 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2880 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2883 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2884 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2887 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2893 if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2896 for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2899 int ref = h->mb.cache.ref[l][x264_scan8[0]];
2902 completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2903 if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2905 x264_log( h, X264_LOG_WARNING, "internal error (MV out of thread range)\n");
2906 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2907 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2908 h->mb.cache.mv[l][x264_scan8[15]][0],
2909 h->mb.cache.mv[l][x264_scan8[15]][1] );
2910 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2911 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2912 fprintf(stderr, "completed: %d \n", completed );
2913 x264_log( h, X264_LOG_WARNING, "recovering by using intra mode\n");
2914 x264_mb_analyse_intra( h, a, COST_MAX );
2915 h->mb.i_type = I_16x16;
2916 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2917 x264_mb_analyse_intra_chroma( h, a );
2924 #include "slicetype.c"