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