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