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