1 /*****************************************************************************
2 * analyse.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003 x264 project
5 * $Id: analyse.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Loren Merritt <lorenm@u.washington.edu>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
30 #include "common/common.h"
31 #include "macroblock.h"
33 #include "ratecontrol.h"
46 int mvc[32][5][2]; /* [ref][0] is 16x16 mv,
47 [ref][1..4] are 8x8 mv from partition [0..3] */
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];
100 int i_satd_i8x8chroma;
101 int i_satd_i8x8chroma_dir[4];
102 int i_predict8x8chroma;
104 /* II: Inter part P/B frame */
105 x264_mb_analysis_list_t l0;
106 x264_mb_analysis_list_t l1;
108 int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
109 int i_cost16x16direct;
111 int i_cost8x8direct[4];
120 int i_mb_partition16x8[2]; /* mb_partition_e */
121 int i_mb_partition8x16[2];
122 int i_mb_type16x8; /* mb_class_e */
125 int b_direct_available;
127 } x264_mb_analysis_t;
129 /* lambda = pow(2,qp/6-2) */
130 static const int i_qp0_cost_table[52] = {
131 1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
132 1, 1, 1, 1, /* 8-11 */
133 1, 1, 1, 1, 2, 2, 2, 2, /* 12-19 */
134 3, 3, 3, 4, 4, 4, 5, 6, /* 20-27 */
135 6, 7, 8, 9,10,11,13,14, /* 28-35 */
136 16,18,20,23,25,29,32,36, /* 36-43 */
137 40,45,51,57,64,72,81,91 /* 44-51 */
140 /* pow(lambda,2) * .9 */
141 static const int i_qp0_cost2_table[52] = {
142 1, 1, 1, 1, 1, 1, /* 0-5 */
143 1, 1, 1, 1, 1, 1, /* 6-11 */
144 1, 1, 1, 2, 2, 3, /* 12-17 */
145 4, 5, 6, 7, 9, 11, /* 18-23 */
146 14, 18, 23, 29, 36, 46, /* 24-29 */
147 58, 73, 91, 115, 145, 183, /* 30-35 */
148 230, 290, 366, 461, 581, 731, /* 36-41 */
149 922,1161,1463,1843,2322,2926, /* 42-47 */
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 /* initialize an array of lambda*nbits for all possible mvs */
170 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
172 static int16_t *p_cost_mv[52];
174 if( !p_cost_mv[a->i_qp] )
176 /* could be faster, but isn't called many times */
177 /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
179 p_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) );
180 p_cost_mv[a->i_qp] += 2*4*2048;
181 for( i = 0; i <= 2*4*2048; i++ )
183 p_cost_mv[a->i_qp][-i] =
184 p_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i );
188 a->p_cost_mv = p_cost_mv[a->i_qp];
191 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
193 memset( a, 0, sizeof( x264_mb_analysis_t ) );
195 /* conduct the analysis using this lamda and QP */
196 a->i_qp = h->mb.i_qp = i_qp;
197 h->mb.i_chroma_qp = i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )];
198 a->i_lambda = i_qp0_cost_table[i_qp];
199 a->i_lambda2 = i_qp0_cost2_table[i_qp];
200 a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
201 ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
203 h->mb.i_me_method = h->param.analyse.i_me_method;
204 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
205 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
206 && h->mb.i_subpel_refine >= 5;
207 h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
208 h->mb.b_transform_8x8 = 0;
209 h->mb.b_noise_reduction = 0;
215 a->i_satd_i8x8chroma = COST_MAX;
219 /* II: Inter part P/B frame */
220 if( h->sh.i_type != SLICE_TYPE_I )
223 int i_fmv_range = h->param.analyse.i_mv_range - 16;
225 /* Calculate max allowed MV range */
226 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range )
227 h->mb.mv_min[0] = 4*( -16*h->mb.i_mb_x - 24 );
228 h->mb.mv_max[0] = 4*( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 24 );
229 h->mb.mv_min_fpel[0] = CLIP_FMV( -16*h->mb.i_mb_x - 8 );
230 h->mb.mv_max_fpel[0] = CLIP_FMV( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 8 );
231 h->mb.mv_min_spel[0] = 4*( h->mb.mv_min_fpel[0] - 16 );
232 h->mb.mv_max_spel[0] = 4*( h->mb.mv_max_fpel[0] + 16 );
233 if( h->mb.i_mb_x == 0)
235 int mb_y = h->mb.i_mb_y >> h->sh.b_mbaff;
236 int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff;
237 h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
238 h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
239 h->mb.mv_min_fpel[1] = CLIP_FMV( -16*mb_y - 8 );
240 h->mb.mv_max_fpel[1] = CLIP_FMV( 16*( mb_height - mb_y - 1 ) + 8 );
241 h->mb.mv_min_spel[1] = 4*( h->mb.mv_min_fpel[1] - 16 );
242 h->mb.mv_max_spel[1] = 4*( h->mb.mv_max_fpel[1] + 16 );
248 a->l0.i_cost8x8 = COST_MAX;
250 for( i = 0; i < 4; i++ )
254 a->l0.i_cost4x8[i] = COST_MAX;
258 a->l0.i_cost8x16 = COST_MAX;
259 if( h->sh.i_type == SLICE_TYPE_B )
263 a->l1.i_cost8x8 = COST_MAX;
265 for( i = 0; i < 4; i++ )
270 a->i_cost8x8direct[i] = COST_MAX;
281 a->i_cost16x16direct =
284 a->i_cost8x16bi = COST_MAX;
287 /* Fast intra decision */
288 if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
290 if( IS_INTRA( h->mb.i_mb_type_left )
291 || IS_INTRA( h->mb.i_mb_type_top )
292 || IS_INTRA( h->mb.i_mb_type_topleft )
293 || IS_INTRA( h->mb.i_mb_type_topright )
294 || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
295 || (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])) )
296 { /* intra is likely */ }
311 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
313 if( i_neighbour & MB_TOPLEFT )
315 /* top and left available */
316 *mode++ = I_PRED_16x16_V;
317 *mode++ = I_PRED_16x16_H;
318 *mode++ = I_PRED_16x16_DC;
319 *mode++ = I_PRED_16x16_P;
322 else if( i_neighbour & MB_LEFT )
325 *mode++ = I_PRED_16x16_DC_LEFT;
326 *mode++ = I_PRED_16x16_H;
329 else if( i_neighbour & MB_TOP )
332 *mode++ = I_PRED_16x16_DC_TOP;
333 *mode++ = I_PRED_16x16_V;
339 *mode = I_PRED_16x16_DC_128;
345 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
347 if( i_neighbour & MB_TOPLEFT )
349 /* top and left available */
350 *mode++ = I_PRED_CHROMA_V;
351 *mode++ = I_PRED_CHROMA_H;
352 *mode++ = I_PRED_CHROMA_DC;
353 *mode++ = I_PRED_CHROMA_P;
356 else if( i_neighbour & MB_LEFT )
359 *mode++ = I_PRED_CHROMA_DC_LEFT;
360 *mode++ = I_PRED_CHROMA_H;
363 else if( i_neighbour & MB_TOP )
366 *mode++ = I_PRED_CHROMA_DC_TOP;
367 *mode++ = I_PRED_CHROMA_V;
373 *mode = I_PRED_CHROMA_DC_128;
379 static void predict_4x4_mode_available( unsigned int i_neighbour,
380 int *mode, int *pi_count )
382 int b_l = i_neighbour & MB_LEFT;
383 int b_t = i_neighbour & MB_TOP;
388 *mode++ = I_PRED_4x4_DC;
389 *mode++ = I_PRED_4x4_H;
390 *mode++ = I_PRED_4x4_V;
391 *mode++ = I_PRED_4x4_DDL;
392 if( i_neighbour & MB_TOPLEFT )
394 *mode++ = I_PRED_4x4_DDR;
395 *mode++ = I_PRED_4x4_VR;
396 *mode++ = I_PRED_4x4_HD;
399 *mode++ = I_PRED_4x4_VL;
400 *mode++ = I_PRED_4x4_HU;
404 *mode++ = I_PRED_4x4_DC_LEFT;
405 *mode++ = I_PRED_4x4_H;
406 *mode++ = I_PRED_4x4_HU;
411 *mode++ = I_PRED_4x4_DC_TOP;
412 *mode++ = I_PRED_4x4_V;
413 *mode++ = I_PRED_4x4_DDL;
414 *mode++ = I_PRED_4x4_VL;
419 *mode++ = I_PRED_4x4_DC_128;
424 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
431 uint8_t *p_dstc[2], *p_srcc[2];
433 if( a->i_satd_i8x8chroma < COST_MAX )
436 /* 8x8 prediction selection for chroma */
437 p_dstc[0] = h->mb.pic.p_fdec[1];
438 p_dstc[1] = h->mb.pic.p_fdec[2];
439 p_srcc[0] = h->mb.pic.p_fenc[1];
440 p_srcc[1] = h->mb.pic.p_fenc[2];
442 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
443 a->i_satd_i8x8chroma = COST_MAX;
444 if( i_max == 4 && h->pixf.intra_satd_x3_8x8c && h->pixf.mbcmp[0] == h->pixf.satd[0] )
446 int satdu[4], satdv[4];
447 h->pixf.intra_satd_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
448 h->pixf.intra_satd_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
449 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
450 h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
451 satdu[I_PRED_CHROMA_P] =
452 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
453 satdv[I_PRED_CHROMA_P] =
454 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
456 for( i=0; i<i_max; i++ )
458 int i_mode = predict_mode[i];
459 int i_satd = satdu[i_mode] + satdv[i_mode]
460 + a->i_lambda * bs_size_ue(i_mode);
462 a->i_satd_i8x8chroma_dir[i] = i_satd;
463 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
468 for( i=0; i<i_max; i++ )
471 int i_mode = predict_mode[i];
473 /* we do the prediction */
474 h->predict_8x8c[i_mode]( p_dstc[0] );
475 h->predict_8x8c[i_mode]( p_dstc[1] );
477 /* we calculate the cost */
478 i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
479 p_srcc[0], FENC_STRIDE ) +
480 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
481 p_srcc[1], FENC_STRIDE ) +
482 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
484 a->i_satd_i8x8chroma_dir[i] = i_satd;
485 COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
489 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
492 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
494 const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
495 uint8_t *p_src = h->mb.pic.p_fenc[0];
496 uint8_t *p_dst = h->mb.pic.p_fdec[0];
501 int b_merged_satd = h->pixf.intra_satd_x3_16x16 && h->pixf.mbcmp[0] == h->pixf.satd[0];
503 /*---------------- Try all mode and calculate their score ---------------*/
505 /* 16x16 prediction selection */
506 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
508 if( b_merged_satd && i_max == 4 )
510 h->pixf.intra_satd_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
511 h->predict_16x16[I_PRED_16x16_P]( p_dst );
512 a->i_satd_i16x16_dir[I_PRED_16x16_P] =
513 h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
516 int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
517 COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
522 for( i = 0; i < i_max; i++ )
525 int i_mode = predict_mode[i];
526 h->predict_16x16[i_mode]( p_dst );
528 i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
529 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
530 COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
531 a->i_satd_i16x16_dir[i_mode] = i_satd;
535 if( h->sh.i_type == SLICE_TYPE_B )
536 /* cavlc mb type prefix */
537 a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
538 if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
541 /* 8x8 prediction selection */
542 if( flags & X264_ANALYSE_I8x8 )
544 DECLARE_ALIGNED( uint8_t, edge[33], 8 );
545 x264_pixel_cmp_t sa8d = (*h->pixf.mbcmp == *h->pixf.sad) ? h->pixf.sad[PIXEL_8x8] : h->pixf.sa8d[PIXEL_8x8];
546 int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
548 b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
550 // FIXME some bias like in i4x4?
551 if( h->sh.i_type == SLICE_TYPE_B )
552 i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
554 for( idx = 0;; idx++ )
558 uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
559 uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
560 int i_best = COST_MAX;
561 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
563 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
564 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
566 if( b_merged_satd && i_max == 9 )
569 h->pixf.intra_sa8d_x3_8x8( p_src_by, edge, satd );
570 if( i_pred_mode < 3 )
571 satd[i_pred_mode] -= 3 * a->i_lambda;
572 for( i=2; i>=0; i-- )
574 int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
575 COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
582 for( ; i<i_max; i++ )
585 int i_mode = predict_mode[i];
587 h->predict_8x8[i_mode]( p_dst_by, edge );
589 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
590 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
592 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
593 a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
597 if( idx == 3 || i_cost > i_satd_thresh )
600 /* we need to encode this block now (for next ones) */
601 h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
602 x264_mb_encode_i8x8( h, idx, a->i_qp );
604 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
608 a->i_satd_i8x8 = i_cost;
611 a->i_satd_i8x8 = COST_MAX;
612 i_cost = i_cost * 4/(idx+1);
614 if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 )
618 /* 4x4 prediction selection */
619 if( flags & X264_ANALYSE_I4x4 )
622 int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
623 b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
625 i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
627 i_cost = a->i_lambda * 24; /* from JVT (SATD0) */
628 if( h->sh.i_type == SLICE_TYPE_B )
629 i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];
631 for( idx = 0;; idx++ )
633 int x = block_idx_x[idx];
634 int y = block_idx_y[idx];
635 uint8_t *p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
636 uint8_t *p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
637 int i_best = COST_MAX;
638 int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
640 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
642 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
643 /* emulate missing topright samples */
644 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
646 if( b_merged_satd && i_max >= 6 )
649 h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
650 if( i_pred_mode < 3 )
651 satd[i_pred_mode] -= 3 * a->i_lambda;
652 for( i=2; i>=0; i-- )
653 COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
654 a->i_predict4x4[idx], i );
660 for( ; i<i_max; i++ )
663 int i_mode = predict_mode[i];
665 h->predict_4x4[i_mode]( p_dst_by );
667 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
668 p_src_by, FENC_STRIDE )
669 + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
671 COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
675 if( i_cost > i_satd_thresh || idx == 15 )
678 /* we need to encode this block now (for next ones) */
679 h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
680 x264_mb_encode_i4x4( h, idx, a->i_qp );
682 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
685 a->i_satd_i4x4 = i_cost;
687 a->i_satd_i4x4 = COST_MAX;
691 static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
693 if( a->i_satd_i16x16 <= i_satd_thresh )
695 h->mb.i_type = I_16x16;
696 x264_analyse_update_cache( h, a );
697 a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
700 a->i_satd_i16x16 = COST_MAX;
702 if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
704 h->mb.i_type = I_4x4;
705 x264_analyse_update_cache( h, a );
706 a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
709 a->i_satd_i4x4 = COST_MAX;
711 if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
713 h->mb.i_type = I_8x8;
714 x264_analyse_update_cache( h, a );
715 a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
718 a->i_satd_i8x8 = COST_MAX;
721 static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
723 uint8_t *p_src = h->mb.pic.p_fenc[0];
724 uint8_t *p_dst = h->mb.pic.p_fdec[0];
727 int i_max, i_satd, i_best, i_mode, i_thresh;
731 if( h->mb.i_type == I_16x16 )
733 int old_pred_mode = a->i_predict16x16;
734 i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
735 i_best = a->i_satd_i16x16;
736 predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
737 for( i = 0; i < i_max; i++ )
739 int i_mode = predict_mode[i];
740 if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
742 h->mb.i_intra16x16_pred_mode = i_mode;
743 i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
744 COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
747 else if( h->mb.i_type == I_4x4 )
749 uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
751 for( idx = 0; idx < 16; idx++ )
757 i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
758 x = block_idx_x[idx];
759 y = block_idx_y[idx];
761 p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
762 p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
763 predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
765 if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
766 /* emulate missing topright samples */
767 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
769 for( i = 0; i < i_max; i++ )
771 i_mode = predict_mode[i];
772 h->predict_4x4[i_mode]( p_dst_by );
773 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
775 if( i_best > i_satd )
777 a->i_predict4x4[idx] = i_mode;
779 pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
780 pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
781 pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
782 pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
783 i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
787 *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
788 *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
789 *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
790 *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
791 h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
793 h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
796 else if( h->mb.i_type == I_8x8 )
798 DECLARE_ALIGNED( uint8_t, edge[33], 8 );
799 for( idx = 0; idx < 4; idx++ )
807 i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
810 i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
814 p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
815 p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
816 predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
817 x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
819 for( i = 0; i < i_max; i++ )
821 i_mode = predict_mode[i];
822 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
824 h->predict_8x8[i_mode]( p_dst_by, edge );
825 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
827 if( i_best > i_satd )
829 a->i_predict8x8[idx] = i_mode;
832 pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
835 pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
837 i_nnz[j] = h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]];
841 *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
844 p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
846 h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]] = i_nnz[j];
848 x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
852 /* RD selection for chroma prediction */
853 predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
856 i_thresh = a->i_satd_i8x8chroma * 5/4;
858 for( i = j = 0; i < i_max; i++ )
859 if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
860 predict_mode[i] != a->i_predict8x8chroma )
862 predict_mode[j++] = predict_mode[i];
868 int i_chroma_lambda = i_qp0_cost2_table[h->mb.i_chroma_qp];
869 /* the previous thing encoded was x264_intra_rd(), so the pixels and
870 * coefs for the current chroma mode are still around, so we only
871 * have to recount the bits. */
872 i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
873 for( i = 0; i < i_max; i++ )
875 i_mode = predict_mode[i];
876 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
877 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
878 /* if we've already found a mode that needs no residual, then
879 * probably any mode with a residual will be worse.
880 * so avoid dct on the remaining modes to improve speed. */
881 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
882 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode );
884 h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
889 #define LOAD_FENC( m, src, xoff, yoff) \
890 (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
891 (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
892 (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
893 (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
894 (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
896 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
897 (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
898 (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
899 (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
900 (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
901 (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
902 (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
903 (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
905 #define REF_COST(list, ref) \
906 (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
908 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
912 int mvc[7][2], i_mvc;
913 int i_halfpel_thresh = INT_MAX;
914 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
916 /* 16x16 Search on all ref frame */
917 m.i_pixel = PIXEL_16x16;
918 m.p_cost_mv = a->p_cost_mv;
919 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
921 a->l0.me16x16.cost = INT_MAX;
922 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
924 const int i_ref_cost = REF_COST( 0, i_ref );
925 i_halfpel_thresh -= i_ref_cost;
926 m.i_ref_cost = i_ref_cost;
929 /* search with ref */
930 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
931 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
932 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
933 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
936 * SSD threshold would probably be better than SATD */
939 && m.cost-m.cost_mv < 300*a->i_lambda
940 && abs(m.mv[0]-h->mb.cache.pskip_mv[0])
941 + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
942 && x264_macroblock_probe_pskip( h ) )
944 h->mb.i_type = P_SKIP;
945 x264_analyse_update_cache( h, a );
949 m.cost += i_ref_cost;
950 i_halfpel_thresh += i_ref_cost;
952 if( m.cost < a->l0.me16x16.cost )
955 /* save mv for predicting neighbors */
956 a->l0.mvc[i_ref][0][0] =
957 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
958 a->l0.mvc[i_ref][0][1] =
959 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
962 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
965 if( a->b_mbrd && a->l0.i_ref == 0
966 && a->l0.me16x16.mv[0] == h->mb.cache.pskip_mv[0]
967 && a->l0.me16x16.mv[1] == h->mb.cache.pskip_mv[1] )
969 h->mb.i_partition = D_16x16;
970 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
971 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
975 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
979 uint8_t **p_fenc = h->mb.pic.p_fenc;
980 int i_halfpel_thresh = INT_MAX;
981 int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
983 int i_maxref = h->mb.pic.i_fref[0]-1;
985 h->mb.i_partition = D_8x8;
987 /* early termination: if 16x16 chose ref 0, then evalute no refs older
988 * than those used by the neighbors */
989 if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
990 h->mb.i_mb_type_top && h->mb.i_mb_type_left )
993 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
994 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
995 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
996 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
997 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
998 i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
1001 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1003 a->l0.mvc[i_ref][0][0] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0];
1004 a->l0.mvc[i_ref][0][1] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1];
1007 for( i = 0; i < 4; i++ )
1009 x264_me_t *l0m = &a->l0.me8x8[i];
1013 m.i_pixel = PIXEL_8x8;
1014 m.p_cost_mv = a->p_cost_mv;
1016 LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
1017 l0m->cost = INT_MAX;
1018 for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1020 const int i_ref_cost = REF_COST( 0, i_ref );
1021 i_halfpel_thresh -= i_ref_cost;
1022 m.i_ref_cost = i_ref_cost;
1025 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
1026 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
1027 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1028 x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
1030 m.cost += i_ref_cost;
1031 i_halfpel_thresh += i_ref_cost;
1032 *(uint64_t*)a->l0.mvc[i_ref][i+1] = *(uint64_t*)m.mv;
1034 if( m.cost < l0m->cost )
1037 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv[0], l0m->mv[1] );
1038 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1041 l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1044 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1045 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1046 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1047 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1050 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1052 const int i_ref = a->l0.me16x16.i_ref;
1053 const int i_ref_cost = REF_COST( 0, i_ref );
1054 uint8_t **p_fref = h->mb.pic.p_fref[0][i_ref];
1055 uint8_t **p_fenc = h->mb.pic.p_fenc;
1057 int (*mvc)[2] = a->l0.mvc[i_ref];
1060 /* XXX Needed for x264_mb_predict_mv */
1061 h->mb.i_partition = D_8x8;
1064 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.me16x16.mv;
1066 for( i = 0; i < 4; i++ )
1068 x264_me_t *m = &a->l0.me8x8[i];
1072 m->i_pixel = PIXEL_8x8;
1073 m->p_cost_mv = a->p_cost_mv;
1074 m->i_ref_cost = i_ref_cost;
1077 LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1078 LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1079 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1080 x264_me_search( h, m, mvc, i_mvc );
1082 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
1084 *(uint64_t*)mvc[i_mvc] = *(uint64_t*)m->mv;
1088 m->cost += i_ref_cost;
1089 m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1092 /* theoretically this should include 4*ref_cost,
1093 * but 3 seems a better approximation of cabac. */
1094 a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1095 a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
1096 REF_COST( 0, a->l0.me16x16.i_ref );
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_p16x8( x264_t *h, x264_mb_analysis_t *a )
1104 uint8_t **p_fenc = h->mb.pic.p_fenc;
1108 /* XXX Needed for x264_mb_predict_mv */
1109 h->mb.i_partition = D_16x8;
1111 for( i = 0; i < 2; i++ )
1113 x264_me_t *l0m = &a->l0.me16x8[i];
1114 const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1115 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1117 m.i_pixel = PIXEL_16x8;
1118 m.p_cost_mv = a->p_cost_mv;
1120 LOAD_FENC( &m, p_fenc, 0, 8*i );
1121 l0m->cost = INT_MAX;
1122 for( j = 0; j < i_ref8s; j++ )
1124 const int i_ref = ref8[j];
1125 const int i_ref_cost = REF_COST( 0, i_ref );
1126 m.i_ref_cost = i_ref_cost;
1129 /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1130 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1131 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][2*i+1];
1132 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][2*i+2];
1134 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1135 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1136 x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1137 x264_me_search( h, &m, mvc, 3 );
1139 m.cost += i_ref_cost;
1141 if( m.cost < l0m->cost )
1144 x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, l0m->mv[0], l0m->mv[1] );
1145 x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1148 a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1151 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1154 uint8_t **p_fenc = h->mb.pic.p_fenc;
1158 /* XXX Needed for x264_mb_predict_mv */
1159 h->mb.i_partition = D_8x16;
1161 for( i = 0; i < 2; i++ )
1163 x264_me_t *l0m = &a->l0.me8x16[i];
1164 const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1165 const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1167 m.i_pixel = PIXEL_8x16;
1168 m.p_cost_mv = a->p_cost_mv;
1170 LOAD_FENC( &m, p_fenc, 8*i, 0 );
1171 l0m->cost = INT_MAX;
1172 for( j = 0; j < i_ref8s; j++ )
1174 const int i_ref = ref8[j];
1175 const int i_ref_cost = REF_COST( 0, i_ref );
1176 m.i_ref_cost = i_ref_cost;
1179 *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1180 *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][i+1];
1181 *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][i+3];
1183 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1184 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1185 x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1186 x264_me_search( h, &m, mvc, 3 );
1188 m.cost += i_ref_cost;
1190 if( m.cost < l0m->cost )
1193 x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, l0m->mv[0], l0m->mv[1] );
1194 x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1197 a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1200 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1202 DECLARE_ALIGNED( uint8_t, pix1[16*8], 8 );
1203 uint8_t *pix2 = pix1+8;
1204 const int i_stride = h->mb.pic.i_stride[1];
1205 const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1206 const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1208 #define CHROMA4x4MC( width, height, me, x, y ) \
1209 h->mc.mc_chroma( &p_fref[4][or+x+y*i_stride], i_stride, &pix1[x+y*16], 16, (me).mv[0], (me).mv[1], width, height ); \
1210 h->mc.mc_chroma( &p_fref[5][or+x+y*i_stride], i_stride, &pix2[x+y*16], 16, (me).mv[0], (me).mv[1], width, height );
1212 if( pixel == PIXEL_4x4 )
1214 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1215 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 0,2 );
1216 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 2,0 );
1217 CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1219 else if( pixel == PIXEL_8x4 )
1221 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1222 CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1226 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1227 CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1230 return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1231 + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1234 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1236 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1237 uint8_t **p_fenc = h->mb.pic.p_fenc;
1238 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1241 /* XXX Needed for x264_mb_predict_mv */
1242 h->mb.i_partition = D_8x8;
1244 for( i4x4 = 0; i4x4 < 4; i4x4++ )
1246 const int idx = 4*i8x8 + i4x4;
1247 const int x4 = block_idx_x[idx];
1248 const int y4 = block_idx_y[idx];
1249 const int i_mvc = (i4x4 == 0);
1251 x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1253 m->i_pixel = PIXEL_4x4;
1254 m->p_cost_mv = a->p_cost_mv;
1256 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1257 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1259 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1260 x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1262 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
1264 a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1265 a->l0.me4x4[i8x8][1].cost +
1266 a->l0.me4x4[i8x8][2].cost +
1267 a->l0.me4x4[i8x8][3].cost +
1268 REF_COST( 0, i_ref ) +
1269 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1270 if( h->mb.b_chroma_me )
1271 a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1274 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1276 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1277 uint8_t **p_fenc = h->mb.pic.p_fenc;
1278 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1281 /* XXX Needed for x264_mb_predict_mv */
1282 h->mb.i_partition = D_8x8;
1284 for( i8x4 = 0; i8x4 < 2; i8x4++ )
1286 const int idx = 4*i8x8 + 2*i8x4;
1287 const int x4 = block_idx_x[idx];
1288 const int y4 = block_idx_y[idx];
1289 const int i_mvc = (i8x4 == 0);
1291 x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1293 m->i_pixel = PIXEL_8x4;
1294 m->p_cost_mv = a->p_cost_mv;
1296 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1297 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1299 x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1300 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1302 x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
1304 a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1305 REF_COST( 0, i_ref ) +
1306 a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1307 if( h->mb.b_chroma_me )
1308 a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1311 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1313 uint8_t **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1314 uint8_t **p_fenc = h->mb.pic.p_fenc;
1315 const int i_ref = a->l0.me8x8[i8x8].i_ref;
1318 /* XXX Needed for x264_mb_predict_mv */
1319 h->mb.i_partition = D_8x8;
1321 for( i4x8 = 0; i4x8 < 2; i4x8++ )
1323 const int idx = 4*i8x8 + i4x8;
1324 const int x4 = block_idx_x[idx];
1325 const int y4 = block_idx_y[idx];
1326 const int i_mvc = (i4x8 == 0);
1328 x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1330 m->i_pixel = PIXEL_4x8;
1331 m->p_cost_mv = a->p_cost_mv;
1333 LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1334 LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1336 x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1337 x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1339 x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
1341 a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1342 REF_COST( 0, i_ref ) +
1343 a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1344 if( h->mb.b_chroma_me )
1345 a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1348 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1350 /* Assumes that fdec still contains the results of
1351 * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1353 uint8_t **p_fenc = h->mb.pic.p_fenc;
1354 uint8_t **p_fdec = h->mb.pic.p_fdec;
1357 a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1358 for( i = 0; i < 4; i++ )
1360 const int x = (i&1)*8;
1361 const int y = (i>>1)*8;
1362 a->i_cost16x16direct +=
1363 a->i_cost8x8direct[i] =
1364 h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1367 a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1371 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1373 if( h->param.analyse.b_weighted_bipred ) \
1374 h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1375 h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1377 h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1380 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1382 uint8_t pix1[16*16], pix2[16*16];
1389 int mvc[8][2], i_mvc;
1390 int i_halfpel_thresh = INT_MAX;
1391 int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1393 /* 16x16 Search on all ref frame */
1394 m.i_pixel = PIXEL_16x16;
1395 m.p_cost_mv = a->p_cost_mv;
1396 LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1399 a->l0.me16x16.cost = INT_MAX;
1400 for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1402 /* search with ref */
1403 LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1404 x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1405 x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1406 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1409 m.cost += REF_COST( 0, i_ref );
1411 if( m.cost < a->l0.me16x16.cost )
1413 a->l0.i_ref = i_ref;
1417 /* save mv for predicting neighbors */
1418 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1419 h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1421 /* subtract ref cost, so we don't have to add it for the other MB types */
1422 a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1425 i_halfpel_thresh = INT_MAX;
1426 p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1427 a->l1.me16x16.cost = INT_MAX;
1428 for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1430 /* search with ref */
1431 LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1432 x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1433 x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1434 x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1437 m.cost += REF_COST( 1, i_ref );
1439 if( m.cost < a->l1.me16x16.cost )
1441 a->l1.i_ref = i_ref;
1445 /* save mv for predicting neighbors */
1446 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1447 h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1449 /* subtract ref cost, so we don't have to add it for the other MB types */
1450 a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1452 /* Set global ref, needed for other modes? */
1453 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1454 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1456 /* get cost of BI mode */
1457 weight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1458 if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
1460 /* l0 reference is halfpel, so get_ref on it will make it faster */
1461 src2 = h->mc.get_ref( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1463 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1465 h->mc.mc_luma( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1467 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1469 weight = 64 - weight;
1473 /* if l0 was qpel, we'll use get_ref on l1 instead */
1474 h->mc.mc_luma( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1476 a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1478 src2 = h->mc.get_ref( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1480 a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1484 if( h->param.analyse.b_weighted_bipred )
1485 h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2, weight );
1487 h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1489 a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix1, 16 )
1490 + REF_COST( 0, a->l0.i_ref )
1491 + REF_COST( 1, a->l1.i_ref )
1492 + a->l0.me16x16.cost_mv
1493 + a->l1.me16x16.cost_mv;
1496 a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1497 a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1498 a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1501 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1503 const int x = 2*(i%2);
1504 const int y = 2*(i/2);
1506 switch( h->mb.i_sub_partition[i] )
1509 x264_macroblock_cache_mv( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv[0], a->l0.me8x8[i].mv[1] );
1512 x264_macroblock_cache_mv( h, x, y+0, 2, 1, 0, a->l0.me8x4[i][0].mv[0], a->l0.me8x4[i][0].mv[1] );
1513 x264_macroblock_cache_mv( h, x, y+1, 2, 1, 0, a->l0.me8x4[i][1].mv[0], a->l0.me8x4[i][1].mv[1] );
1516 x264_macroblock_cache_mv( h, x+0, y, 1, 2, 0, a->l0.me4x8[i][0].mv[0], a->l0.me4x8[i][0].mv[1] );
1517 x264_macroblock_cache_mv( h, x+1, y, 1, 2, 0, a->l0.me4x8[i][1].mv[0], a->l0.me4x8[i][1].mv[1] );
1520 x264_macroblock_cache_mv( h, x+0, y+0, 1, 1, 0, a->l0.me4x4[i][0].mv[0], a->l0.me4x4[i][0].mv[1] );
1521 x264_macroblock_cache_mv( h, x+1, y+0, 1, 1, 0, a->l0.me4x4[i][1].mv[0], a->l0.me4x4[i][1].mv[1] );
1522 x264_macroblock_cache_mv( h, x+0, y+1, 1, 1, 0, a->l0.me4x4[i][2].mv[0], a->l0.me4x4[i][2].mv[1] );
1523 x264_macroblock_cache_mv( h, x+1, y+1, 1, 1, 0, a->l0.me4x4[i][3].mv[0], a->l0.me4x4[i][3].mv[1] );
1526 x264_log( h, X264_LOG_ERROR, "internal error\n" );
1531 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1532 if( x264_mb_partition_listX_table[0][part] ) \
1534 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1535 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
1539 x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1540 x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0, 0 ); \
1542 x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
1544 if( x264_mb_partition_listX_table[1][part] ) \
1546 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1547 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
1551 x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1552 x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0, 0 ); \
1554 x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
1557 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1561 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1563 x264_mb_load_mv_direct8x8( h, i );
1566 x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0, 0 );
1567 x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0, 0 );
1568 x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1573 CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1576 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1578 CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1580 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1582 CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1586 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1588 uint8_t **p_fref[2] =
1589 { h->mb.pic.p_fref[0][a->l0.i_ref],
1590 h->mb.pic.p_fref[1][a->l1.i_ref] };
1591 uint8_t pix[2][8*8];
1594 /* XXX Needed for x264_mb_predict_mv */
1595 h->mb.i_partition = D_8x8;
1599 for( i = 0; i < 4; i++ )
1604 int i_part_cost_bi = 0;
1606 for( l = 0; l < 2; l++ )
1608 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1609 x264_me_t *m = &lX->me8x8[i];
1611 m->i_pixel = PIXEL_8x8;
1612 m->p_cost_mv = a->p_cost_mv;
1614 LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1615 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1617 x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1618 x264_me_search( h, m, &lX->me16x16.mv, 1 );
1620 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
1623 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1624 m->mv[0], m->mv[1], 8, 8 );
1625 i_part_cost_bi += m->cost_mv;
1626 /* FIXME: ref cost */
1629 WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1630 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1631 + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1632 a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1633 a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1635 i_part_cost = a->l0.me8x8[i].cost;
1636 h->mb.i_sub_partition[i] = D_L0_8x8;
1637 COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1638 COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1639 COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1640 a->i_cost8x8bi += i_part_cost;
1642 /* XXX Needed for x264_mb_predict_mv */
1643 x264_mb_cache_mv_b8x8( h, a, i, 0 );
1647 a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1650 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1652 uint8_t **p_fref[2] =
1653 { h->mb.pic.p_fref[0][a->l0.i_ref],
1654 h->mb.pic.p_fref[1][a->l1.i_ref] };
1655 DECLARE_ALIGNED( uint8_t, pix[2][16*8], 16 );
1659 h->mb.i_partition = D_16x8;
1660 a->i_cost16x8bi = 0;
1662 for( i = 0; i < 2; i++ )
1665 int i_part_cost_bi = 0;
1667 /* TODO: check only the list(s) that were used in b8x8? */
1668 for( l = 0; l < 2; l++ )
1670 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1671 x264_me_t *m = &lX->me16x8[i];
1673 m->i_pixel = PIXEL_16x8;
1674 m->p_cost_mv = a->p_cost_mv;
1676 LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1677 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1679 mvc[0][0] = lX->me8x8[2*i].mv[0];
1680 mvc[0][1] = lX->me8x8[2*i].mv[1];
1681 mvc[1][0] = lX->me8x8[2*i+1].mv[0];
1682 mvc[1][1] = lX->me8x8[2*i+1].mv[1];
1684 x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp );
1685 x264_me_search( h, m, mvc, 2 );
1688 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 16,
1689 m->mv[0], m->mv[1], 16, 8 );
1690 /* FIXME: ref cost */
1691 i_part_cost_bi += m->cost_mv;
1694 WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1695 i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1697 i_part_cost = a->l0.me16x8[i].cost;
1698 a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1699 if( a->l1.me16x8[i].cost < i_part_cost )
1701 i_part_cost = a->l1.me16x8[i].cost;
1702 a->i_mb_partition16x8[i] = D_L1_8x8;
1704 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1706 i_part_cost = i_part_cost_bi;
1707 a->i_mb_partition16x8[i] = D_BI_8x8;
1709 a->i_cost16x8bi += i_part_cost;
1711 x264_mb_cache_mv_b16x8( h, a, i, 0 );
1715 a->i_mb_type16x8 = B_L0_L0
1716 + (a->i_mb_partition16x8[0]>>2) * 3
1717 + (a->i_mb_partition16x8[1]>>2);
1718 a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1721 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1723 uint8_t **p_fref[2] =
1724 { h->mb.pic.p_fref[0][a->l0.i_ref],
1725 h->mb.pic.p_fref[1][a->l1.i_ref] };
1726 uint8_t pix[2][8*16];
1730 h->mb.i_partition = D_8x16;
1731 a->i_cost8x16bi = 0;
1733 for( i = 0; i < 2; i++ )
1736 int i_part_cost_bi = 0;
1738 for( l = 0; l < 2; l++ )
1740 x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1741 x264_me_t *m = &lX->me8x16[i];
1743 m->i_pixel = PIXEL_8x16;
1744 m->p_cost_mv = a->p_cost_mv;
1746 LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1747 LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1749 mvc[0][0] = lX->me8x8[i].mv[0];
1750 mvc[0][1] = lX->me8x8[i].mv[1];
1751 mvc[1][0] = lX->me8x8[i+2].mv[0];
1752 mvc[1][1] = lX->me8x8[i+2].mv[1];
1754 x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1755 x264_me_search( h, m, mvc, 2 );
1758 h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1759 m->mv[0], m->mv[1], 8, 16 );
1760 /* FIXME: ref cost */
1761 i_part_cost_bi += m->cost_mv;
1764 WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1765 i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1767 i_part_cost = a->l0.me8x16[i].cost;
1768 a->i_mb_partition8x16[i] = D_L0_8x8;
1769 if( a->l1.me8x16[i].cost < i_part_cost )
1771 i_part_cost = a->l1.me8x16[i].cost;
1772 a->i_mb_partition8x16[i] = D_L1_8x8;
1774 if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1776 i_part_cost = i_part_cost_bi;
1777 a->i_mb_partition8x16[i] = D_BI_8x8;
1779 a->i_cost8x16bi += i_part_cost;
1781 x264_mb_cache_mv_b8x16( h, a, i, 0 );
1785 a->i_mb_type8x16 = B_L0_L0
1786 + (a->i_mb_partition8x16[0]>>2) * 3
1787 + (a->i_mb_partition8x16[1]>>2);
1788 a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1791 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1793 int thresh = i_satd * 5/4;
1795 h->mb.i_type = P_L0;
1796 if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1798 h->mb.i_partition = D_16x16;
1799 x264_analyse_update_cache( h, a );
1800 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1802 a->l0.me16x16.cost = a->l0.i_rd16x16;
1804 if( a->l0.i_cost16x8 <= thresh )
1806 h->mb.i_partition = D_16x8;
1807 x264_analyse_update_cache( h, a );
1808 a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1811 a->l0.i_cost16x8 = COST_MAX;
1813 if( a->l0.i_cost8x16 <= thresh )
1815 h->mb.i_partition = D_8x16;
1816 x264_analyse_update_cache( h, a );
1817 a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1820 a->l0.i_cost8x16 = COST_MAX;
1822 if( a->l0.i_cost8x8 <= thresh )
1824 h->mb.i_type = P_8x8;
1825 x264_analyse_update_cache( h, a );
1826 a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1828 if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1830 /* FIXME: RD per subpartition */
1834 for( i=0; i<4; i++ )
1836 part_bak[i] = h->mb.i_sub_partition[i];
1837 b_sub8x8 |= (part_bak[i] != D_L0_8x8);
1841 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1842 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1843 i_cost = x264_rd_cost_mb( h, a->i_lambda2 );
1844 if( a->l0.i_cost8x8 < i_cost )
1846 for( i=0; i<4; i++ )
1847 h->mb.i_sub_partition[i] = part_bak[i];
1850 a->l0.i_cost8x8 = i_cost;
1855 a->l0.i_cost8x8 = COST_MAX;
1858 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
1860 int thresh = i_satd_inter * 17/16;
1862 if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
1864 h->mb.i_type = B_DIRECT;
1865 x264_analyse_update_cache( h, a );
1866 a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
1869 //FIXME not all the update_cache calls are needed
1870 h->mb.i_partition = D_16x16;
1872 if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
1874 h->mb.i_type = B_L0_L0;
1875 x264_analyse_update_cache( h, a );
1876 a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1880 if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
1882 h->mb.i_type = B_L1_L1;
1883 x264_analyse_update_cache( h, a );
1884 a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1888 if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
1890 h->mb.i_type = B_BI_BI;
1891 x264_analyse_update_cache( h, a );
1892 a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1896 if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
1898 h->mb.i_type = B_8x8;
1899 h->mb.i_partition = D_8x8;
1900 x264_analyse_update_cache( h, a );
1901 a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1902 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1906 if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
1908 h->mb.i_type = a->i_mb_type16x8;
1909 h->mb.i_partition = D_16x8;
1910 x264_analyse_update_cache( h, a );
1911 a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1915 if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
1917 h->mb.i_type = a->i_mb_type8x16;
1918 h->mb.i_partition = D_8x16;
1919 x264_analyse_update_cache( h, a );
1920 a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1924 static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
1926 const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1929 switch( h->mb.i_partition )
1932 if( h->mb.i_type == B_BI_BI )
1933 x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
1936 for( i=0; i<2; i++ )
1937 if( a->i_mb_partition16x8[i] == D_BI_8x8 )
1938 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
1941 for( i=0; i<2; i++ )
1942 if( a->i_mb_partition8x16[i] == D_BI_8x8 )
1943 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
1946 for( i=0; i<4; i++ )
1947 if( h->mb.i_sub_partition[i] == D_BI_8x8 )
1948 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
1953 static inline void x264_mb_analyse_transform( x264_t *h )
1955 h->mb.cache.b_transform_8x8_allowed =
1956 h->pps->b_transform_8x8_mode
1957 && !IS_INTRA( h->mb.i_type ) && x264_mb_transform_8x8_allowed( h );
1959 if( h->mb.cache.b_transform_8x8_allowed && h->param.analyse.b_transform_8x8 )
1961 int i_cost4, i_cost8;
1962 /* FIXME only luma mc is needed */
1965 i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
1966 h->mb.pic.p_fdec[0], FDEC_STRIDE );
1967 i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
1968 h->mb.pic.p_fdec[0], FDEC_STRIDE );
1970 h->mb.b_transform_8x8 = i_cost8 < i_cost4;
1974 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
1976 h->mb.cache.b_transform_8x8_allowed =
1977 h->pps->b_transform_8x8_mode && x264_mb_transform_8x8_allowed( h );
1979 if( h->mb.cache.b_transform_8x8_allowed && h->param.analyse.b_transform_8x8 )
1982 x264_analyse_update_cache( h, a );
1983 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1984 /* FIXME only luma is needed, but the score for comparison already includes chroma */
1985 i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
1987 if( *i_rd >= i_rd8 )
1990 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
1991 /* prevent a rare division by zero in estimated intra cost */
1998 h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2003 /*****************************************************************************
2004 * x264_macroblock_analyse:
2005 *****************************************************************************/
2006 void x264_macroblock_analyse( x264_t *h )
2008 x264_mb_analysis_t analysis;
2009 int i_cost = COST_MAX;
2013 x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
2015 /*--------------------------- Do the analysis ---------------------------*/
2016 if( h->sh.i_type == SLICE_TYPE_I )
2018 x264_mb_analyse_intra( h, &analysis, COST_MAX );
2019 if( analysis.b_mbrd )
2020 x264_intra_rd( h, &analysis, COST_MAX );
2022 i_cost = analysis.i_satd_i16x16;
2023 h->mb.i_type = I_16x16;
2024 if( analysis.i_satd_i4x4 < i_cost )
2026 i_cost = analysis.i_satd_i4x4;
2027 h->mb.i_type = I_4x4;
2029 if( analysis.i_satd_i8x8 < i_cost )
2030 h->mb.i_type = I_8x8;
2032 if( h->mb.i_subpel_refine >= 7 )
2033 x264_intra_rd_refine( h, &analysis );
2035 else if( h->sh.i_type == SLICE_TYPE_P )
2038 int i_intra_cost, i_intra_type;
2040 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2042 /* Fast P_SKIP detection */
2043 analysis.b_try_pskip = 0;
2044 if( h->param.analyse.b_fast_pskip )
2046 if( h->param.analyse.i_subpel_refine >= 3 )
2047 analysis.b_try_pskip = 1;
2048 else if( h->mb.i_mb_type_left == P_SKIP ||
2049 h->mb.i_mb_type_top == P_SKIP ||
2050 h->mb.i_mb_type_topleft == P_SKIP ||
2051 h->mb.i_mb_type_topright == P_SKIP )
2052 b_skip = x264_macroblock_probe_pskip( h );
2055 h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2059 h->mb.i_type = P_SKIP;
2060 h->mb.i_partition = D_16x16;
2064 const unsigned int flags = h->param.analyse.inter;
2068 int i_satd_inter, i_satd_intra;
2070 x264_mb_analyse_load_costs( h, &analysis );
2072 x264_mb_analyse_inter_p16x16( h, &analysis );
2074 if( h->mb.i_type == P_SKIP )
2077 if( flags & X264_ANALYSE_PSUB16x16 )
2079 if( h->param.analyse.b_mixed_references )
2080 x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2082 x264_mb_analyse_inter_p8x8( h, &analysis );
2085 /* Select best inter mode */
2087 i_partition = D_16x16;
2088 i_cost = analysis.l0.me16x16.cost;
2090 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2091 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2094 i_partition = D_8x8;
2095 i_cost = analysis.l0.i_cost8x8;
2098 if( flags & X264_ANALYSE_PSUB8x8 )
2100 for( i = 0; i < 4; i++ )
2102 x264_mb_analyse_inter_p4x4( h, &analysis, i );
2103 if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2105 int i_cost8x8 = analysis.l0.i_cost4x4[i];
2106 h->mb.i_sub_partition[i] = D_L0_4x4;
2108 x264_mb_analyse_inter_p8x4( h, &analysis, i );
2109 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2110 h->mb.i_sub_partition[i], D_L0_8x4 );
2112 x264_mb_analyse_inter_p4x8( h, &analysis, i );
2113 COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2114 h->mb.i_sub_partition[i], D_L0_4x8 );
2116 i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2118 x264_mb_cache_mv_p8x8( h, &analysis, i );
2120 analysis.l0.i_cost8x8 = i_cost;
2124 /* Now do 16x8/8x16 */
2125 i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2126 if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2127 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2129 x264_mb_analyse_inter_p16x8( h, &analysis );
2130 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2132 x264_mb_analyse_inter_p8x16( h, &analysis );
2133 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2136 h->mb.i_partition = i_partition;
2139 //FIXME mb_type costs?
2140 if( analysis.b_mbrd )
2144 else if( i_partition == D_16x16 )
2146 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2147 i_cost = analysis.l0.me16x16.cost;
2149 else if( i_partition == D_16x8 )
2151 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2152 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2153 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2155 else if( i_partition == D_8x16 )
2157 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2158 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2159 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2161 else if( i_partition == D_8x8 )
2165 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2167 switch( h->mb.i_sub_partition[i8x8] )
2170 x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2171 i_cost += analysis.l0.me8x8[i8x8].cost;
2174 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2175 x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2176 i_cost += analysis.l0.me8x4[i8x8][0].cost +
2177 analysis.l0.me8x4[i8x8][1].cost;
2180 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2181 x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2182 i_cost += analysis.l0.me4x8[i8x8][0].cost +
2183 analysis.l0.me4x8[i8x8][1].cost;
2187 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2188 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2189 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2190 x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2191 i_cost += analysis.l0.me4x4[i8x8][0].cost +
2192 analysis.l0.me4x4[i8x8][1].cost +
2193 analysis.l0.me4x4[i8x8][2].cost +
2194 analysis.l0.me4x4[i8x8][3].cost;
2197 x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2203 if( h->mb.b_chroma_me )
2205 x264_mb_analyse_intra_chroma( h, &analysis );
2206 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2207 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2208 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2209 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2212 x264_mb_analyse_intra( h, &analysis, i_cost );
2214 i_satd_inter = i_cost;
2215 i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2216 analysis.i_satd_i8x8,
2217 analysis.i_satd_i4x4 );
2219 if( analysis.b_mbrd )
2221 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2223 i_partition = D_16x16;
2224 i_cost = analysis.l0.me16x16.cost;
2225 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2226 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2227 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2228 h->mb.i_type = i_type;
2229 h->mb.i_partition = i_partition;
2230 if( i_cost < COST_MAX )
2231 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2232 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2235 i_intra_type = I_16x16;
2236 i_intra_cost = analysis.i_satd_i16x16;
2237 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
2238 COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
2239 COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
2241 if( i_intra_cost == COST_MAX )
2242 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
2244 h->mb.i_type = i_type;
2245 h->stat.frame.i_intra_cost += i_intra_cost;
2246 h->stat.frame.i_inter_cost += i_cost;
2248 if( h->mb.i_subpel_refine >= 7 )
2250 if( IS_INTRA( h->mb.i_type ) )
2252 x264_intra_rd_refine( h, &analysis );
2254 else if( i_partition == D_16x16 )
2256 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2257 x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
2259 else if( i_partition == D_16x8 )
2261 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2262 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2263 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
2264 x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
2266 else if( i_partition == D_8x16 )
2268 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2269 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2270 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
2271 x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
2273 else if( i_partition == D_8x8 )
2276 x264_analyse_update_cache( h, &analysis );
2277 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2278 if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2279 x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
2284 else if( h->sh.i_type == SLICE_TYPE_B )
2286 int i_bskip_cost = COST_MAX;
2289 h->mb.i_type = B_SKIP;
2290 if( h->mb.b_direct_auto_write )
2292 /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2293 for( i = 0; i < 2; i++ )
2296 h->sh.b_direct_spatial_mv_pred ^= 1;
2297 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2298 if( analysis.b_direct_available )
2303 b_skip = x264_macroblock_probe_bskip( h );
2305 h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2312 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2314 if( analysis.b_direct_available )
2316 if( !h->mb.b_direct_auto_write )
2318 if( h->mb.b_lossless )
2320 /* chance of skip is too small to bother */
2322 else if( analysis.b_mbrd )
2324 i_bskip_cost = ssd_mb( h );
2326 /* 6 = minimum cavlc cost of a non-skipped MB */
2327 if( i_bskip_cost <= 6 * analysis.i_lambda2 )
2329 h->mb.i_type = B_SKIP;
2330 x264_analyse_update_cache( h, &analysis );
2334 else if( !h->mb.b_direct_auto_write )
2336 /* Conditioning the probe on neighboring block types
2337 * doesn't seem to help speed or quality. */
2338 b_skip = x264_macroblock_probe_bskip( h );
2344 const unsigned int flags = h->param.analyse.inter;
2348 x264_mb_analyse_load_costs( h, &analysis );
2350 /* select best inter mode */
2351 /* direct must be first */
2352 if( analysis.b_direct_available )
2353 x264_mb_analyse_inter_direct( h, &analysis );
2355 x264_mb_analyse_inter_b16x16( h, &analysis );
2358 i_partition = D_16x16;
2359 i_cost = analysis.l0.me16x16.cost;
2360 COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2361 COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2362 COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2364 if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2366 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2367 if( i_bskip_cost < analysis.i_rd16x16direct &&
2368 i_bskip_cost < analysis.i_rd16x16bi &&
2369 i_bskip_cost < analysis.l0.i_rd16x16 &&
2370 i_bskip_cost < analysis.l1.i_rd16x16 )
2372 h->mb.i_type = B_SKIP;
2373 x264_analyse_update_cache( h, &analysis );
2378 if( flags & X264_ANALYSE_BSUB16x16 )
2380 x264_mb_analyse_inter_b8x8( h, &analysis );
2381 if( analysis.i_cost8x8bi < i_cost )
2384 i_partition = D_8x8;
2385 i_cost = analysis.i_cost8x8bi;
2387 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2388 h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2390 x264_mb_analyse_inter_b16x8( h, &analysis );
2391 COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2392 i_type, analysis.i_mb_type16x8,
2393 i_partition, D_16x8 );
2395 if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2396 h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2398 x264_mb_analyse_inter_b8x16( h, &analysis );
2399 COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2400 i_type, analysis.i_mb_type8x16,
2401 i_partition, D_8x16 );
2406 if( analysis.b_mbrd )
2411 else if( i_partition == D_16x16 )
2413 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2414 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2415 if( i_type == B_L0_L0 )
2417 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2418 i_cost = analysis.l0.me16x16.cost
2419 + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2421 else if( i_type == B_L1_L1 )
2423 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2424 i_cost = analysis.l1.me16x16.cost
2425 + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2427 else if( i_type == B_BI_BI )
2429 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2430 x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2433 else if( i_partition == D_16x8 )
2435 for( i=0; i<2; i++ )
2437 if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2438 x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2439 if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2440 x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2443 else if( i_partition == D_8x16 )
2445 for( i=0; i<2; i++ )
2447 if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2448 x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2449 if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2450 x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2453 else if( i_partition == D_8x8 )
2455 for( i=0; i<4; i++ )
2458 int i_part_cost_old;
2460 int i_part_type = h->mb.i_sub_partition[i];
2461 int b_bidir = (i_part_type == D_BI_8x8);
2463 if( i_part_type == D_DIRECT_8x8 )
2465 if( x264_mb_partition_listX_table[0][i_part_type] )
2467 m = &analysis.l0.me8x8[i];
2468 i_part_cost_old = m->cost;
2469 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2470 m->cost -= i_type_cost;
2471 x264_me_refine_qpel( h, m );
2473 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2475 if( x264_mb_partition_listX_table[1][i_part_type] )
2477 m = &analysis.l1.me8x8[i];
2478 i_part_cost_old = m->cost;
2479 i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2480 m->cost -= i_type_cost;
2481 x264_me_refine_qpel( h, m );
2483 analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2485 /* TODO: update mvp? */
2489 x264_mb_analyse_intra( h, &analysis, i_cost );
2491 if( analysis.b_mbrd )
2493 int i_satd_inter = i_cost;
2494 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2496 i_cost = i_bskip_cost;
2497 i_partition = D_16x16;
2498 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2499 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2500 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2501 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2502 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2503 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2504 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2506 h->mb.i_type = i_type;
2507 h->mb.i_partition = i_partition;
2508 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2509 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2512 COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2513 COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2514 COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2516 h->mb.i_type = i_type;
2517 h->mb.i_partition = i_partition;
2519 if( h->param.analyse.b_bidir_me )
2520 refine_bidir( h, &analysis );
2524 x264_analyse_update_cache( h, &analysis );
2526 if( !analysis.b_mbrd )
2527 x264_mb_analyse_transform( h );
2529 h->mb.b_trellis = h->param.analyse.i_trellis;
2530 h->mb.b_noise_reduction = h->param.analyse.i_noise_reduction;
2533 /*-------------------- Update MB from the analysis ----------------------*/
2534 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a )
2538 switch( h->mb.i_type )
2541 for( i = 0; i < 16; i++ )
2542 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2544 x264_mb_analyse_intra_chroma( h, a );
2547 for( i = 0; i < 4; i++ )
2548 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2550 x264_mb_analyse_intra_chroma( h, a );
2553 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2554 x264_mb_analyse_intra_chroma( h, a );
2558 switch( h->mb.i_partition )
2561 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2562 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2566 x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2567 x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2568 x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv[0], a->l0.me16x8[0].mv[1] );
2569 x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv[0], a->l0.me16x8[1].mv[1] );
2573 x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2574 x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2575 x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv[0], a->l0.me8x16[0].mv[1] );
2576 x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv[0], a->l0.me8x16[1].mv[1] );
2580 x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2586 x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2587 x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2588 x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2589 x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2590 for( i = 0; i < 4; i++ )
2591 x264_mb_cache_mv_p8x8( h, a, i );
2596 h->mb.i_partition = D_16x16;
2597 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2598 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv[0],
2599 h->mb.cache.pskip_mv[1] );
2605 x264_mb_load_mv_direct8x8( h, 0 );
2606 x264_mb_load_mv_direct8x8( h, 1 );
2607 x264_mb_load_mv_direct8x8( h, 2 );
2608 x264_mb_load_mv_direct8x8( h, 3 );
2612 /* optimize: cache might not need to be rewritten */
2613 for( i = 0; i < 4; i++ )
2614 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2617 default: /* the rest of the B types */
2618 switch( h->mb.i_partition )
2621 switch( h->mb.i_type )
2624 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2625 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2627 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2628 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0, 0 );
2629 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0, 0 );
2632 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2633 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0, 0 );
2634 x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0, 0 );
2636 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2637 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2640 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2641 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2643 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2644 x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2649 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2650 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2653 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2654 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2657 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2663 #include "slicetype.c"