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