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