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