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