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>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 *****************************************************************************/
30 #include "common/common.h"
31 #include "macroblock.h"
33 #include "ratecontrol.h"
46 /* [ref][0] is 16x16 mv, [ref][1..4] are 8x8 mv from partition [0..3] */
47 DECLARE_ALIGNED_4( int16_t mvc[32][5][2] );
51 int i_cost4x4[4]; /* cost per 8x8 partition */
52 x264_me_t me4x4[4][4];
55 int i_cost8x4[4]; /* cost per 8x8 partition */
56 x264_me_t me8x4[4][2];
59 int i_cost4x8[4]; /* cost per 8x8 partition */
60 x264_me_t me4x8[4][2];
70 } x264_mb_analysis_list_t;
74 /* conduct the analysis using this lamda and QP */
83 /* Take some shortcuts in intra search if intra is deemed unlikely */
89 int i_satd_i16x16_dir[7];
93 int i_satd_i8x8_dir[12][4];
102 int i_satd_i8x8chroma;
103 int i_satd_i8x8chroma_dir[4];
104 int i_predict8x8chroma;
106 /* II: Inter part P/B frame */
107 x264_mb_analysis_list_t l0;
108 x264_mb_analysis_list_t l1;
110 int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
111 int i_cost16x16direct;
113 int i_cost8x8direct[4];
122 int i_mb_partition16x8[2]; /* mb_partition_e */
123 int i_mb_partition8x16[2];
124 int i_mb_type16x8; /* mb_class_e */
127 int b_direct_available;
129 } x264_mb_analysis_t;
131 /* lambda = pow(2,qp/6-2) */
132 const int x264_lambda_tab[52] = {
133 1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
134 1, 1, 1, 1, /* 8-11 */
135 1, 1, 1, 1, 2, 2, 2, 2, /* 12-19 */
136 3, 3, 3, 4, 4, 4, 5, 6, /* 20-27 */
137 6, 7, 8, 9,10,11,13,14, /* 28-35 */
138 16,18,20,23,25,29,32,36, /* 36-43 */
139 40,45,51,57,64,72,81,91 /* 44-51 */
142 /* lambda2 = pow(lambda,2) * .9 * 256 */
143 const int x264_lambda2_tab[52] = {
144 14, 18, 22, 28, 36, 45, 57, 72, /* 0 - 7 */
145 91, 115, 145, 182, 230, 290, 365, 460, /* 8 - 15 */
146 580, 731, 921, 1161, 1462, 1843, 2322, 2925, /* 16 - 23 */
147 3686, 4644, 5851, 7372, 9289, 11703, 14745, 18578, /* 24 - 31 */
148 23407, 29491, 37156, 46814, 58982, 74313, 93628, 117964, /* 32 - 39 */
149 148626, 187257, 235929, 297252, 374514, 471859, 594505, 749029, /* 40 - 47 */
150 943718, 1189010, 1498059, 1887436 /* 48 - 51 */
153 /* TODO: calculate CABAC costs */
154 static const int i_mb_b_cost_table[X264_MBTYPE_MAX] = {
155 9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
157 static const int i_mb_b16x8_cost_table[17] = {
158 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
160 static const int i_sub_mb_b_cost_table[13] = {
161 7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
163 static const int i_sub_mb_p_cost_table[4] = {
167 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
169 uint16_t *x264_cost_mv_fpel[52][4];
171 /* initialize an array of lambda*nbits for all possible mvs */
172 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
174 static int16_t *p_cost_mv[52];
177 if( !p_cost_mv[a->i_qp] )
179 /* could be faster, but isn't called many times */
180 /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
181 p_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) );
182 p_cost_mv[a->i_qp] += 2*4*2048;
183 for( i = 0; i <= 2*4*2048; i++ )
185 p_cost_mv[a->i_qp][-i] =
186 p_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i );
189 a->p_cost_mv = p_cost_mv[a->i_qp];
191 /* FIXME is this useful for all me methods? */
192 if( h->param.analyse.i_me_method >= X264_ME_ESA && !x264_cost_mv_fpel[a->i_qp][0] )
196 x264_cost_mv_fpel[a->i_qp][j] = x264_malloc( (4*2048 + 1) * sizeof(int16_t) );
197 x264_cost_mv_fpel[a->i_qp][j] += 2*2048;
198 for( i = -2*2048; i < 2*2048; i++ )
199 x264_cost_mv_fpel[a->i_qp][j][i] = p_cost_mv[a->i_qp][i*4+j];
204 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
206 /* conduct the analysis using this lamda and QP */
207 a->i_qp = h->mb.i_qp = i_qp;
208 h->mb.i_chroma_qp = h->chroma_qp_table[i_qp];
209 a->i_lambda = x264_lambda_tab[i_qp];
210 a->i_lambda2 = x264_lambda2_tab[i_qp];
211 a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
212 ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
214 h->mb.i_me_method = h->param.analyse.i_me_method;
215 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
216 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
217 && h->mb.i_subpel_refine >= 5;
218 h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
219 h->mb.b_transform_8x8 = 0;
220 h->mb.b_noise_reduction = 0;
226 a->i_satd_i8x8chroma = COST_MAX;
228 /* non-RD PCM decision is inaccurate, so don't do it */
229 a->i_satd_pcm = a->b_mbrd ? ((uint64_t)X264_PCM_COST*a->i_lambda2 + 128) >> 8 : COST_MAX;
233 h->mb.b_lossless ? 0 :
235 !h->param.analyse.i_trellis && !h->param.analyse.i_noise_reduction;
237 /* II: Inter part P/B frame */
238 if( h->sh.i_type != SLICE_TYPE_I )
241 int i_fmv_range = 4 * h->param.analyse.i_mv_range;
242 // limit motion search to a slightly smaller range than the theoretical limit,
243 // since the search may go a few iterations past its given range
244 int i_fpel_border = 5; // umh unconditional radius
245 int i_spel_border = 8; // 1.5 for subpel_satd, 1.5 for subpel_rd, 2 for bime, round up
247 /* Calculate max allowed MV range */
248 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range-1 )
249 h->mb.mv_min[0] = 4*( -16*h->mb.i_mb_x - 24 );
250 h->mb.mv_max[0] = 4*( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 24 );
251 h->mb.mv_min_spel[0] = CLIP_FMV( h->mb.mv_min[0] );
252 h->mb.mv_max_spel[0] = CLIP_FMV( h->mb.mv_max[0] );
253 h->mb.mv_min_fpel[0] = (h->mb.mv_min_spel[0]>>2) + i_fpel_border;
254 h->mb.mv_max_fpel[0] = (h->mb.mv_max_spel[0]>>2) - i_fpel_border;
255 if( h->mb.i_mb_x == 0)
257 int mb_y = h->mb.i_mb_y >> h->sh.b_mbaff;
258 int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff;
259 int thread_mvy_range = i_fmv_range;
261 if( h->param.i_threads > 1 )
263 int pix_y = (h->mb.i_mb_y | h->mb.b_interlaced) * 16;
264 int thresh = pix_y + h->param.analyse.i_mv_range_thread;
265 for( i = (h->sh.i_type == SLICE_TYPE_B); i >= 0; i-- )
267 x264_frame_t **fref = i ? h->fref1 : h->fref0;
268 int i_ref = i ? h->i_ref1 : h->i_ref0;
269 for( j=0; j<i_ref; j++ )
271 x264_frame_cond_wait( fref[j], thresh );
272 thread_mvy_range = X264_MIN( thread_mvy_range, fref[j]->i_lines_completed - pix_y );
275 if( h->param.b_deterministic )
276 thread_mvy_range = h->param.analyse.i_mv_range_thread;
277 if( h->mb.b_interlaced )
278 thread_mvy_range >>= 1;
281 h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
282 h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
283 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 );
284 h->mb.mv_max_spel[1] = CLIP_FMV( h->mb.mv_max[1] );
285 h->mb.mv_max_spel[1] = X264_MIN( h->mb.mv_max_spel[1], thread_mvy_range*4 );
286 h->mb.mv_min_fpel[1] = (h->mb.mv_min_spel[1]>>2) + i_fpel_border;
287 h->mb.mv_max_fpel[1] = (h->mb.mv_max_spel[1]>>2) - i_fpel_border;
293 a->l0.i_cost8x8 = COST_MAX;
295 for( i = 0; i < 4; i++ )
299 a->l0.i_cost4x8[i] = COST_MAX;
303 a->l0.i_cost8x16 = COST_MAX;
304 if( h->sh.i_type == SLICE_TYPE_B )
308 a->l1.i_cost8x8 = COST_MAX;
310 for( i = 0; i < 4; i++ )
315 a->i_cost8x8direct[i] = COST_MAX;
326 a->i_cost16x16direct =
329 a->i_cost8x16bi = COST_MAX;
332 /* Fast intra decision */
333 if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
335 if( IS_INTRA( h->mb.i_mb_type_left )
336 || IS_INTRA( h->mb.i_mb_type_top )
337 || IS_INTRA( h->mb.i_mb_type_topleft )
338 || IS_INTRA( h->mb.i_mb_type_topright )
339 || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
340 || (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])) )
341 { /* intra is likely */ }
357 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
359 if( i_neighbour & MB_TOPLEFT )
361 /* top and left available */
362 *mode++ = I_PRED_16x16_V;
363 *mode++ = I_PRED_16x16_H;
364 *mode++ = I_PRED_16x16_DC;
365 *mode++ = I_PRED_16x16_P;
368 else if( i_neighbour & MB_LEFT )
371 *mode++ = I_PRED_16x16_DC_LEFT;
372 *mode++ = I_PRED_16x16_H;
375 else if( i_neighbour & MB_TOP )
378 *mode++ = I_PRED_16x16_DC_TOP;
379 *mode++ = I_PRED_16x16_V;
385 *mode = I_PRED_16x16_DC_128;
391 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
393 if( i_neighbour & MB_TOPLEFT )
395 /* top and left available */
396 *mode++ = I_PRED_CHROMA_V;
397 *mode++ = I_PRED_CHROMA_H;
398 *mode++ = I_PRED_CHROMA_DC;
399 *mode++ = I_PRED_CHROMA_P;
402 else if( i_neighbour & MB_LEFT )
405 *mode++ = I_PRED_CHROMA_DC_LEFT;
406 *mode++ = I_PRED_CHROMA_H;
409 else if( i_neighbour & MB_TOP )
412 *mode++ = I_PRED_CHROMA_DC_TOP;
413 *mode++ = I_PRED_CHROMA_V;
419 *mode = I_PRED_CHROMA_DC_128;
425 static void predict_4x4_mode_available( unsigned int i_neighbour,
426 int *mode, int *pi_count )
428 int b_l = i_neighbour & MB_LEFT;
429 int b_t = i_neighbour & MB_TOP;
434 *mode++ = I_PRED_4x4_DC;
435 *mode++ = I_PRED_4x4_H;
436 *mode++ = I_PRED_4x4_V;
437 *mode++ = I_PRED_4x4_DDL;
438 if( i_neighbour & MB_TOPLEFT )
440 *mode++ = I_PRED_4x4_DDR;
441 *mode++ = I_PRED_4x4_VR;
442 *mode++ = I_PRED_4x4_HD;
445 *mode++ = I_PRED_4x4_VL;
446 *mode++ = I_PRED_4x4_HU;
450 *mode++ = I_PRED_4x4_DC_LEFT;
451 *mode++ = I_PRED_4x4_H;
452 *mode++ = I_PRED_4x4_HU;
457 *mode++ = I_PRED_4x4_DC_TOP;
458 *mode++ = I_PRED_4x4_V;
459 *mode++ = I_PRED_4x4_DDL;
460 *mode++ = I_PRED_4x4_VL;
465 *mode++ = I_PRED_4x4_DC_128;
470 /* 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. */
471 static void inline x264_psy_trellis_init( x264_t *h, int do_both_dct )
473 DECLARE_ALIGNED_16( int16_t dct8x8[4][8][8] );
474 DECLARE_ALIGNED_16( int16_t dct4x4[16][4][4] );
475 DECLARE_ALIGNED_16( uint8_t zero[16*FDEC_STRIDE] ) = {0};
478 if( do_both_dct || h->mb.b_transform_8x8 )
480 h->dctf.sub16x16_dct8( dct8x8, h->mb.pic.p_fenc[0], zero );
481 for( i = 0; i < 4; i++ )
482 h->zigzagf.scan_8x8( h->mb.pic.fenc_dct8[i], dct8x8[i] );
484 if( do_both_dct || !h->mb.b_transform_8x8 )
486 h->dctf.sub16x16_dct( dct4x4, h->mb.pic.p_fenc[0], zero );
487 for( i = 0; i < 16; i++ )
488 h->zigzagf.scan_4x4( h->mb.pic.fenc_dct4[i], dct4x4[i] );
492 /* Pre-calculate fenc satd scores for psy RD, minus DC coefficients */
493 static inline void x264_mb_cache_fenc_satd( x264_t *h )
495 DECLARE_ALIGNED_16(uint8_t zero[16]) = {0};
497 int x, y, satd_sum = 0, sa8d_sum = 0;
498 if( h->param.analyse.i_trellis == 2 && h->mb.i_psy_trellis )
499 x264_psy_trellis_init( h, h->param.analyse.b_transform_8x8 );
500 if( !h->mb.i_psy_rd )
502 for( y = 0; y < 4; y++ )
503 for( x = 0; x < 4; x++ )
505 fenc = h->mb.pic.p_fenc[0]+x*4+y*4*FENC_STRIDE;
506 h->mb.pic.fenc_satd[y][x] = h->pixf.satd[PIXEL_4x4]( zero, 0, fenc, FENC_STRIDE )
507 - (h->pixf.sad[PIXEL_4x4]( zero, 0, fenc, FENC_STRIDE )>>1);
508 satd_sum += h->mb.pic.fenc_satd[y][x];
510 for( y = 0; y < 2; y++ )
511 for( x = 0; x < 2; x++ )
513 fenc = h->mb.pic.p_fenc[0]+x*8+y*8*FENC_STRIDE;
514 h->mb.pic.fenc_sa8d[y][x] = h->pixf.sa8d[PIXEL_8x8]( zero, 0, fenc, FENC_STRIDE )
515 - (h->pixf.sad[PIXEL_8x8]( zero, 0, fenc, FENC_STRIDE )>>2);
516 sa8d_sum += h->mb.pic.fenc_sa8d[y][x];
518 h->mb.pic.fenc_satd_sum = satd_sum;
519 h->mb.pic.fenc_sa8d_sum = sa8d_sum;
522 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
529 uint8_t *p_dstc[2], *p_srcc[2];
531 if( a->i_satd_i8x8chroma < COST_MAX )
534 /* 8x8 prediction selection for chroma */
535 p_dstc[0] = h->mb.pic.p_fdec[1];
536 p_dstc[1] = h->mb.pic.p_fdec[2];
537 p_srcc[0] = h->mb.pic.p_fenc[1];
538 p_srcc[1] = h->mb.pic.p_fenc[2];
540 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
541 a->i_satd_i8x8chroma = COST_MAX;
542 if( i_max == 4 && h->pixf.intra_satd_x3_8x8c && h->pixf.mbcmp[0] == h->pixf.satd[0] )
544 int satdu[4], satdv[4];
545 h->pixf.intra_satd_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
546 h->pixf.intra_satd_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
547 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
548 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
549 satdu[I_PRED_CHROMA_P] =
550 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
551 satdv[I_PRED_CHROMA_P] =
552 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
554 for( i=0; i<i_max; i++ )
556 int i_mode = predict_mode[i];
557 int i_satd = satdu[i_mode] + satdv[i_mode]
558 + a->i_lambda * bs_size_ue(i_mode);
560 a->i_satd_i8x8chroma_dir[i] = i_satd;
561 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
566 for( i=0; i<i_max; i++ )
569 int i_mode = predict_mode[i];
571 /* we do the prediction */
572 h->predict_8x8c[i_mode]( p_dstc[0] );
573 h->predict_8x8c[i_mode]( p_dstc[1] );
575 /* we calculate the cost */
576 i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
577 p_srcc[0], FENC_STRIDE ) +
578 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
579 p_srcc[1], FENC_STRIDE ) +
580 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
582 a->i_satd_i8x8chroma_dir[i] = i_satd;
583 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
587 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
590 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
592 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
593 uint8_t *p_src = h->mb.pic.p_fenc[0];
594 uint8_t *p_dst = h->mb.pic.p_fdec[0];
599 int b_merged_satd = !!h->pixf.intra_mbcmp_x3_16x16;
601 /*---------------- Try all mode and calculate their score ---------------*/
603 /* 16x16 prediction selection */
604 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
606 if( b_merged_satd && i_max == 4 )
608 h->pixf.intra_mbcmp_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
609 h->predict_16x16[I_PRED_16x16_P]( p_dst );
610 a->i_satd_i16x16_dir[I_PRED_16x16_P] =
611 h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
614 int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
615 COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
620 for( i = 0; i < i_max; i++ )
623 int i_mode = predict_mode[i];
624 h->predict_16x16[i_mode]( p_dst );
626 i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
627 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
628 COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
629 a->i_satd_i16x16_dir[i_mode] = i_satd;
633 if( h->sh.i_type == SLICE_TYPE_B )
634 /* cavlc mb type prefix */
635 a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
636 if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
639 /* 8x8 prediction selection */
640 if( flags & X264_ANALYSE_I8x8 )
642 DECLARE_ALIGNED_16( uint8_t edge[33] );
643 x264_pixel_cmp_t sa8d = (h->pixf.mbcmp[0] == h->pixf.satd[0]) ? h->pixf.sa8d[PIXEL_8x8] : h->pixf.mbcmp[PIXEL_8x8];
644 int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
646 b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
648 // FIXME some bias like in i4x4?
649 if( h->sh.i_type == SLICE_TYPE_B )
650 i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
652 for( idx = 0;; idx++ )
656 uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
657 uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
658 int i_best = COST_MAX;
659 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
661 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
662 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
664 if( b_merged_satd && i_max == 9 )
667 h->pixf.intra_sa8d_x3_8x8( p_src_by, edge, satd );
668 satd[i_pred_mode] -= 3 * a->i_lambda;
669 for( i=2; i>=0; i-- )
671 int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
672 COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
679 for( ; i<i_max; i++ )
682 int i_mode = predict_mode[i];
684 h->predict_8x8[i_mode]( p_dst_by, edge );
686 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
687 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
689 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
690 a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
694 if( idx == 3 || i_cost > i_satd_thresh )
697 /* we need to encode this block now (for next ones) */
698 h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
699 x264_mb_encode_i8x8( h, idx, a->i_qp );
701 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
706 a->i_satd_i8x8 = i_cost;
707 if( h->mb.i_skip_intra )
709 h->mc.copy[PIXEL_16x16]( h->mb.pic.i8x8_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
710 if( h->mb.i_skip_intra == 2 )
711 h->mc.memcpy_aligned( h->mb.pic.i8x8_dct_buf, h->dct.luma8x8, sizeof(h->mb.pic.i8x8_dct_buf) );
716 a->i_satd_i8x8 = COST_MAX;
717 i_cost = i_cost * 4/(idx+1);
719 if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 )
723 /* 4x4 prediction selection */
724 if( flags & X264_ANALYSE_I4x4 )
727 int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
728 b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
730 i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
732 i_cost = a->i_lambda * 24; /* from JVT (SATD0) */
733 if( h->sh.i_type == SLICE_TYPE_B )
734 i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];
736 for( idx = 0;; idx++ )
738 uint8_t *p_src_by = p_src + block_idx_xy_fenc[idx];
739 uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
740 int i_best = COST_MAX;
741 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
743 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
745 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
746 /* emulate missing topright samples */
747 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
749 if( b_merged_satd && i_max >= 6 )
752 h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
753 satd[i_pred_mode] -= 3 * a->i_lambda;
754 for( i=2; i>=0; i-- )
755 COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
756 a->i_predict4x4[idx], i );
762 for( ; i<i_max; i++ )
765 int i_mode = predict_mode[i];
767 h->predict_4x4[i_mode]( p_dst_by );
769 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
770 p_src_by, FENC_STRIDE )
771 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
773 COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
777 if( i_cost > i_satd_thresh || idx == 15 )
780 /* we need to encode this block now (for next ones) */
781 h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
782 x264_mb_encode_i4x4( h, idx, a->i_qp );
784 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
788 a->i_satd_i4x4 = i_cost;
789 if( h->mb.i_skip_intra )
791 h->mc.copy[PIXEL_16x16]( h->mb.pic.i4x4_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
792 if( h->mb.i_skip_intra == 2 )
793 h->mc.memcpy_aligned( h->mb.pic.i4x4_dct_buf, h->dct.luma4x4, sizeof(h->mb.pic.i4x4_dct_buf) );
797 a->i_satd_i4x4 = COST_MAX;
801 static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
803 if( a->i_satd_i16x16 <= i_satd_thresh )
805 h->mb.i_type = I_16x16;
806 x264_analyse_update_cache( h, a );
807 a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
810 a->i_satd_i16x16 = COST_MAX;
812 if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
814 h->mb.i_type = I_4x4;
815 x264_analyse_update_cache( h, a );
816 a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
819 a->i_satd_i4x4 = COST_MAX;
821 if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
823 h->mb.i_type = I_8x8;
824 x264_analyse_update_cache( h, a );
825 a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
828 a->i_satd_i8x8 = COST_MAX;
831 static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
833 uint8_t *p_src = h->mb.pic.p_fenc[0];
834 uint8_t *p_dst = h->mb.pic.p_fdec[0];
837 int i_max, i_mode, i_thresh;
838 uint64_t i_satd, i_best;
841 h->mb.i_skip_intra = 0;
843 if( h->mb.i_type == I_16x16 )
845 int old_pred_mode = a->i_predict16x16;
846 i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
847 i_best = a->i_satd_i16x16;
848 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
849 for( i = 0; i < i_max; i++ )
851 int i_mode = predict_mode[i];
852 if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
854 h->mb.i_intra16x16_pred_mode = i_mode;
855 i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
856 COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
859 else if( h->mb.i_type == I_4x4 )
861 uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
863 for( idx = 0; idx < 16; idx++ )
865 uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
868 i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
870 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
872 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
873 /* emulate missing topright samples */
874 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
876 for( i = 0; i < i_max; i++ )
878 i_mode = predict_mode[i];
879 h->predict_4x4[i_mode]( p_dst_by );
880 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
882 if( i_best > i_satd )
884 a->i_predict4x4[idx] = i_mode;
886 pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
887 pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
888 pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
889 pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
890 i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
894 *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
895 *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
896 *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
897 *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
898 h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
900 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
903 else if( h->mb.i_type == I_8x8 )
905 DECLARE_ALIGNED_16( uint8_t edge[33] );
906 for( idx = 0; idx < 4; idx++ )
914 i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
917 i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
921 p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
922 p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
923 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
924 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
926 for( i = 0; i < i_max; i++ )
928 i_mode = predict_mode[i];
929 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
931 h->predict_8x8[i_mode]( p_dst_by, edge );
932 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
934 if( i_best > i_satd )
936 a->i_predict8x8[idx] = i_mode;
939 pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
942 pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
944 i_nnz[j] = h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]];
948 *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
951 p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
953 h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]] = i_nnz[j];
955 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
959 /* RD selection for chroma prediction */
960 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
963 i_thresh = a->i_satd_i8x8chroma * 5/4;
965 for( i = j = 0; i < i_max; i++ )
966 if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
967 predict_mode[i] != a->i_predict8x8chroma )
969 predict_mode[j++] = predict_mode[i];
975 int i_chroma_lambda = x264_lambda2_tab[h->mb.i_chroma_qp];
976 /* the previous thing encoded was x264_intra_rd(), so the pixels and
977 * coefs for the current chroma mode are still around, so we only
978 * have to recount the bits. */
979 i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
980 for( i = 0; i < i_max; i++ )
982 i_mode = predict_mode[i];
983 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
984 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
985 /* if we've already found a mode that needs no residual, then
986 * probably any mode with a residual will be worse.
987 * so avoid dct on the remaining modes to improve speed. */
988 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
989 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode );
991 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
996 #define LOAD_FENC( m, src, xoff, yoff) \
997 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
998 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
999 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
1000 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
1001 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
1003 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
1004 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
1005 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
1006 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
1007 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
1008 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
1009 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
1010 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
1012 #define REF_COST(list, ref) \
1013 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
1015 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
1019 DECLARE_ALIGNED_4( int16_t mvc[8][2] );
1020 int i_halfpel_thresh = INT_MAX;
1021 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1023 /* 16x16 Search on all ref frame */
1024 m.i_pixel = PIXEL_16x16;
1025 m.p_cost_mv = a->p_cost_mv;
1026 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1028 a->l0.me16x16.cost = INT_MAX;
1029 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1031 const int i_ref_cost = REF_COST( 0, i_ref );
1032 i_halfpel_thresh -= i_ref_cost;
1033 m.i_ref_cost = i_ref_cost;
1036 /* search with ref */
1037 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1038 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1039 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1040 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1042 /* early termination
1043 * SSD threshold would probably be better than SATD */
1046 && m.cost-m.cost_mv < 300*a->i_lambda
1047 && abs(m.mv[0]-h->mb.cache.pskip_mv[0])
1048 + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
1049 && x264_macroblock_probe_pskip( h ) )
1051 h->mb.i_type = P_SKIP;
1052 x264_analyse_update_cache( h, a );
1053 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1057 m.cost += i_ref_cost;
1058 i_halfpel_thresh += i_ref_cost;
1060 if( m.cost < a->l0.me16x16.cost )
1061 h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1063 /* save mv for predicting neighbors */
1064 *(uint32_t*)a->l0.mvc[i_ref][0] =
1065 *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1068 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
1069 assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1071 h->mb.i_type = P_L0;
1074 x264_mb_cache_fenc_satd( h );
1075 if( a->l0.me16x16.i_ref == 0 && *(uint32_t*)a->l0.me16x16.mv == *(uint32_t*)h->mb.cache.pskip_mv )
1077 h->mb.i_partition = D_16x16;
1078 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
1079 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1084 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
1088 uint8_t **p_fenc = h->mb.pic.p_fenc;
1089 int i_halfpel_thresh = INT_MAX;
1090 int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
1092 int i_maxref = h->mb.pic.i_fref[0]-1;
1094 h->mb.i_partition = D_8x8;
1096 /* early termination: if 16x16 chose ref 0, then evalute no refs older
1097 * than those used by the neighbors */
1098 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
1099 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
1102 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
1103 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
1104 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
1105 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
1106 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
1107 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
1110 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1111 *(uint32_t*)a->l0.mvc[i_ref][0] = *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy];
1113 for( i = 0; i < 4; i++ )
1115 x264_me_t *l0m = &a->l0.me8x8[i];
1119 m.i_pixel = PIXEL_8x8;
1120 m.p_cost_mv = a->p_cost_mv;
1122 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
1123 l0m->cost = INT_MAX;
1124 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1126 const int i_ref_cost = REF_COST( 0, i_ref );
1127 i_halfpel_thresh -= i_ref_cost;
1128 m.i_ref_cost = i_ref_cost;
1131 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
1132 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
1133 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1134 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
1136 m.cost += i_ref_cost;
1137 i_halfpel_thresh += i_ref_cost;
1138 *(uint32_t*)a->l0.mvc[i_ref][i+1] = *(uint32_t*)m.mv;
1140 if( m.cost < l0m->cost )
1141 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1143 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv );
1144 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1147 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1150 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1151 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1152 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1153 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1156 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1158 const int i_ref = a->l0.me16x16.i_ref;
1159 const int i_ref_cost = REF_COST( 0, i_ref );
1160 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
1161 uint8_t **p_fenc = h->mb.pic.p_fenc;
1163 int16_t (*mvc)[2] = a->l0.mvc[i_ref];
1166 /* XXX Needed for x264_mb_predict_mv */
1167 h->mb.i_partition = D_8x8;
1170 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.me16x16.mv;
1172 for( i = 0; i < 4; i++ )
1174 x264_me_t *m = &a->l0.me8x8[i];
1178 m->i_pixel = PIXEL_8x8;
1179 m->p_cost_mv = a->p_cost_mv;
1180 m->i_ref_cost = i_ref_cost;
1183 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1184 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1185 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1186 x264_me_search( h, m, mvc, i_mvc );
1188 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, m->mv );
1190 *(uint32_t*)mvc[i_mvc] = *(uint32_t*)m->mv;
1194 m->cost += i_ref_cost;
1195 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1198 /* theoretically this should include 4*ref_cost,
1199 * but 3 seems a better approximation of cabac. */
1200 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1201 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
1202 REF_COST( 0, a->l0.me16x16.i_ref );
1203 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1204 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1207 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
1210 uint8_t **p_fenc = h->mb.pic.p_fenc;
1211 DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1214 /* XXX Needed for x264_mb_predict_mv */
1215 h->mb.i_partition = D_16x8;
1217 for( i = 0; i < 2; i++ )
1219 x264_me_t *l0m = &a->l0.me16x8[i];
1220 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1221 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1223 m.i_pixel = PIXEL_16x8;
1224 m.p_cost_mv = a->p_cost_mv;
1226 LOAD_FENC( &m, p_fenc, 0, 8*i );
1227 l0m->cost = INT_MAX;
1228 for( j = 0; j < i_ref8s; j++ )
1230 const int i_ref = ref8[j];
1231 const int i_ref_cost = REF_COST( 0, i_ref );
1232 m.i_ref_cost = i_ref_cost;
1235 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1236 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1237 *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][2*i+1];
1238 *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][2*i+2];
1240 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1241 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1242 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1243 x264_me_search( h, &m, mvc, 3 );
1245 m.cost += i_ref_cost;
1247 if( m.cost < l0m->cost )
1248 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1250 x264_macroblock_cache_mv_ptr( h, 0, 2*i, 4, 2, 0, l0m->mv );
1251 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1254 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1257 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1260 uint8_t **p_fenc = h->mb.pic.p_fenc;
1261 DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1264 /* XXX Needed for x264_mb_predict_mv */
1265 h->mb.i_partition = D_8x16;
1267 for( i = 0; i < 2; i++ )
1269 x264_me_t *l0m = &a->l0.me8x16[i];
1270 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1271 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1273 m.i_pixel = PIXEL_8x16;
1274 m.p_cost_mv = a->p_cost_mv;
1276 LOAD_FENC( &m, p_fenc, 8*i, 0 );
1277 l0m->cost = INT_MAX;
1278 for( j = 0; j < i_ref8s; j++ )
1280 const int i_ref = ref8[j];
1281 const int i_ref_cost = REF_COST( 0, i_ref );
1282 m.i_ref_cost = i_ref_cost;
1285 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1286 *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][i+1];
1287 *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][i+3];
1289 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1290 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1291 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1292 x264_me_search( h, &m, mvc, 3 );
1294 m.cost += i_ref_cost;
1296 if( m.cost < l0m->cost )
1297 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1299 x264_macroblock_cache_mv_ptr( h, 2*i, 0, 2, 4, 0, l0m->mv );
1300 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1303 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1306 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1308 DECLARE_ALIGNED_8( uint8_t pix1[16*8] );
1309 uint8_t *pix2 = pix1+8;
1310 const int i_stride = h->mb.pic.i_stride[1];
1311 const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1312 const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1314 #define CHROMA4x4MC( width, height, me, x, y ) \
1315 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 ); \
1316 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 );
1318 if( pixel == PIXEL_4x4 )
1320 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1321 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 2,0 );
1322 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 0,2 );
1323 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1325 else if( pixel == PIXEL_8x4 )
1327 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1328 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1332 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1333 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1336 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1337 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1340 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1342 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1343 uint8_t **p_fenc = h->mb.pic.p_fenc;
1344 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1347 /* XXX Needed for x264_mb_predict_mv */
1348 h->mb.i_partition = D_8x8;
1350 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1352 const int idx = 4*i8x8 + i4x4;
1353 const int x4 = block_idx_x[idx];
1354 const int y4 = block_idx_y[idx];
1355 const int i_mvc = (i4x4 == 0);
1357 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1359 m->i_pixel = PIXEL_4x4;
1360 m->p_cost_mv = a->p_cost_mv;
1362 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1363 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1365 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1366 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1368 x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 1, 0, m->mv );
1370 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1371 a->l0.me4x4[i8x8][1].cost +
1372 a->l0.me4x4[i8x8][2].cost +
1373 a->l0.me4x4[i8x8][3].cost +
1374 REF_COST( 0, i_ref ) +
1375 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1376 if( h->mb.b_chroma_me )
1377 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1380 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1382 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1383 uint8_t **p_fenc = h->mb.pic.p_fenc;
1384 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1387 /* XXX Needed for x264_mb_predict_mv */
1388 h->mb.i_partition = D_8x8;
1390 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1392 const int idx = 4*i8x8 + 2*i8x4;
1393 const int x4 = block_idx_x[idx];
1394 const int y4 = block_idx_y[idx];
1395 const int i_mvc = (i8x4 == 0);
1397 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1399 m->i_pixel = PIXEL_8x4;
1400 m->p_cost_mv = a->p_cost_mv;
1402 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1403 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1405 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1406 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1408 x264_macroblock_cache_mv_ptr( h, x4, y4, 2, 1, 0, m->mv );
1410 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1411 REF_COST( 0, i_ref ) +
1412 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1413 if( h->mb.b_chroma_me )
1414 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1417 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1419 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1420 uint8_t **p_fenc = h->mb.pic.p_fenc;
1421 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1424 /* XXX Needed for x264_mb_predict_mv */
1425 h->mb.i_partition = D_8x8;
1427 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1429 const int idx = 4*i8x8 + i4x8;
1430 const int x4 = block_idx_x[idx];
1431 const int y4 = block_idx_y[idx];
1432 const int i_mvc = (i4x8 == 0);
1434 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1436 m->i_pixel = PIXEL_4x8;
1437 m->p_cost_mv = a->p_cost_mv;
1439 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1440 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1442 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1443 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1445 x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 2, 0, m->mv );
1447 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1448 REF_COST( 0, i_ref ) +
1449 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1450 if( h->mb.b_chroma_me )
1451 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1454 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1456 /* Assumes that fdec still contains the results of
1457 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1459 uint8_t **p_fenc = h->mb.pic.p_fenc;
1460 uint8_t **p_fdec = h->mb.pic.p_fdec;
1463 a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1464 for( i = 0; i < 4; i++ )
1466 const int x = (i&1)*8;
1467 const int y = (i>>1)*8;
1468 a->i_cost16x16direct +=
1469 a->i_cost8x8direct[i] =
1470 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1473 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1477 #define WEIGHTED_AVG( size, pix, stride, src1, stride1, src2, stride2 ) \
1479 h->mc.avg[size]( pix, stride, src1, stride1, src2, stride2, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1482 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1484 DECLARE_ALIGNED_16( uint8_t pix0[16*16] );
1485 DECLARE_ALIGNED_16( uint8_t pix1[16*16] );
1486 uint8_t *src0, *src1;
1487 int stride0 = 16, stride1 = 16;
1491 DECLARE_ALIGNED_4( int16_t mvc[9][2] );
1492 int i_halfpel_thresh = INT_MAX;
1493 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1495 /* 16x16 Search on all ref frame */
1496 m.i_pixel = PIXEL_16x16;
1497 m.p_cost_mv = a->p_cost_mv;
1498 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1501 a->l0.me16x16.cost = INT_MAX;
1502 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1504 /* search with ref */
1505 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1506 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1507 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1508 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1511 m.cost += REF_COST( 0, i_ref );
1513 if( m.cost < a->l0.me16x16.cost )
1515 a->l0.i_ref = i_ref;
1516 h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1519 /* save mv for predicting neighbors */
1520 *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1522 /* subtract ref cost, so we don't have to add it for the other MB types */
1523 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1526 i_halfpel_thresh = INT_MAX;
1527 p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1528 a->l1.me16x16.cost = INT_MAX;
1529 for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1531 /* search with ref */
1532 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1533 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1534 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1535 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1538 m.cost += REF_COST( 1, i_ref );
1540 if( m.cost < a->l1.me16x16.cost )
1542 a->l1.i_ref = i_ref;
1543 h->mc.memcpy_aligned( &a->l1.me16x16, &m, sizeof(x264_me_t) );
1546 /* save mv for predicting neighbors */
1547 *(uint32_t*)h->mb.mvr[1][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1549 /* subtract ref cost, so we don't have to add it for the other MB types */
1550 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1552 /* Set global ref, needed for other modes? */
1553 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1554 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1556 /* get cost of BI mode */
1557 src0 = h->mc.get_ref( pix0, &stride0,
1558 h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1559 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1], 16, 16 );
1560 src1 = h->mc.get_ref( pix1, &stride1,
1561 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1562 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1], 16, 16 );
1564 h->mc.avg[PIXEL_16x16]( pix0, 16, src0, stride0, src1, stride1, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
1566 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix0, 16 )
1567 + REF_COST( 0, a->l0.i_ref )
1568 + REF_COST( 1, a->l1.i_ref )
1569 + a->l0.me16x16.cost_mv
1570 + a->l1.me16x16.cost_mv;
1573 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1574 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1575 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1578 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1580 const int x = 2*(i%2);
1581 const int y = 2*(i/2);
1583 switch( h->mb.i_sub_partition[i] )
1586 x264_macroblock_cache_mv_ptr( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv );
1589 x264_macroblock_cache_mv_ptr( h, x, y+0, 2, 1, 0, a->l0.me8x4[i][0].mv );
1590 x264_macroblock_cache_mv_ptr( h, x, y+1, 2, 1, 0, a->l0.me8x4[i][1].mv );
1593 x264_macroblock_cache_mv_ptr( h, x+0, y, 1, 2, 0, a->l0.me4x8[i][0].mv );
1594 x264_macroblock_cache_mv_ptr( h, x+1, y, 1, 2, 0, a->l0.me4x8[i][1].mv );
1597 x264_macroblock_cache_mv_ptr( h, x+0, y+0, 1, 1, 0, a->l0.me4x4[i][0].mv );
1598 x264_macroblock_cache_mv_ptr( h, x+1, y+0, 1, 1, 0, a->l0.me4x4[i][1].mv );
1599 x264_macroblock_cache_mv_ptr( h, x+0, y+1, 1, 1, 0, a->l0.me4x4[i][2].mv );
1600 x264_macroblock_cache_mv_ptr( h, x+1, y+1, 1, 1, 0, a->l0.me4x4[i][3].mv );
1603 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1608 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1609 if( x264_mb_partition_listX_table[0][part] ) \
1611 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1612 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 0, me0.mv ); \
1616 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1617 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0 ); \
1619 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0 ); \
1621 if( x264_mb_partition_listX_table[1][part] ) \
1623 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1624 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 1, me1.mv ); \
1628 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1629 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0 ); \
1631 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0 ); \
1634 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1638 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1640 x264_mb_load_mv_direct8x8( h, i );
1643 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0 );
1644 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0 );
1645 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1650 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1653 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1655 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1657 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1659 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1663 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1665 uint8_t **p_fref[2] =
1666 { h->mb.pic.p_fref[0][a->l0.i_ref],
1667 h->mb.pic.p_fref[1][a->l1.i_ref] };
1668 DECLARE_ALIGNED_8( uint8_t pix[2][8*8] );
1671 /* XXX Needed for x264_mb_predict_mv */
1672 h->mb.i_partition = D_8x8;
1676 for( i = 0; i < 4; i++ )
1681 int i_part_cost_bi = 0;
1682 int stride[2] = {8,8};
1685 for( l = 0; l < 2; l++ )
1687 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1688 x264_me_t *m = &lX->me8x8[i];
1690 m->i_pixel = PIXEL_8x8;
1691 m->p_cost_mv = a->p_cost_mv;
1693 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1694 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1696 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1697 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1699 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, l, m->mv );
1702 src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1703 m->mv[0], m->mv[1], 8, 8 );
1704 i_part_cost_bi += m->cost_mv;
1705 /* FIXME: ref cost */
1707 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] );
1708 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1709 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1710 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1711 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1713 i_part_cost = a->l0.me8x8[i].cost;
1714 h->mb.i_sub_partition[i] = D_L0_8x8;
1715 COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1716 COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1717 COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1718 a->i_cost8x8bi += i_part_cost;
1720 /* XXX Needed for x264_mb_predict_mv */
1721 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1725 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1728 static void x264_mb_analyse_inter_b16x8( 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_16( uint8_t pix[2][16*8] );
1734 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1737 h->mb.i_partition = D_16x8;
1738 a->i_cost16x8bi = 0;
1740 for( i = 0; i < 2; i++ )
1743 int i_part_cost_bi = 0;
1744 int stride[2] = {16,16};
1747 /* TODO: check only the list(s) that were used in b8x8? */
1748 for( l = 0; l < 2; l++ )
1750 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1751 x264_me_t *m = &lX->me16x8[i];
1753 m->i_pixel = PIXEL_16x8;
1754 m->p_cost_mv = a->p_cost_mv;
1756 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1757 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1759 *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[2*i].mv;
1760 *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[2*i+1].mv;
1762 x264_mb_predict_mv( h, l, 8*i, 2, m->mvp );
1763 x264_me_search( h, m, mvc, 2 );
1766 src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1767 m->mv[0], m->mv[1], 16, 8 );
1768 /* FIXME: ref cost */
1769 i_part_cost_bi += m->cost_mv;
1771 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] );
1772 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1774 i_part_cost = a->l0.me16x8[i].cost;
1775 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1776 if( a->l1.me16x8[i].cost < i_part_cost )
1778 i_part_cost = a->l1.me16x8[i].cost;
1779 a->i_mb_partition16x8[i] = D_L1_8x8;
1781 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1783 i_part_cost = i_part_cost_bi;
1784 a->i_mb_partition16x8[i] = D_BI_8x8;
1786 a->i_cost16x8bi += i_part_cost;
1788 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1792 a->i_mb_type16x8 = B_L0_L0
1793 + (a->i_mb_partition16x8[0]>>2) * 3
1794 + (a->i_mb_partition16x8[1]>>2);
1795 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1798 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1800 uint8_t **p_fref[2] =
1801 { h->mb.pic.p_fref[0][a->l0.i_ref],
1802 h->mb.pic.p_fref[1][a->l1.i_ref] };
1803 DECLARE_ALIGNED_8( uint8_t pix[2][8*16] );
1804 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1807 h->mb.i_partition = D_8x16;
1808 a->i_cost8x16bi = 0;
1810 for( i = 0; i < 2; i++ )
1813 int i_part_cost_bi = 0;
1814 int stride[2] = {8,8};
1817 for( l = 0; l < 2; l++ )
1819 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1820 x264_me_t *m = &lX->me8x16[i];
1822 m->i_pixel = PIXEL_8x16;
1823 m->p_cost_mv = a->p_cost_mv;
1825 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1826 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1828 *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[i].mv;
1829 *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[i+2].mv;
1831 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1832 x264_me_search( h, m, mvc, 2 );
1835 src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1836 m->mv[0], m->mv[1], 8, 16 );
1837 /* FIXME: ref cost */
1838 i_part_cost_bi += m->cost_mv;
1841 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] );
1842 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1844 i_part_cost = a->l0.me8x16[i].cost;
1845 a->i_mb_partition8x16[i] = D_L0_8x8;
1846 if( a->l1.me8x16[i].cost < i_part_cost )
1848 i_part_cost = a->l1.me8x16[i].cost;
1849 a->i_mb_partition8x16[i] = D_L1_8x8;
1851 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1853 i_part_cost = i_part_cost_bi;
1854 a->i_mb_partition8x16[i] = D_BI_8x8;
1856 a->i_cost8x16bi += i_part_cost;
1858 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1862 a->i_mb_type8x16 = B_L0_L0
1863 + (a->i_mb_partition8x16[0]>>2) * 3
1864 + (a->i_mb_partition8x16[1]>>2);
1865 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1868 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1870 int thresh = i_satd * 5/4;
1872 h->mb.i_type = P_L0;
1873 if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1875 h->mb.i_partition = D_16x16;
1876 x264_analyse_update_cache( h, a );
1877 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1879 a->l0.me16x16.cost = a->l0.i_rd16x16;
1881 if( a->l0.i_cost16x8 <= thresh )
1883 h->mb.i_partition = D_16x8;
1884 x264_analyse_update_cache( h, a );
1885 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1888 a->l0.i_cost16x8 = COST_MAX;
1890 if( a->l0.i_cost8x16 <= thresh )
1892 h->mb.i_partition = D_8x16;
1893 x264_analyse_update_cache( h, a );
1894 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1897 a->l0.i_cost8x16 = COST_MAX;
1899 if( a->l0.i_cost8x8 <= thresh )
1901 h->mb.i_type = P_8x8;
1902 h->mb.i_partition = D_8x8;
1903 x264_analyse_update_cache( h, a );
1904 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1906 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1908 /* FIXME: RD per subpartition */
1912 for( i=0; i<4; i++ )
1914 part_bak[i] = h->mb.i_sub_partition[i];
1915 b_sub8x8 |= (part_bak[i] != D_L0_8x8);
1919 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1920 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1921 x264_analyse_update_cache( h, a );
1922 i_cost = x264_rd_cost_mb( h, a->i_lambda2 );
1923 if( a->l0.i_cost8x8 < i_cost )
1925 for( i=0; i<4; i++ )
1926 h->mb.i_sub_partition[i] = part_bak[i];
1929 a->l0.i_cost8x8 = i_cost;
1934 a->l0.i_cost8x8 = COST_MAX;
1937 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
1939 int thresh = i_satd_inter * (17 + (!!h->mb.i_psy_rd))/16;
1941 if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
1943 h->mb.i_type = B_DIRECT;
1944 /* Assumes direct/skip MC is still in fdec */
1945 /* Requires b-rdo to be done before intra analysis */
1946 h->mb.b_skip_mc = 1;
1947 x264_analyse_update_cache( h, a );
1948 a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
1949 h->mb.b_skip_mc = 0;
1952 //FIXME not all the update_cache calls are needed
1953 h->mb.i_partition = D_16x16;
1955 if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
1957 h->mb.i_type = B_L0_L0;
1958 x264_analyse_update_cache( h, a );
1959 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1963 if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
1965 h->mb.i_type = B_L1_L1;
1966 x264_analyse_update_cache( h, a );
1967 a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1971 if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
1973 h->mb.i_type = B_BI_BI;
1974 x264_analyse_update_cache( h, a );
1975 a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1979 if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
1981 h->mb.i_type = B_8x8;
1982 h->mb.i_partition = D_8x8;
1983 x264_analyse_update_cache( h, a );
1984 a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1985 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1989 if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
1991 h->mb.i_type = a->i_mb_type16x8;
1992 h->mb.i_partition = D_16x8;
1993 x264_analyse_update_cache( h, a );
1994 a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1998 if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
2000 h->mb.i_type = a->i_mb_type8x16;
2001 h->mb.i_partition = D_8x16;
2002 x264_analyse_update_cache( h, a );
2003 a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
2007 static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
2009 const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
2012 switch( h->mb.i_partition )
2015 if( h->mb.i_type == B_BI_BI )
2016 x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
2019 for( i=0; i<2; i++ )
2020 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
2021 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
2024 for( i=0; i<2; i++ )
2025 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
2026 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
2029 for( i=0; i<4; i++ )
2030 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
2031 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
2036 static inline void x264_mb_analyse_transform( x264_t *h )
2038 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2040 int i_cost4, i_cost8;
2041 /* Only luma MC is really needed, but the full MC is re-used in macroblock_encode. */
2044 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2045 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2046 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2047 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2049 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2050 h->mb.b_skip_mc = 1;
2054 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2056 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2059 x264_analyse_update_cache( h, a );
2060 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2061 /* FIXME only luma is needed, but the score for comparison already includes chroma */
2062 i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2064 if( *i_rd >= i_rd8 )
2067 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2068 /* prevent a rare division by zero in estimated intra cost */
2075 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2080 /*****************************************************************************
2081 * x264_macroblock_analyse:
2082 *****************************************************************************/
2083 void x264_macroblock_analyse( x264_t *h )
2085 x264_mb_analysis_t analysis;
2086 int i_cost = COST_MAX;
2089 h->mb.i_qp = x264_ratecontrol_qp( h );
2090 if( h->param.rc.i_aq_mode )
2091 x264_adaptive_quant( h );
2093 x264_mb_analyse_init( h, &analysis, h->mb.i_qp );
2095 /*--------------------------- Do the analysis ---------------------------*/
2096 if( h->sh.i_type == SLICE_TYPE_I )
2098 if( analysis.b_mbrd )
2099 x264_mb_cache_fenc_satd( h );
2100 x264_mb_analyse_intra( h, &analysis, COST_MAX );
2101 if( analysis.b_mbrd )
2102 x264_intra_rd( h, &analysis, COST_MAX );
2104 i_cost = analysis.i_satd_i16x16;
2105 h->mb.i_type = I_16x16;
2106 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, h->mb.i_type, I_4x4 );
2107 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, h->mb.i_type, I_8x8 );
2108 if( analysis.i_satd_pcm < i_cost )
2109 h->mb.i_type = I_PCM;
2111 else if( h->mb.i_subpel_refine >= 7 )
2112 x264_intra_rd_refine( h, &analysis );
2114 else if( h->sh.i_type == SLICE_TYPE_P )
2117 int i_intra_cost, i_intra_type;
2119 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2121 /* Fast P_SKIP detection */
2122 analysis.b_try_pskip = 0;
2123 if( h->param.analyse.b_fast_pskip )
2125 if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2126 // FIXME don't need to check this if the reference frame is done
2128 else if( h->param.analyse.i_subpel_refine >= 3 )
2129 analysis.b_try_pskip = 1;
2130 else if( h->mb.i_mb_type_left == P_SKIP ||
2131 h->mb.i_mb_type_top == P_SKIP ||
2132 h->mb.i_mb_type_topleft == P_SKIP ||
2133 h->mb.i_mb_type_topright == P_SKIP )
2134 b_skip = x264_macroblock_probe_pskip( h );
2137 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2141 h->mb.i_type = P_SKIP;
2142 h->mb.i_partition = D_16x16;
2143 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2147 const unsigned int flags = h->param.analyse.inter;
2151 int i_satd_inter, i_satd_intra;
2153 x264_mb_analyse_load_costs( h, &analysis );
2155 x264_mb_analyse_inter_p16x16( h, &analysis );
2157 if( h->mb.i_type == P_SKIP )
2160 if( flags & X264_ANALYSE_PSUB16x16 )
2162 if( h->param.analyse.b_mixed_references )
2163 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2165 x264_mb_analyse_inter_p8x8( h, &analysis );
2168 /* Select best inter mode */
2170 i_partition = D_16x16;
2171 i_cost = analysis.l0.me16x16.cost;
2173 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2174 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2177 i_partition = D_8x8;
2178 i_cost = analysis.l0.i_cost8x8;
2181 if( flags & X264_ANALYSE_PSUB8x8 )
2183 for( i = 0; i < 4; i++ )
2185 x264_mb_analyse_inter_p4x4( h, &analysis, i );
2186 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2188 int i_cost8x8 = analysis.l0.i_cost4x4[i];
2189 h->mb.i_sub_partition[i] = D_L0_4x4;
2191 x264_mb_analyse_inter_p8x4( h, &analysis, i );
2192 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2193 h->mb.i_sub_partition[i], D_L0_8x4 );
2195 x264_mb_analyse_inter_p4x8( h, &analysis, i );
2196 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2197 h->mb.i_sub_partition[i], D_L0_4x8 );
2199 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2201 x264_mb_cache_mv_p8x8( h, &analysis, i );
2203 analysis.l0.i_cost8x8 = i_cost;
2207 /* Now do 16x8/8x16 */
2208 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2209 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2210 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2212 x264_mb_analyse_inter_p16x8( h, &analysis );
2213 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2215 x264_mb_analyse_inter_p8x16( h, &analysis );
2216 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2219 h->mb.i_partition = i_partition;
2222 //FIXME mb_type costs?
2223 if( analysis.b_mbrd )
2227 else if( i_partition == D_16x16 )
2229 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2230 i_cost = analysis.l0.me16x16.cost;
2232 else if( i_partition == D_16x8 )
2234 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2235 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2236 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2238 else if( i_partition == D_8x16 )
2240 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2241 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2242 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2244 else if( i_partition == D_8x8 )
2248 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2250 switch( h->mb.i_sub_partition[i8x8] )
2253 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2254 i_cost += analysis.l0.me8x8[i8x8].cost;
2257 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2258 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2259 i_cost += analysis.l0.me8x4[i8x8][0].cost +
2260 analysis.l0.me8x4[i8x8][1].cost;
2263 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2264 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2265 i_cost += analysis.l0.me4x8[i8x8][0].cost +
2266 analysis.l0.me4x8[i8x8][1].cost;
2270 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2271 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2272 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2273 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2274 i_cost += analysis.l0.me4x4[i8x8][0].cost +
2275 analysis.l0.me4x4[i8x8][1].cost +
2276 analysis.l0.me4x4[i8x8][2].cost +
2277 analysis.l0.me4x4[i8x8][3].cost;
2280 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2286 if( h->mb.b_chroma_me )
2288 x264_mb_analyse_intra_chroma( h, &analysis );
2289 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2290 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2291 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2292 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2295 x264_mb_analyse_intra( h, &analysis, i_cost );
2297 i_satd_inter = i_cost;
2298 i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2299 analysis.i_satd_i8x8,
2300 analysis.i_satd_i4x4 );
2302 if( analysis.b_mbrd )
2304 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2306 i_partition = D_16x16;
2307 i_cost = analysis.l0.me16x16.cost;
2308 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2309 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2310 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2311 h->mb.i_type = i_type;
2312 h->mb.i_partition = i_partition;
2313 if( i_cost < COST_MAX )
2314 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2315 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2318 i_intra_type = I_16x16;
2319 i_intra_cost = analysis.i_satd_i16x16;
2320 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
2321 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
2322 COPY2_IF_LT( i_intra_cost, analysis.i_satd_pcm, i_intra_type, I_PCM );
2323 COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
2325 if( i_intra_cost == COST_MAX )
2326 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
2328 h->mb.i_type = i_type;
2329 h->stat.frame.i_intra_cost += i_intra_cost;
2330 h->stat.frame.i_inter_cost += i_cost;
2331 h->stat.frame.i_mbs_analysed++;
2333 if( h->mb.i_subpel_refine >= 7 && h->mb.i_type != I_PCM )
2335 if( IS_INTRA( h->mb.i_type ) )
2337 x264_intra_rd_refine( h, &analysis );
2339 else if( i_partition == D_16x16 )
2341 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2342 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
2344 else if( i_partition == D_16x8 )
2346 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2347 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2348 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2349 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2350 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
2351 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
2353 else if( i_partition == D_8x16 )
2355 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2356 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2357 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2358 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2359 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
2360 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
2362 else if( i_partition == D_8x8 )
2365 x264_analyse_update_cache( h, &analysis );
2366 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2367 if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2368 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
2373 else if( h->sh.i_type == SLICE_TYPE_B )
2375 int i_bskip_cost = COST_MAX;
2378 if( analysis.b_mbrd )
2379 x264_mb_cache_fenc_satd( h );
2381 h->mb.i_type = B_SKIP;
2382 if( h->mb.b_direct_auto_write )
2384 /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2385 for( i = 0; i < 2; i++ )
2388 h->sh.b_direct_spatial_mv_pred ^= 1;
2389 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2390 if( analysis.b_direct_available )
2395 b_skip = x264_macroblock_probe_bskip( h );
2397 h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2404 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2406 if( analysis.b_direct_available )
2408 if( !h->mb.b_direct_auto_write )
2410 if( h->mb.b_lossless )
2412 /* chance of skip is too small to bother */
2414 else if( analysis.b_mbrd )
2416 i_bskip_cost = ssd_mb( h );
2417 /* 6 = minimum cavlc cost of a non-skipped MB */
2418 b_skip = h->mb.b_skip_mc = i_bskip_cost <= ((6 * analysis.i_lambda2 + 128) >> 8);
2420 else if( !h->mb.b_direct_auto_write )
2422 /* Conditioning the probe on neighboring block types
2423 * doesn't seem to help speed or quality. */
2424 b_skip = x264_macroblock_probe_bskip( h );
2430 const unsigned int flags = h->param.analyse.inter;
2433 int i_satd_inter = 0; // shut up uninitialized warning
2434 h->mb.b_skip_mc = 0;
2436 x264_mb_analyse_load_costs( h, &analysis );
2438 /* select best inter mode */
2439 /* direct must be first */
2440 if( analysis.b_direct_available )
2441 x264_mb_analyse_inter_direct( h, &analysis );
2443 x264_mb_analyse_inter_b16x16( h, &analysis );
2446 i_partition = D_16x16;
2447 i_cost = analysis.l0.me16x16.cost;
2448 COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2449 COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2450 COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2452 if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2454 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2455 if( i_bskip_cost < analysis.i_rd16x16direct &&
2456 i_bskip_cost < analysis.i_rd16x16bi &&
2457 i_bskip_cost < analysis.l0.i_rd16x16 &&
2458 i_bskip_cost < analysis.l1.i_rd16x16 )
2460 h->mb.i_type = B_SKIP;
2461 x264_analyse_update_cache( h, &analysis );
2466 if( flags & X264_ANALYSE_BSUB16x16 )
2468 x264_mb_analyse_inter_b8x8( h, &analysis );
2469 if( analysis.i_cost8x8bi < i_cost )
2472 i_partition = D_8x8;
2473 i_cost = analysis.i_cost8x8bi;
2475 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2476 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2478 x264_mb_analyse_inter_b16x8( h, &analysis );
2479 COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2480 i_type, analysis.i_mb_type16x8,
2481 i_partition, D_16x8 );
2483 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2484 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2486 x264_mb_analyse_inter_b8x16( h, &analysis );
2487 COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2488 i_type, analysis.i_mb_type8x16,
2489 i_partition, D_8x16 );
2494 if( analysis.b_mbrd )
2499 else if( i_partition == D_16x16 )
2501 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2502 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2503 if( i_type == B_L0_L0 )
2505 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2506 i_cost = analysis.l0.me16x16.cost
2507 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2509 else if( i_type == B_L1_L1 )
2511 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2512 i_cost = analysis.l1.me16x16.cost
2513 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2515 else if( i_type == B_BI_BI )
2517 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2518 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2521 else if( i_partition == D_16x8 )
2523 for( i=0; i<2; i++ )
2525 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2526 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2527 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2528 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2531 else if( i_partition == D_8x16 )
2533 for( i=0; i<2; i++ )
2535 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2536 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2537 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2538 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2541 else if( i_partition == D_8x8 )
2543 for( i=0; i<4; i++ )
2546 int i_part_cost_old;
2548 int i_part_type = h->mb.i_sub_partition[i];
2549 int b_bidir = (i_part_type == D_BI_8x8);
2551 if( i_part_type == D_DIRECT_8x8 )
2553 if( x264_mb_partition_listX_table[0][i_part_type] )
2555 m = &analysis.l0.me8x8[i];
2556 i_part_cost_old = m->cost;
2557 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2558 m->cost -= i_type_cost;
2559 x264_me_refine_qpel( h, m );
2561 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2563 if( x264_mb_partition_listX_table[1][i_part_type] )
2565 m = &analysis.l1.me8x8[i];
2566 i_part_cost_old = m->cost;
2567 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2568 m->cost -= i_type_cost;
2569 x264_me_refine_qpel( h, m );
2571 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2573 /* TODO: update mvp? */
2577 if( analysis.b_mbrd )
2579 i_satd_inter = i_cost;
2580 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2582 i_cost = i_bskip_cost;
2583 i_partition = D_16x16;
2584 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2585 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2586 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2587 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2588 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2589 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2590 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2592 h->mb.i_type = i_type;
2593 h->mb.i_partition = i_partition;
2596 x264_mb_analyse_intra( h, &analysis, i_satd_inter );
2598 if( analysis.b_mbrd )
2600 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2601 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2604 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2605 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2606 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2607 COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
2609 h->mb.i_type = i_type;
2610 h->mb.i_partition = i_partition;
2612 if( analysis.b_mbrd && h->mb.i_subpel_refine >= 7 && IS_INTRA( i_type ) && i_type != I_PCM )
2613 x264_intra_rd_refine( h, &analysis );
2614 else if( h->param.analyse.b_bidir_me )
2615 refine_bidir( h, &analysis );
2619 x264_analyse_update_cache( h, &analysis );
2621 if( !analysis.b_mbrd )
2622 x264_mb_analyse_transform( h );
2624 h->mb.b_trellis = h->param.analyse.i_trellis;
2625 h->mb.b_noise_reduction = !!h->param.analyse.i_noise_reduction;
2626 if( !IS_SKIP(h->mb.i_type) && h->mb.i_psy_trellis && h->param.analyse.i_trellis == 1 )
2627 x264_psy_trellis_init( h, 0 );
2628 if( h->mb.b_trellis == 1 || h->mb.b_noise_reduction )
2629 h->mb.i_skip_intra = 0;
2632 /*-------------------- Update MB from the analysis ----------------------*/
2633 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2637 switch( h->mb.i_type )
2640 for( i = 0; i < 16; i++ )
2641 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2643 x264_mb_analyse_intra_chroma( h, a );
2646 for( i = 0; i < 4; i++ )
2647 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2649 x264_mb_analyse_intra_chroma( h, a );
2652 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2653 x264_mb_analyse_intra_chroma( h, a );
2660 switch( h->mb.i_partition )
2663 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2664 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2668 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2669 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2670 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv );
2671 x264_macroblock_cache_mv_ptr( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv );
2675 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2676 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2677 x264_macroblock_cache_mv_ptr( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv );
2678 x264_macroblock_cache_mv_ptr( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv );
2682 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2688 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2689 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2690 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2691 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2692 for( i = 0; i < 4; i++ )
2693 x264_mb_cache_mv_p8x8( h, a, i );
2698 h->mb.i_partition = D_16x16;
2699 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2700 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv );
2706 x264_mb_load_mv_direct8x8( h, 0 );
2707 x264_mb_load_mv_direct8x8( h, 1 );
2708 x264_mb_load_mv_direct8x8( h, 2 );
2709 x264_mb_load_mv_direct8x8( h, 3 );
2713 /* optimize: cache might not need to be rewritten */
2714 for( i = 0; i < 4; i++ )
2715 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2718 default: /* the rest of the B types */
2719 switch( h->mb.i_partition )
2722 switch( h->mb.i_type )
2725 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2726 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2728 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2729 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0 );
2730 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0 );
2733 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2734 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0 );
2735 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0 );
2737 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2738 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2741 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2742 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2744 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2745 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2750 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2751 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2754 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2755 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2758 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2764 if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2767 for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2770 int ref = h->mb.cache.ref[l][x264_scan8[0]];
2773 completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2774 if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2776 x264_log( h, X264_LOG_WARNING, "internal error (MV out of thread range)\n");
2777 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2778 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2779 h->mb.cache.mv[l][x264_scan8[15]][0],
2780 h->mb.cache.mv[l][x264_scan8[15]][1] );
2781 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2782 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2783 fprintf(stderr, "completed: %d \n", completed );
2784 x264_log( h, X264_LOG_WARNING, "recovering by using intra mode\n");
2785 x264_mb_analyse_intra( h, a, COST_MAX );
2786 h->mb.i_type = I_16x16;
2787 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2788 x264_mb_analyse_intra_chroma( h, a );
2795 #include "slicetype.c"