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 int i = h->param.analyse.i_subpel_refine - (h->sh.i_type == SLICE_TYPE_B);
207 /* mbrd == 1 -> RD mode decision */
208 /* mbrd == 2 -> RD refinement */
209 a->i_mbrd = (i>=6) + (i>=8);
210 /* conduct the analysis using this lamda and QP */
211 a->i_qp = h->mb.i_qp = i_qp;
212 h->mb.i_chroma_qp = h->chroma_qp_table[i_qp];
213 a->i_lambda = x264_lambda_tab[i_qp];
214 a->i_lambda2 = x264_lambda2_tab[i_qp];
215 h->mb.i_me_method = h->param.analyse.i_me_method;
216 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
217 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
218 && h->mb.i_subpel_refine >= 5;
219 h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->i_mbrd;
220 h->mb.b_transform_8x8 = 0;
221 h->mb.b_noise_reduction = 0;
227 a->i_satd_i8x8chroma = COST_MAX;
229 /* non-RD PCM decision is inaccurate (as is psy-rd), so don't do it */
230 a->i_satd_pcm = !h->mb.i_psy_rd && a->i_mbrd ? ((uint64_t)X264_PCM_COST*a->i_lambda2 + 128) >> 8 : COST_MAX;
234 h->mb.b_lossless ? 0 :
236 !h->param.analyse.i_trellis && !h->param.analyse.i_noise_reduction;
238 /* II: Inter part P/B frame */
239 if( h->sh.i_type != SLICE_TYPE_I )
242 int i_fmv_range = 4 * h->param.analyse.i_mv_range;
243 // limit motion search to a slightly smaller range than the theoretical limit,
244 // since the search may go a few iterations past its given range
245 int i_fpel_border = 5; // umh unconditional radius
246 int i_spel_border = 8; // 1.5 for subpel_satd, 1.5 for subpel_rd, 2 for bime, round up
248 /* Calculate max allowed MV range */
249 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range-1 )
250 h->mb.mv_min[0] = 4*( -16*h->mb.i_mb_x - 24 );
251 h->mb.mv_max[0] = 4*( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 24 );
252 h->mb.mv_min_spel[0] = CLIP_FMV( h->mb.mv_min[0] );
253 h->mb.mv_max_spel[0] = CLIP_FMV( h->mb.mv_max[0] );
254 h->mb.mv_min_fpel[0] = (h->mb.mv_min_spel[0]>>2) + i_fpel_border;
255 h->mb.mv_max_fpel[0] = (h->mb.mv_max_spel[0]>>2) - i_fpel_border;
256 if( h->mb.i_mb_x == 0)
258 int mb_y = h->mb.i_mb_y >> h->sh.b_mbaff;
259 int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff;
260 int thread_mvy_range = i_fmv_range;
262 if( h->param.i_threads > 1 )
264 int pix_y = (h->mb.i_mb_y | h->mb.b_interlaced) * 16;
265 int thresh = pix_y + h->param.analyse.i_mv_range_thread;
266 for( i = (h->sh.i_type == SLICE_TYPE_B); i >= 0; i-- )
268 x264_frame_t **fref = i ? h->fref1 : h->fref0;
269 int i_ref = i ? h->i_ref1 : h->i_ref0;
270 for( j=0; j<i_ref; j++ )
272 x264_frame_cond_wait( fref[j], thresh );
273 thread_mvy_range = X264_MIN( thread_mvy_range, fref[j]->i_lines_completed - pix_y );
276 if( h->param.b_deterministic )
277 thread_mvy_range = h->param.analyse.i_mv_range_thread;
278 if( h->mb.b_interlaced )
279 thread_mvy_range >>= 1;
282 h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
283 h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
284 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 );
285 h->mb.mv_max_spel[1] = CLIP_FMV( h->mb.mv_max[1] );
286 h->mb.mv_max_spel[1] = X264_MIN( h->mb.mv_max_spel[1], thread_mvy_range*4 );
287 h->mb.mv_min_fpel[1] = (h->mb.mv_min_spel[1]>>2) + i_fpel_border;
288 h->mb.mv_max_fpel[1] = (h->mb.mv_max_spel[1]>>2) - i_fpel_border;
294 a->l0.i_cost8x8 = COST_MAX;
296 for( i = 0; i < 4; i++ )
300 a->l0.i_cost4x8[i] = COST_MAX;
304 a->l0.i_cost8x16 = COST_MAX;
305 if( h->sh.i_type == SLICE_TYPE_B )
309 a->l1.i_cost8x8 = COST_MAX;
311 for( i = 0; i < 4; i++ )
316 a->i_cost8x8direct[i] = COST_MAX;
327 a->i_cost16x16direct =
330 a->i_cost8x16bi = COST_MAX;
333 /* Fast intra decision */
334 if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
336 if( IS_INTRA( h->mb.i_mb_type_left )
337 || IS_INTRA( h->mb.i_mb_type_top )
338 || IS_INTRA( h->mb.i_mb_type_topleft )
339 || IS_INTRA( h->mb.i_mb_type_topright )
340 || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
341 || (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])) )
342 { /* intra is likely */ }
358 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
360 if( i_neighbour & MB_TOPLEFT )
362 /* top and left available */
363 *mode++ = I_PRED_16x16_V;
364 *mode++ = I_PRED_16x16_H;
365 *mode++ = I_PRED_16x16_DC;
366 *mode++ = I_PRED_16x16_P;
369 else if( i_neighbour & MB_LEFT )
372 *mode++ = I_PRED_16x16_DC_LEFT;
373 *mode++ = I_PRED_16x16_H;
376 else if( i_neighbour & MB_TOP )
379 *mode++ = I_PRED_16x16_DC_TOP;
380 *mode++ = I_PRED_16x16_V;
386 *mode = I_PRED_16x16_DC_128;
392 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
394 if( i_neighbour & MB_TOPLEFT )
396 /* top and left available */
397 *mode++ = I_PRED_CHROMA_V;
398 *mode++ = I_PRED_CHROMA_H;
399 *mode++ = I_PRED_CHROMA_DC;
400 *mode++ = I_PRED_CHROMA_P;
403 else if( i_neighbour & MB_LEFT )
406 *mode++ = I_PRED_CHROMA_DC_LEFT;
407 *mode++ = I_PRED_CHROMA_H;
410 else if( i_neighbour & MB_TOP )
413 *mode++ = I_PRED_CHROMA_DC_TOP;
414 *mode++ = I_PRED_CHROMA_V;
420 *mode = I_PRED_CHROMA_DC_128;
426 static void predict_4x4_mode_available( unsigned int i_neighbour,
427 int *mode, int *pi_count )
429 int b_l = i_neighbour & MB_LEFT;
430 int b_t = i_neighbour & MB_TOP;
435 *mode++ = I_PRED_4x4_DC;
436 *mode++ = I_PRED_4x4_H;
437 *mode++ = I_PRED_4x4_V;
438 *mode++ = I_PRED_4x4_DDL;
439 if( i_neighbour & MB_TOPLEFT )
441 *mode++ = I_PRED_4x4_DDR;
442 *mode++ = I_PRED_4x4_VR;
443 *mode++ = I_PRED_4x4_HD;
446 *mode++ = I_PRED_4x4_VL;
447 *mode++ = I_PRED_4x4_HU;
451 *mode++ = I_PRED_4x4_DC_LEFT;
452 *mode++ = I_PRED_4x4_H;
453 *mode++ = I_PRED_4x4_HU;
458 *mode++ = I_PRED_4x4_DC_TOP;
459 *mode++ = I_PRED_4x4_V;
460 *mode++ = I_PRED_4x4_DDL;
461 *mode++ = I_PRED_4x4_VL;
466 *mode++ = I_PRED_4x4_DC_128;
471 /* 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. */
472 static void inline x264_psy_trellis_init( x264_t *h, int do_both_dct )
474 DECLARE_ALIGNED_16( int16_t dct8x8[4][8][8] );
475 DECLARE_ALIGNED_16( int16_t dct4x4[16][4][4] );
476 DECLARE_ALIGNED_16( uint8_t zero[16*FDEC_STRIDE] ) = {0};
479 if( do_both_dct || h->mb.b_transform_8x8 )
481 h->dctf.sub16x16_dct8( dct8x8, h->mb.pic.p_fenc[0], zero );
482 for( i = 0; i < 4; i++ )
483 h->zigzagf.scan_8x8( h->mb.pic.fenc_dct8[i], dct8x8[i] );
485 if( do_both_dct || !h->mb.b_transform_8x8 )
487 h->dctf.sub16x16_dct( dct4x4, h->mb.pic.p_fenc[0], zero );
488 for( i = 0; i < 16; i++ )
489 h->zigzagf.scan_4x4( h->mb.pic.fenc_dct4[i], dct4x4[i] );
493 /* Pre-calculate fenc satd scores for psy RD, minus DC coefficients */
494 static inline void x264_mb_cache_fenc_satd( x264_t *h )
496 DECLARE_ALIGNED_16(uint8_t zero[16]) = {0};
498 int x, y, satd_sum = 0, sa8d_sum = 0;
499 if( h->param.analyse.i_trellis == 2 && h->mb.i_psy_trellis )
500 x264_psy_trellis_init( h, h->param.analyse.b_transform_8x8 );
501 if( !h->mb.i_psy_rd )
503 for( y = 0; y < 4; y++ )
504 for( x = 0; x < 4; x++ )
506 fenc = h->mb.pic.p_fenc[0]+x*4+y*4*FENC_STRIDE;
507 h->mb.pic.fenc_satd[y][x] = h->pixf.satd[PIXEL_4x4]( zero, 0, fenc, FENC_STRIDE )
508 - (h->pixf.sad[PIXEL_4x4]( zero, 0, fenc, FENC_STRIDE )>>1);
509 satd_sum += h->mb.pic.fenc_satd[y][x];
511 for( y = 0; y < 2; y++ )
512 for( x = 0; x < 2; x++ )
514 fenc = h->mb.pic.p_fenc[0]+x*8+y*8*FENC_STRIDE;
515 h->mb.pic.fenc_sa8d[y][x] = h->pixf.sa8d[PIXEL_8x8]( zero, 0, fenc, FENC_STRIDE )
516 - (h->pixf.sad[PIXEL_8x8]( zero, 0, fenc, FENC_STRIDE )>>2);
517 sa8d_sum += h->mb.pic.fenc_sa8d[y][x];
519 h->mb.pic.fenc_satd_sum = satd_sum;
520 h->mb.pic.fenc_sa8d_sum = sa8d_sum;
523 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
530 uint8_t *p_dstc[2], *p_srcc[2];
532 if( a->i_satd_i8x8chroma < COST_MAX )
535 /* 8x8 prediction selection for chroma */
536 p_dstc[0] = h->mb.pic.p_fdec[1];
537 p_dstc[1] = h->mb.pic.p_fdec[2];
538 p_srcc[0] = h->mb.pic.p_fenc[1];
539 p_srcc[1] = h->mb.pic.p_fenc[2];
541 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
542 a->i_satd_i8x8chroma = COST_MAX;
543 if( i_max == 4 && h->pixf.intra_satd_x3_8x8c && h->pixf.mbcmp[0] == h->pixf.satd[0] )
545 int satdu[4], satdv[4];
546 h->pixf.intra_satd_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
547 h->pixf.intra_satd_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
548 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
549 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
550 satdu[I_PRED_CHROMA_P] =
551 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
552 satdv[I_PRED_CHROMA_P] =
553 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
555 for( i=0; i<i_max; i++ )
557 int i_mode = predict_mode[i];
558 int i_satd = satdu[i_mode] + satdv[i_mode]
559 + a->i_lambda * bs_size_ue(i_mode);
561 a->i_satd_i8x8chroma_dir[i] = i_satd;
562 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
567 for( i=0; i<i_max; i++ )
570 int i_mode = predict_mode[i];
572 /* we do the prediction */
573 if( h->mb.b_lossless )
574 x264_predict_lossless_8x8_chroma( h, i_mode );
577 h->predict_8x8c[i_mode]( p_dstc[0] );
578 h->predict_8x8c[i_mode]( p_dstc[1] );
581 /* we calculate the cost */
582 i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
583 p_srcc[0], FENC_STRIDE ) +
584 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
585 p_srcc[1], FENC_STRIDE ) +
586 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
588 a->i_satd_i8x8chroma_dir[i] = i_satd;
589 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
593 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
596 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
598 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
599 uint8_t *p_src = h->mb.pic.p_fenc[0];
600 uint8_t *p_dst = h->mb.pic.p_fdec[0];
605 int b_merged_satd = !!h->pixf.intra_mbcmp_x3_16x16 && !h->mb.b_lossless;
607 /*---------------- Try all mode and calculate their score ---------------*/
609 /* 16x16 prediction selection */
610 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
612 if( b_merged_satd && i_max == 4 )
614 h->pixf.intra_mbcmp_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
615 h->predict_16x16[I_PRED_16x16_P]( p_dst );
616 a->i_satd_i16x16_dir[I_PRED_16x16_P] =
617 h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
620 int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
621 COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
626 for( i = 0; i < i_max; i++ )
629 int i_mode = predict_mode[i];
631 if( h->mb.b_lossless )
632 x264_predict_lossless_16x16( h, i_mode );
634 h->predict_16x16[i_mode]( p_dst );
636 i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
637 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
638 COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
639 a->i_satd_i16x16_dir[i_mode] = i_satd;
643 if( h->sh.i_type == SLICE_TYPE_B )
644 /* cavlc mb type prefix */
645 a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
646 if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
649 /* 8x8 prediction selection */
650 if( flags & X264_ANALYSE_I8x8 )
652 DECLARE_ALIGNED_16( uint8_t edge[33] );
653 x264_pixel_cmp_t sa8d = (h->pixf.mbcmp[0] == h->pixf.satd[0]) ? h->pixf.sa8d[PIXEL_8x8] : h->pixf.mbcmp[PIXEL_8x8];
654 int i_satd_thresh = a->i_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
656 b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
658 // FIXME some bias like in i4x4?
659 if( h->sh.i_type == SLICE_TYPE_B )
660 i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
662 for( idx = 0;; idx++ )
666 uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
667 uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
668 int i_best = COST_MAX;
669 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
671 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
672 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
674 if( b_merged_satd && i_max == 9 )
677 h->pixf.intra_sa8d_x3_8x8( p_src_by, edge, satd );
678 satd[i_pred_mode] -= 3 * a->i_lambda;
679 for( i=2; i>=0; i-- )
681 int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
682 COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
689 for( ; i<i_max; i++ )
692 int i_mode = predict_mode[i];
694 if( h->mb.b_lossless )
695 x264_predict_lossless_8x8( h, p_dst_by, idx, i_mode, edge );
697 h->predict_8x8[i_mode]( p_dst_by, edge );
699 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
700 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
702 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
703 a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
707 if( idx == 3 || i_cost > i_satd_thresh )
710 /* we need to encode this block now (for next ones) */
711 h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
712 x264_mb_encode_i8x8( h, idx, a->i_qp );
714 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
719 a->i_satd_i8x8 = i_cost;
720 if( h->mb.i_skip_intra )
722 h->mc.copy[PIXEL_16x16]( h->mb.pic.i8x8_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
723 if( h->mb.i_skip_intra == 2 )
724 h->mc.memcpy_aligned( h->mb.pic.i8x8_dct_buf, h->dct.luma8x8, sizeof(h->mb.pic.i8x8_dct_buf) );
729 a->i_satd_i8x8 = COST_MAX;
730 i_cost = i_cost * 4/(idx+1);
732 if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+!!a->i_mbrd)/4 )
736 /* 4x4 prediction selection */
737 if( flags & X264_ANALYSE_I4x4 )
740 int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
741 b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
743 i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
745 i_cost = a->i_lambda * 24; /* from JVT (SATD0) */
746 if( h->sh.i_type == SLICE_TYPE_B )
747 i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];
749 for( idx = 0;; idx++ )
751 uint8_t *p_src_by = p_src + block_idx_xy_fenc[idx];
752 uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
753 int i_best = COST_MAX;
754 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
756 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
758 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
759 /* emulate missing topright samples */
760 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
762 if( b_merged_satd && i_max >= 6 )
765 h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
766 satd[i_pred_mode] -= 3 * a->i_lambda;
767 for( i=2; i>=0; i-- )
768 COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
769 a->i_predict4x4[idx], i );
775 for( ; i<i_max; i++ )
778 int i_mode = predict_mode[i];
779 if( h->mb.b_lossless )
780 x264_predict_lossless_4x4( h, p_dst_by, idx, i_mode );
782 h->predict_4x4[i_mode]( p_dst_by );
784 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
785 p_src_by, FENC_STRIDE )
786 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
788 COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
792 if( i_cost > i_satd_thresh || idx == 15 )
795 /* we need to encode this block now (for next ones) */
796 h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
797 x264_mb_encode_i4x4( h, idx, a->i_qp );
799 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
803 a->i_satd_i4x4 = i_cost;
804 if( h->mb.i_skip_intra )
806 h->mc.copy[PIXEL_16x16]( h->mb.pic.i4x4_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
807 if( h->mb.i_skip_intra == 2 )
808 h->mc.memcpy_aligned( h->mb.pic.i4x4_dct_buf, h->dct.luma4x4, sizeof(h->mb.pic.i4x4_dct_buf) );
812 a->i_satd_i4x4 = COST_MAX;
816 static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
818 if( a->i_satd_i16x16 <= i_satd_thresh )
820 h->mb.i_type = I_16x16;
821 x264_analyse_update_cache( h, a );
822 a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
825 a->i_satd_i16x16 = COST_MAX;
827 if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
829 h->mb.i_type = I_4x4;
830 x264_analyse_update_cache( h, a );
831 a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
834 a->i_satd_i4x4 = COST_MAX;
836 if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
838 h->mb.i_type = I_8x8;
839 x264_analyse_update_cache( h, a );
840 a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
843 a->i_satd_i8x8 = COST_MAX;
846 static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
848 uint8_t *p_src = h->mb.pic.p_fenc[0];
849 uint8_t *p_dst = h->mb.pic.p_fdec[0];
852 int i_max, i_mode, i_thresh;
853 uint64_t i_satd, i_best;
856 h->mb.i_skip_intra = 0;
858 if( h->mb.i_type == I_16x16 )
860 int old_pred_mode = a->i_predict16x16;
861 i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
862 i_best = a->i_satd_i16x16;
863 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
864 for( i = 0; i < i_max; i++ )
866 int i_mode = predict_mode[i];
867 if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
869 h->mb.i_intra16x16_pred_mode = i_mode;
870 i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
871 COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
874 else if( h->mb.i_type == I_4x4 )
876 uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
878 for( idx = 0; idx < 16; idx++ )
880 uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
883 i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
885 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
887 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
888 /* emulate missing topright samples */
889 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
891 for( i = 0; i < i_max; i++ )
893 i_mode = predict_mode[i];
894 if( h->mb.b_lossless )
895 x264_predict_lossless_4x4( h, p_dst_by, idx, i_mode );
897 h->predict_4x4[i_mode]( p_dst_by );
898 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
900 if( i_best > i_satd )
902 a->i_predict4x4[idx] = i_mode;
904 pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
905 pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
906 pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
907 pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
908 i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
912 *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
913 *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
914 *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
915 *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
916 h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
918 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
921 else if( h->mb.i_type == I_8x8 )
923 DECLARE_ALIGNED_16( uint8_t edge[33] );
924 for( idx = 0; idx < 4; idx++ )
932 i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
935 i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
939 p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
940 p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
941 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
942 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
944 for( i = 0; i < i_max; i++ )
946 i_mode = predict_mode[i];
947 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
949 if( h->mb.b_lossless )
950 x264_predict_lossless_8x8( h, p_dst_by, idx, i_mode, edge );
952 h->predict_8x8[i_mode]( p_dst_by, edge );
953 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
955 if( i_best > i_satd )
957 a->i_predict8x8[idx] = i_mode;
960 pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
963 pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
965 i_nnz[j] = h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]];
969 *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
972 p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
974 h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]] = i_nnz[j];
976 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
980 /* RD selection for chroma prediction */
981 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
984 i_thresh = a->i_satd_i8x8chroma * 5/4;
986 for( i = j = 0; i < i_max; i++ )
987 if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
988 predict_mode[i] != a->i_predict8x8chroma )
990 predict_mode[j++] = predict_mode[i];
996 int i_chroma_lambda = x264_lambda2_tab[h->mb.i_chroma_qp];
997 /* the previous thing encoded was x264_intra_rd(), so the pixels and
998 * coefs for the current chroma mode are still around, so we only
999 * have to recount the bits. */
1000 i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
1001 for( i = 0; i < i_max; i++ )
1003 i_mode = predict_mode[i];
1004 if( h->mb.b_lossless )
1005 x264_predict_lossless_8x8_chroma( h, i_mode );
1008 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
1009 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
1011 /* if we've already found a mode that needs no residual, then
1012 * probably any mode with a residual will be worse.
1013 * so avoid dct on the remaining modes to improve speed. */
1014 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
1015 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode );
1017 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
1022 #define LOAD_FENC( m, src, xoff, yoff) \
1023 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
1024 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
1025 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
1026 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
1027 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
1029 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
1030 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
1031 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
1032 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
1033 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
1034 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
1035 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
1036 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
1038 #define REF_COST(list, ref) \
1039 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
1041 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
1045 DECLARE_ALIGNED_4( int16_t mvc[8][2] );
1046 int i_halfpel_thresh = INT_MAX;
1047 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1049 /* 16x16 Search on all ref frame */
1050 m.i_pixel = PIXEL_16x16;
1051 m.p_cost_mv = a->p_cost_mv;
1052 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1054 a->l0.me16x16.cost = INT_MAX;
1055 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1057 const int i_ref_cost = REF_COST( 0, i_ref );
1058 i_halfpel_thresh -= i_ref_cost;
1059 m.i_ref_cost = i_ref_cost;
1062 /* search with ref */
1063 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1064 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1065 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1066 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1068 /* early termination
1069 * SSD threshold would probably be better than SATD */
1072 && m.cost-m.cost_mv < 300*a->i_lambda
1073 && abs(m.mv[0]-h->mb.cache.pskip_mv[0])
1074 + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
1075 && x264_macroblock_probe_pskip( h ) )
1077 h->mb.i_type = P_SKIP;
1078 x264_analyse_update_cache( h, a );
1079 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1083 m.cost += i_ref_cost;
1084 i_halfpel_thresh += i_ref_cost;
1086 if( m.cost < a->l0.me16x16.cost )
1087 h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1089 /* save mv for predicting neighbors */
1090 *(uint32_t*)a->l0.mvc[i_ref][0] =
1091 *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1094 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
1095 assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1097 h->mb.i_type = P_L0;
1100 x264_mb_cache_fenc_satd( h );
1101 if( a->l0.me16x16.i_ref == 0 && *(uint32_t*)a->l0.me16x16.mv == *(uint32_t*)h->mb.cache.pskip_mv )
1103 h->mb.i_partition = D_16x16;
1104 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
1105 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1110 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
1114 uint8_t **p_fenc = h->mb.pic.p_fenc;
1115 int i_halfpel_thresh = INT_MAX;
1116 int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
1118 int i_maxref = h->mb.pic.i_fref[0]-1;
1120 h->mb.i_partition = D_8x8;
1122 /* early termination: if 16x16 chose ref 0, then evalute no refs older
1123 * than those used by the neighbors */
1124 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
1125 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
1128 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
1129 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
1130 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
1131 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
1132 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
1133 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
1136 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1137 *(uint32_t*)a->l0.mvc[i_ref][0] = *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy];
1139 for( i = 0; i < 4; i++ )
1141 x264_me_t *l0m = &a->l0.me8x8[i];
1145 m.i_pixel = PIXEL_8x8;
1146 m.p_cost_mv = a->p_cost_mv;
1148 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
1149 l0m->cost = INT_MAX;
1150 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1152 const int i_ref_cost = REF_COST( 0, i_ref );
1153 i_halfpel_thresh -= i_ref_cost;
1154 m.i_ref_cost = i_ref_cost;
1157 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
1158 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
1159 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1160 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
1162 m.cost += i_ref_cost;
1163 i_halfpel_thresh += i_ref_cost;
1164 *(uint32_t*)a->l0.mvc[i_ref][i+1] = *(uint32_t*)m.mv;
1166 if( m.cost < l0m->cost )
1167 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1169 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv );
1170 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1173 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1176 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1177 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1178 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1179 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1182 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1184 const int i_ref = a->l0.me16x16.i_ref;
1185 const int i_ref_cost = REF_COST( 0, i_ref );
1186 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
1187 uint8_t **p_fenc = h->mb.pic.p_fenc;
1189 int16_t (*mvc)[2] = a->l0.mvc[i_ref];
1192 /* XXX Needed for x264_mb_predict_mv */
1193 h->mb.i_partition = D_8x8;
1196 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.me16x16.mv;
1198 for( i = 0; i < 4; i++ )
1200 x264_me_t *m = &a->l0.me8x8[i];
1204 m->i_pixel = PIXEL_8x8;
1205 m->p_cost_mv = a->p_cost_mv;
1206 m->i_ref_cost = i_ref_cost;
1209 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1210 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1211 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1212 x264_me_search( h, m, mvc, i_mvc );
1214 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, m->mv );
1216 *(uint32_t*)mvc[i_mvc] = *(uint32_t*)m->mv;
1220 m->cost += i_ref_cost;
1221 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1224 /* theoretically this should include 4*ref_cost,
1225 * but 3 seems a better approximation of cabac. */
1226 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1227 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
1228 REF_COST( 0, a->l0.me16x16.i_ref );
1229 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1230 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1233 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
1236 uint8_t **p_fenc = h->mb.pic.p_fenc;
1237 DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1240 /* XXX Needed for x264_mb_predict_mv */
1241 h->mb.i_partition = D_16x8;
1243 for( i = 0; i < 2; i++ )
1245 x264_me_t *l0m = &a->l0.me16x8[i];
1246 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1247 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1249 m.i_pixel = PIXEL_16x8;
1250 m.p_cost_mv = a->p_cost_mv;
1252 LOAD_FENC( &m, p_fenc, 0, 8*i );
1253 l0m->cost = INT_MAX;
1254 for( j = 0; j < i_ref8s; j++ )
1256 const int i_ref = ref8[j];
1257 const int i_ref_cost = REF_COST( 0, i_ref );
1258 m.i_ref_cost = i_ref_cost;
1261 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1262 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1263 *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][2*i+1];
1264 *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][2*i+2];
1266 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1267 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1268 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1269 x264_me_search( h, &m, mvc, 3 );
1271 m.cost += i_ref_cost;
1273 if( m.cost < l0m->cost )
1274 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1276 x264_macroblock_cache_mv_ptr( h, 0, 2*i, 4, 2, 0, l0m->mv );
1277 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1280 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1283 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1286 uint8_t **p_fenc = h->mb.pic.p_fenc;
1287 DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1290 /* XXX Needed for x264_mb_predict_mv */
1291 h->mb.i_partition = D_8x16;
1293 for( i = 0; i < 2; i++ )
1295 x264_me_t *l0m = &a->l0.me8x16[i];
1296 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1297 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1299 m.i_pixel = PIXEL_8x16;
1300 m.p_cost_mv = a->p_cost_mv;
1302 LOAD_FENC( &m, p_fenc, 8*i, 0 );
1303 l0m->cost = INT_MAX;
1304 for( j = 0; j < i_ref8s; j++ )
1306 const int i_ref = ref8[j];
1307 const int i_ref_cost = REF_COST( 0, i_ref );
1308 m.i_ref_cost = i_ref_cost;
1311 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1312 *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][i+1];
1313 *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][i+3];
1315 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1316 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1317 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1318 x264_me_search( h, &m, mvc, 3 );
1320 m.cost += i_ref_cost;
1322 if( m.cost < l0m->cost )
1323 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1325 x264_macroblock_cache_mv_ptr( h, 2*i, 0, 2, 4, 0, l0m->mv );
1326 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1329 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1332 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1334 DECLARE_ALIGNED_8( uint8_t pix1[16*8] );
1335 uint8_t *pix2 = pix1+8;
1336 const int i_stride = h->mb.pic.i_stride[1];
1337 const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1338 const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1340 #define CHROMA4x4MC( width, height, me, x, y ) \
1341 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 ); \
1342 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 );
1344 if( pixel == PIXEL_4x4 )
1346 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1347 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 2,0 );
1348 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 0,2 );
1349 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1351 else if( pixel == PIXEL_8x4 )
1353 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1354 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1358 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1359 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1362 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1363 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1366 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1368 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1369 uint8_t **p_fenc = h->mb.pic.p_fenc;
1370 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1373 /* XXX Needed for x264_mb_predict_mv */
1374 h->mb.i_partition = D_8x8;
1376 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1378 const int idx = 4*i8x8 + i4x4;
1379 const int x4 = block_idx_x[idx];
1380 const int y4 = block_idx_y[idx];
1381 const int i_mvc = (i4x4 == 0);
1383 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1385 m->i_pixel = PIXEL_4x4;
1386 m->p_cost_mv = a->p_cost_mv;
1388 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1389 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1391 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1392 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1394 x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 1, 0, m->mv );
1396 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1397 a->l0.me4x4[i8x8][1].cost +
1398 a->l0.me4x4[i8x8][2].cost +
1399 a->l0.me4x4[i8x8][3].cost +
1400 REF_COST( 0, i_ref ) +
1401 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1402 if( h->mb.b_chroma_me )
1403 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1406 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1408 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1409 uint8_t **p_fenc = h->mb.pic.p_fenc;
1410 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1413 /* XXX Needed for x264_mb_predict_mv */
1414 h->mb.i_partition = D_8x8;
1416 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1418 const int idx = 4*i8x8 + 2*i8x4;
1419 const int x4 = block_idx_x[idx];
1420 const int y4 = block_idx_y[idx];
1421 const int i_mvc = (i8x4 == 0);
1423 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1425 m->i_pixel = PIXEL_8x4;
1426 m->p_cost_mv = a->p_cost_mv;
1428 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1429 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1431 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1432 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1434 x264_macroblock_cache_mv_ptr( h, x4, y4, 2, 1, 0, m->mv );
1436 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1437 REF_COST( 0, i_ref ) +
1438 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1439 if( h->mb.b_chroma_me )
1440 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1443 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1445 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1446 uint8_t **p_fenc = h->mb.pic.p_fenc;
1447 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1450 /* XXX Needed for x264_mb_predict_mv */
1451 h->mb.i_partition = D_8x8;
1453 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1455 const int idx = 4*i8x8 + i4x8;
1456 const int x4 = block_idx_x[idx];
1457 const int y4 = block_idx_y[idx];
1458 const int i_mvc = (i4x8 == 0);
1460 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1462 m->i_pixel = PIXEL_4x8;
1463 m->p_cost_mv = a->p_cost_mv;
1465 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1466 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1468 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1469 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1471 x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 2, 0, m->mv );
1473 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1474 REF_COST( 0, i_ref ) +
1475 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1476 if( h->mb.b_chroma_me )
1477 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1480 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1482 /* Assumes that fdec still contains the results of
1483 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1485 uint8_t **p_fenc = h->mb.pic.p_fenc;
1486 uint8_t **p_fdec = h->mb.pic.p_fdec;
1489 a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1490 for( i = 0; i < 4; i++ )
1492 const int x = (i&1)*8;
1493 const int y = (i>>1)*8;
1494 a->i_cost16x16direct +=
1495 a->i_cost8x8direct[i] =
1496 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1499 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1503 #define WEIGHTED_AVG( size, pix, stride, src1, stride1, src2, stride2 ) \
1505 h->mc.avg[size]( pix, stride, src1, stride1, src2, stride2, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1508 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1510 DECLARE_ALIGNED_16( uint8_t pix0[16*16] );
1511 DECLARE_ALIGNED_16( uint8_t pix1[16*16] );
1512 uint8_t *src0, *src1;
1513 int stride0 = 16, stride1 = 16;
1517 DECLARE_ALIGNED_4( int16_t mvc[9][2] );
1518 int i_halfpel_thresh = INT_MAX;
1519 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1521 /* 16x16 Search on all ref frame */
1522 m.i_pixel = PIXEL_16x16;
1523 m.p_cost_mv = a->p_cost_mv;
1524 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1527 a->l0.me16x16.cost = INT_MAX;
1528 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1530 /* search with ref */
1531 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1532 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1533 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1534 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1537 m.cost += REF_COST( 0, i_ref );
1539 if( m.cost < a->l0.me16x16.cost )
1541 a->l0.i_ref = i_ref;
1542 h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1545 /* save mv for predicting neighbors */
1546 *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1548 /* subtract ref cost, so we don't have to add it for the other MB types */
1549 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1552 i_halfpel_thresh = INT_MAX;
1553 p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1554 a->l1.me16x16.cost = INT_MAX;
1555 for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1557 /* search with ref */
1558 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1559 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1560 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1561 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1564 m.cost += REF_COST( 1, i_ref );
1566 if( m.cost < a->l1.me16x16.cost )
1568 a->l1.i_ref = i_ref;
1569 h->mc.memcpy_aligned( &a->l1.me16x16, &m, sizeof(x264_me_t) );
1572 /* save mv for predicting neighbors */
1573 *(uint32_t*)h->mb.mvr[1][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1575 /* subtract ref cost, so we don't have to add it for the other MB types */
1576 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1578 /* Set global ref, needed for other modes? */
1579 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1580 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1582 /* get cost of BI mode */
1583 src0 = h->mc.get_ref( pix0, &stride0,
1584 h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1585 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1], 16, 16 );
1586 src1 = h->mc.get_ref( pix1, &stride1,
1587 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1588 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1], 16, 16 );
1590 h->mc.avg[PIXEL_16x16]( pix0, 16, src0, stride0, src1, stride1, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
1592 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix0, 16 )
1593 + REF_COST( 0, a->l0.i_ref )
1594 + REF_COST( 1, a->l1.i_ref )
1595 + a->l0.me16x16.cost_mv
1596 + a->l1.me16x16.cost_mv;
1599 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1600 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1601 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1604 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1606 const int x = 2*(i%2);
1607 const int y = 2*(i/2);
1609 switch( h->mb.i_sub_partition[i] )
1612 x264_macroblock_cache_mv_ptr( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv );
1615 x264_macroblock_cache_mv_ptr( h, x, y+0, 2, 1, 0, a->l0.me8x4[i][0].mv );
1616 x264_macroblock_cache_mv_ptr( h, x, y+1, 2, 1, 0, a->l0.me8x4[i][1].mv );
1619 x264_macroblock_cache_mv_ptr( h, x+0, y, 1, 2, 0, a->l0.me4x8[i][0].mv );
1620 x264_macroblock_cache_mv_ptr( h, x+1, y, 1, 2, 0, a->l0.me4x8[i][1].mv );
1623 x264_macroblock_cache_mv_ptr( h, x+0, y+0, 1, 1, 0, a->l0.me4x4[i][0].mv );
1624 x264_macroblock_cache_mv_ptr( h, x+1, y+0, 1, 1, 0, a->l0.me4x4[i][1].mv );
1625 x264_macroblock_cache_mv_ptr( h, x+0, y+1, 1, 1, 0, a->l0.me4x4[i][2].mv );
1626 x264_macroblock_cache_mv_ptr( h, x+1, y+1, 1, 1, 0, a->l0.me4x4[i][3].mv );
1629 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1634 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1635 if( x264_mb_partition_listX_table[0][part] ) \
1637 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1638 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 0, me0.mv ); \
1642 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1643 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0 ); \
1645 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0 ); \
1647 if( x264_mb_partition_listX_table[1][part] ) \
1649 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1650 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 1, me1.mv ); \
1654 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1655 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0 ); \
1657 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0 ); \
1660 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1664 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1666 x264_mb_load_mv_direct8x8( h, i );
1669 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0 );
1670 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0 );
1671 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1676 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1679 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1681 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1683 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1685 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1689 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1691 uint8_t **p_fref[2] =
1692 { h->mb.pic.p_fref[0][a->l0.i_ref],
1693 h->mb.pic.p_fref[1][a->l1.i_ref] };
1694 DECLARE_ALIGNED_8( uint8_t pix[2][8*8] );
1697 /* XXX Needed for x264_mb_predict_mv */
1698 h->mb.i_partition = D_8x8;
1702 for( i = 0; i < 4; i++ )
1707 int i_part_cost_bi = 0;
1708 int stride[2] = {8,8};
1711 for( l = 0; l < 2; l++ )
1713 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1714 x264_me_t *m = &lX->me8x8[i];
1716 m->i_pixel = PIXEL_8x8;
1717 m->p_cost_mv = a->p_cost_mv;
1719 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1720 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1722 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1723 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1725 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, l, m->mv );
1728 src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1729 m->mv[0], m->mv[1], 8, 8 );
1730 i_part_cost_bi += m->cost_mv;
1731 /* FIXME: ref cost */
1733 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] );
1734 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1735 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1736 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1737 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1739 i_part_cost = a->l0.me8x8[i].cost;
1740 h->mb.i_sub_partition[i] = D_L0_8x8;
1741 COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1742 COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1743 COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1744 a->i_cost8x8bi += i_part_cost;
1746 /* XXX Needed for x264_mb_predict_mv */
1747 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1751 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1754 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1756 uint8_t **p_fref[2] =
1757 { h->mb.pic.p_fref[0][a->l0.i_ref],
1758 h->mb.pic.p_fref[1][a->l1.i_ref] };
1759 DECLARE_ALIGNED_16( uint8_t pix[2][16*8] );
1760 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1763 h->mb.i_partition = D_16x8;
1764 a->i_cost16x8bi = 0;
1766 for( i = 0; i < 2; i++ )
1769 int i_part_cost_bi = 0;
1770 int stride[2] = {16,16};
1773 /* TODO: check only the list(s) that were used in b8x8? */
1774 for( l = 0; l < 2; l++ )
1776 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1777 x264_me_t *m = &lX->me16x8[i];
1779 m->i_pixel = PIXEL_16x8;
1780 m->p_cost_mv = a->p_cost_mv;
1782 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1783 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1785 *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[2*i].mv;
1786 *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[2*i+1].mv;
1788 x264_mb_predict_mv( h, l, 8*i, 2, m->mvp );
1789 x264_me_search( h, m, mvc, 2 );
1792 src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1793 m->mv[0], m->mv[1], 16, 8 );
1794 /* FIXME: ref cost */
1795 i_part_cost_bi += m->cost_mv;
1797 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] );
1798 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1800 i_part_cost = a->l0.me16x8[i].cost;
1801 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1802 if( a->l1.me16x8[i].cost < i_part_cost )
1804 i_part_cost = a->l1.me16x8[i].cost;
1805 a->i_mb_partition16x8[i] = D_L1_8x8;
1807 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1809 i_part_cost = i_part_cost_bi;
1810 a->i_mb_partition16x8[i] = D_BI_8x8;
1812 a->i_cost16x8bi += i_part_cost;
1814 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1818 a->i_mb_type16x8 = B_L0_L0
1819 + (a->i_mb_partition16x8[0]>>2) * 3
1820 + (a->i_mb_partition16x8[1]>>2);
1821 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1824 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1826 uint8_t **p_fref[2] =
1827 { h->mb.pic.p_fref[0][a->l0.i_ref],
1828 h->mb.pic.p_fref[1][a->l1.i_ref] };
1829 DECLARE_ALIGNED_8( uint8_t pix[2][8*16] );
1830 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1833 h->mb.i_partition = D_8x16;
1834 a->i_cost8x16bi = 0;
1836 for( i = 0; i < 2; i++ )
1839 int i_part_cost_bi = 0;
1840 int stride[2] = {8,8};
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 src[l] = h->mc.get_ref( pix[l], &stride[l], 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 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] );
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 x264_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 if( IS_INTRA(h->mb.i_type) )
2041 switch( h->mb.i_partition )
2044 if( h->mb.i_type == B_BI_BI )
2045 x264_me_refine_bidir_satd( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
2048 for( i=0; i<2; i++ )
2049 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
2050 x264_me_refine_bidir_satd( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
2053 for( i=0; i<2; i++ )
2054 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
2055 x264_me_refine_bidir_satd( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
2058 for( i=0; i<4; i++ )
2059 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
2060 x264_me_refine_bidir_satd( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
2065 static inline void x264_mb_analyse_transform( x264_t *h )
2067 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 && !h->mb.b_lossless )
2069 int i_cost4, i_cost8;
2070 /* Only luma MC is really needed, but the full MC is re-used in macroblock_encode. */
2073 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2074 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2075 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2076 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2078 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2079 h->mb.b_skip_mc = 1;
2083 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2085 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2088 x264_analyse_update_cache( h, a );
2089 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2090 /* FIXME only luma is needed, but the score for comparison already includes chroma */
2091 i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2093 if( *i_rd >= i_rd8 )
2096 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2097 /* prevent a rare division by zero in estimated intra cost */
2104 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2109 /*****************************************************************************
2110 * x264_macroblock_analyse:
2111 *****************************************************************************/
2112 void x264_macroblock_analyse( x264_t *h )
2114 x264_mb_analysis_t analysis;
2115 int i_cost = COST_MAX;
2118 h->mb.i_qp = x264_ratecontrol_qp( h );
2119 if( h->param.rc.i_aq_mode )
2120 x264_adaptive_quant( h );
2122 x264_mb_analyse_init( h, &analysis, h->mb.i_qp );
2124 /*--------------------------- Do the analysis ---------------------------*/
2125 if( h->sh.i_type == SLICE_TYPE_I )
2127 if( analysis.i_mbrd )
2128 x264_mb_cache_fenc_satd( h );
2129 x264_mb_analyse_intra( h, &analysis, COST_MAX );
2130 if( analysis.i_mbrd )
2131 x264_intra_rd( h, &analysis, COST_MAX );
2133 i_cost = analysis.i_satd_i16x16;
2134 h->mb.i_type = I_16x16;
2135 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, h->mb.i_type, I_4x4 );
2136 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, h->mb.i_type, I_8x8 );
2137 if( analysis.i_satd_pcm < i_cost )
2138 h->mb.i_type = I_PCM;
2140 else if( analysis.i_mbrd >= 2 )
2141 x264_intra_rd_refine( h, &analysis );
2143 else if( h->sh.i_type == SLICE_TYPE_P )
2146 int i_intra_cost, i_intra_type;
2148 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2150 /* Fast P_SKIP detection */
2151 analysis.b_try_pskip = 0;
2152 if( h->param.analyse.b_fast_pskip )
2154 if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2155 // FIXME don't need to check this if the reference frame is done
2157 else if( h->param.analyse.i_subpel_refine >= 3 )
2158 analysis.b_try_pskip = 1;
2159 else if( h->mb.i_mb_type_left == P_SKIP ||
2160 h->mb.i_mb_type_top == P_SKIP ||
2161 h->mb.i_mb_type_topleft == P_SKIP ||
2162 h->mb.i_mb_type_topright == P_SKIP )
2163 b_skip = x264_macroblock_probe_pskip( h );
2166 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2170 h->mb.i_type = P_SKIP;
2171 h->mb.i_partition = D_16x16;
2172 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2176 const unsigned int flags = h->param.analyse.inter;
2180 int i_satd_inter, i_satd_intra;
2182 x264_mb_analyse_load_costs( h, &analysis );
2184 x264_mb_analyse_inter_p16x16( h, &analysis );
2186 if( h->mb.i_type == P_SKIP )
2189 if( flags & X264_ANALYSE_PSUB16x16 )
2191 if( h->param.analyse.b_mixed_references )
2192 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2194 x264_mb_analyse_inter_p8x8( h, &analysis );
2197 /* Select best inter mode */
2199 i_partition = D_16x16;
2200 i_cost = analysis.l0.me16x16.cost;
2202 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2203 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2206 i_partition = D_8x8;
2207 i_cost = analysis.l0.i_cost8x8;
2210 if( flags & X264_ANALYSE_PSUB8x8 )
2212 for( i = 0; i < 4; i++ )
2214 x264_mb_analyse_inter_p4x4( h, &analysis, i );
2215 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2217 int i_cost8x8 = analysis.l0.i_cost4x4[i];
2218 h->mb.i_sub_partition[i] = D_L0_4x4;
2220 x264_mb_analyse_inter_p8x4( h, &analysis, i );
2221 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2222 h->mb.i_sub_partition[i], D_L0_8x4 );
2224 x264_mb_analyse_inter_p4x8( h, &analysis, i );
2225 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2226 h->mb.i_sub_partition[i], D_L0_4x8 );
2228 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2230 x264_mb_cache_mv_p8x8( h, &analysis, i );
2232 analysis.l0.i_cost8x8 = i_cost;
2236 /* Now do 16x8/8x16 */
2237 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2238 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2239 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2241 x264_mb_analyse_inter_p16x8( h, &analysis );
2242 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2244 x264_mb_analyse_inter_p8x16( h, &analysis );
2245 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2248 h->mb.i_partition = i_partition;
2251 //FIXME mb_type costs?
2252 if( analysis.i_mbrd )
2256 else if( i_partition == D_16x16 )
2258 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2259 i_cost = analysis.l0.me16x16.cost;
2261 else if( i_partition == D_16x8 )
2263 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2264 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2265 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2267 else if( i_partition == D_8x16 )
2269 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2270 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2271 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2273 else if( i_partition == D_8x8 )
2277 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2279 switch( h->mb.i_sub_partition[i8x8] )
2282 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2283 i_cost += analysis.l0.me8x8[i8x8].cost;
2286 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2287 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2288 i_cost += analysis.l0.me8x4[i8x8][0].cost +
2289 analysis.l0.me8x4[i8x8][1].cost;
2292 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2293 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2294 i_cost += analysis.l0.me4x8[i8x8][0].cost +
2295 analysis.l0.me4x8[i8x8][1].cost;
2299 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2300 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2301 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2302 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2303 i_cost += analysis.l0.me4x4[i8x8][0].cost +
2304 analysis.l0.me4x4[i8x8][1].cost +
2305 analysis.l0.me4x4[i8x8][2].cost +
2306 analysis.l0.me4x4[i8x8][3].cost;
2309 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2315 if( h->mb.b_chroma_me )
2317 x264_mb_analyse_intra_chroma( h, &analysis );
2318 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2319 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2320 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2321 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2324 x264_mb_analyse_intra( h, &analysis, i_cost );
2326 i_satd_inter = i_cost;
2327 i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2328 analysis.i_satd_i8x8,
2329 analysis.i_satd_i4x4 );
2331 if( analysis.i_mbrd )
2333 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2335 i_partition = D_16x16;
2336 i_cost = analysis.l0.me16x16.cost;
2337 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2338 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2339 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2340 h->mb.i_type = i_type;
2341 h->mb.i_partition = i_partition;
2342 if( i_cost < COST_MAX )
2343 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2344 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2347 i_intra_type = I_16x16;
2348 i_intra_cost = analysis.i_satd_i16x16;
2349 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
2350 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
2351 COPY2_IF_LT( i_intra_cost, analysis.i_satd_pcm, i_intra_type, I_PCM );
2352 COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
2354 if( i_intra_cost == COST_MAX )
2355 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
2357 h->mb.i_type = i_type;
2358 h->stat.frame.i_intra_cost += i_intra_cost;
2359 h->stat.frame.i_inter_cost += i_cost;
2360 h->stat.frame.i_mbs_analysed++;
2362 if( analysis.i_mbrd >= 2 && h->mb.i_type != I_PCM )
2364 if( IS_INTRA( h->mb.i_type ) )
2366 x264_intra_rd_refine( h, &analysis );
2368 else if( i_partition == D_16x16 )
2370 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2371 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 );
2373 else if( i_partition == D_16x8 )
2375 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2376 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2377 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2378 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2379 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0, 0 );
2380 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 8, 0 );
2382 else if( i_partition == D_8x16 )
2384 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2385 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2386 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2387 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2388 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0, 0 );
2389 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 4, 0 );
2391 else if( i_partition == D_8x8 )
2394 x264_analyse_update_cache( h, &analysis );
2395 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2397 if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2399 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8*4, 0 );
2401 else if( h->mb.i_sub_partition[i8x8] == D_L0_8x4 )
2403 x264_me_refine_qpel_rd( h, &analysis.l0.me8x4[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
2404 x264_me_refine_qpel_rd( h, &analysis.l0.me8x4[i8x8][1], analysis.i_lambda2, i8x8*4+2, 0 );
2406 else if( h->mb.i_sub_partition[i8x8] == D_L0_4x8 )
2408 x264_me_refine_qpel_rd( h, &analysis.l0.me4x8[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
2409 x264_me_refine_qpel_rd( h, &analysis.l0.me4x8[i8x8][1], analysis.i_lambda2, i8x8*4+1, 0 );
2411 else if( h->mb.i_sub_partition[i8x8] == D_L0_4x4 )
2413 x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
2414 x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][1], analysis.i_lambda2, i8x8*4+1, 0 );
2415 x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][2], analysis.i_lambda2, i8x8*4+2, 0 );
2416 x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][3], analysis.i_lambda2, i8x8*4+3, 0 );
2423 else if( h->sh.i_type == SLICE_TYPE_B )
2425 int i_bskip_cost = COST_MAX;
2428 if( analysis.i_mbrd )
2429 x264_mb_cache_fenc_satd( h );
2431 h->mb.i_type = B_SKIP;
2432 if( h->mb.b_direct_auto_write )
2434 /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2435 for( i = 0; i < 2; i++ )
2438 h->sh.b_direct_spatial_mv_pred ^= 1;
2439 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2440 if( analysis.b_direct_available )
2445 b_skip = x264_macroblock_probe_bskip( h );
2447 h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2454 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2456 if( analysis.b_direct_available )
2458 if( !h->mb.b_direct_auto_write )
2460 if( h->mb.b_lossless )
2462 /* chance of skip is too small to bother */
2464 else if( analysis.i_mbrd )
2466 i_bskip_cost = ssd_mb( h );
2467 /* 6 = minimum cavlc cost of a non-skipped MB */
2468 b_skip = h->mb.b_skip_mc = i_bskip_cost <= ((6 * analysis.i_lambda2 + 128) >> 8);
2470 else if( !h->mb.b_direct_auto_write )
2472 /* Conditioning the probe on neighboring block types
2473 * doesn't seem to help speed or quality. */
2474 b_skip = x264_macroblock_probe_bskip( h );
2480 const unsigned int flags = h->param.analyse.inter;
2483 int i_satd_inter = 0; // shut up uninitialized warning
2484 h->mb.b_skip_mc = 0;
2486 x264_mb_analyse_load_costs( h, &analysis );
2488 /* select best inter mode */
2489 /* direct must be first */
2490 if( analysis.b_direct_available )
2491 x264_mb_analyse_inter_direct( h, &analysis );
2493 x264_mb_analyse_inter_b16x16( h, &analysis );
2496 i_partition = D_16x16;
2497 i_cost = analysis.l0.me16x16.cost;
2498 COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2499 COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2500 COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2502 if( analysis.i_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2504 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2505 if( i_bskip_cost < analysis.i_rd16x16direct &&
2506 i_bskip_cost < analysis.i_rd16x16bi &&
2507 i_bskip_cost < analysis.l0.i_rd16x16 &&
2508 i_bskip_cost < analysis.l1.i_rd16x16 )
2510 h->mb.i_type = B_SKIP;
2511 x264_analyse_update_cache( h, &analysis );
2516 if( flags & X264_ANALYSE_BSUB16x16 )
2518 x264_mb_analyse_inter_b8x8( h, &analysis );
2519 if( analysis.i_cost8x8bi < i_cost )
2522 i_partition = D_8x8;
2523 i_cost = analysis.i_cost8x8bi;
2525 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2526 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2528 x264_mb_analyse_inter_b16x8( h, &analysis );
2529 COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2530 i_type, analysis.i_mb_type16x8,
2531 i_partition, D_16x8 );
2533 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2534 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2536 x264_mb_analyse_inter_b8x16( h, &analysis );
2537 COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2538 i_type, analysis.i_mb_type8x16,
2539 i_partition, D_8x16 );
2544 if( analysis.i_mbrd )
2549 else if( i_partition == D_16x16 )
2551 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2552 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2553 if( i_type == B_L0_L0 )
2555 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2556 i_cost = analysis.l0.me16x16.cost
2557 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2559 else if( i_type == B_L1_L1 )
2561 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2562 i_cost = analysis.l1.me16x16.cost
2563 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2565 else if( i_type == B_BI_BI )
2567 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2568 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2571 else if( i_partition == D_16x8 )
2573 for( i=0; i<2; i++ )
2575 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2576 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2577 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2578 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2581 else if( i_partition == D_8x16 )
2583 for( i=0; i<2; i++ )
2585 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2586 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2587 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2588 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2591 else if( i_partition == D_8x8 )
2593 for( i=0; i<4; i++ )
2596 int i_part_cost_old;
2598 int i_part_type = h->mb.i_sub_partition[i];
2599 int b_bidir = (i_part_type == D_BI_8x8);
2601 if( i_part_type == D_DIRECT_8x8 )
2603 if( x264_mb_partition_listX_table[0][i_part_type] )
2605 m = &analysis.l0.me8x8[i];
2606 i_part_cost_old = m->cost;
2607 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2608 m->cost -= i_type_cost;
2609 x264_me_refine_qpel( h, m );
2611 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2613 if( x264_mb_partition_listX_table[1][i_part_type] )
2615 m = &analysis.l1.me8x8[i];
2616 i_part_cost_old = m->cost;
2617 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2618 m->cost -= i_type_cost;
2619 x264_me_refine_qpel( h, m );
2621 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2623 /* TODO: update mvp? */
2627 if( analysis.i_mbrd )
2629 i_satd_inter = i_cost;
2630 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2632 i_cost = i_bskip_cost;
2633 i_partition = D_16x16;
2634 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2635 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2636 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2637 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2638 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2639 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2640 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2642 h->mb.i_type = i_type;
2643 h->mb.i_partition = i_partition;
2646 x264_mb_analyse_intra( h, &analysis, i_satd_inter );
2648 if( analysis.i_mbrd )
2650 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2651 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2654 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2655 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2656 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2657 COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
2659 h->mb.i_type = i_type;
2660 h->mb.i_partition = i_partition;
2662 if( analysis.i_mbrd >= 2 && IS_INTRA( i_type ) && i_type != I_PCM )
2663 x264_intra_rd_refine( h, &analysis );
2664 if( h->mb.i_subpel_refine >= 5 )
2665 x264_refine_bidir( h, &analysis );
2667 if( analysis.i_mbrd >= 2 && i_type > B_DIRECT && i_type < B_SKIP )
2669 const int i_biweight = h->mb.bipred_weight[analysis.l0.i_ref][analysis.l1.i_ref];
2670 x264_analyse_update_cache( h, &analysis );
2672 if( i_partition == D_16x16 )
2674 if( i_type == B_L0_L0 )
2675 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 );
2676 else if( i_type == B_L1_L1 )
2677 x264_me_refine_qpel_rd( h, &analysis.l1.me16x16, analysis.i_lambda2, 0, 1 );
2678 else if( i_type == B_BI_BI )
2679 x264_me_refine_bidir_rd( h, &analysis.l0.me16x16, &analysis.l1.me16x16, i_biweight, 0, analysis.i_lambda2 );
2681 else if( i_partition == D_16x8 )
2683 for( i = 0; i < 2; i++ )
2685 h->mb.i_sub_partition[i*2] = h->mb.i_sub_partition[i*2+1] = analysis.i_mb_partition16x8[i];
2686 if( analysis.i_mb_partition16x8[i] == D_L0_8x8 )
2687 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[i], analysis.i_lambda2, i*8, 0 );
2688 else if( analysis.i_mb_partition16x8[i] == D_L1_8x8 )
2689 x264_me_refine_qpel_rd( h, &analysis.l1.me16x8[i], analysis.i_lambda2, i*8, 1 );
2690 else if( analysis.i_mb_partition16x8[i] == D_BI_8x8 )
2691 x264_me_refine_bidir_rd( h, &analysis.l0.me16x8[i], &analysis.l1.me16x8[i], i_biweight, i*2, analysis.i_lambda2 );
2694 else if( i_partition == D_8x16 )
2696 for( i = 0; i < 2; i++ )
2698 h->mb.i_sub_partition[i] = h->mb.i_sub_partition[i+2] = analysis.i_mb_partition8x16[i];
2699 if( analysis.i_mb_partition8x16[i] == D_L0_8x8 )
2700 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[i], analysis.i_lambda2, i*4, 0 );
2701 else if( analysis.i_mb_partition8x16[i] == D_L1_8x8 )
2702 x264_me_refine_qpel_rd( h, &analysis.l1.me8x16[i], analysis.i_lambda2, i*4, 1 );
2703 else if( analysis.i_mb_partition8x16[i] == D_BI_8x8 )
2704 x264_me_refine_bidir_rd( h, &analysis.l0.me8x16[i], &analysis.l1.me8x16[i], i_biweight, i, analysis.i_lambda2 );
2707 else if( i_partition == D_8x8 )
2709 for( i = 0; i < 4; i++ )
2711 if( h->mb.i_sub_partition[i] == D_L0_8x8 )
2712 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i], analysis.i_lambda2, i*4, 0 );
2713 else if( h->mb.i_sub_partition[i] == D_L1_8x8 )
2714 x264_me_refine_qpel_rd( h, &analysis.l1.me8x8[i], analysis.i_lambda2, i*4, 1 );
2715 else if( h->mb.i_sub_partition[i] == D_BI_8x8 )
2716 x264_me_refine_bidir_rd( h, &analysis.l0.me8x8[i], &analysis.l1.me8x8[i], i_biweight, i, analysis.i_lambda2 );
2723 x264_analyse_update_cache( h, &analysis );
2725 if( !analysis.i_mbrd )
2726 x264_mb_analyse_transform( h );
2728 h->mb.b_trellis = h->param.analyse.i_trellis;
2729 h->mb.b_noise_reduction = !!h->param.analyse.i_noise_reduction;
2730 if( !IS_SKIP(h->mb.i_type) && h->mb.i_psy_trellis && h->param.analyse.i_trellis == 1 )
2731 x264_psy_trellis_init( h, 0 );
2732 if( h->mb.b_trellis == 1 || h->mb.b_noise_reduction )
2733 h->mb.i_skip_intra = 0;
2736 /*-------------------- Update MB from the analysis ----------------------*/
2737 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2741 switch( h->mb.i_type )
2744 for( i = 0; i < 16; i++ )
2745 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2747 x264_mb_analyse_intra_chroma( h, a );
2750 for( i = 0; i < 4; i++ )
2751 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2753 x264_mb_analyse_intra_chroma( h, a );
2756 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2757 x264_mb_analyse_intra_chroma( h, a );
2764 switch( h->mb.i_partition )
2767 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2768 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2772 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2773 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2774 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv );
2775 x264_macroblock_cache_mv_ptr( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv );
2779 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2780 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2781 x264_macroblock_cache_mv_ptr( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv );
2782 x264_macroblock_cache_mv_ptr( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv );
2786 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2792 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2793 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2794 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2795 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2796 for( i = 0; i < 4; i++ )
2797 x264_mb_cache_mv_p8x8( h, a, i );
2802 h->mb.i_partition = D_16x16;
2803 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2804 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv );
2810 x264_mb_load_mv_direct8x8( h, 0 );
2811 x264_mb_load_mv_direct8x8( h, 1 );
2812 x264_mb_load_mv_direct8x8( h, 2 );
2813 x264_mb_load_mv_direct8x8( h, 3 );
2817 /* optimize: cache might not need to be rewritten */
2818 for( i = 0; i < 4; i++ )
2819 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2822 default: /* the rest of the B types */
2823 switch( h->mb.i_partition )
2826 switch( h->mb.i_type )
2829 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2830 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2832 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2833 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0 );
2834 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0 );
2837 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2838 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0 );
2839 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0 );
2841 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2842 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2845 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2846 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2848 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2849 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2854 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2855 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2858 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2859 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2862 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2868 if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2871 for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2874 int ref = h->mb.cache.ref[l][x264_scan8[0]];
2877 completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2878 if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2880 x264_log( h, X264_LOG_WARNING, "internal error (MV out of thread range)\n");
2881 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2882 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2883 h->mb.cache.mv[l][x264_scan8[15]][0],
2884 h->mb.cache.mv[l][x264_scan8[15]][1] );
2885 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2886 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2887 fprintf(stderr, "completed: %d \n", completed );
2888 x264_log( h, X264_LOG_WARNING, "recovering by using intra mode\n");
2889 x264_mb_analyse_intra( h, a, COST_MAX );
2890 h->mb.i_type = I_16x16;
2891 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2892 x264_mb_analyse_intra_chroma( h, a );
2899 #include "slicetype.c"