]> git.sesse.net Git - x264/blob - encoder/analyse.c
move os/compiler specific defines to their own header
[x264] / encoder / analyse.c
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 $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Loren Merritt <lorenm@u.washington.edu>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include <math.h>
26 #include <limits.h>
27 #ifndef _MSC_VER
28 #include <unistd.h>
29 #endif
30
31 #include "common/common.h"
32 #include "macroblock.h"
33 #include "me.h"
34 #include "ratecontrol.h"
35 #include "analyse.h"
36 #include "rdo.c"
37
38 typedef struct
39 {
40     /* 16x16 */
41     int i_ref;
42     int       i_rd16x16;
43     x264_me_t me16x16;
44
45     /* 8x8 */
46     int       i_cost8x8;
47     int       mvc[32][5][2]; /* [ref][0] is 16x16 mv,
48                                 [ref][1..4] are 8x8 mv from partition [0..3] */
49     x264_me_t me8x8[4];
50
51     /* Sub 4x4 */
52     int       i_cost4x4[4]; /* cost per 8x8 partition */
53     x264_me_t me4x4[4][4];
54
55     /* Sub 8x4 */
56     int       i_cost8x4[4]; /* cost per 8x8 partition */
57     x264_me_t me8x4[4][2];
58
59     /* Sub 4x8 */
60     int       i_cost4x8[4]; /* cost per 8x8 partition */
61     x264_me_t me4x8[4][2];
62
63     /* 16x8 */
64     int       i_cost16x8;
65     x264_me_t me16x8[2];
66
67     /* 8x16 */
68     int       i_cost8x16;
69     x264_me_t me8x16[2];
70
71 } x264_mb_analysis_list_t;
72
73 typedef struct
74 {
75     /* conduct the analysis using this lamda and QP */
76     int i_lambda;
77     int i_lambda2;
78     int i_qp;
79     int16_t *p_cost_mv;
80     int b_mbrd;
81
82
83     /* I: Intra part */
84     /* Take some shortcuts in intra search if intra is deemed unlikely */
85     int b_fast_intra;
86     int b_try_pskip;
87
88     /* Luma part */
89     int i_satd_i16x16;
90     int i_satd_i16x16_dir[7];
91     int i_predict16x16;
92
93     int i_satd_i8x8;
94     int i_satd_i8x8_dir[12][4];
95     int i_predict8x8[4];
96
97     int i_satd_i4x4;
98     int i_predict4x4[16];
99
100     /* Chroma part */
101     int i_satd_i8x8chroma;
102     int i_satd_i8x8chroma_dir[4];
103     int i_predict8x8chroma;
104
105     /* II: Inter part P/B frame */
106     x264_mb_analysis_list_t l0;
107     x264_mb_analysis_list_t l1;
108
109     int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
110     int i_cost16x16direct;
111     int i_cost8x8bi;
112     int i_cost8x8direct[4];
113     int i_cost16x8bi;
114     int i_cost8x16bi;
115     int i_rd16x16bi;
116     int i_rd16x16direct;
117     int i_rd16x8bi;
118     int i_rd8x16bi;
119     int i_rd8x8bi;
120
121     int i_mb_partition16x8[2]; /* mb_partition_e */
122     int i_mb_partition8x16[2];
123     int i_mb_type16x8; /* mb_class_e */
124     int i_mb_type8x16;
125
126     int b_direct_available;
127
128 } x264_mb_analysis_t;
129
130 /* lambda = pow(2,qp/6-2) */
131 static const int i_qp0_cost_table[52] = {
132    1, 1, 1, 1, 1, 1, 1, 1,  /*  0-7 */
133    1, 1, 1, 1,              /*  8-11 */
134    1, 1, 1, 1, 2, 2, 2, 2,  /* 12-19 */
135    3, 3, 3, 4, 4, 4, 5, 6,  /* 20-27 */
136    6, 7, 8, 9,10,11,13,14,  /* 28-35 */
137   16,18,20,23,25,29,32,36,  /* 36-43 */
138   40,45,51,57,64,72,81,91   /* 44-51 */
139 };
140
141 /* pow(lambda,2) * .9 */
142 static const int i_qp0_cost2_table[52] = {
143    1,   1,   1,   1,   1,   1, /*  0-5  */
144    1,   1,   1,   1,   1,   1, /*  6-11 */
145    1,   1,   1,   2,   2,   3, /* 12-17 */
146    4,   5,   6,   7,   9,  11, /* 18-23 */
147   14,  18,  23,  29,  36,  46, /* 24-29 */
148   58,  73,  91, 115, 145, 183, /* 30-35 */
149  230, 290, 366, 461, 581, 731, /* 36-41 */
150  922,1161,1463,1843,2322,2926, /* 42-47 */
151 3686,4645,5852,7373
152 };
153
154 /* TODO: calculate CABAC costs */
155 static const int i_mb_b_cost_table[X264_MBTYPE_MAX] = {
156     9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
157 };
158 static const int i_mb_b16x8_cost_table[17] = {
159     0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
160 };
161 static const int i_sub_mb_b_cost_table[13] = {
162     7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
163 };
164 static const int i_sub_mb_p_cost_table[4] = {
165     5, 3, 3, 1
166 };
167
168 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
169
170 /* initialize an array of lambda*nbits for all possible mvs */
171 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
172 {
173     static int16_t *p_cost_mv[52];
174
175     if( !p_cost_mv[a->i_qp] )
176     {
177         /* could be faster, but isn't called many times */
178         /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
179         int i;
180         p_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) );
181         p_cost_mv[a->i_qp] += 2*4*2048;
182         for( i = 0; i <= 2*4*2048; i++ )
183         {
184             p_cost_mv[a->i_qp][-i] =
185             p_cost_mv[a->i_qp][i]  = a->i_lambda * bs_size_se( i );
186         }
187     }
188
189     a->p_cost_mv = p_cost_mv[a->i_qp];
190 }
191
192 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
193 {
194     memset( a, 0, sizeof( x264_mb_analysis_t ) );
195
196     /* conduct the analysis using this lamda and QP */
197     a->i_qp = h->mb.i_qp = i_qp;
198     h->mb.i_chroma_qp = i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )];
199     a->i_lambda = i_qp0_cost_table[i_qp];
200     a->i_lambda2 = i_qp0_cost2_table[i_qp];
201     a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
202                 ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
203
204     h->mb.i_me_method = h->param.analyse.i_me_method;
205     h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
206     h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
207                         && h->mb.i_subpel_refine >= 5;
208     h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
209     h->mb.b_transform_8x8 = 0;
210     h->mb.b_noise_reduction = 0;
211
212     /* I: Intra part */
213     a->i_satd_i16x16 =
214     a->i_satd_i8x8   =
215     a->i_satd_i4x4   =
216     a->i_satd_i8x8chroma = COST_MAX;
217
218     a->b_fast_intra = 0;
219
220     /* II: Inter part P/B frame */
221     if( h->sh.i_type != SLICE_TYPE_I )
222     {
223         int i, j;
224         int i_fmv_range = 4 * h->param.analyse.i_mv_range;
225         // limit motion search to a slightly smaller range than the theoretical limit,
226         // since the search may go a few iterations past its given range
227         int i_fpel_border = 5; // umh unconditional radius
228         int i_spel_border = 8; // 1.5 for subpel_satd, 1.5 for subpel_rd, 2 for bime, round up
229
230         /* Calculate max allowed MV range */
231 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range )
232         h->mb.mv_min[0] = 4*( -16*h->mb.i_mb_x - 24 );
233         h->mb.mv_max[0] = 4*( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 24 );
234         h->mb.mv_min_spel[0] = CLIP_FMV( h->mb.mv_min[0] );
235         h->mb.mv_max_spel[0] = CLIP_FMV( h->mb.mv_max[0] );
236         h->mb.mv_min_fpel[0] = (h->mb.mv_min_spel[0]>>2) + i_fpel_border;
237         h->mb.mv_max_fpel[0] = (h->mb.mv_max_spel[0]>>2) - i_fpel_border;
238         if( h->mb.i_mb_x == 0)
239         {
240             int mb_y = h->mb.i_mb_y >> h->sh.b_mbaff;
241             int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff;
242             int thread_mvy_range = i_fmv_range;
243
244             if( h->param.i_threads > 1 )
245             {
246                 int pix_y = (h->mb.i_mb_y | h->mb.b_interlaced) * 16;
247                 int thresh = pix_y + h->param.analyse.i_mv_range_thread;
248                 for( i = (h->sh.i_type == SLICE_TYPE_B); i >= 0; i-- )
249                 {
250                     x264_frame_t **fref = i ? h->fref1 : h->fref0;
251                     int i_ref = i ? h->i_ref1 : h->i_ref0;
252                     for( j=0; j<i_ref; j++ )
253                     {
254                         x264_frame_cond_wait( fref[j], thresh );
255                         thread_mvy_range = X264_MIN( thread_mvy_range, fref[j]->i_lines_completed - pix_y );
256                     }
257                 }
258                 if( h->param.b_deterministic )
259                     thread_mvy_range = h->param.analyse.i_mv_range_thread;
260                 if( h->mb.b_interlaced )
261                     thread_mvy_range >>= 1;
262             }
263
264             h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
265             h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
266             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 );
267             h->mb.mv_max_spel[1] = CLIP_FMV( h->mb.mv_max[1] );
268             h->mb.mv_max_spel[1] = X264_MIN( h->mb.mv_max_spel[1], thread_mvy_range*4 );
269             h->mb.mv_min_fpel[1] = (h->mb.mv_min_spel[1]>>2) + i_fpel_border;
270             h->mb.mv_max_fpel[1] = (h->mb.mv_max_spel[1]>>2) - i_fpel_border;
271         }
272 #undef CLIP_FMV
273
274         a->l0.me16x16.cost =
275         a->l0.i_rd16x16    =
276         a->l0.i_cost8x8    = COST_MAX;
277
278         for( i = 0; i < 4; i++ )
279         {
280             a->l0.i_cost4x4[i] =
281             a->l0.i_cost8x4[i] =
282             a->l0.i_cost4x8[i] = COST_MAX;
283         }
284
285         a->l0.i_cost16x8   =
286         a->l0.i_cost8x16   = COST_MAX;
287         if( h->sh.i_type == SLICE_TYPE_B )
288         {
289             a->l1.me16x16.cost =
290             a->l1.i_rd16x16    =
291             a->l1.i_cost8x8    = COST_MAX;
292
293             for( i = 0; i < 4; i++ )
294             {
295                 a->l1.i_cost4x4[i] =
296                 a->l1.i_cost8x4[i] =
297                 a->l1.i_cost4x8[i] =
298                 a->i_cost8x8direct[i] = COST_MAX;
299             }
300
301             a->l1.i_cost16x8   =
302             a->l1.i_cost8x16   =
303             a->i_rd16x16bi     =
304             a->i_rd16x16direct =
305             a->i_rd8x8bi       =
306             a->i_rd16x8bi      =
307             a->i_rd8x16bi      =
308             a->i_cost16x16bi   =
309             a->i_cost16x16direct =
310             a->i_cost8x8bi     =
311             a->i_cost16x8bi    =
312             a->i_cost8x16bi    = COST_MAX;
313         }
314
315         /* Fast intra decision */
316         if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
317         {
318             if(   IS_INTRA( h->mb.i_mb_type_left )
319                || IS_INTRA( h->mb.i_mb_type_top )
320                || IS_INTRA( h->mb.i_mb_type_topleft )
321                || IS_INTRA( h->mb.i_mb_type_topright )
322                || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
323                || (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])) )
324             { /* intra is likely */ }
325             else
326             {
327                 a->b_fast_intra = 1;
328             }
329         }
330     }
331 }
332
333
334
335 /*
336  * Handle intra mb
337  */
338 /* Max = 4 */
339 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
340 {
341     if( i_neighbour & MB_TOPLEFT )
342     {
343         /* top and left available */
344         *mode++ = I_PRED_16x16_V;
345         *mode++ = I_PRED_16x16_H;
346         *mode++ = I_PRED_16x16_DC;
347         *mode++ = I_PRED_16x16_P;
348         *pi_count = 4;
349     }
350     else if( i_neighbour & MB_LEFT )
351     {
352         /* left available*/
353         *mode++ = I_PRED_16x16_DC_LEFT;
354         *mode++ = I_PRED_16x16_H;
355         *pi_count = 2;
356     }
357     else if( i_neighbour & MB_TOP )
358     {
359         /* top available*/
360         *mode++ = I_PRED_16x16_DC_TOP;
361         *mode++ = I_PRED_16x16_V;
362         *pi_count = 2;
363     }
364     else
365     {
366         /* none available */
367         *mode = I_PRED_16x16_DC_128;
368         *pi_count = 1;
369     }
370 }
371
372 /* Max = 4 */
373 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
374 {
375     if( i_neighbour & MB_TOPLEFT )
376     {
377         /* top and left available */
378         *mode++ = I_PRED_CHROMA_V;
379         *mode++ = I_PRED_CHROMA_H;
380         *mode++ = I_PRED_CHROMA_DC;
381         *mode++ = I_PRED_CHROMA_P;
382         *pi_count = 4;
383     }
384     else if( i_neighbour & MB_LEFT )
385     {
386         /* left available*/
387         *mode++ = I_PRED_CHROMA_DC_LEFT;
388         *mode++ = I_PRED_CHROMA_H;
389         *pi_count = 2;
390     }
391     else if( i_neighbour & MB_TOP )
392     {
393         /* top available*/
394         *mode++ = I_PRED_CHROMA_DC_TOP;
395         *mode++ = I_PRED_CHROMA_V;
396         *pi_count = 2;
397     }
398     else
399     {
400         /* none available */
401         *mode = I_PRED_CHROMA_DC_128;
402         *pi_count = 1;
403     }
404 }
405
406 /* MAX = 9 */
407 static void predict_4x4_mode_available( unsigned int i_neighbour,
408                                         int *mode, int *pi_count )
409 {
410     int b_l = i_neighbour & MB_LEFT;
411     int b_t = i_neighbour & MB_TOP;
412
413     if( b_l && b_t )
414     {
415         *pi_count = 6;
416         *mode++ = I_PRED_4x4_DC;
417         *mode++ = I_PRED_4x4_H;
418         *mode++ = I_PRED_4x4_V;
419         *mode++ = I_PRED_4x4_DDL;
420         if( i_neighbour & MB_TOPLEFT )
421         {
422             *mode++ = I_PRED_4x4_DDR;
423             *mode++ = I_PRED_4x4_VR;
424             *mode++ = I_PRED_4x4_HD;
425             *pi_count += 3;
426         }
427         *mode++ = I_PRED_4x4_VL;
428         *mode++ = I_PRED_4x4_HU;
429     }
430     else if( b_l )
431     {
432         *mode++ = I_PRED_4x4_DC_LEFT;
433         *mode++ = I_PRED_4x4_H;
434         *mode++ = I_PRED_4x4_HU;
435         *pi_count = 3;
436     }
437     else if( b_t )
438     {
439         *mode++ = I_PRED_4x4_DC_TOP;
440         *mode++ = I_PRED_4x4_V;
441         *mode++ = I_PRED_4x4_DDL;
442         *mode++ = I_PRED_4x4_VL;
443         *pi_count = 4;
444     }
445     else
446     {
447         *mode++ = I_PRED_4x4_DC_128;
448         *pi_count = 1;
449     }
450 }
451
452 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
453 {
454     int i;
455
456     int i_max;
457     int predict_mode[4];
458
459     uint8_t *p_dstc[2], *p_srcc[2];
460
461     if( a->i_satd_i8x8chroma < COST_MAX )
462         return;
463
464     /* 8x8 prediction selection for chroma */
465     p_dstc[0] = h->mb.pic.p_fdec[1];
466     p_dstc[1] = h->mb.pic.p_fdec[2];
467     p_srcc[0] = h->mb.pic.p_fenc[1];
468     p_srcc[1] = h->mb.pic.p_fenc[2];
469
470     predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
471     a->i_satd_i8x8chroma = COST_MAX;
472     if( i_max == 4 && h->pixf.intra_satd_x3_8x8c && h->pixf.mbcmp[0] == h->pixf.satd[0] )
473     {
474         int satdu[4], satdv[4];
475         h->pixf.intra_satd_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
476         h->pixf.intra_satd_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
477         h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
478         h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
479         satdu[I_PRED_CHROMA_P] =
480             h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
481         satdv[I_PRED_CHROMA_P] =
482             h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
483         
484         for( i=0; i<i_max; i++ )
485         {
486             int i_mode = predict_mode[i];
487             int i_satd = satdu[i_mode] + satdv[i_mode]
488                        + a->i_lambda * bs_size_ue(i_mode);
489
490             a->i_satd_i8x8chroma_dir[i] = i_satd;
491             COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
492         }
493     }
494     else
495     {
496         for( i=0; i<i_max; i++ )
497         {
498             int i_satd;
499             int i_mode = predict_mode[i];
500
501             /* we do the prediction */
502             h->predict_8x8c[i_mode]( p_dstc[0] );
503             h->predict_8x8c[i_mode]( p_dstc[1] );
504
505             /* we calculate the cost */
506             i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
507                                                p_srcc[0], FENC_STRIDE ) +
508                      h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
509                                                p_srcc[1], FENC_STRIDE ) +
510                      a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
511
512             a->i_satd_i8x8chroma_dir[i] = i_satd;
513             COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
514         }
515     }
516
517     h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
518 }
519
520 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
521 {
522     const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
523     uint8_t  *p_src = h->mb.pic.p_fenc[0];
524     uint8_t  *p_dst = h->mb.pic.p_fdec[0];
525
526     int i, idx;
527     int i_max;
528     int predict_mode[9];
529     int b_merged_satd = h->pixf.intra_satd_x3_16x16 && h->pixf.mbcmp[0] == h->pixf.satd[0];
530
531     /*---------------- Try all mode and calculate their score ---------------*/
532
533     /* 16x16 prediction selection */
534     predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
535
536     if( b_merged_satd && i_max == 4 )
537     {
538         h->pixf.intra_satd_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
539         h->predict_16x16[I_PRED_16x16_P]( p_dst );
540         a->i_satd_i16x16_dir[I_PRED_16x16_P] =
541             h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
542         for( i=0; i<4; i++ )
543         {
544             int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
545             COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
546         }
547     }
548     else
549     {
550         for( i = 0; i < i_max; i++ )
551         {
552             int i_satd;
553             int i_mode = predict_mode[i];
554             h->predict_16x16[i_mode]( p_dst );
555
556             i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
557                     a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
558             COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
559             a->i_satd_i16x16_dir[i_mode] = i_satd;
560         }
561     }
562
563     if( h->sh.i_type == SLICE_TYPE_B )
564         /* cavlc mb type prefix */
565         a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
566     if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
567         return;
568
569     /* 8x8 prediction selection */
570     if( flags & X264_ANALYSE_I8x8 )
571     {
572         DECLARE_ALIGNED( uint8_t, edge[33], 8 );
573         x264_pixel_cmp_t sa8d = (*h->pixf.mbcmp == *h->pixf.sad) ? h->pixf.sad[PIXEL_8x8] : h->pixf.sa8d[PIXEL_8x8];
574         int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
575         int i_cost = 0;
576         b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
577
578         // FIXME some bias like in i4x4?
579         if( h->sh.i_type == SLICE_TYPE_B )
580             i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
581
582         for( idx = 0;; idx++ )
583         {
584             int x = idx&1;
585             int y = idx>>1;
586             uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
587             uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
588             int i_best = COST_MAX;
589             int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
590
591             predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
592             x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
593
594             if( b_merged_satd && i_max == 9 )
595             {
596                 int satd[3];
597                 h->pixf.intra_sa8d_x3_8x8( p_src_by, edge, satd );
598                 if( i_pred_mode < 3 )
599                     satd[i_pred_mode] -= 3 * a->i_lambda;
600                 for( i=2; i>=0; i-- )
601                 {
602                     int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
603                     COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
604                 }
605                 i = 3;
606             }
607             else
608                 i = 0;
609
610             for( ; i<i_max; i++ )
611             {
612                 int i_satd;
613                 int i_mode = predict_mode[i];
614
615                 h->predict_8x8[i_mode]( p_dst_by, edge );
616
617                 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
618                        + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
619
620                 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
621                 a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
622             }
623             i_cost += i_best;
624
625             if( idx == 3 || i_cost > i_satd_thresh )
626                 break;
627
628             /* we need to encode this block now (for next ones) */
629             h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
630             x264_mb_encode_i8x8( h, idx, a->i_qp );
631
632             x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
633         }
634
635         if( idx == 3 )
636             a->i_satd_i8x8 = i_cost;
637         else
638         {
639             a->i_satd_i8x8 = COST_MAX;
640             i_cost = i_cost * 4/(idx+1);
641         }
642         if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 )
643             return;
644     }
645
646     /* 4x4 prediction selection */
647     if( flags & X264_ANALYSE_I4x4 )
648     {
649         int i_cost;
650         int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
651         b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
652         if( a->b_mbrd )
653             i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
654
655         i_cost = a->i_lambda * 24;    /* from JVT (SATD0) */
656         if( h->sh.i_type == SLICE_TYPE_B )
657             i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];
658
659         for( idx = 0;; idx++ )
660         {
661             int x = block_idx_x[idx];
662             int y = block_idx_y[idx];
663             uint8_t *p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
664             uint8_t *p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
665             int i_best = COST_MAX;
666             int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
667
668             predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
669
670             if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
671                 /* emulate missing topright samples */
672                 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
673
674             if( b_merged_satd && i_max >= 6 )
675             {
676                 int satd[3];
677                 h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
678                 if( i_pred_mode < 3 )
679                     satd[i_pred_mode] -= 3 * a->i_lambda;
680                 for( i=2; i>=0; i-- )
681                     COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
682                                  a->i_predict4x4[idx], i );
683                 i = 3;
684             }
685             else
686                 i = 0;
687
688             for( ; i<i_max; i++ )
689             {
690                 int i_satd;
691                 int i_mode = predict_mode[i];
692
693                 h->predict_4x4[i_mode]( p_dst_by );
694
695                 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
696                                                    p_src_by, FENC_STRIDE )
697                        + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
698
699                 COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
700             }
701             i_cost += i_best;
702
703             if( i_cost > i_satd_thresh || idx == 15 )
704                 break;
705
706             /* we need to encode this block now (for next ones) */
707             h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
708             x264_mb_encode_i4x4( h, idx, a->i_qp );
709
710             h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
711         }
712         if( idx == 15 )
713             a->i_satd_i4x4 = i_cost;
714         else
715             a->i_satd_i4x4 = COST_MAX;
716     }
717 }
718
719 static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
720 {
721     if( a->i_satd_i16x16 <= i_satd_thresh )
722     {
723         h->mb.i_type = I_16x16;
724         x264_analyse_update_cache( h, a );
725         a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
726     }
727     else
728         a->i_satd_i16x16 = COST_MAX;
729
730     if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
731     {
732         h->mb.i_type = I_4x4;
733         x264_analyse_update_cache( h, a );
734         a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
735     }
736     else
737         a->i_satd_i4x4 = COST_MAX;
738
739     if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
740     {
741         h->mb.i_type = I_8x8;
742         x264_analyse_update_cache( h, a );
743         a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
744     }
745     else
746         a->i_satd_i8x8 = COST_MAX;
747 }
748
749 static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
750 {
751     uint8_t  *p_src = h->mb.pic.p_fenc[0];
752     uint8_t  *p_dst = h->mb.pic.p_fdec[0];
753
754     int i, j, idx, x, y;
755     int i_max, i_satd, i_best, i_mode, i_thresh;
756     int i_pred_mode;
757     int predict_mode[9];
758
759     if( h->mb.i_type == I_16x16 )
760     {
761         int old_pred_mode = a->i_predict16x16;
762         i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
763         i_best = a->i_satd_i16x16;
764         predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
765         for( i = 0; i < i_max; i++ )
766         {
767             int i_mode = predict_mode[i];
768             if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
769                 continue;
770             h->mb.i_intra16x16_pred_mode = i_mode;
771             i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
772             COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
773         }
774     }
775     else if( h->mb.i_type == I_4x4 )
776     {
777         uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
778         int i_nnz = 0;
779         for( idx = 0; idx < 16; idx++ )
780         {
781             uint8_t *p_src_by;
782             uint8_t *p_dst_by;
783             i_best = COST_MAX;
784
785             i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
786             x = block_idx_x[idx];
787             y = block_idx_y[idx];
788
789             p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
790             p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
791             predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
792
793             if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
794                 /* emulate missing topright samples */
795                 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
796
797             for( i = 0; i < i_max; i++ )
798             {
799                 i_mode = predict_mode[i];
800                 h->predict_4x4[i_mode]( p_dst_by );
801                 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
802
803                 if( i_best > i_satd )
804                 {
805                     a->i_predict4x4[idx] = i_mode;
806                     i_best = i_satd;
807                     pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
808                     pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
809                     pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
810                     pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
811                     i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
812                 }
813             }
814
815             *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
816             *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
817             *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
818             *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
819             h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
820
821             h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
822         }
823     }
824     else if( h->mb.i_type == I_8x8 )
825     {
826         DECLARE_ALIGNED( uint8_t, edge[33], 8 );
827         for( idx = 0; idx < 4; idx++ )
828         {
829             uint64_t pels_h = 0;
830             uint8_t pels_v[7];
831             int i_nnz[3];
832             uint8_t *p_src_by;
833             uint8_t *p_dst_by;
834             int j;
835             i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
836
837             i_best = COST_MAX;
838             i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
839             x = idx&1;
840             y = idx>>1;
841
842             p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
843             p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
844             predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
845             x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
846
847             for( i = 0; i < i_max; i++ )
848             {
849                 i_mode = predict_mode[i];
850                 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
851                     continue;
852                 h->predict_8x8[i_mode]( p_dst_by, edge );
853                 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
854
855                 if( i_best > i_satd )
856                 {
857                     a->i_predict8x8[idx] = i_mode;
858                     i_best = i_satd;
859
860                     pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
861                     if( !(idx&1) )
862                         for( j=0; j<7; j++ )
863                             pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
864                     for( j=0; j<3; j++ )
865                         i_nnz[j] = h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]];
866                 }
867             }
868
869             *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
870             if( !(idx&1) )
871                 for( j=0; j<7; j++ )
872                     p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
873             for( j=0; j<3; j++ )
874                 h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]] = i_nnz[j];
875
876             x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
877         }
878     }
879
880     /* RD selection for chroma prediction */
881     predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
882     if( i_max > 1 )
883     {
884         i_thresh = a->i_satd_i8x8chroma * 5/4;
885
886         for( i = j = 0; i < i_max; i++ )
887             if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
888                 predict_mode[i] != a->i_predict8x8chroma )
889             {
890                 predict_mode[j++] = predict_mode[i];
891             }
892         i_max = j;
893
894         if( i_max > 0 )
895         {
896             int i_chroma_lambda = i_qp0_cost2_table[h->mb.i_chroma_qp];
897             /* the previous thing encoded was x264_intra_rd(), so the pixels and
898              * coefs for the current chroma mode are still around, so we only
899              * have to recount the bits. */
900             i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
901             for( i = 0; i < i_max; i++ )
902             {
903                 i_mode = predict_mode[i];
904                 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
905                 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
906                 /* if we've already found a mode that needs no residual, then
907                  * probably any mode with a residual will be worse.
908                  * so avoid dct on the remaining modes to improve speed. */
909                 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
910                 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode );
911             }
912             h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
913         }
914     }
915 }
916
917 #define LOAD_FENC( m, src, xoff, yoff) \
918     (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
919     (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
920     (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
921     (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
922     (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
923
924 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
925     (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
926     (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
927     (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
928     (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
929     (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
930     (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
931     (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
932
933 #define REF_COST(list, ref) \
934     (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
935
936 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
937 {
938     x264_me_t m;
939     int i_ref;
940     int mvc[7][2], i_mvc;
941     int i_halfpel_thresh = INT_MAX;
942     int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
943
944     /* 16x16 Search on all ref frame */
945     m.i_pixel = PIXEL_16x16;
946     m.p_cost_mv = a->p_cost_mv;
947     LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
948
949     a->l0.me16x16.cost = INT_MAX;
950     for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
951     {
952         const int i_ref_cost = REF_COST( 0, i_ref );
953         i_halfpel_thresh -= i_ref_cost;
954         m.i_ref_cost = i_ref_cost;
955         m.i_ref = i_ref;
956
957         /* search with ref */
958         LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
959         x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
960         x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
961         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
962
963         /* early termination
964          * SSD threshold would probably be better than SATD */
965         if( i_ref == 0
966             && a->b_try_pskip
967             && m.cost-m.cost_mv < 300*a->i_lambda
968             &&  abs(m.mv[0]-h->mb.cache.pskip_mv[0])
969               + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
970             && x264_macroblock_probe_pskip( h ) )
971         {
972             h->mb.i_type = P_SKIP;
973             x264_analyse_update_cache( h, a );
974             assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
975             return;
976         }
977
978         m.cost += i_ref_cost;
979         i_halfpel_thresh += i_ref_cost;
980
981         if( m.cost < a->l0.me16x16.cost )
982             a->l0.me16x16 = m;
983
984         /* save mv for predicting neighbors */
985         a->l0.mvc[i_ref][0][0] =
986         h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
987         a->l0.mvc[i_ref][0][1] =
988         h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
989     }
990
991     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
992     assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
993
994     h->mb.i_type = P_L0;
995     if( a->b_mbrd && a->l0.i_ref == 0
996         && a->l0.me16x16.mv[0] == h->mb.cache.pskip_mv[0]
997         && a->l0.me16x16.mv[1] == h->mb.cache.pskip_mv[1] )
998     {
999         h->mb.i_partition = D_16x16;
1000         x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
1001         a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1002     }
1003 }
1004
1005 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
1006 {
1007     x264_me_t m;
1008     int i_ref;
1009     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1010     int i_halfpel_thresh = INT_MAX;
1011     int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
1012     int i;
1013     int i_maxref = h->mb.pic.i_fref[0]-1;
1014
1015     h->mb.i_partition = D_8x8;
1016
1017     /* early termination: if 16x16 chose ref 0, then evalute no refs older
1018      * than those used by the neighbors */
1019     if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
1020         h->mb.i_mb_type_top && h->mb.i_mb_type_left )
1021     {
1022         i_maxref = 0;
1023         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
1024         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
1025         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
1026         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
1027         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
1028         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
1029     }
1030
1031     for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1032     {
1033          a->l0.mvc[i_ref][0][0] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0];
1034          a->l0.mvc[i_ref][0][1] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1];
1035     }
1036
1037     for( i = 0; i < 4; i++ )
1038     {
1039         x264_me_t *l0m = &a->l0.me8x8[i];
1040         const int x8 = i%2;
1041         const int y8 = i/2;
1042
1043         m.i_pixel = PIXEL_8x8;
1044         m.p_cost_mv = a->p_cost_mv;
1045
1046         LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
1047         l0m->cost = INT_MAX;
1048         for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1049         {
1050              const int i_ref_cost = REF_COST( 0, i_ref );
1051              i_halfpel_thresh -= i_ref_cost;
1052              m.i_ref_cost = i_ref_cost;
1053              m.i_ref = i_ref;
1054
1055              LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
1056              x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
1057              x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1058              x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
1059
1060              m.cost += i_ref_cost;
1061              i_halfpel_thresh += i_ref_cost;
1062              *(uint64_t*)a->l0.mvc[i_ref][i+1] = *(uint64_t*)m.mv;
1063
1064              if( m.cost < l0m->cost )
1065                  *l0m = m;
1066         }
1067         x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv[0], l0m->mv[1] );
1068         x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1069
1070         /* mb type cost */
1071         l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1072     }
1073
1074     a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1075                       a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1076     h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1077     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1078 }
1079
1080 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1081 {
1082     const int i_ref = a->l0.me16x16.i_ref;
1083     const int i_ref_cost = REF_COST( 0, i_ref );
1084     uint8_t  **p_fref = h->mb.pic.p_fref[0][i_ref];
1085     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1086     int i_mvc;
1087     int (*mvc)[2] = a->l0.mvc[i_ref];
1088     int i;
1089
1090     /* XXX Needed for x264_mb_predict_mv */
1091     h->mb.i_partition = D_8x8;
1092
1093     i_mvc = 1;
1094     *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.me16x16.mv;
1095
1096     for( i = 0; i < 4; i++ )
1097     {
1098         x264_me_t *m = &a->l0.me8x8[i];
1099         const int x8 = i%2;
1100         const int y8 = i/2;
1101
1102         m->i_pixel = PIXEL_8x8;
1103         m->p_cost_mv = a->p_cost_mv;
1104         m->i_ref_cost = i_ref_cost;
1105         m->i_ref = i_ref;
1106
1107         LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1108         LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1109         x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1110         x264_me_search( h, m, mvc, i_mvc );
1111
1112         x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
1113
1114         *(uint64_t*)mvc[i_mvc] = *(uint64_t*)m->mv;
1115         i_mvc++;
1116
1117         /* mb type cost */
1118         m->cost += i_ref_cost;
1119         m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1120     }
1121
1122     /* theoretically this should include 4*ref_cost,
1123      * but 3 seems a better approximation of cabac. */
1124     a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1125                       a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
1126                       REF_COST( 0, a->l0.me16x16.i_ref );
1127     h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1128     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1129 }
1130
1131 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
1132 {
1133     x264_me_t m;
1134     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1135     int mvc[3][2];
1136     int i, j;
1137
1138     /* XXX Needed for x264_mb_predict_mv */
1139     h->mb.i_partition = D_16x8;
1140
1141     for( i = 0; i < 2; i++ )
1142     {
1143         x264_me_t *l0m = &a->l0.me16x8[i];
1144         const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1145         const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1146
1147         m.i_pixel = PIXEL_16x8;
1148         m.p_cost_mv = a->p_cost_mv;
1149
1150         LOAD_FENC( &m, p_fenc, 0, 8*i );
1151         l0m->cost = INT_MAX;
1152         for( j = 0; j < i_ref8s; j++ )
1153         {
1154              const int i_ref = ref8[j];
1155              const int i_ref_cost = REF_COST( 0, i_ref );
1156              m.i_ref_cost = i_ref_cost;
1157              m.i_ref = i_ref;
1158
1159              /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1160              *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1161              *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][2*i+1];
1162              *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][2*i+2];
1163
1164              LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1165              x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1166              x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1167              x264_me_search( h, &m, mvc, 3 );
1168
1169              m.cost += i_ref_cost;
1170
1171              if( m.cost < l0m->cost )
1172                  *l0m = m;
1173         }
1174         x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, l0m->mv[0], l0m->mv[1] );
1175         x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1176     }
1177
1178     a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1179 }
1180
1181 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1182 {
1183     x264_me_t m;
1184     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1185     int mvc[3][2];
1186     int i, j;
1187
1188     /* XXX Needed for x264_mb_predict_mv */
1189     h->mb.i_partition = D_8x16;
1190
1191     for( i = 0; i < 2; i++ )
1192     {
1193         x264_me_t *l0m = &a->l0.me8x16[i];
1194         const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1195         const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1196
1197         m.i_pixel = PIXEL_8x16;
1198         m.p_cost_mv = a->p_cost_mv;
1199
1200         LOAD_FENC( &m, p_fenc, 8*i, 0 );
1201         l0m->cost = INT_MAX;
1202         for( j = 0; j < i_ref8s; j++ )
1203         {
1204              const int i_ref = ref8[j];
1205              const int i_ref_cost = REF_COST( 0, i_ref );
1206              m.i_ref_cost = i_ref_cost;
1207              m.i_ref = i_ref;
1208
1209              *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
1210              *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][i+1];
1211              *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][i+3];
1212
1213              LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1214              x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1215              x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1216              x264_me_search( h, &m, mvc, 3 );
1217
1218              m.cost += i_ref_cost;
1219
1220              if( m.cost < l0m->cost )
1221                  *l0m = m;
1222         }
1223         x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, l0m->mv[0], l0m->mv[1] );
1224         x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1225     }
1226
1227     a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1228 }
1229
1230 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1231 {
1232     DECLARE_ALIGNED( uint8_t, pix1[16*8], 8 );
1233     uint8_t *pix2 = pix1+8;
1234     const int i_stride = h->mb.pic.i_stride[1];
1235     const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1236     const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1237
1238 #define CHROMA4x4MC( width, height, me, x, y ) \
1239     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 ); \
1240     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 );
1241
1242     if( pixel == PIXEL_4x4 )
1243     {
1244         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1245         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 0,2 );
1246         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 2,0 );
1247         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1248     }
1249     else if( pixel == PIXEL_8x4 )
1250     {
1251         CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1252         CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1253     }
1254     else
1255     {
1256         CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1257         CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1258     }
1259
1260     return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1261          + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1262 }
1263
1264 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1265 {
1266     uint8_t  **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1267     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1268     const int i_ref = a->l0.me8x8[i8x8].i_ref;
1269     int i4x4;
1270
1271     /* XXX Needed for x264_mb_predict_mv */
1272     h->mb.i_partition = D_8x8;
1273
1274     for( i4x4 = 0; i4x4 < 4; i4x4++ )
1275     {
1276         const int idx = 4*i8x8 + i4x4;
1277         const int x4 = block_idx_x[idx];
1278         const int y4 = block_idx_y[idx];
1279         const int i_mvc = (i4x4 == 0);
1280
1281         x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1282
1283         m->i_pixel = PIXEL_4x4;
1284         m->p_cost_mv = a->p_cost_mv;
1285
1286         LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1287         LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1288
1289         x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1290         x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1291
1292         x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
1293     }
1294     a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1295                             a->l0.me4x4[i8x8][1].cost +
1296                             a->l0.me4x4[i8x8][2].cost +
1297                             a->l0.me4x4[i8x8][3].cost +
1298                             REF_COST( 0, i_ref ) +
1299                             a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1300     if( h->mb.b_chroma_me )
1301         a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1302 }
1303
1304 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1305 {
1306     uint8_t  **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1307     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1308     const int i_ref = a->l0.me8x8[i8x8].i_ref;
1309     int i8x4;
1310
1311     /* XXX Needed for x264_mb_predict_mv */
1312     h->mb.i_partition = D_8x8;
1313
1314     for( i8x4 = 0; i8x4 < 2; i8x4++ )
1315     {
1316         const int idx = 4*i8x8 + 2*i8x4;
1317         const int x4 = block_idx_x[idx];
1318         const int y4 = block_idx_y[idx];
1319         const int i_mvc = (i8x4 == 0);
1320
1321         x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1322
1323         m->i_pixel = PIXEL_8x4;
1324         m->p_cost_mv = a->p_cost_mv;
1325
1326         LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1327         LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1328
1329         x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1330         x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1331
1332         x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
1333     }
1334     a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1335                             REF_COST( 0, i_ref ) +
1336                             a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1337     if( h->mb.b_chroma_me )
1338         a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1339 }
1340
1341 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1342 {
1343     uint8_t  **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1344     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1345     const int i_ref = a->l0.me8x8[i8x8].i_ref;
1346     int i4x8;
1347
1348     /* XXX Needed for x264_mb_predict_mv */
1349     h->mb.i_partition = D_8x8;
1350
1351     for( i4x8 = 0; i4x8 < 2; i4x8++ )
1352     {
1353         const int idx = 4*i8x8 + i4x8;
1354         const int x4 = block_idx_x[idx];
1355         const int y4 = block_idx_y[idx];
1356         const int i_mvc = (i4x8 == 0);
1357
1358         x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1359
1360         m->i_pixel = PIXEL_4x8;
1361         m->p_cost_mv = a->p_cost_mv;
1362
1363         LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1364         LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1365
1366         x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1367         x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1368
1369         x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
1370     }
1371     a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1372                             REF_COST( 0, i_ref ) +
1373                             a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1374     if( h->mb.b_chroma_me )
1375         a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1376 }
1377
1378 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1379 {
1380     /* Assumes that fdec still contains the results of
1381      * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1382
1383     uint8_t **p_fenc = h->mb.pic.p_fenc;
1384     uint8_t **p_fdec = h->mb.pic.p_fdec;
1385     int i;
1386
1387     a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1388     for( i = 0; i < 4; i++ )
1389     {
1390         const int x = (i&1)*8;
1391         const int y = (i>>1)*8;
1392         a->i_cost16x16direct +=
1393         a->i_cost8x8direct[i] =
1394             h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1395
1396         /* mb type cost */
1397         a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1398     }
1399 }
1400
1401 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1402     { \
1403         if( h->param.analyse.b_weighted_bipred ) \
1404             h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1405                     h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1406         else \
1407             h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1408     }
1409
1410 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1411 {
1412     uint8_t pix1[16*16], pix2[16*16];
1413     uint8_t *src2;
1414     int stride2 = 16;
1415     int weight;
1416
1417     x264_me_t m;
1418     int i_ref;
1419     int mvc[8][2], i_mvc;
1420     int i_halfpel_thresh = INT_MAX;
1421     int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1422
1423     /* 16x16 Search on all ref frame */
1424     m.i_pixel = PIXEL_16x16;
1425     m.p_cost_mv = a->p_cost_mv;
1426     LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1427
1428     /* ME for List 0 */
1429     a->l0.me16x16.cost = INT_MAX;
1430     for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1431     {
1432         /* search with ref */
1433         LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1434         x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1435         x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1436         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1437
1438         /* add ref cost */
1439         m.cost += REF_COST( 0, i_ref );
1440
1441         if( m.cost < a->l0.me16x16.cost )
1442         {
1443             a->l0.i_ref = i_ref;
1444             a->l0.me16x16 = m;
1445         }
1446
1447         /* save mv for predicting neighbors */
1448         h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1449         h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1450     }
1451     /* subtract ref cost, so we don't have to add it for the other MB types */
1452     a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1453
1454     /* ME for list 1 */
1455     i_halfpel_thresh = INT_MAX;
1456     p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1457     a->l1.me16x16.cost = INT_MAX;
1458     for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1459     {
1460         /* search with ref */
1461         LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1462         x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1463         x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1464         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1465
1466         /* add ref cost */
1467         m.cost += REF_COST( 1, i_ref );
1468
1469         if( m.cost < a->l1.me16x16.cost )
1470         {
1471             a->l1.i_ref = i_ref;
1472             a->l1.me16x16 = m;
1473         }
1474
1475         /* save mv for predicting neighbors */
1476         h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1477         h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1478     }
1479     /* subtract ref cost, so we don't have to add it for the other MB types */
1480     a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1481
1482     /* Set global ref, needed for other modes? */
1483     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1484     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1485
1486     /* get cost of BI mode */
1487     weight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1488     if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
1489     {
1490         /* l0 reference is halfpel, so get_ref on it will make it faster */
1491         src2 = h->mc.get_ref( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1492                         pix2, &stride2,
1493                         a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1494                         16, 16 );
1495         h->mc.mc_luma( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1496                         pix1, 16,
1497                         a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1498                         16, 16 );
1499         weight = 64 - weight;
1500     } 
1501     else
1502     {
1503         /* if l0 was qpel, we'll use get_ref on l1 instead */
1504         h->mc.mc_luma( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1505                         pix1, 16,
1506                         a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1507                         16, 16 );
1508         src2 = h->mc.get_ref( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1509                         pix2, &stride2,
1510                         a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1511                         16, 16 );
1512     }
1513
1514     if( h->param.analyse.b_weighted_bipred )
1515         h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2, weight );
1516     else
1517         h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1518
1519     a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix1, 16 )
1520                      + REF_COST( 0, a->l0.i_ref )
1521                      + REF_COST( 1, a->l1.i_ref )
1522                      + a->l0.me16x16.cost_mv
1523                      + a->l1.me16x16.cost_mv;
1524
1525     /* mb type cost */
1526     a->i_cost16x16bi   += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1527     a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1528     a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1529 }
1530
1531 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1532 {
1533     const int x = 2*(i%2);
1534     const int y = 2*(i/2);
1535
1536     switch( h->mb.i_sub_partition[i] )
1537     {
1538         case D_L0_8x8:
1539             x264_macroblock_cache_mv( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv[0], a->l0.me8x8[i].mv[1] );
1540             break;
1541         case D_L0_8x4:
1542             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] );
1543             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] );
1544             break;
1545         case D_L0_4x8:
1546             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] );
1547             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] );
1548             break;
1549         case D_L0_4x4:
1550             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] );
1551             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] );
1552             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] );
1553             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] );
1554             break;
1555         default:
1556             x264_log( h, X264_LOG_ERROR, "internal error\n" );
1557             break;
1558     }
1559 }
1560
1561 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1562     if( x264_mb_partition_listX_table[0][part] ) \
1563     { \
1564         x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1565         x264_macroblock_cache_mv(  h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
1566     } \
1567     else \
1568     { \
1569         x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1570         x264_macroblock_cache_mv(  h, x,y,dx,dy, 0, 0, 0 ); \
1571         if( b_mvd ) \
1572             x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
1573     } \
1574     if( x264_mb_partition_listX_table[1][part] ) \
1575     { \
1576         x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1577         x264_macroblock_cache_mv(  h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
1578     } \
1579     else \
1580     { \
1581         x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1582         x264_macroblock_cache_mv(  h, x,y,dx,dy, 1, 0, 0 ); \
1583         if( b_mvd ) \
1584             x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
1585     }
1586
1587 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1588 {
1589     int x = (i%2)*2;
1590     int y = (i/2)*2;
1591     if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1592     {
1593         x264_mb_load_mv_direct8x8( h, i );
1594         if( b_mvd )
1595         {
1596             x264_macroblock_cache_mvd(  h, x, y, 2, 2, 0, 0, 0 );
1597             x264_macroblock_cache_mvd(  h, x, y, 2, 2, 1, 0, 0 );
1598             x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1599         }
1600     }
1601     else
1602     {
1603         CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1604     }
1605 }
1606 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1607 {
1608     CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1609 }
1610 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1611 {
1612     CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1613 }
1614 #undef CACHE_MV_BI
1615
1616 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1617 {
1618     uint8_t **p_fref[2] =
1619         { h->mb.pic.p_fref[0][a->l0.i_ref],
1620           h->mb.pic.p_fref[1][a->l1.i_ref] };
1621     uint8_t pix[2][8*8];
1622     int i, l;
1623
1624     /* XXX Needed for x264_mb_predict_mv */
1625     h->mb.i_partition = D_8x8;
1626
1627     a->i_cost8x8bi = 0;
1628
1629     for( i = 0; i < 4; i++ )
1630     {
1631         const int x8 = i%2;
1632         const int y8 = i/2;
1633         int i_part_cost;
1634         int i_part_cost_bi = 0;
1635
1636         for( l = 0; l < 2; l++ )
1637         {
1638             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1639             x264_me_t *m = &lX->me8x8[i];
1640
1641             m->i_pixel = PIXEL_8x8;
1642             m->p_cost_mv = a->p_cost_mv;
1643
1644             LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1645             LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1646
1647             x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1648             x264_me_search( h, m, &lX->me16x16.mv, 1 );
1649
1650             x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
1651
1652             /* BI mode */
1653             h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1654                             m->mv[0], m->mv[1], 8, 8 );
1655             i_part_cost_bi += m->cost_mv;
1656             /* FIXME: ref cost */
1657         }
1658
1659         WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1660         i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1661                         + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1662         a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1663         a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1664
1665         i_part_cost = a->l0.me8x8[i].cost;
1666         h->mb.i_sub_partition[i] = D_L0_8x8;
1667         COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1668         COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1669         COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1670         a->i_cost8x8bi += i_part_cost;
1671
1672         /* XXX Needed for x264_mb_predict_mv */
1673         x264_mb_cache_mv_b8x8( h, a, i, 0 );
1674     }
1675
1676     /* mb type cost */
1677     a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1678 }
1679
1680 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1681 {
1682     uint8_t **p_fref[2] =
1683         { h->mb.pic.p_fref[0][a->l0.i_ref],
1684           h->mb.pic.p_fref[1][a->l1.i_ref] };
1685     DECLARE_ALIGNED( uint8_t,  pix[2][16*8], 16 );
1686     int mvc[2][2];
1687     int i, l;
1688
1689     h->mb.i_partition = D_16x8;
1690     a->i_cost16x8bi = 0;
1691
1692     for( i = 0; i < 2; i++ )
1693     {
1694         int i_part_cost;
1695         int i_part_cost_bi = 0;
1696
1697         /* TODO: check only the list(s) that were used in b8x8? */
1698         for( l = 0; l < 2; l++ )
1699         {
1700             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1701             x264_me_t *m = &lX->me16x8[i];
1702
1703             m->i_pixel = PIXEL_16x8;
1704             m->p_cost_mv = a->p_cost_mv;
1705
1706             LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1707             LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1708
1709             mvc[0][0] = lX->me8x8[2*i].mv[0];
1710             mvc[0][1] = lX->me8x8[2*i].mv[1];
1711             mvc[1][0] = lX->me8x8[2*i+1].mv[0];
1712             mvc[1][1] = lX->me8x8[2*i+1].mv[1];
1713
1714             x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp );
1715             x264_me_search( h, m, mvc, 2 );
1716
1717             /* BI mode */
1718             h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 16,
1719                             m->mv[0], m->mv[1], 16, 8 );
1720             /* FIXME: ref cost */
1721             i_part_cost_bi += m->cost_mv;
1722         }
1723
1724         WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1725         i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1726
1727         i_part_cost = a->l0.me16x8[i].cost;
1728         a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1729         if( a->l1.me16x8[i].cost < i_part_cost )
1730         {
1731             i_part_cost = a->l1.me16x8[i].cost;
1732             a->i_mb_partition16x8[i] = D_L1_8x8;
1733         }
1734         if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1735         {
1736             i_part_cost = i_part_cost_bi;
1737             a->i_mb_partition16x8[i] = D_BI_8x8;
1738         }
1739         a->i_cost16x8bi += i_part_cost;
1740
1741         x264_mb_cache_mv_b16x8( h, a, i, 0 );
1742     }
1743
1744     /* mb type cost */
1745     a->i_mb_type16x8 = B_L0_L0
1746         + (a->i_mb_partition16x8[0]>>2) * 3
1747         + (a->i_mb_partition16x8[1]>>2);
1748     a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1749 }
1750
1751 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1752 {
1753     uint8_t **p_fref[2] =
1754         { h->mb.pic.p_fref[0][a->l0.i_ref],
1755           h->mb.pic.p_fref[1][a->l1.i_ref] };
1756     uint8_t pix[2][8*16];
1757     int mvc[2][2];
1758     int i, l;
1759
1760     h->mb.i_partition = D_8x16;
1761     a->i_cost8x16bi = 0;
1762
1763     for( i = 0; i < 2; i++ )
1764     {
1765         int i_part_cost;
1766         int i_part_cost_bi = 0;
1767
1768         for( l = 0; l < 2; l++ )
1769         {
1770             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1771             x264_me_t *m = &lX->me8x16[i];
1772
1773             m->i_pixel = PIXEL_8x16;
1774             m->p_cost_mv = a->p_cost_mv;
1775
1776             LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1777             LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1778
1779             mvc[0][0] = lX->me8x8[i].mv[0];
1780             mvc[0][1] = lX->me8x8[i].mv[1];
1781             mvc[1][0] = lX->me8x8[i+2].mv[0];
1782             mvc[1][1] = lX->me8x8[i+2].mv[1];
1783
1784             x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1785             x264_me_search( h, m, mvc, 2 );
1786
1787             /* BI mode */
1788             h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1789                             m->mv[0], m->mv[1], 8, 16 );
1790             /* FIXME: ref cost */
1791             i_part_cost_bi += m->cost_mv;
1792         }
1793
1794         WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1795         i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1796
1797         i_part_cost = a->l0.me8x16[i].cost;
1798         a->i_mb_partition8x16[i] = D_L0_8x8;
1799         if( a->l1.me8x16[i].cost < i_part_cost )
1800         {
1801             i_part_cost = a->l1.me8x16[i].cost;
1802             a->i_mb_partition8x16[i] = D_L1_8x8;
1803         }
1804         if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1805         {
1806             i_part_cost = i_part_cost_bi;
1807             a->i_mb_partition8x16[i] = D_BI_8x8;
1808         }
1809         a->i_cost8x16bi += i_part_cost;
1810
1811         x264_mb_cache_mv_b8x16( h, a, i, 0 );
1812     }
1813
1814     /* mb type cost */
1815     a->i_mb_type8x16 = B_L0_L0
1816         + (a->i_mb_partition8x16[0]>>2) * 3
1817         + (a->i_mb_partition8x16[1]>>2);
1818     a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1819 }
1820
1821 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1822 {
1823     int thresh = i_satd * 5/4;
1824
1825     h->mb.i_type = P_L0;
1826     if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1827     {
1828         h->mb.i_partition = D_16x16;
1829         x264_analyse_update_cache( h, a );
1830         a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1831     }
1832     a->l0.me16x16.cost = a->l0.i_rd16x16;
1833
1834     if( a->l0.i_cost16x8 <= thresh )
1835     {
1836         h->mb.i_partition = D_16x8;
1837         x264_analyse_update_cache( h, a );
1838         a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1839     }
1840     else
1841         a->l0.i_cost16x8 = COST_MAX;
1842
1843     if( a->l0.i_cost8x16 <= thresh )
1844     {
1845         h->mb.i_partition = D_8x16;
1846         x264_analyse_update_cache( h, a );
1847         a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1848     }
1849     else
1850         a->l0.i_cost8x16 = COST_MAX;
1851
1852     if( a->l0.i_cost8x8 <= thresh )
1853     {
1854         h->mb.i_type = P_8x8;
1855         x264_analyse_update_cache( h, a );
1856         a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1857
1858         if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1859         {
1860             /* FIXME: RD per subpartition */
1861             int part_bak[4];
1862             int i, i_cost;
1863             int b_sub8x8 = 0;
1864             for( i=0; i<4; i++ )
1865             {
1866                 part_bak[i] = h->mb.i_sub_partition[i];
1867                 b_sub8x8 |= (part_bak[i] != D_L0_8x8);
1868             }
1869             if( b_sub8x8 )
1870             {
1871                 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1872                 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1873                 i_cost = x264_rd_cost_mb( h, a->i_lambda2 );
1874                 if( a->l0.i_cost8x8 < i_cost )
1875                 {
1876                     for( i=0; i<4; i++ )
1877                         h->mb.i_sub_partition[i] = part_bak[i];
1878                 }
1879                 else
1880                    a->l0.i_cost8x8 = i_cost;
1881             }
1882         }
1883     }
1884     else
1885         a->l0.i_cost8x8 = COST_MAX;
1886 }
1887
1888 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
1889 {
1890     int thresh = i_satd_inter * 17/16;
1891
1892     if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
1893     {
1894         h->mb.i_type = B_DIRECT;
1895         x264_analyse_update_cache( h, a );
1896         a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
1897     }
1898
1899     //FIXME not all the update_cache calls are needed
1900     h->mb.i_partition = D_16x16;
1901     /* L0 */
1902     if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
1903     {
1904         h->mb.i_type = B_L0_L0;
1905         x264_analyse_update_cache( h, a );
1906         a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1907     }
1908
1909     /* L1 */
1910     if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
1911     {
1912         h->mb.i_type = B_L1_L1;
1913         x264_analyse_update_cache( h, a );
1914         a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1915     }
1916
1917     /* BI */
1918     if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
1919     {
1920         h->mb.i_type = B_BI_BI;
1921         x264_analyse_update_cache( h, a );
1922         a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1923     }
1924
1925     /* 8x8 */
1926     if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
1927     {
1928         h->mb.i_type = B_8x8;
1929         h->mb.i_partition = D_8x8;
1930         x264_analyse_update_cache( h, a );
1931         a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1932         x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1933     }
1934
1935     /* 16x8 */
1936     if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
1937     {
1938         h->mb.i_type = a->i_mb_type16x8;
1939         h->mb.i_partition = D_16x8;
1940         x264_analyse_update_cache( h, a );
1941         a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1942     }
1943
1944     /* 8x16 */
1945     if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
1946     {
1947         h->mb.i_type = a->i_mb_type8x16;
1948         h->mb.i_partition = D_8x16;
1949         x264_analyse_update_cache( h, a );
1950         a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1951     }
1952 }
1953
1954 static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
1955 {
1956     const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
1957     int i;
1958
1959     switch( h->mb.i_partition )
1960     {
1961     case D_16x16:
1962         if( h->mb.i_type == B_BI_BI )
1963             x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
1964         break;
1965     case D_16x8:
1966         for( i=0; i<2; i++ )
1967             if( a->i_mb_partition16x8[i] == D_BI_8x8 )
1968                 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
1969         break;
1970     case D_8x16:
1971         for( i=0; i<2; i++ )
1972             if( a->i_mb_partition8x16[i] == D_BI_8x8 )
1973                 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
1974         break;
1975     case D_8x8:
1976         for( i=0; i<4; i++ )
1977             if( h->mb.i_sub_partition[i] == D_BI_8x8 )
1978                 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
1979         break;
1980     }
1981 }
1982
1983 static inline void x264_mb_analyse_transform( x264_t *h )
1984 {
1985     h->mb.cache.b_transform_8x8_allowed =
1986         h->pps->b_transform_8x8_mode
1987         && !IS_INTRA( h->mb.i_type ) && x264_mb_transform_8x8_allowed( h );
1988
1989     if( h->mb.cache.b_transform_8x8_allowed && h->param.analyse.b_transform_8x8 )
1990     {
1991         int i_cost4, i_cost8;
1992         /* FIXME only luma mc is needed */
1993         x264_mb_mc( h );
1994
1995         i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
1996                                              h->mb.pic.p_fdec[0], FDEC_STRIDE );
1997         i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
1998                                              h->mb.pic.p_fdec[0], FDEC_STRIDE );
1999
2000         h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2001     }
2002 }
2003
2004 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2005 {
2006     h->mb.cache.b_transform_8x8_allowed =
2007         h->pps->b_transform_8x8_mode && x264_mb_transform_8x8_allowed( h );
2008
2009     if( h->mb.cache.b_transform_8x8_allowed && h->param.analyse.b_transform_8x8 )
2010     {
2011         int i_rd8;
2012         x264_analyse_update_cache( h, a );
2013         h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2014         /* FIXME only luma is needed, but the score for comparison already includes chroma */
2015         i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2016
2017         if( *i_rd >= i_rd8 )
2018         {
2019             if( *i_rd > 0 )
2020                 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2021             /* prevent a rare division by zero in estimated intra cost */
2022             if( *i_satd == 0 )
2023                 *i_satd = 1;
2024
2025             *i_rd = i_rd8;
2026         }
2027         else
2028             h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2029     }
2030 }
2031
2032
2033 /*****************************************************************************
2034  * x264_macroblock_analyse:
2035  *****************************************************************************/
2036 void x264_macroblock_analyse( x264_t *h )
2037 {
2038     x264_mb_analysis_t analysis;
2039     int i_cost = COST_MAX;
2040     int i;
2041
2042     /* init analysis */
2043     x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
2044
2045     /*--------------------------- Do the analysis ---------------------------*/
2046     if( h->sh.i_type == SLICE_TYPE_I )
2047     {
2048         x264_mb_analyse_intra( h, &analysis, COST_MAX );
2049         if( analysis.b_mbrd )
2050             x264_intra_rd( h, &analysis, COST_MAX );
2051
2052         i_cost = analysis.i_satd_i16x16;
2053         h->mb.i_type = I_16x16;
2054         if( analysis.i_satd_i4x4 < i_cost )
2055         {
2056             i_cost = analysis.i_satd_i4x4;
2057             h->mb.i_type = I_4x4;
2058         }
2059         if( analysis.i_satd_i8x8 < i_cost )
2060             h->mb.i_type = I_8x8;
2061
2062         if( h->mb.i_subpel_refine >= 7 )
2063             x264_intra_rd_refine( h, &analysis );
2064     }
2065     else if( h->sh.i_type == SLICE_TYPE_P )
2066     {
2067         int b_skip = 0;
2068         int i_intra_cost, i_intra_type;
2069
2070         h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2071
2072         /* Fast P_SKIP detection */
2073         analysis.b_try_pskip = 0;
2074         if( h->param.analyse.b_fast_pskip )
2075         {
2076             if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2077                 // FIXME don't need to check this if the reference frame is done
2078                 {}
2079             else if( h->param.analyse.i_subpel_refine >= 3 )
2080                 analysis.b_try_pskip = 1;
2081             else if( h->mb.i_mb_type_left == P_SKIP ||
2082                      h->mb.i_mb_type_top == P_SKIP ||
2083                      h->mb.i_mb_type_topleft == P_SKIP ||
2084                      h->mb.i_mb_type_topright == P_SKIP )
2085                 b_skip = x264_macroblock_probe_pskip( h );
2086         }
2087
2088         h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2089
2090         if( b_skip )
2091         {
2092             h->mb.i_type = P_SKIP;
2093             h->mb.i_partition = D_16x16;
2094             assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2095         }
2096         else
2097         {
2098             const unsigned int flags = h->param.analyse.inter;
2099             int i_type;
2100             int i_partition;
2101             int i_thresh16x8;
2102             int i_satd_inter, i_satd_intra;
2103
2104             x264_mb_analyse_load_costs( h, &analysis );
2105
2106             x264_mb_analyse_inter_p16x16( h, &analysis );
2107
2108             if( h->mb.i_type == P_SKIP )
2109                 return;
2110
2111             if( flags & X264_ANALYSE_PSUB16x16 )
2112             {
2113                 if( h->param.analyse.b_mixed_references )
2114                     x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2115                 else
2116                     x264_mb_analyse_inter_p8x8( h, &analysis );
2117             }
2118
2119             /* Select best inter mode */
2120             i_type = P_L0;
2121             i_partition = D_16x16;
2122             i_cost = analysis.l0.me16x16.cost;
2123
2124             if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2125                 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2126             {
2127                 i_type = P_8x8;
2128                 i_partition = D_8x8;
2129                 i_cost = analysis.l0.i_cost8x8;
2130
2131                 /* Do sub 8x8 */
2132                 if( flags & X264_ANALYSE_PSUB8x8 )
2133                 {
2134                     for( i = 0; i < 4; i++ )
2135                     {
2136                         x264_mb_analyse_inter_p4x4( h, &analysis, i );
2137                         if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2138                         {
2139                             int i_cost8x8 = analysis.l0.i_cost4x4[i];
2140                             h->mb.i_sub_partition[i] = D_L0_4x4;
2141
2142                             x264_mb_analyse_inter_p8x4( h, &analysis, i );
2143                             COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2144                                          h->mb.i_sub_partition[i], D_L0_8x4 );
2145
2146                             x264_mb_analyse_inter_p4x8( h, &analysis, i );
2147                             COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2148                                          h->mb.i_sub_partition[i], D_L0_4x8 );
2149
2150                             i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2151                         }
2152                         x264_mb_cache_mv_p8x8( h, &analysis, i );
2153                     }
2154                     analysis.l0.i_cost8x8 = i_cost;
2155                 }
2156             }
2157
2158             /* Now do 16x8/8x16 */
2159             i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2160             if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2161                 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2162             {
2163                 x264_mb_analyse_inter_p16x8( h, &analysis );
2164                 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2165
2166                 x264_mb_analyse_inter_p8x16( h, &analysis );
2167                 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2168             }
2169
2170             h->mb.i_partition = i_partition;
2171
2172             /* refine qpel */
2173             //FIXME mb_type costs?
2174             if( analysis.b_mbrd )
2175             {
2176                 /* refine later */
2177             }
2178             else if( i_partition == D_16x16 )
2179             {
2180                 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2181                 i_cost = analysis.l0.me16x16.cost;
2182             }
2183             else if( i_partition == D_16x8 )
2184             {
2185                 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2186                 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2187                 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2188             }
2189             else if( i_partition == D_8x16 )
2190             {
2191                 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2192                 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2193                 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2194             }
2195             else if( i_partition == D_8x8 )
2196             {
2197                 int i8x8;
2198                 i_cost = 0;
2199                 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2200                 {
2201                     switch( h->mb.i_sub_partition[i8x8] )
2202                     {
2203                         case D_L0_8x8:
2204                             x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2205                             i_cost += analysis.l0.me8x8[i8x8].cost;
2206                             break;
2207                         case D_L0_8x4:
2208                             x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2209                             x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2210                             i_cost += analysis.l0.me8x4[i8x8][0].cost +
2211                                       analysis.l0.me8x4[i8x8][1].cost;
2212                             break;
2213                         case D_L0_4x8:
2214                             x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2215                             x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2216                             i_cost += analysis.l0.me4x8[i8x8][0].cost +
2217                                       analysis.l0.me4x8[i8x8][1].cost;
2218                             break;
2219
2220                         case D_L0_4x4:
2221                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2222                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2223                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2224                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2225                             i_cost += analysis.l0.me4x4[i8x8][0].cost +
2226                                       analysis.l0.me4x4[i8x8][1].cost +
2227                                       analysis.l0.me4x4[i8x8][2].cost +
2228                                       analysis.l0.me4x4[i8x8][3].cost;
2229                             break;
2230                         default:
2231                             x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2232                             break;
2233                     }
2234                 }
2235             }
2236
2237             if( h->mb.b_chroma_me )
2238             {
2239                 x264_mb_analyse_intra_chroma( h, &analysis );
2240                 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2241                 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2242                 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2243                 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2244             }
2245             else
2246                 x264_mb_analyse_intra( h, &analysis, i_cost );
2247
2248             i_satd_inter = i_cost;
2249             i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2250                                       analysis.i_satd_i8x8,
2251                                       analysis.i_satd_i4x4 );
2252
2253             if( analysis.b_mbrd )
2254             {
2255                 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2256                 i_type = P_L0;
2257                 i_partition = D_16x16;
2258                 i_cost = analysis.l0.me16x16.cost;
2259                 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2260                 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2261                 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2262                 h->mb.i_type = i_type;
2263                 h->mb.i_partition = i_partition;
2264                 if( i_cost < COST_MAX )
2265                     x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2266                 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2267             }
2268
2269             i_intra_type = I_16x16;
2270             i_intra_cost = analysis.i_satd_i16x16;
2271             COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
2272             COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
2273             COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
2274
2275             if( i_intra_cost == COST_MAX )
2276                 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
2277
2278             h->mb.i_type = i_type;
2279             h->stat.frame.i_intra_cost += i_intra_cost;
2280             h->stat.frame.i_inter_cost += i_cost;
2281             h->stat.frame.i_mbs_analysed++;
2282
2283             if( h->mb.i_subpel_refine >= 7 )
2284             {
2285                 if( IS_INTRA( h->mb.i_type ) )
2286                 {
2287                     x264_intra_rd_refine( h, &analysis );
2288                 }
2289                 else if( i_partition == D_16x16 )
2290                 {
2291                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2292                     x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
2293                 }
2294                 else if( i_partition == D_16x8 )
2295                 {
2296                     x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2297                     x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2298                     x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
2299                     x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
2300                 }
2301                 else if( i_partition == D_8x16 )
2302                 {
2303                     x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2304                     x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2305                     x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
2306                     x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
2307                 }
2308                 else if( i_partition == D_8x8 )
2309                 {
2310                     int i8x8;
2311                     x264_analyse_update_cache( h, &analysis );
2312                     for( i8x8 = 0; i8x8 < 4; i8x8++ )
2313                          if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2314                              x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
2315                 }
2316             }
2317         }
2318     }
2319     else if( h->sh.i_type == SLICE_TYPE_B )
2320     {
2321         int i_bskip_cost = COST_MAX;
2322         int b_skip = 0;
2323
2324         h->mb.i_type = B_SKIP;
2325         if( h->mb.b_direct_auto_write )
2326         {
2327             /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2328             for( i = 0; i < 2; i++ )
2329             {
2330                 int b_changed = 1;
2331                 h->sh.b_direct_spatial_mv_pred ^= 1;
2332                 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2333                 if( analysis.b_direct_available )
2334                 {
2335                     if( b_changed )
2336                     {
2337                         x264_mb_mc( h );
2338                         b_skip = x264_macroblock_probe_bskip( h );
2339                     }
2340                     h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2341                 }
2342                 else
2343                     b_skip = 0;
2344             }
2345         }
2346         else
2347             analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2348
2349         if( analysis.b_direct_available )
2350         {
2351             if( !h->mb.b_direct_auto_write )
2352                 x264_mb_mc( h );
2353             if( h->mb.b_lossless )
2354             {
2355                 /* chance of skip is too small to bother */
2356             }
2357             else if( analysis.b_mbrd )
2358             {
2359                 i_bskip_cost = ssd_mb( h );
2360
2361                 /* 6 = minimum cavlc cost of a non-skipped MB */
2362                 if( i_bskip_cost <= 6 * analysis.i_lambda2 )
2363                 {
2364                     h->mb.i_type = B_SKIP;
2365                     x264_analyse_update_cache( h, &analysis );
2366                     return;
2367                 }
2368             }
2369             else if( !h->mb.b_direct_auto_write )
2370             {
2371                 /* Conditioning the probe on neighboring block types
2372                  * doesn't seem to help speed or quality. */
2373                 b_skip = x264_macroblock_probe_bskip( h );
2374             }
2375         }
2376
2377         if( !b_skip )
2378         {
2379             const unsigned int flags = h->param.analyse.inter;
2380             int i_type;
2381             int i_partition;
2382
2383             x264_mb_analyse_load_costs( h, &analysis );
2384
2385             /* select best inter mode */
2386             /* direct must be first */
2387             if( analysis.b_direct_available )
2388                 x264_mb_analyse_inter_direct( h, &analysis );
2389
2390             x264_mb_analyse_inter_b16x16( h, &analysis );
2391
2392             i_type = B_L0_L0;
2393             i_partition = D_16x16;
2394             i_cost = analysis.l0.me16x16.cost;
2395             COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2396             COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2397             COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2398
2399             if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2400             {
2401                 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2402                 if( i_bskip_cost < analysis.i_rd16x16direct &&
2403                     i_bskip_cost < analysis.i_rd16x16bi &&
2404                     i_bskip_cost < analysis.l0.i_rd16x16 &&
2405                     i_bskip_cost < analysis.l1.i_rd16x16 )
2406                 {
2407                     h->mb.i_type = B_SKIP;
2408                     x264_analyse_update_cache( h, &analysis );
2409                     return;
2410                 }
2411             }
2412
2413             if( flags & X264_ANALYSE_BSUB16x16 )
2414             {
2415                 x264_mb_analyse_inter_b8x8( h, &analysis );
2416                 if( analysis.i_cost8x8bi < i_cost )
2417                 {
2418                     i_type = B_8x8;
2419                     i_partition = D_8x8;
2420                     i_cost = analysis.i_cost8x8bi;
2421
2422                     if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2423                         h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2424                     {
2425                         x264_mb_analyse_inter_b16x8( h, &analysis );
2426                         COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2427                                      i_type, analysis.i_mb_type16x8,
2428                                      i_partition, D_16x8 );
2429                     }
2430                     if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2431                         h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2432                     {
2433                         x264_mb_analyse_inter_b8x16( h, &analysis );
2434                         COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2435                                      i_type, analysis.i_mb_type8x16,
2436                                      i_partition, D_8x16 );
2437                     }
2438                 }
2439             }
2440
2441             if( analysis.b_mbrd )
2442             {
2443                 /* refine later */
2444             }
2445             /* refine qpel */
2446             else if( i_partition == D_16x16 )
2447             {
2448                 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2449                 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2450                 if( i_type == B_L0_L0 )
2451                 {
2452                     x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2453                     i_cost = analysis.l0.me16x16.cost
2454                            + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2455                 }
2456                 else if( i_type == B_L1_L1 )
2457                 {
2458                     x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2459                     i_cost = analysis.l1.me16x16.cost
2460                            + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2461                 }
2462                 else if( i_type == B_BI_BI )
2463                 {
2464                     x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2465                     x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2466                 }
2467             }
2468             else if( i_partition == D_16x8 )
2469             {
2470                 for( i=0; i<2; i++ )
2471                 {
2472                     if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2473                         x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2474                     if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2475                         x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2476                 }
2477             }
2478             else if( i_partition == D_8x16 )
2479             {
2480                 for( i=0; i<2; i++ )
2481                 {
2482                     if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2483                         x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2484                     if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2485                         x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2486                 }
2487             }
2488             else if( i_partition == D_8x8 )
2489             {
2490                 for( i=0; i<4; i++ )
2491                 {
2492                     x264_me_t *m;
2493                     int i_part_cost_old;
2494                     int i_type_cost;
2495                     int i_part_type = h->mb.i_sub_partition[i];
2496                     int b_bidir = (i_part_type == D_BI_8x8);
2497
2498                     if( i_part_type == D_DIRECT_8x8 )
2499                         continue;
2500                     if( x264_mb_partition_listX_table[0][i_part_type] )
2501                     {
2502                         m = &analysis.l0.me8x8[i];
2503                         i_part_cost_old = m->cost;
2504                         i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2505                         m->cost -= i_type_cost;
2506                         x264_me_refine_qpel( h, m );
2507                         if( !b_bidir )
2508                             analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2509                     }
2510                     if( x264_mb_partition_listX_table[1][i_part_type] )
2511                     {
2512                         m = &analysis.l1.me8x8[i];
2513                         i_part_cost_old = m->cost;
2514                         i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2515                         m->cost -= i_type_cost;
2516                         x264_me_refine_qpel( h, m );
2517                         if( !b_bidir )
2518                             analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2519                     }
2520                     /* TODO: update mvp? */
2521                 }
2522             }
2523
2524             x264_mb_analyse_intra( h, &analysis, i_cost );
2525
2526             if( analysis.b_mbrd )
2527             {
2528                 int i_satd_inter = i_cost;
2529                 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2530                 i_type = B_SKIP;
2531                 i_cost = i_bskip_cost;
2532                 i_partition = D_16x16;
2533                 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2534                 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2535                 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2536                 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2537                 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2538                 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2539                 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2540
2541                 h->mb.i_type = i_type;
2542                 h->mb.i_partition = i_partition;
2543                 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2544                 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2545             }
2546
2547             COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2548             COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2549             COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2550
2551             h->mb.i_type = i_type;
2552             h->mb.i_partition = i_partition;
2553
2554             if( h->param.analyse.b_bidir_me )
2555                 refine_bidir( h, &analysis );
2556         }
2557     }
2558
2559     x264_analyse_update_cache( h, &analysis );
2560
2561     if( !analysis.b_mbrd )
2562         x264_mb_analyse_transform( h );
2563
2564     h->mb.b_trellis = h->param.analyse.i_trellis;
2565     h->mb.b_noise_reduction = h->param.analyse.i_noise_reduction;
2566 }
2567
2568 /*-------------------- Update MB from the analysis ----------------------*/
2569 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a  )
2570 {
2571     int i;
2572
2573     switch( h->mb.i_type )
2574     {
2575         case I_4x4:
2576             for( i = 0; i < 16; i++ )
2577                 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2578
2579             x264_mb_analyse_intra_chroma( h, a );
2580             break;
2581         case I_8x8:
2582             for( i = 0; i < 4; i++ )
2583                 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2584
2585             x264_mb_analyse_intra_chroma( h, a );
2586             break;
2587         case I_16x16:
2588             h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2589             x264_mb_analyse_intra_chroma( h, a );
2590             break;
2591
2592         case P_L0:
2593             switch( h->mb.i_partition )
2594             {
2595                 case D_16x16:
2596                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2597                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2598                     break;
2599
2600                 case D_16x8:
2601                     x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2602                     x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2603                     x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv[0], a->l0.me16x8[0].mv[1] );
2604                     x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv[0], a->l0.me16x8[1].mv[1] );
2605                     break;
2606
2607                 case D_8x16:
2608                     x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2609                     x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2610                     x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv[0], a->l0.me8x16[0].mv[1] );
2611                     x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv[0], a->l0.me8x16[1].mv[1] );
2612                     break;
2613
2614                 default:
2615                     x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2616                     break;
2617             }
2618             break;
2619
2620         case P_8x8:
2621             x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2622             x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2623             x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2624             x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2625             for( i = 0; i < 4; i++ )
2626                 x264_mb_cache_mv_p8x8( h, a, i );
2627             break;
2628
2629         case P_SKIP:
2630         {
2631             h->mb.i_partition = D_16x16;
2632             x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2633             x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv[0],
2634                                                          h->mb.cache.pskip_mv[1] );
2635             break;
2636         }
2637
2638         case B_SKIP:
2639         case B_DIRECT:
2640             x264_mb_load_mv_direct8x8( h, 0 );
2641             x264_mb_load_mv_direct8x8( h, 1 );
2642             x264_mb_load_mv_direct8x8( h, 2 );
2643             x264_mb_load_mv_direct8x8( h, 3 );
2644             break;
2645
2646         case B_8x8:
2647             /* optimize: cache might not need to be rewritten */
2648             for( i = 0; i < 4; i++ )
2649                 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2650             break;
2651
2652         default: /* the rest of the B types */
2653             switch( h->mb.i_partition )
2654             {
2655             case D_16x16:
2656                 switch( h->mb.i_type )
2657                 {
2658                 case B_L0_L0:
2659                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2660                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2661
2662                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2663                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1,  0, 0 );
2664                     x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1,  0, 0 );
2665                     break;
2666                 case B_L1_L1:
2667                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2668                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0,  0, 0 );
2669                     x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0,  0, 0 );
2670
2671                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2672                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2673                     break;
2674                 case B_BI_BI:
2675                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2676                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2677
2678                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2679                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2680                     break;
2681                 }
2682                 break;
2683             case D_16x8:
2684                 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2685                 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2686                 break;
2687             case D_8x16:
2688                 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2689                 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2690                 break;
2691             default:
2692                 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2693                 break;
2694             }
2695     }
2696
2697 #ifndef NDEBUG
2698     if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2699     {
2700         int l;
2701         for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2702         {
2703             int completed;
2704             int ref = h->mb.cache.ref[l][x264_scan8[0]];
2705             if( ref < 0 )
2706                 continue;
2707             completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2708             if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2709             {
2710                 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2711                 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2712                                 h->mb.cache.mv[l][x264_scan8[15]][0],
2713                                 h->mb.cache.mv[l][x264_scan8[15]][1] );
2714                 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2715                 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2716                 fprintf(stderr, "completed: %d \n", completed );
2717                 assert(0);
2718             }
2719         }
2720     }
2721 #endif
2722 }
2723
2724 #include "slicetype.c"
2725