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[7][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, pix1, stride1, src2, stride2 ) \
1479 if( h->param.analyse.b_weighted_bipred ) \
1480 h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1481 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1483 h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1486 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1488 DECLARE_ALIGNED_16( uint8_t pix1[16*16] );
1489 DECLARE_ALIGNED_16( uint8_t pix2[16*16] );
1496 DECLARE_ALIGNED_4( int16_t mvc[8][2] );
1497 int i_halfpel_thresh = INT_MAX;
1498 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1500 /* 16x16 Search on all ref frame */
1501 m.i_pixel = PIXEL_16x16;
1502 m.p_cost_mv = a->p_cost_mv;
1503 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1506 a->l0.me16x16.cost = INT_MAX;
1507 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1509 /* search with ref */
1510 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1511 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1512 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1513 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1516 m.cost += REF_COST( 0, i_ref );
1518 if( m.cost < a->l0.me16x16.cost )
1520 a->l0.i_ref = i_ref;
1521 h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1524 /* save mv for predicting neighbors */
1525 *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1527 /* subtract ref cost, so we don't have to add it for the other MB types */
1528 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1531 i_halfpel_thresh = INT_MAX;
1532 p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1533 a->l1.me16x16.cost = INT_MAX;
1534 for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1536 /* search with ref */
1537 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1538 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1539 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1540 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1543 m.cost += REF_COST( 1, i_ref );
1545 if( m.cost < a->l1.me16x16.cost )
1547 a->l1.i_ref = i_ref;
1548 h->mc.memcpy_aligned( &a->l1.me16x16, &m, sizeof(x264_me_t) );
1551 /* save mv for predicting neighbors */
1552 *(uint32_t*)h->mb.mvr[1][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1554 /* subtract ref cost, so we don't have to add it for the other MB types */
1555 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1557 /* Set global ref, needed for other modes? */
1558 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1559 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1561 /* get cost of BI mode */
1562 weight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1563 if ( (*(uint32_t*)a->l0.me16x16.mv & 0x10001) == 0 )
1565 /* l0 reference is halfpel, so get_ref on it will make it faster */
1567 h->mc.get_ref( pix2, &stride2,
1568 h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1569 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1571 h->mc.mc_luma( pix1, 16,
1572 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1573 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1575 weight = 64 - weight;
1579 /* if l0 was qpel, we'll use get_ref on l1 instead */
1580 h->mc.mc_luma( pix1, 16,
1581 h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1582 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1585 h->mc.get_ref( pix2, &stride2,
1586 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1587 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1591 if( h->param.analyse.b_weighted_bipred )
1592 h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2, weight );
1594 h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1596 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix1, 16 )
1597 + REF_COST( 0, a->l0.i_ref )
1598 + REF_COST( 1, a->l1.i_ref )
1599 + a->l0.me16x16.cost_mv
1600 + a->l1.me16x16.cost_mv;
1603 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1604 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1605 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1608 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1610 const int x = 2*(i%2);
1611 const int y = 2*(i/2);
1613 switch( h->mb.i_sub_partition[i] )
1616 x264_macroblock_cache_mv_ptr( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv );
1619 x264_macroblock_cache_mv_ptr( h, x, y+0, 2, 1, 0, a->l0.me8x4[i][0].mv );
1620 x264_macroblock_cache_mv_ptr( h, x, y+1, 2, 1, 0, a->l0.me8x4[i][1].mv );
1623 x264_macroblock_cache_mv_ptr( h, x+0, y, 1, 2, 0, a->l0.me4x8[i][0].mv );
1624 x264_macroblock_cache_mv_ptr( h, x+1, y, 1, 2, 0, a->l0.me4x8[i][1].mv );
1627 x264_macroblock_cache_mv_ptr( h, x+0, y+0, 1, 1, 0, a->l0.me4x4[i][0].mv );
1628 x264_macroblock_cache_mv_ptr( h, x+1, y+0, 1, 1, 0, a->l0.me4x4[i][1].mv );
1629 x264_macroblock_cache_mv_ptr( h, x+0, y+1, 1, 1, 0, a->l0.me4x4[i][2].mv );
1630 x264_macroblock_cache_mv_ptr( h, x+1, y+1, 1, 1, 0, a->l0.me4x4[i][3].mv );
1633 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1638 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1639 if( x264_mb_partition_listX_table[0][part] ) \
1641 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1642 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 0, me0.mv ); \
1646 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1647 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0 ); \
1649 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0 ); \
1651 if( x264_mb_partition_listX_table[1][part] ) \
1653 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1654 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 1, me1.mv ); \
1658 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1659 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0 ); \
1661 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0 ); \
1664 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1668 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1670 x264_mb_load_mv_direct8x8( h, i );
1673 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0 );
1674 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0 );
1675 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1680 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1683 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1685 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1687 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1689 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1693 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1695 uint8_t **p_fref[2] =
1696 { h->mb.pic.p_fref[0][a->l0.i_ref],
1697 h->mb.pic.p_fref[1][a->l1.i_ref] };
1698 DECLARE_ALIGNED_8( uint8_t pix[2][8*8] );
1701 /* XXX Needed for x264_mb_predict_mv */
1702 h->mb.i_partition = D_8x8;
1706 for( i = 0; i < 4; i++ )
1711 int i_part_cost_bi = 0;
1713 for( l = 0; l < 2; l++ )
1715 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1716 x264_me_t *m = &lX->me8x8[i];
1718 m->i_pixel = PIXEL_8x8;
1719 m->p_cost_mv = a->p_cost_mv;
1721 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1722 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1724 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1725 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1727 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, l, m->mv );
1730 h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
1731 m->mv[0], m->mv[1], 8, 8 );
1732 i_part_cost_bi += m->cost_mv;
1733 /* FIXME: ref cost */
1736 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1737 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1738 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1739 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1740 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1742 i_part_cost = a->l0.me8x8[i].cost;
1743 h->mb.i_sub_partition[i] = D_L0_8x8;
1744 COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1745 COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1746 COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1747 a->i_cost8x8bi += i_part_cost;
1749 /* XXX Needed for x264_mb_predict_mv */
1750 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1754 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1757 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1759 uint8_t **p_fref[2] =
1760 { h->mb.pic.p_fref[0][a->l0.i_ref],
1761 h->mb.pic.p_fref[1][a->l1.i_ref] };
1762 DECLARE_ALIGNED_16( uint8_t pix[2][16*8] );
1763 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1766 h->mb.i_partition = D_16x8;
1767 a->i_cost16x8bi = 0;
1769 for( i = 0; i < 2; i++ )
1772 int i_part_cost_bi = 0;
1774 /* TODO: check only the list(s) that were used in b8x8? */
1775 for( l = 0; l < 2; l++ )
1777 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1778 x264_me_t *m = &lX->me16x8[i];
1780 m->i_pixel = PIXEL_16x8;
1781 m->p_cost_mv = a->p_cost_mv;
1783 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1784 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1786 *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[2*i].mv;
1787 *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[2*i+1].mv;
1789 x264_mb_predict_mv( h, l, 8*i, 2, m->mvp );
1790 x264_me_search( h, m, mvc, 2 );
1793 h->mc.mc_luma( pix[l], 16, m->p_fref, m->i_stride[0],
1794 m->mv[0], m->mv[1], 16, 8 );
1795 /* FIXME: ref cost */
1796 i_part_cost_bi += m->cost_mv;
1799 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1800 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1802 i_part_cost = a->l0.me16x8[i].cost;
1803 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1804 if( a->l1.me16x8[i].cost < i_part_cost )
1806 i_part_cost = a->l1.me16x8[i].cost;
1807 a->i_mb_partition16x8[i] = D_L1_8x8;
1809 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1811 i_part_cost = i_part_cost_bi;
1812 a->i_mb_partition16x8[i] = D_BI_8x8;
1814 a->i_cost16x8bi += i_part_cost;
1816 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1820 a->i_mb_type16x8 = B_L0_L0
1821 + (a->i_mb_partition16x8[0]>>2) * 3
1822 + (a->i_mb_partition16x8[1]>>2);
1823 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1826 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1828 uint8_t **p_fref[2] =
1829 { h->mb.pic.p_fref[0][a->l0.i_ref],
1830 h->mb.pic.p_fref[1][a->l1.i_ref] };
1831 DECLARE_ALIGNED_8( uint8_t pix[2][8*16] );
1832 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1835 h->mb.i_partition = D_8x16;
1836 a->i_cost8x16bi = 0;
1838 for( i = 0; i < 2; i++ )
1841 int i_part_cost_bi = 0;
1843 for( l = 0; l < 2; l++ )
1845 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1846 x264_me_t *m = &lX->me8x16[i];
1848 m->i_pixel = PIXEL_8x16;
1849 m->p_cost_mv = a->p_cost_mv;
1851 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1852 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1854 *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[i].mv;
1855 *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[i+2].mv;
1857 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1858 x264_me_search( h, m, mvc, 2 );
1861 h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
1862 m->mv[0], m->mv[1], 8, 16 );
1863 /* FIXME: ref cost */
1864 i_part_cost_bi += m->cost_mv;
1867 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1868 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1870 i_part_cost = a->l0.me8x16[i].cost;
1871 a->i_mb_partition8x16[i] = D_L0_8x8;
1872 if( a->l1.me8x16[i].cost < i_part_cost )
1874 i_part_cost = a->l1.me8x16[i].cost;
1875 a->i_mb_partition8x16[i] = D_L1_8x8;
1877 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1879 i_part_cost = i_part_cost_bi;
1880 a->i_mb_partition8x16[i] = D_BI_8x8;
1882 a->i_cost8x16bi += i_part_cost;
1884 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1888 a->i_mb_type8x16 = B_L0_L0
1889 + (a->i_mb_partition8x16[0]>>2) * 3
1890 + (a->i_mb_partition8x16[1]>>2);
1891 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1894 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1896 int thresh = i_satd * 5/4;
1898 h->mb.i_type = P_L0;
1899 if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1901 h->mb.i_partition = D_16x16;
1902 x264_analyse_update_cache( h, a );
1903 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1905 a->l0.me16x16.cost = a->l0.i_rd16x16;
1907 if( a->l0.i_cost16x8 <= thresh )
1909 h->mb.i_partition = D_16x8;
1910 x264_analyse_update_cache( h, a );
1911 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1914 a->l0.i_cost16x8 = COST_MAX;
1916 if( a->l0.i_cost8x16 <= thresh )
1918 h->mb.i_partition = D_8x16;
1919 x264_analyse_update_cache( h, a );
1920 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1923 a->l0.i_cost8x16 = COST_MAX;
1925 if( a->l0.i_cost8x8 <= thresh )
1927 h->mb.i_type = P_8x8;
1928 h->mb.i_partition = D_8x8;
1929 x264_analyse_update_cache( h, a );
1930 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1932 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1934 /* FIXME: RD per subpartition */
1938 for( i=0; i<4; i++ )
1940 part_bak[i] = h->mb.i_sub_partition[i];
1941 b_sub8x8 |= (part_bak[i] != D_L0_8x8);
1945 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1946 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1947 x264_analyse_update_cache( h, a );
1948 i_cost = x264_rd_cost_mb( h, a->i_lambda2 );
1949 if( a->l0.i_cost8x8 < i_cost )
1951 for( i=0; i<4; i++ )
1952 h->mb.i_sub_partition[i] = part_bak[i];
1955 a->l0.i_cost8x8 = i_cost;
1960 a->l0.i_cost8x8 = COST_MAX;
1963 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
1965 int thresh = i_satd_inter * (17 + (!!h->mb.i_psy_rd))/16;
1967 if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
1969 h->mb.i_type = B_DIRECT;
1970 /* Assumes direct/skip MC is still in fdec */
1971 /* Requires b-rdo to be done before intra analysis */
1972 h->mb.b_skip_mc = 1;
1973 x264_analyse_update_cache( h, a );
1974 a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
1975 h->mb.b_skip_mc = 0;
1978 //FIXME not all the update_cache calls are needed
1979 h->mb.i_partition = D_16x16;
1981 if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
1983 h->mb.i_type = B_L0_L0;
1984 x264_analyse_update_cache( h, a );
1985 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1989 if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
1991 h->mb.i_type = B_L1_L1;
1992 x264_analyse_update_cache( h, a );
1993 a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1997 if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
1999 h->mb.i_type = B_BI_BI;
2000 x264_analyse_update_cache( h, a );
2001 a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
2005 if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
2007 h->mb.i_type = B_8x8;
2008 h->mb.i_partition = D_8x8;
2009 x264_analyse_update_cache( h, a );
2010 a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
2011 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
2015 if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
2017 h->mb.i_type = a->i_mb_type16x8;
2018 h->mb.i_partition = D_16x8;
2019 x264_analyse_update_cache( h, a );
2020 a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
2024 if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
2026 h->mb.i_type = a->i_mb_type8x16;
2027 h->mb.i_partition = D_8x16;
2028 x264_analyse_update_cache( h, a );
2029 a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
2033 static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
2035 const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
2038 switch( h->mb.i_partition )
2041 if( h->mb.i_type == B_BI_BI )
2042 x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
2045 for( i=0; i<2; i++ )
2046 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
2047 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
2050 for( i=0; i<2; i++ )
2051 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
2052 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
2055 for( i=0; i<4; i++ )
2056 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
2057 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
2062 static inline void x264_mb_analyse_transform( x264_t *h )
2064 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2066 int i_cost4, i_cost8;
2067 /* Only luma MC is really needed, but the full MC is re-used in macroblock_encode. */
2070 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2071 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2072 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2073 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2075 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2076 h->mb.b_skip_mc = 1;
2080 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2082 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2085 x264_analyse_update_cache( h, a );
2086 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2087 /* FIXME only luma is needed, but the score for comparison already includes chroma */
2088 i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2090 if( *i_rd >= i_rd8 )
2093 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2094 /* prevent a rare division by zero in estimated intra cost */
2101 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2106 /*****************************************************************************
2107 * x264_macroblock_analyse:
2108 *****************************************************************************/
2109 void x264_macroblock_analyse( x264_t *h )
2111 x264_mb_analysis_t analysis;
2112 int i_cost = COST_MAX;
2115 h->mb.i_qp = x264_ratecontrol_qp( h );
2116 if( h->param.rc.i_aq_mode )
2117 x264_adaptive_quant( h );
2119 x264_mb_analyse_init( h, &analysis, h->mb.i_qp );
2121 /*--------------------------- Do the analysis ---------------------------*/
2122 if( h->sh.i_type == SLICE_TYPE_I )
2124 if( analysis.b_mbrd )
2125 x264_mb_cache_fenc_satd( h );
2126 x264_mb_analyse_intra( h, &analysis, COST_MAX );
2127 if( analysis.b_mbrd )
2128 x264_intra_rd( h, &analysis, COST_MAX );
2130 i_cost = analysis.i_satd_i16x16;
2131 h->mb.i_type = I_16x16;
2132 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, h->mb.i_type, I_4x4 );
2133 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, h->mb.i_type, I_8x8 );
2134 if( analysis.i_satd_pcm < i_cost )
2135 h->mb.i_type = I_PCM;
2137 else if( h->mb.i_subpel_refine >= 7 )
2138 x264_intra_rd_refine( h, &analysis );
2140 else if( h->sh.i_type == SLICE_TYPE_P )
2143 int i_intra_cost, i_intra_type;
2145 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2147 /* Fast P_SKIP detection */
2148 analysis.b_try_pskip = 0;
2149 if( h->param.analyse.b_fast_pskip )
2151 if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2152 // FIXME don't need to check this if the reference frame is done
2154 else if( h->param.analyse.i_subpel_refine >= 3 )
2155 analysis.b_try_pskip = 1;
2156 else if( h->mb.i_mb_type_left == P_SKIP ||
2157 h->mb.i_mb_type_top == P_SKIP ||
2158 h->mb.i_mb_type_topleft == P_SKIP ||
2159 h->mb.i_mb_type_topright == P_SKIP )
2160 b_skip = x264_macroblock_probe_pskip( h );
2163 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2167 h->mb.i_type = P_SKIP;
2168 h->mb.i_partition = D_16x16;
2169 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2173 const unsigned int flags = h->param.analyse.inter;
2177 int i_satd_inter, i_satd_intra;
2179 x264_mb_analyse_load_costs( h, &analysis );
2181 x264_mb_analyse_inter_p16x16( h, &analysis );
2183 if( h->mb.i_type == P_SKIP )
2186 if( flags & X264_ANALYSE_PSUB16x16 )
2188 if( h->param.analyse.b_mixed_references )
2189 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2191 x264_mb_analyse_inter_p8x8( h, &analysis );
2194 /* Select best inter mode */
2196 i_partition = D_16x16;
2197 i_cost = analysis.l0.me16x16.cost;
2199 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2200 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2203 i_partition = D_8x8;
2204 i_cost = analysis.l0.i_cost8x8;
2207 if( flags & X264_ANALYSE_PSUB8x8 )
2209 for( i = 0; i < 4; i++ )
2211 x264_mb_analyse_inter_p4x4( h, &analysis, i );
2212 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2214 int i_cost8x8 = analysis.l0.i_cost4x4[i];
2215 h->mb.i_sub_partition[i] = D_L0_4x4;
2217 x264_mb_analyse_inter_p8x4( h, &analysis, i );
2218 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2219 h->mb.i_sub_partition[i], D_L0_8x4 );
2221 x264_mb_analyse_inter_p4x8( h, &analysis, i );
2222 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2223 h->mb.i_sub_partition[i], D_L0_4x8 );
2225 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2227 x264_mb_cache_mv_p8x8( h, &analysis, i );
2229 analysis.l0.i_cost8x8 = i_cost;
2233 /* Now do 16x8/8x16 */
2234 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2235 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2236 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2238 x264_mb_analyse_inter_p16x8( h, &analysis );
2239 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2241 x264_mb_analyse_inter_p8x16( h, &analysis );
2242 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2245 h->mb.i_partition = i_partition;
2248 //FIXME mb_type costs?
2249 if( analysis.b_mbrd )
2253 else if( i_partition == D_16x16 )
2255 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2256 i_cost = analysis.l0.me16x16.cost;
2258 else if( i_partition == D_16x8 )
2260 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2261 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2262 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2264 else if( i_partition == D_8x16 )
2266 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2267 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2268 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2270 else if( i_partition == D_8x8 )
2274 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2276 switch( h->mb.i_sub_partition[i8x8] )
2279 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2280 i_cost += analysis.l0.me8x8[i8x8].cost;
2283 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2284 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2285 i_cost += analysis.l0.me8x4[i8x8][0].cost +
2286 analysis.l0.me8x4[i8x8][1].cost;
2289 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2290 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2291 i_cost += analysis.l0.me4x8[i8x8][0].cost +
2292 analysis.l0.me4x8[i8x8][1].cost;
2296 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2297 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2298 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2299 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2300 i_cost += analysis.l0.me4x4[i8x8][0].cost +
2301 analysis.l0.me4x4[i8x8][1].cost +
2302 analysis.l0.me4x4[i8x8][2].cost +
2303 analysis.l0.me4x4[i8x8][3].cost;
2306 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2312 if( h->mb.b_chroma_me )
2314 x264_mb_analyse_intra_chroma( h, &analysis );
2315 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2316 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2317 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2318 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2321 x264_mb_analyse_intra( h, &analysis, i_cost );
2323 i_satd_inter = i_cost;
2324 i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2325 analysis.i_satd_i8x8,
2326 analysis.i_satd_i4x4 );
2328 if( analysis.b_mbrd )
2330 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2332 i_partition = D_16x16;
2333 i_cost = analysis.l0.me16x16.cost;
2334 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2335 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2336 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2337 h->mb.i_type = i_type;
2338 h->mb.i_partition = i_partition;
2339 if( i_cost < COST_MAX )
2340 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2341 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2344 i_intra_type = I_16x16;
2345 i_intra_cost = analysis.i_satd_i16x16;
2346 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
2347 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
2348 COPY2_IF_LT( i_intra_cost, analysis.i_satd_pcm, i_intra_type, I_PCM );
2349 COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
2351 if( i_intra_cost == COST_MAX )
2352 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
2354 h->mb.i_type = i_type;
2355 h->stat.frame.i_intra_cost += i_intra_cost;
2356 h->stat.frame.i_inter_cost += i_cost;
2357 h->stat.frame.i_mbs_analysed++;
2359 if( h->mb.i_subpel_refine >= 7 && h->mb.i_type != I_PCM )
2361 if( IS_INTRA( h->mb.i_type ) )
2363 x264_intra_rd_refine( h, &analysis );
2365 else if( i_partition == D_16x16 )
2367 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2368 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
2370 else if( i_partition == D_16x8 )
2372 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2373 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2374 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2375 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2376 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
2377 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
2379 else if( i_partition == D_8x16 )
2381 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2382 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2383 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2384 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2385 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
2386 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
2388 else if( i_partition == D_8x8 )
2391 x264_analyse_update_cache( h, &analysis );
2392 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2393 if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2394 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
2399 else if( h->sh.i_type == SLICE_TYPE_B )
2401 int i_bskip_cost = COST_MAX;
2404 if( analysis.b_mbrd )
2405 x264_mb_cache_fenc_satd( h );
2407 h->mb.i_type = B_SKIP;
2408 if( h->mb.b_direct_auto_write )
2410 /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2411 for( i = 0; i < 2; i++ )
2414 h->sh.b_direct_spatial_mv_pred ^= 1;
2415 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2416 if( analysis.b_direct_available )
2421 b_skip = x264_macroblock_probe_bskip( h );
2423 h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2430 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2432 if( analysis.b_direct_available )
2434 if( !h->mb.b_direct_auto_write )
2436 if( h->mb.b_lossless )
2438 /* chance of skip is too small to bother */
2440 else if( analysis.b_mbrd )
2442 i_bskip_cost = ssd_mb( h );
2443 /* 6 = minimum cavlc cost of a non-skipped MB */
2444 b_skip = h->mb.b_skip_mc = i_bskip_cost <= ((6 * analysis.i_lambda2 + 128) >> 8);
2446 else if( !h->mb.b_direct_auto_write )
2448 /* Conditioning the probe on neighboring block types
2449 * doesn't seem to help speed or quality. */
2450 b_skip = x264_macroblock_probe_bskip( h );
2456 const unsigned int flags = h->param.analyse.inter;
2459 int i_satd_inter = 0; // shut up uninitialized warning
2460 h->mb.b_skip_mc = 0;
2462 x264_mb_analyse_load_costs( h, &analysis );
2464 /* select best inter mode */
2465 /* direct must be first */
2466 if( analysis.b_direct_available )
2467 x264_mb_analyse_inter_direct( h, &analysis );
2469 x264_mb_analyse_inter_b16x16( h, &analysis );
2472 i_partition = D_16x16;
2473 i_cost = analysis.l0.me16x16.cost;
2474 COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2475 COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2476 COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2478 if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2480 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2481 if( i_bskip_cost < analysis.i_rd16x16direct &&
2482 i_bskip_cost < analysis.i_rd16x16bi &&
2483 i_bskip_cost < analysis.l0.i_rd16x16 &&
2484 i_bskip_cost < analysis.l1.i_rd16x16 )
2486 h->mb.i_type = B_SKIP;
2487 x264_analyse_update_cache( h, &analysis );
2492 if( flags & X264_ANALYSE_BSUB16x16 )
2494 x264_mb_analyse_inter_b8x8( h, &analysis );
2495 if( analysis.i_cost8x8bi < i_cost )
2498 i_partition = D_8x8;
2499 i_cost = analysis.i_cost8x8bi;
2501 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2502 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2504 x264_mb_analyse_inter_b16x8( h, &analysis );
2505 COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2506 i_type, analysis.i_mb_type16x8,
2507 i_partition, D_16x8 );
2509 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2510 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2512 x264_mb_analyse_inter_b8x16( h, &analysis );
2513 COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2514 i_type, analysis.i_mb_type8x16,
2515 i_partition, D_8x16 );
2520 if( analysis.b_mbrd )
2525 else if( i_partition == D_16x16 )
2527 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2528 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2529 if( i_type == B_L0_L0 )
2531 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2532 i_cost = analysis.l0.me16x16.cost
2533 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2535 else if( i_type == B_L1_L1 )
2537 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2538 i_cost = analysis.l1.me16x16.cost
2539 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2541 else if( i_type == B_BI_BI )
2543 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2544 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2547 else if( i_partition == D_16x8 )
2549 for( i=0; i<2; i++ )
2551 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2552 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2553 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2554 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2557 else if( i_partition == D_8x16 )
2559 for( i=0; i<2; i++ )
2561 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2562 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2563 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2564 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2567 else if( i_partition == D_8x8 )
2569 for( i=0; i<4; i++ )
2572 int i_part_cost_old;
2574 int i_part_type = h->mb.i_sub_partition[i];
2575 int b_bidir = (i_part_type == D_BI_8x8);
2577 if( i_part_type == D_DIRECT_8x8 )
2579 if( x264_mb_partition_listX_table[0][i_part_type] )
2581 m = &analysis.l0.me8x8[i];
2582 i_part_cost_old = m->cost;
2583 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2584 m->cost -= i_type_cost;
2585 x264_me_refine_qpel( h, m );
2587 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2589 if( x264_mb_partition_listX_table[1][i_part_type] )
2591 m = &analysis.l1.me8x8[i];
2592 i_part_cost_old = m->cost;
2593 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2594 m->cost -= i_type_cost;
2595 x264_me_refine_qpel( h, m );
2597 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2599 /* TODO: update mvp? */
2603 if( analysis.b_mbrd )
2605 i_satd_inter = i_cost;
2606 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2608 i_cost = i_bskip_cost;
2609 i_partition = D_16x16;
2610 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2611 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2612 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2613 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2614 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2615 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2616 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2618 h->mb.i_type = i_type;
2619 h->mb.i_partition = i_partition;
2622 x264_mb_analyse_intra( h, &analysis, i_satd_inter );
2624 if( analysis.b_mbrd )
2626 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2627 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2630 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2631 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2632 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2633 COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
2635 h->mb.i_type = i_type;
2636 h->mb.i_partition = i_partition;
2638 if( analysis.b_mbrd && h->mb.i_subpel_refine >= 7 && IS_INTRA( i_type ) && i_type != I_PCM )
2639 x264_intra_rd_refine( h, &analysis );
2640 else if( h->param.analyse.b_bidir_me )
2641 refine_bidir( h, &analysis );
2645 x264_analyse_update_cache( h, &analysis );
2647 if( !analysis.b_mbrd )
2648 x264_mb_analyse_transform( h );
2650 h->mb.b_trellis = h->param.analyse.i_trellis;
2651 h->mb.b_noise_reduction = !!h->param.analyse.i_noise_reduction;
2652 if( !IS_SKIP(h->mb.i_type) && h->mb.i_psy_trellis && h->param.analyse.i_trellis == 1 )
2653 x264_psy_trellis_init( h, 0 );
2654 if( h->mb.b_trellis == 1 || h->mb.b_noise_reduction )
2655 h->mb.i_skip_intra = 0;
2658 /*-------------------- Update MB from the analysis ----------------------*/
2659 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2663 switch( h->mb.i_type )
2666 for( i = 0; i < 16; i++ )
2667 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2669 x264_mb_analyse_intra_chroma( h, a );
2672 for( i = 0; i < 4; i++ )
2673 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2675 x264_mb_analyse_intra_chroma( h, a );
2678 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2679 x264_mb_analyse_intra_chroma( h, a );
2686 switch( h->mb.i_partition )
2689 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2690 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2694 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2695 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2696 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv );
2697 x264_macroblock_cache_mv_ptr( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv );
2701 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2702 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2703 x264_macroblock_cache_mv_ptr( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv );
2704 x264_macroblock_cache_mv_ptr( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv );
2708 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2714 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2715 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2716 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2717 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2718 for( i = 0; i < 4; i++ )
2719 x264_mb_cache_mv_p8x8( h, a, i );
2724 h->mb.i_partition = D_16x16;
2725 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2726 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv );
2732 x264_mb_load_mv_direct8x8( h, 0 );
2733 x264_mb_load_mv_direct8x8( h, 1 );
2734 x264_mb_load_mv_direct8x8( h, 2 );
2735 x264_mb_load_mv_direct8x8( h, 3 );
2739 /* optimize: cache might not need to be rewritten */
2740 for( i = 0; i < 4; i++ )
2741 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2744 default: /* the rest of the B types */
2745 switch( h->mb.i_partition )
2748 switch( h->mb.i_type )
2751 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2752 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2754 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2755 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0 );
2756 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0 );
2759 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2760 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0 );
2761 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0 );
2763 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2764 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2767 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2768 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2770 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2771 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2776 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2777 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2780 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2781 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2784 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2790 if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2793 for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2796 int ref = h->mb.cache.ref[l][x264_scan8[0]];
2799 completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2800 if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2802 x264_log( h, X264_LOG_WARNING, "internal error (MV out of thread range)\n");
2803 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2804 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2805 h->mb.cache.mv[l][x264_scan8[15]][0],
2806 h->mb.cache.mv[l][x264_scan8[15]][1] );
2807 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2808 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2809 fprintf(stderr, "completed: %d \n", completed );
2810 x264_log( h, X264_LOG_WARNING, "recovering by using intra mode\n");
2811 x264_mb_analyse_intra( h, a, COST_MAX );
2812 h->mb.i_type = I_16x16;
2813 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2814 x264_mb_analyse_intra_chroma( h, a );
2821 #include "slicetype.c"