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 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
477 uint8_t *p_dstc[2], *p_srcc[2];
479 if( a->i_satd_i8x8chroma < COST_MAX )
482 /* 8x8 prediction selection for chroma */
483 p_dstc[0] = h->mb.pic.p_fdec[1];
484 p_dstc[1] = h->mb.pic.p_fdec[2];
485 p_srcc[0] = h->mb.pic.p_fenc[1];
486 p_srcc[1] = h->mb.pic.p_fenc[2];
488 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
489 a->i_satd_i8x8chroma = COST_MAX;
490 if( i_max == 4 && h->pixf.intra_satd_x3_8x8c && h->pixf.mbcmp[0] == h->pixf.satd[0] )
492 int satdu[4], satdv[4];
493 h->pixf.intra_satd_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
494 h->pixf.intra_satd_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
495 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
496 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
497 satdu[I_PRED_CHROMA_P] =
498 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
499 satdv[I_PRED_CHROMA_P] =
500 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
502 for( i=0; i<i_max; i++ )
504 int i_mode = predict_mode[i];
505 int i_satd = satdu[i_mode] + satdv[i_mode]
506 + a->i_lambda * bs_size_ue(i_mode);
508 a->i_satd_i8x8chroma_dir[i] = i_satd;
509 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
514 for( i=0; i<i_max; i++ )
517 int i_mode = predict_mode[i];
519 /* we do the prediction */
520 h->predict_8x8c[i_mode]( p_dstc[0] );
521 h->predict_8x8c[i_mode]( p_dstc[1] );
523 /* we calculate the cost */
524 i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
525 p_srcc[0], FENC_STRIDE ) +
526 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
527 p_srcc[1], FENC_STRIDE ) +
528 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
530 a->i_satd_i8x8chroma_dir[i] = i_satd;
531 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
535 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
538 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
540 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
541 uint8_t *p_src = h->mb.pic.p_fenc[0];
542 uint8_t *p_dst = h->mb.pic.p_fdec[0];
547 int b_merged_satd = !!h->pixf.intra_mbcmp_x3_16x16;
549 /*---------------- Try all mode and calculate their score ---------------*/
551 /* 16x16 prediction selection */
552 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
554 if( b_merged_satd && i_max == 4 )
556 h->pixf.intra_mbcmp_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
557 h->predict_16x16[I_PRED_16x16_P]( p_dst );
558 a->i_satd_i16x16_dir[I_PRED_16x16_P] =
559 h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
562 int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
563 COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
568 for( i = 0; i < i_max; i++ )
571 int i_mode = predict_mode[i];
572 h->predict_16x16[i_mode]( p_dst );
574 i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
575 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
576 COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
577 a->i_satd_i16x16_dir[i_mode] = i_satd;
581 if( h->sh.i_type == SLICE_TYPE_B )
582 /* cavlc mb type prefix */
583 a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
584 if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
587 /* 8x8 prediction selection */
588 if( flags & X264_ANALYSE_I8x8 )
590 DECLARE_ALIGNED_16( uint8_t edge[33] );
591 x264_pixel_cmp_t sa8d = (h->pixf.mbcmp[0] == h->pixf.satd[0]) ? h->pixf.sa8d[PIXEL_8x8] : h->pixf.mbcmp[PIXEL_8x8];
592 int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
594 b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
596 // FIXME some bias like in i4x4?
597 if( h->sh.i_type == SLICE_TYPE_B )
598 i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
600 for( idx = 0;; idx++ )
604 uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
605 uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
606 int i_best = COST_MAX;
607 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
609 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
610 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
612 if( b_merged_satd && i_max == 9 )
615 h->pixf.intra_sa8d_x3_8x8( p_src_by, edge, satd );
616 satd[i_pred_mode] -= 3 * a->i_lambda;
617 for( i=2; i>=0; i-- )
619 int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
620 COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
627 for( ; i<i_max; i++ )
630 int i_mode = predict_mode[i];
632 h->predict_8x8[i_mode]( p_dst_by, edge );
634 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
635 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
637 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
638 a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
642 if( idx == 3 || i_cost > i_satd_thresh )
645 /* we need to encode this block now (for next ones) */
646 h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
647 x264_mb_encode_i8x8( h, idx, a->i_qp );
649 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
654 a->i_satd_i8x8 = i_cost;
655 if( h->mb.i_skip_intra )
657 h->mc.copy[PIXEL_16x16]( h->mb.pic.i8x8_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
658 if( h->mb.i_skip_intra == 2 )
659 h->mc.memcpy_aligned( h->mb.pic.i8x8_dct_buf, h->dct.luma8x8, sizeof(h->mb.pic.i8x8_dct_buf) );
664 a->i_satd_i8x8 = COST_MAX;
665 i_cost = i_cost * 4/(idx+1);
667 if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 )
671 /* 4x4 prediction selection */
672 if( flags & X264_ANALYSE_I4x4 )
675 int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
676 b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
678 i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
680 i_cost = a->i_lambda * 24; /* from JVT (SATD0) */
681 if( h->sh.i_type == SLICE_TYPE_B )
682 i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];
684 for( idx = 0;; idx++ )
686 uint8_t *p_src_by = p_src + block_idx_xy_fenc[idx];
687 uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
688 int i_best = COST_MAX;
689 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
691 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
693 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
694 /* emulate missing topright samples */
695 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
697 if( b_merged_satd && i_max >= 6 )
700 h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
701 satd[i_pred_mode] -= 3 * a->i_lambda;
702 for( i=2; i>=0; i-- )
703 COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
704 a->i_predict4x4[idx], i );
710 for( ; i<i_max; i++ )
713 int i_mode = predict_mode[i];
715 h->predict_4x4[i_mode]( p_dst_by );
717 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
718 p_src_by, FENC_STRIDE )
719 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
721 COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
725 if( i_cost > i_satd_thresh || idx == 15 )
728 /* we need to encode this block now (for next ones) */
729 h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
730 x264_mb_encode_i4x4( h, idx, a->i_qp );
732 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
736 a->i_satd_i4x4 = i_cost;
737 if( h->mb.i_skip_intra )
739 h->mc.copy[PIXEL_16x16]( h->mb.pic.i4x4_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
740 if( h->mb.i_skip_intra == 2 )
741 h->mc.memcpy_aligned( h->mb.pic.i4x4_dct_buf, h->dct.luma4x4, sizeof(h->mb.pic.i4x4_dct_buf) );
745 a->i_satd_i4x4 = COST_MAX;
749 static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
751 if( a->i_satd_i16x16 <= i_satd_thresh )
753 h->mb.i_type = I_16x16;
754 x264_analyse_update_cache( h, a );
755 a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
758 a->i_satd_i16x16 = COST_MAX;
760 if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
762 h->mb.i_type = I_4x4;
763 x264_analyse_update_cache( h, a );
764 a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
767 a->i_satd_i4x4 = COST_MAX;
769 if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
771 h->mb.i_type = I_8x8;
772 x264_analyse_update_cache( h, a );
773 a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
776 a->i_satd_i8x8 = COST_MAX;
779 static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
781 uint8_t *p_src = h->mb.pic.p_fenc[0];
782 uint8_t *p_dst = h->mb.pic.p_fdec[0];
785 int i_max, i_mode, i_thresh;
786 uint64_t i_satd, i_best;
789 h->mb.i_skip_intra = 0;
791 if( h->mb.i_type == I_16x16 )
793 int old_pred_mode = a->i_predict16x16;
794 i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
795 i_best = a->i_satd_i16x16;
796 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
797 for( i = 0; i < i_max; i++ )
799 int i_mode = predict_mode[i];
800 if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
802 h->mb.i_intra16x16_pred_mode = i_mode;
803 i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
804 COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
807 else if( h->mb.i_type == I_4x4 )
809 uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
811 for( idx = 0; idx < 16; idx++ )
813 uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
816 i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
818 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
820 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
821 /* emulate missing topright samples */
822 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
824 for( i = 0; i < i_max; i++ )
826 i_mode = predict_mode[i];
827 h->predict_4x4[i_mode]( p_dst_by );
828 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
830 if( i_best > i_satd )
832 a->i_predict4x4[idx] = i_mode;
834 pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
835 pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
836 pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
837 pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
838 i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
842 *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
843 *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
844 *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
845 *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
846 h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
848 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
851 else if( h->mb.i_type == I_8x8 )
853 DECLARE_ALIGNED_16( uint8_t edge[33] );
854 for( idx = 0; idx < 4; idx++ )
862 i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
865 i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
869 p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
870 p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
871 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
872 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
874 for( i = 0; i < i_max; i++ )
876 i_mode = predict_mode[i];
877 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
879 h->predict_8x8[i_mode]( p_dst_by, edge );
880 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
882 if( i_best > i_satd )
884 a->i_predict8x8[idx] = i_mode;
887 pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
890 pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
892 i_nnz[j] = h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]];
896 *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
899 p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
901 h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]] = i_nnz[j];
903 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
907 /* RD selection for chroma prediction */
908 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
911 i_thresh = a->i_satd_i8x8chroma * 5/4;
913 for( i = j = 0; i < i_max; i++ )
914 if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
915 predict_mode[i] != a->i_predict8x8chroma )
917 predict_mode[j++] = predict_mode[i];
923 int i_chroma_lambda = x264_lambda2_tab[h->mb.i_chroma_qp];
924 /* the previous thing encoded was x264_intra_rd(), so the pixels and
925 * coefs for the current chroma mode are still around, so we only
926 * have to recount the bits. */
927 i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
928 for( i = 0; i < i_max; i++ )
930 i_mode = predict_mode[i];
931 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
932 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
933 /* if we've already found a mode that needs no residual, then
934 * probably any mode with a residual will be worse.
935 * so avoid dct on the remaining modes to improve speed. */
936 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
937 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode );
939 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
944 #define LOAD_FENC( m, src, xoff, yoff) \
945 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
946 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
947 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
948 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
949 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
951 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
952 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
953 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
954 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
955 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
956 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
957 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
958 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
960 #define REF_COST(list, ref) \
961 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
963 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
967 DECLARE_ALIGNED_4( int16_t mvc[7][2] );
968 int i_halfpel_thresh = INT_MAX;
969 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
971 /* 16x16 Search on all ref frame */
972 m.i_pixel = PIXEL_16x16;
973 m.p_cost_mv = a->p_cost_mv;
974 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
976 a->l0.me16x16.cost = INT_MAX;
977 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
979 const int i_ref_cost = REF_COST( 0, i_ref );
980 i_halfpel_thresh -= i_ref_cost;
981 m.i_ref_cost = i_ref_cost;
984 /* search with ref */
985 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
986 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
987 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
988 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
991 * SSD threshold would probably be better than SATD */
994 && m.cost-m.cost_mv < 300*a->i_lambda
995 && abs(m.mv[0]-h->mb.cache.pskip_mv[0])
996 + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
997 && x264_macroblock_probe_pskip( h ) )
999 h->mb.i_type = P_SKIP;
1000 x264_analyse_update_cache( h, a );
1001 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1005 m.cost += i_ref_cost;
1006 i_halfpel_thresh += i_ref_cost;
1008 if( m.cost < a->l0.me16x16.cost )
1009 h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1011 /* save mv for predicting neighbors */
1012 *(uint32_t*)a->l0.mvc[i_ref][0] =
1013 *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1016 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
1017 assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1019 h->mb.i_type = P_L0;
1020 if( a->b_mbrd && a->l0.me16x16.i_ref == 0
1021 && *(uint32_t*)a->l0.me16x16.mv == *(uint32_t*)h->mb.cache.pskip_mv )
1023 h->mb.i_partition = D_16x16;
1024 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
1025 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1029 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
1033 uint8_t **p_fenc = h->mb.pic.p_fenc;
1034 int i_halfpel_thresh = INT_MAX;
1035 int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
1037 int i_maxref = h->mb.pic.i_fref[0]-1;
1039 h->mb.i_partition = D_8x8;
1041 /* early termination: if 16x16 chose ref 0, then evalute no refs older
1042 * than those used by the neighbors */
1043 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
1044 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
1047 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
1048 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
1049 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
1050 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
1051 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
1052 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
1055 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1056 *(uint32_t*)a->l0.mvc[i_ref][0] = *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy];
1058 for( i = 0; i < 4; i++ )
1060 x264_me_t *l0m = &a->l0.me8x8[i];
1064 m.i_pixel = PIXEL_8x8;
1065 m.p_cost_mv = a->p_cost_mv;
1067 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
1068 l0m->cost = INT_MAX;
1069 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1071 const int i_ref_cost = REF_COST( 0, i_ref );
1072 i_halfpel_thresh -= i_ref_cost;
1073 m.i_ref_cost = i_ref_cost;
1076 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
1077 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
1078 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1079 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
1081 m.cost += i_ref_cost;
1082 i_halfpel_thresh += i_ref_cost;
1083 *(uint32_t*)a->l0.mvc[i_ref][i+1] = *(uint32_t*)m.mv;
1085 if( m.cost < l0m->cost )
1086 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1088 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv );
1089 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1092 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1095 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1096 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1097 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1098 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1101 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1103 const int i_ref = a->l0.me16x16.i_ref;
1104 const int i_ref_cost = REF_COST( 0, i_ref );
1105 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
1106 uint8_t **p_fenc = h->mb.pic.p_fenc;
1108 int16_t (*mvc)[2] = a->l0.mvc[i_ref];
1111 /* XXX Needed for x264_mb_predict_mv */
1112 h->mb.i_partition = D_8x8;
1115 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.me16x16.mv;
1117 for( i = 0; i < 4; i++ )
1119 x264_me_t *m = &a->l0.me8x8[i];
1123 m->i_pixel = PIXEL_8x8;
1124 m->p_cost_mv = a->p_cost_mv;
1125 m->i_ref_cost = i_ref_cost;
1128 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1129 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1130 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1131 x264_me_search( h, m, mvc, i_mvc );
1133 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, m->mv );
1135 *(uint32_t*)mvc[i_mvc] = *(uint32_t*)m->mv;
1139 m->cost += i_ref_cost;
1140 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1143 /* theoretically this should include 4*ref_cost,
1144 * but 3 seems a better approximation of cabac. */
1145 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1146 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
1147 REF_COST( 0, a->l0.me16x16.i_ref );
1148 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1149 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1152 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
1155 uint8_t **p_fenc = h->mb.pic.p_fenc;
1156 DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1159 /* XXX Needed for x264_mb_predict_mv */
1160 h->mb.i_partition = D_16x8;
1162 for( i = 0; i < 2; i++ )
1164 x264_me_t *l0m = &a->l0.me16x8[i];
1165 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1166 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1168 m.i_pixel = PIXEL_16x8;
1169 m.p_cost_mv = a->p_cost_mv;
1171 LOAD_FENC( &m, p_fenc, 0, 8*i );
1172 l0m->cost = INT_MAX;
1173 for( j = 0; j < i_ref8s; j++ )
1175 const int i_ref = ref8[j];
1176 const int i_ref_cost = REF_COST( 0, i_ref );
1177 m.i_ref_cost = i_ref_cost;
1180 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1181 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1182 *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][2*i+1];
1183 *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][2*i+2];
1185 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1186 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1187 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1188 x264_me_search( h, &m, mvc, 3 );
1190 m.cost += i_ref_cost;
1192 if( m.cost < l0m->cost )
1193 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1195 x264_macroblock_cache_mv_ptr( h, 0, 2*i, 4, 2, 0, l0m->mv );
1196 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1199 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1202 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1205 uint8_t **p_fenc = h->mb.pic.p_fenc;
1206 DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1209 /* XXX Needed for x264_mb_predict_mv */
1210 h->mb.i_partition = D_8x16;
1212 for( i = 0; i < 2; i++ )
1214 x264_me_t *l0m = &a->l0.me8x16[i];
1215 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1216 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1218 m.i_pixel = PIXEL_8x16;
1219 m.p_cost_mv = a->p_cost_mv;
1221 LOAD_FENC( &m, p_fenc, 8*i, 0 );
1222 l0m->cost = INT_MAX;
1223 for( j = 0; j < i_ref8s; j++ )
1225 const int i_ref = ref8[j];
1226 const int i_ref_cost = REF_COST( 0, i_ref );
1227 m.i_ref_cost = i_ref_cost;
1230 *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1231 *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][i+1];
1232 *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][i+3];
1234 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1235 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1236 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1237 x264_me_search( h, &m, mvc, 3 );
1239 m.cost += i_ref_cost;
1241 if( m.cost < l0m->cost )
1242 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1244 x264_macroblock_cache_mv_ptr( h, 2*i, 0, 2, 4, 0, l0m->mv );
1245 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1248 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1251 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1253 DECLARE_ALIGNED_8( uint8_t pix1[16*8] );
1254 uint8_t *pix2 = pix1+8;
1255 const int i_stride = h->mb.pic.i_stride[1];
1256 const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1257 const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1259 #define CHROMA4x4MC( width, height, me, x, y ) \
1260 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 ); \
1261 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 );
1263 if( pixel == PIXEL_4x4 )
1265 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1266 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 2,0 );
1267 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 0,2 );
1268 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1270 else if( pixel == PIXEL_8x4 )
1272 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1273 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1277 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1278 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1281 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1282 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1285 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1287 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1288 uint8_t **p_fenc = h->mb.pic.p_fenc;
1289 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1292 /* XXX Needed for x264_mb_predict_mv */
1293 h->mb.i_partition = D_8x8;
1295 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1297 const int idx = 4*i8x8 + i4x4;
1298 const int x4 = block_idx_x[idx];
1299 const int y4 = block_idx_y[idx];
1300 const int i_mvc = (i4x4 == 0);
1302 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1304 m->i_pixel = PIXEL_4x4;
1305 m->p_cost_mv = a->p_cost_mv;
1307 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1308 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1310 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1311 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1313 x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 1, 0, m->mv );
1315 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1316 a->l0.me4x4[i8x8][1].cost +
1317 a->l0.me4x4[i8x8][2].cost +
1318 a->l0.me4x4[i8x8][3].cost +
1319 REF_COST( 0, i_ref ) +
1320 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1321 if( h->mb.b_chroma_me )
1322 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1325 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1327 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1328 uint8_t **p_fenc = h->mb.pic.p_fenc;
1329 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1332 /* XXX Needed for x264_mb_predict_mv */
1333 h->mb.i_partition = D_8x8;
1335 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1337 const int idx = 4*i8x8 + 2*i8x4;
1338 const int x4 = block_idx_x[idx];
1339 const int y4 = block_idx_y[idx];
1340 const int i_mvc = (i8x4 == 0);
1342 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1344 m->i_pixel = PIXEL_8x4;
1345 m->p_cost_mv = a->p_cost_mv;
1347 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1348 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1350 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1351 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1353 x264_macroblock_cache_mv_ptr( h, x4, y4, 2, 1, 0, m->mv );
1355 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1356 REF_COST( 0, i_ref ) +
1357 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1358 if( h->mb.b_chroma_me )
1359 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1362 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1364 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1365 uint8_t **p_fenc = h->mb.pic.p_fenc;
1366 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1369 /* XXX Needed for x264_mb_predict_mv */
1370 h->mb.i_partition = D_8x8;
1372 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1374 const int idx = 4*i8x8 + i4x8;
1375 const int x4 = block_idx_x[idx];
1376 const int y4 = block_idx_y[idx];
1377 const int i_mvc = (i4x8 == 0);
1379 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1381 m->i_pixel = PIXEL_4x8;
1382 m->p_cost_mv = a->p_cost_mv;
1384 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1385 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1387 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1388 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1390 x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 2, 0, m->mv );
1392 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1393 REF_COST( 0, i_ref ) +
1394 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1395 if( h->mb.b_chroma_me )
1396 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1399 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1401 /* Assumes that fdec still contains the results of
1402 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1404 uint8_t **p_fenc = h->mb.pic.p_fenc;
1405 uint8_t **p_fdec = h->mb.pic.p_fdec;
1408 a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1409 for( i = 0; i < 4; i++ )
1411 const int x = (i&1)*8;
1412 const int y = (i>>1)*8;
1413 a->i_cost16x16direct +=
1414 a->i_cost8x8direct[i] =
1415 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1418 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1422 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1424 if( h->param.analyse.b_weighted_bipred ) \
1425 h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1426 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1428 h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1431 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1433 DECLARE_ALIGNED_16( uint8_t pix1[16*16] );
1434 DECLARE_ALIGNED_16( uint8_t pix2[16*16] );
1441 DECLARE_ALIGNED_4( int16_t mvc[8][2] );
1442 int i_halfpel_thresh = INT_MAX;
1443 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1445 /* 16x16 Search on all ref frame */
1446 m.i_pixel = PIXEL_16x16;
1447 m.p_cost_mv = a->p_cost_mv;
1448 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1451 a->l0.me16x16.cost = INT_MAX;
1452 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1454 /* search with ref */
1455 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1456 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1457 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1458 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1461 m.cost += REF_COST( 0, i_ref );
1463 if( m.cost < a->l0.me16x16.cost )
1465 a->l0.i_ref = i_ref;
1466 h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1469 /* save mv for predicting neighbors */
1470 *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1472 /* subtract ref cost, so we don't have to add it for the other MB types */
1473 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1476 i_halfpel_thresh = INT_MAX;
1477 p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1478 a->l1.me16x16.cost = INT_MAX;
1479 for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1481 /* search with ref */
1482 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1483 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1484 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1485 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1488 m.cost += REF_COST( 1, i_ref );
1490 if( m.cost < a->l1.me16x16.cost )
1492 a->l1.i_ref = i_ref;
1493 h->mc.memcpy_aligned( &a->l1.me16x16, &m, sizeof(x264_me_t) );
1496 /* save mv for predicting neighbors */
1497 *(uint32_t*)h->mb.mvr[1][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1499 /* subtract ref cost, so we don't have to add it for the other MB types */
1500 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1502 /* Set global ref, needed for other modes? */
1503 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1504 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1506 /* get cost of BI mode */
1507 weight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1508 if ( (*(uint32_t*)a->l0.me16x16.mv & 0x10001) == 0 )
1510 /* l0 reference is halfpel, so get_ref on it will make it faster */
1512 h->mc.get_ref( pix2, &stride2,
1513 h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1514 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1516 h->mc.mc_luma( pix1, 16,
1517 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1518 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1520 weight = 64 - weight;
1524 /* if l0 was qpel, we'll use get_ref on l1 instead */
1525 h->mc.mc_luma( pix1, 16,
1526 h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1527 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1530 h->mc.get_ref( pix2, &stride2,
1531 h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1532 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1536 if( h->param.analyse.b_weighted_bipred )
1537 h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2, weight );
1539 h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1541 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix1, 16 )
1542 + REF_COST( 0, a->l0.i_ref )
1543 + REF_COST( 1, a->l1.i_ref )
1544 + a->l0.me16x16.cost_mv
1545 + a->l1.me16x16.cost_mv;
1548 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1549 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1550 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1553 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1555 const int x = 2*(i%2);
1556 const int y = 2*(i/2);
1558 switch( h->mb.i_sub_partition[i] )
1561 x264_macroblock_cache_mv_ptr( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv );
1564 x264_macroblock_cache_mv_ptr( h, x, y+0, 2, 1, 0, a->l0.me8x4[i][0].mv );
1565 x264_macroblock_cache_mv_ptr( h, x, y+1, 2, 1, 0, a->l0.me8x4[i][1].mv );
1568 x264_macroblock_cache_mv_ptr( h, x+0, y, 1, 2, 0, a->l0.me4x8[i][0].mv );
1569 x264_macroblock_cache_mv_ptr( h, x+1, y, 1, 2, 0, a->l0.me4x8[i][1].mv );
1572 x264_macroblock_cache_mv_ptr( h, x+0, y+0, 1, 1, 0, a->l0.me4x4[i][0].mv );
1573 x264_macroblock_cache_mv_ptr( h, x+1, y+0, 1, 1, 0, a->l0.me4x4[i][1].mv );
1574 x264_macroblock_cache_mv_ptr( h, x+0, y+1, 1, 1, 0, a->l0.me4x4[i][2].mv );
1575 x264_macroblock_cache_mv_ptr( h, x+1, y+1, 1, 1, 0, a->l0.me4x4[i][3].mv );
1578 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1583 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1584 if( x264_mb_partition_listX_table[0][part] ) \
1586 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1587 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 0, me0.mv ); \
1591 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1592 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0 ); \
1594 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0 ); \
1596 if( x264_mb_partition_listX_table[1][part] ) \
1598 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1599 x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 1, me1.mv ); \
1603 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1604 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0 ); \
1606 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0 ); \
1609 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1613 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1615 x264_mb_load_mv_direct8x8( h, i );
1618 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0 );
1619 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0 );
1620 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1625 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1628 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1630 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1632 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1634 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1638 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1640 uint8_t **p_fref[2] =
1641 { h->mb.pic.p_fref[0][a->l0.i_ref],
1642 h->mb.pic.p_fref[1][a->l1.i_ref] };
1643 DECLARE_ALIGNED_8( uint8_t pix[2][8*8] );
1646 /* XXX Needed for x264_mb_predict_mv */
1647 h->mb.i_partition = D_8x8;
1651 for( i = 0; i < 4; i++ )
1656 int i_part_cost_bi = 0;
1658 for( l = 0; l < 2; l++ )
1660 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1661 x264_me_t *m = &lX->me8x8[i];
1663 m->i_pixel = PIXEL_8x8;
1664 m->p_cost_mv = a->p_cost_mv;
1666 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1667 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1669 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1670 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1672 x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, l, m->mv );
1675 h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
1676 m->mv[0], m->mv[1], 8, 8 );
1677 i_part_cost_bi += m->cost_mv;
1678 /* FIXME: ref cost */
1681 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1682 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1683 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1684 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1685 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1687 i_part_cost = a->l0.me8x8[i].cost;
1688 h->mb.i_sub_partition[i] = D_L0_8x8;
1689 COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1690 COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1691 COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1692 a->i_cost8x8bi += i_part_cost;
1694 /* XXX Needed for x264_mb_predict_mv */
1695 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1699 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1702 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1704 uint8_t **p_fref[2] =
1705 { h->mb.pic.p_fref[0][a->l0.i_ref],
1706 h->mb.pic.p_fref[1][a->l1.i_ref] };
1707 DECLARE_ALIGNED_16( uint8_t pix[2][16*8] );
1708 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1711 h->mb.i_partition = D_16x8;
1712 a->i_cost16x8bi = 0;
1714 for( i = 0; i < 2; i++ )
1717 int i_part_cost_bi = 0;
1719 /* TODO: check only the list(s) that were used in b8x8? */
1720 for( l = 0; l < 2; l++ )
1722 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1723 x264_me_t *m = &lX->me16x8[i];
1725 m->i_pixel = PIXEL_16x8;
1726 m->p_cost_mv = a->p_cost_mv;
1728 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1729 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1731 *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[2*i].mv;
1732 *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[2*i+1].mv;
1734 x264_mb_predict_mv( h, l, 8*i, 2, m->mvp );
1735 x264_me_search( h, m, mvc, 2 );
1738 h->mc.mc_luma( pix[l], 16, m->p_fref, m->i_stride[0],
1739 m->mv[0], m->mv[1], 16, 8 );
1740 /* FIXME: ref cost */
1741 i_part_cost_bi += m->cost_mv;
1744 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1745 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1747 i_part_cost = a->l0.me16x8[i].cost;
1748 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1749 if( a->l1.me16x8[i].cost < i_part_cost )
1751 i_part_cost = a->l1.me16x8[i].cost;
1752 a->i_mb_partition16x8[i] = D_L1_8x8;
1754 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1756 i_part_cost = i_part_cost_bi;
1757 a->i_mb_partition16x8[i] = D_BI_8x8;
1759 a->i_cost16x8bi += i_part_cost;
1761 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1765 a->i_mb_type16x8 = B_L0_L0
1766 + (a->i_mb_partition16x8[0]>>2) * 3
1767 + (a->i_mb_partition16x8[1]>>2);
1768 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1771 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1773 uint8_t **p_fref[2] =
1774 { h->mb.pic.p_fref[0][a->l0.i_ref],
1775 h->mb.pic.p_fref[1][a->l1.i_ref] };
1776 DECLARE_ALIGNED_8( uint8_t pix[2][8*16] );
1777 DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1780 h->mb.i_partition = D_8x16;
1781 a->i_cost8x16bi = 0;
1783 for( i = 0; i < 2; i++ )
1786 int i_part_cost_bi = 0;
1788 for( l = 0; l < 2; l++ )
1790 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1791 x264_me_t *m = &lX->me8x16[i];
1793 m->i_pixel = PIXEL_8x16;
1794 m->p_cost_mv = a->p_cost_mv;
1796 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1797 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1799 *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[i].mv;
1800 *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[i+2].mv;
1802 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1803 x264_me_search( h, m, mvc, 2 );
1806 h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
1807 m->mv[0], m->mv[1], 8, 16 );
1808 /* FIXME: ref cost */
1809 i_part_cost_bi += m->cost_mv;
1812 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1813 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1815 i_part_cost = a->l0.me8x16[i].cost;
1816 a->i_mb_partition8x16[i] = D_L0_8x8;
1817 if( a->l1.me8x16[i].cost < i_part_cost )
1819 i_part_cost = a->l1.me8x16[i].cost;
1820 a->i_mb_partition8x16[i] = D_L1_8x8;
1822 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1824 i_part_cost = i_part_cost_bi;
1825 a->i_mb_partition8x16[i] = D_BI_8x8;
1827 a->i_cost8x16bi += i_part_cost;
1829 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1833 a->i_mb_type8x16 = B_L0_L0
1834 + (a->i_mb_partition8x16[0]>>2) * 3
1835 + (a->i_mb_partition8x16[1]>>2);
1836 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1839 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1841 int thresh = i_satd * 5/4;
1843 h->mb.i_type = P_L0;
1844 if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1846 h->mb.i_partition = D_16x16;
1847 x264_analyse_update_cache( h, a );
1848 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1850 a->l0.me16x16.cost = a->l0.i_rd16x16;
1852 if( a->l0.i_cost16x8 <= thresh )
1854 h->mb.i_partition = D_16x8;
1855 x264_analyse_update_cache( h, a );
1856 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1859 a->l0.i_cost16x8 = COST_MAX;
1861 if( a->l0.i_cost8x16 <= thresh )
1863 h->mb.i_partition = D_8x16;
1864 x264_analyse_update_cache( h, a );
1865 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1868 a->l0.i_cost8x16 = COST_MAX;
1870 if( a->l0.i_cost8x8 <= thresh )
1872 h->mb.i_type = P_8x8;
1873 h->mb.i_partition = D_8x8;
1874 x264_analyse_update_cache( h, a );
1875 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1877 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1879 /* FIXME: RD per subpartition */
1883 for( i=0; i<4; i++ )
1885 part_bak[i] = h->mb.i_sub_partition[i];
1886 b_sub8x8 |= (part_bak[i] != D_L0_8x8);
1890 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1891 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1892 x264_analyse_update_cache( h, a );
1893 i_cost = x264_rd_cost_mb( h, a->i_lambda2 );
1894 if( a->l0.i_cost8x8 < i_cost )
1896 for( i=0; i<4; i++ )
1897 h->mb.i_sub_partition[i] = part_bak[i];
1900 a->l0.i_cost8x8 = i_cost;
1905 a->l0.i_cost8x8 = COST_MAX;
1908 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
1910 int thresh = i_satd_inter * 17/16;
1912 if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
1914 h->mb.i_type = B_DIRECT;
1915 /* Assumes direct/skip MC is still in fdec */
1916 /* Requires b-rdo to be done before intra analysis */
1917 h->mb.b_skip_mc = 1;
1918 x264_analyse_update_cache( h, a );
1919 a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
1920 h->mb.b_skip_mc = 0;
1923 //FIXME not all the update_cache calls are needed
1924 h->mb.i_partition = D_16x16;
1926 if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
1928 h->mb.i_type = B_L0_L0;
1929 x264_analyse_update_cache( h, a );
1930 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1934 if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
1936 h->mb.i_type = B_L1_L1;
1937 x264_analyse_update_cache( h, a );
1938 a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1942 if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
1944 h->mb.i_type = B_BI_BI;
1945 x264_analyse_update_cache( h, a );
1946 a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1950 if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
1952 h->mb.i_type = B_8x8;
1953 h->mb.i_partition = D_8x8;
1954 x264_analyse_update_cache( h, a );
1955 a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1956 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1960 if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
1962 h->mb.i_type = a->i_mb_type16x8;
1963 h->mb.i_partition = D_16x8;
1964 x264_analyse_update_cache( h, a );
1965 a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1969 if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
1971 h->mb.i_type = a->i_mb_type8x16;
1972 h->mb.i_partition = D_8x16;
1973 x264_analyse_update_cache( h, a );
1974 a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1978 static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
1980 const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1983 switch( h->mb.i_partition )
1986 if( h->mb.i_type == B_BI_BI )
1987 x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
1990 for( i=0; i<2; i++ )
1991 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
1992 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
1995 for( i=0; i<2; i++ )
1996 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
1997 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
2000 for( i=0; i<4; i++ )
2001 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
2002 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
2007 static inline void x264_mb_analyse_transform( x264_t *h )
2009 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2011 int i_cost4, i_cost8;
2012 /* Only luma MC is really needed, but the full MC is re-used in macroblock_encode. */
2015 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2016 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2017 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2018 h->mb.pic.p_fdec[0], FDEC_STRIDE );
2020 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2021 h->mb.b_skip_mc = 1;
2025 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2027 if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2030 x264_analyse_update_cache( h, a );
2031 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2032 /* FIXME only luma is needed, but the score for comparison already includes chroma */
2033 i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2035 if( *i_rd >= i_rd8 )
2038 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2039 /* prevent a rare division by zero in estimated intra cost */
2046 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2051 /*****************************************************************************
2052 * x264_macroblock_analyse:
2053 *****************************************************************************/
2054 void x264_macroblock_analyse( x264_t *h )
2056 x264_mb_analysis_t analysis;
2057 int i_cost = COST_MAX;
2060 h->mb.i_qp = x264_ratecontrol_qp( h );
2061 if( h->param.rc.i_aq_mode )
2062 x264_adaptive_quant( h );
2064 x264_mb_analyse_init( h, &analysis, h->mb.i_qp );
2066 /*--------------------------- Do the analysis ---------------------------*/
2067 if( h->sh.i_type == SLICE_TYPE_I )
2069 x264_mb_analyse_intra( h, &analysis, COST_MAX );
2070 if( analysis.b_mbrd )
2071 x264_intra_rd( h, &analysis, COST_MAX );
2073 i_cost = analysis.i_satd_i16x16;
2074 h->mb.i_type = I_16x16;
2075 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, h->mb.i_type, I_4x4 );
2076 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, h->mb.i_type, I_8x8 );
2077 if( analysis.i_satd_pcm < i_cost )
2078 h->mb.i_type = I_PCM;
2080 else if( h->mb.i_subpel_refine >= 7 )
2081 x264_intra_rd_refine( h, &analysis );
2083 else if( h->sh.i_type == SLICE_TYPE_P )
2086 int i_intra_cost, i_intra_type;
2088 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2090 /* Fast P_SKIP detection */
2091 analysis.b_try_pskip = 0;
2092 if( h->param.analyse.b_fast_pskip )
2094 if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2095 // FIXME don't need to check this if the reference frame is done
2097 else if( h->param.analyse.i_subpel_refine >= 3 )
2098 analysis.b_try_pskip = 1;
2099 else if( h->mb.i_mb_type_left == P_SKIP ||
2100 h->mb.i_mb_type_top == P_SKIP ||
2101 h->mb.i_mb_type_topleft == P_SKIP ||
2102 h->mb.i_mb_type_topright == P_SKIP )
2103 b_skip = x264_macroblock_probe_pskip( h );
2106 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2110 h->mb.i_type = P_SKIP;
2111 h->mb.i_partition = D_16x16;
2112 assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2116 const unsigned int flags = h->param.analyse.inter;
2120 int i_satd_inter, i_satd_intra;
2122 x264_mb_analyse_load_costs( h, &analysis );
2124 x264_mb_analyse_inter_p16x16( h, &analysis );
2126 if( h->mb.i_type == P_SKIP )
2129 if( flags & X264_ANALYSE_PSUB16x16 )
2131 if( h->param.analyse.b_mixed_references )
2132 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2134 x264_mb_analyse_inter_p8x8( h, &analysis );
2137 /* Select best inter mode */
2139 i_partition = D_16x16;
2140 i_cost = analysis.l0.me16x16.cost;
2142 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2143 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2146 i_partition = D_8x8;
2147 i_cost = analysis.l0.i_cost8x8;
2150 if( flags & X264_ANALYSE_PSUB8x8 )
2152 for( i = 0; i < 4; i++ )
2154 x264_mb_analyse_inter_p4x4( h, &analysis, i );
2155 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2157 int i_cost8x8 = analysis.l0.i_cost4x4[i];
2158 h->mb.i_sub_partition[i] = D_L0_4x4;
2160 x264_mb_analyse_inter_p8x4( h, &analysis, i );
2161 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2162 h->mb.i_sub_partition[i], D_L0_8x4 );
2164 x264_mb_analyse_inter_p4x8( h, &analysis, i );
2165 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2166 h->mb.i_sub_partition[i], D_L0_4x8 );
2168 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2170 x264_mb_cache_mv_p8x8( h, &analysis, i );
2172 analysis.l0.i_cost8x8 = i_cost;
2176 /* Now do 16x8/8x16 */
2177 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2178 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2179 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2181 x264_mb_analyse_inter_p16x8( h, &analysis );
2182 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2184 x264_mb_analyse_inter_p8x16( h, &analysis );
2185 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2188 h->mb.i_partition = i_partition;
2191 //FIXME mb_type costs?
2192 if( analysis.b_mbrd )
2196 else if( i_partition == D_16x16 )
2198 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2199 i_cost = analysis.l0.me16x16.cost;
2201 else if( i_partition == D_16x8 )
2203 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2204 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2205 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2207 else if( i_partition == D_8x16 )
2209 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2210 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2211 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2213 else if( i_partition == D_8x8 )
2217 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2219 switch( h->mb.i_sub_partition[i8x8] )
2222 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2223 i_cost += analysis.l0.me8x8[i8x8].cost;
2226 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2227 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2228 i_cost += analysis.l0.me8x4[i8x8][0].cost +
2229 analysis.l0.me8x4[i8x8][1].cost;
2232 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2233 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2234 i_cost += analysis.l0.me4x8[i8x8][0].cost +
2235 analysis.l0.me4x8[i8x8][1].cost;
2239 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2240 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2241 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2242 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2243 i_cost += analysis.l0.me4x4[i8x8][0].cost +
2244 analysis.l0.me4x4[i8x8][1].cost +
2245 analysis.l0.me4x4[i8x8][2].cost +
2246 analysis.l0.me4x4[i8x8][3].cost;
2249 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2255 if( h->mb.b_chroma_me )
2257 x264_mb_analyse_intra_chroma( h, &analysis );
2258 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2259 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2260 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2261 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2264 x264_mb_analyse_intra( h, &analysis, i_cost );
2266 i_satd_inter = i_cost;
2267 i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2268 analysis.i_satd_i8x8,
2269 analysis.i_satd_i4x4 );
2271 if( analysis.b_mbrd )
2273 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2275 i_partition = D_16x16;
2276 i_cost = analysis.l0.me16x16.cost;
2277 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2278 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2279 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2280 h->mb.i_type = i_type;
2281 h->mb.i_partition = i_partition;
2282 if( i_cost < COST_MAX )
2283 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2284 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2287 i_intra_type = I_16x16;
2288 i_intra_cost = analysis.i_satd_i16x16;
2289 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
2290 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
2291 COPY2_IF_LT( i_intra_cost, analysis.i_satd_pcm, i_intra_type, I_PCM );
2292 COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
2294 if( i_intra_cost == COST_MAX )
2295 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
2297 h->mb.i_type = i_type;
2298 h->stat.frame.i_intra_cost += i_intra_cost;
2299 h->stat.frame.i_inter_cost += i_cost;
2300 h->stat.frame.i_mbs_analysed++;
2302 if( h->mb.i_subpel_refine >= 7 && h->mb.i_type != I_PCM )
2304 if( IS_INTRA( h->mb.i_type ) )
2306 x264_intra_rd_refine( h, &analysis );
2308 else if( i_partition == D_16x16 )
2310 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2311 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
2313 else if( i_partition == D_16x8 )
2315 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2316 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2317 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2318 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2319 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
2320 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
2322 else if( i_partition == D_8x16 )
2324 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2325 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2326 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2327 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2328 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
2329 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
2331 else if( i_partition == D_8x8 )
2334 x264_analyse_update_cache( h, &analysis );
2335 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2336 if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2337 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
2342 else if( h->sh.i_type == SLICE_TYPE_B )
2344 int i_bskip_cost = COST_MAX;
2347 h->mb.i_type = B_SKIP;
2348 if( h->mb.b_direct_auto_write )
2350 /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2351 for( i = 0; i < 2; i++ )
2354 h->sh.b_direct_spatial_mv_pred ^= 1;
2355 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2356 if( analysis.b_direct_available )
2361 b_skip = x264_macroblock_probe_bskip( h );
2363 h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2370 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2372 if( analysis.b_direct_available )
2374 if( !h->mb.b_direct_auto_write )
2376 if( h->mb.b_lossless )
2378 /* chance of skip is too small to bother */
2380 else if( analysis.b_mbrd )
2382 i_bskip_cost = ssd_mb( h );
2383 /* 6 = minimum cavlc cost of a non-skipped MB */
2384 b_skip = h->mb.b_skip_mc = i_bskip_cost <= ((6 * analysis.i_lambda2 + 128) >> 8);
2386 else if( !h->mb.b_direct_auto_write )
2388 /* Conditioning the probe on neighboring block types
2389 * doesn't seem to help speed or quality. */
2390 b_skip = x264_macroblock_probe_bskip( h );
2396 const unsigned int flags = h->param.analyse.inter;
2399 int i_satd_inter = 0; // shut up uninitialized warning
2400 h->mb.b_skip_mc = 0;
2402 x264_mb_analyse_load_costs( h, &analysis );
2404 /* select best inter mode */
2405 /* direct must be first */
2406 if( analysis.b_direct_available )
2407 x264_mb_analyse_inter_direct( h, &analysis );
2409 x264_mb_analyse_inter_b16x16( h, &analysis );
2412 i_partition = D_16x16;
2413 i_cost = analysis.l0.me16x16.cost;
2414 COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2415 COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2416 COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2418 if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2420 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2421 if( i_bskip_cost < analysis.i_rd16x16direct &&
2422 i_bskip_cost < analysis.i_rd16x16bi &&
2423 i_bskip_cost < analysis.l0.i_rd16x16 &&
2424 i_bskip_cost < analysis.l1.i_rd16x16 )
2426 h->mb.i_type = B_SKIP;
2427 x264_analyse_update_cache( h, &analysis );
2432 if( flags & X264_ANALYSE_BSUB16x16 )
2434 x264_mb_analyse_inter_b8x8( h, &analysis );
2435 if( analysis.i_cost8x8bi < i_cost )
2438 i_partition = D_8x8;
2439 i_cost = analysis.i_cost8x8bi;
2441 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2442 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2444 x264_mb_analyse_inter_b16x8( h, &analysis );
2445 COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2446 i_type, analysis.i_mb_type16x8,
2447 i_partition, D_16x8 );
2449 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2450 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2452 x264_mb_analyse_inter_b8x16( h, &analysis );
2453 COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2454 i_type, analysis.i_mb_type8x16,
2455 i_partition, D_8x16 );
2460 if( analysis.b_mbrd )
2465 else if( i_partition == D_16x16 )
2467 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2468 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2469 if( i_type == B_L0_L0 )
2471 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2472 i_cost = analysis.l0.me16x16.cost
2473 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2475 else if( i_type == B_L1_L1 )
2477 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2478 i_cost = analysis.l1.me16x16.cost
2479 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2481 else if( i_type == B_BI_BI )
2483 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2484 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2487 else if( i_partition == D_16x8 )
2489 for( i=0; i<2; i++ )
2491 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2492 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2493 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2494 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2497 else if( i_partition == D_8x16 )
2499 for( i=0; i<2; i++ )
2501 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2502 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2503 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2504 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2507 else if( i_partition == D_8x8 )
2509 for( i=0; i<4; i++ )
2512 int i_part_cost_old;
2514 int i_part_type = h->mb.i_sub_partition[i];
2515 int b_bidir = (i_part_type == D_BI_8x8);
2517 if( i_part_type == D_DIRECT_8x8 )
2519 if( x264_mb_partition_listX_table[0][i_part_type] )
2521 m = &analysis.l0.me8x8[i];
2522 i_part_cost_old = m->cost;
2523 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2524 m->cost -= i_type_cost;
2525 x264_me_refine_qpel( h, m );
2527 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2529 if( x264_mb_partition_listX_table[1][i_part_type] )
2531 m = &analysis.l1.me8x8[i];
2532 i_part_cost_old = m->cost;
2533 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2534 m->cost -= i_type_cost;
2535 x264_me_refine_qpel( h, m );
2537 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2539 /* TODO: update mvp? */
2543 if( analysis.b_mbrd )
2545 i_satd_inter = i_cost;
2546 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2548 i_cost = i_bskip_cost;
2549 i_partition = D_16x16;
2550 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2551 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2552 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2553 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2554 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2555 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2556 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2558 h->mb.i_type = i_type;
2559 h->mb.i_partition = i_partition;
2562 x264_mb_analyse_intra( h, &analysis, i_satd_inter );
2564 if( analysis.b_mbrd )
2566 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2567 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2570 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2571 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2572 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2573 COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
2575 h->mb.i_type = i_type;
2576 h->mb.i_partition = i_partition;
2578 if( analysis.b_mbrd && h->mb.i_subpel_refine >= 7 && IS_INTRA( i_type ) && i_type != I_PCM )
2579 x264_intra_rd_refine( h, &analysis );
2580 else if( h->param.analyse.b_bidir_me )
2581 refine_bidir( h, &analysis );
2585 x264_analyse_update_cache( h, &analysis );
2587 if( !analysis.b_mbrd )
2588 x264_mb_analyse_transform( h );
2590 h->mb.b_trellis = h->param.analyse.i_trellis;
2591 h->mb.b_noise_reduction = !!h->param.analyse.i_noise_reduction;
2592 if( h->mb.b_trellis == 1 || h->mb.b_noise_reduction )
2593 h->mb.i_skip_intra = 0;
2596 /*-------------------- Update MB from the analysis ----------------------*/
2597 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2601 switch( h->mb.i_type )
2604 for( i = 0; i < 16; i++ )
2605 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2607 x264_mb_analyse_intra_chroma( h, a );
2610 for( i = 0; i < 4; i++ )
2611 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2613 x264_mb_analyse_intra_chroma( h, a );
2616 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2617 x264_mb_analyse_intra_chroma( h, a );
2624 switch( h->mb.i_partition )
2627 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2628 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2632 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2633 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2634 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv );
2635 x264_macroblock_cache_mv_ptr( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv );
2639 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2640 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2641 x264_macroblock_cache_mv_ptr( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv );
2642 x264_macroblock_cache_mv_ptr( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv );
2646 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2652 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2653 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2654 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2655 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2656 for( i = 0; i < 4; i++ )
2657 x264_mb_cache_mv_p8x8( h, a, i );
2662 h->mb.i_partition = D_16x16;
2663 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2664 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv );
2670 x264_mb_load_mv_direct8x8( h, 0 );
2671 x264_mb_load_mv_direct8x8( h, 1 );
2672 x264_mb_load_mv_direct8x8( h, 2 );
2673 x264_mb_load_mv_direct8x8( h, 3 );
2677 /* optimize: cache might not need to be rewritten */
2678 for( i = 0; i < 4; i++ )
2679 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2682 default: /* the rest of the B types */
2683 switch( h->mb.i_partition )
2686 switch( h->mb.i_type )
2689 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2690 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2692 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2693 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0 );
2694 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0 );
2697 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2698 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0 );
2699 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0 );
2701 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2702 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2705 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2706 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2708 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2709 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2714 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2715 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2718 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2719 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2722 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2728 if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2731 for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2734 int ref = h->mb.cache.ref[l][x264_scan8[0]];
2737 completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2738 if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2740 x264_log( h, X264_LOG_WARNING, "internal error (MV out of thread range)\n");
2741 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2742 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2743 h->mb.cache.mv[l][x264_scan8[15]][0],
2744 h->mb.cache.mv[l][x264_scan8[15]][1] );
2745 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2746 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2747 fprintf(stderr, "completed: %d \n", completed );
2748 x264_log( h, X264_LOG_WARNING, "recovering by using intra mode\n");
2749 x264_mb_analyse_intra( h, a, COST_MAX );
2750 h->mb.i_type = I_16x16;
2751 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2752 x264_mb_analyse_intra_chroma( h, a );
2759 #include "slicetype.c"