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