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