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