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