]> git.sesse.net Git - x264/blob - encoder/analyse.c
CAVLC optimizations
[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_src = h->mb.pic.p_fenc[0];
877     uint8_t  *p_dst = h->mb.pic.p_fdec[0];
878
879     int i, j, idx, x, y;
880     int i_max, i_mode, i_thresh;
881     uint64_t i_satd, i_best;
882     int i_pred_mode;
883     int predict_mode[9];
884     h->mb.i_skip_intra = 0;
885
886     if( h->mb.i_type == I_16x16 )
887     {
888         int old_pred_mode = a->i_predict16x16;
889         i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
890         i_best = a->i_satd_i16x16;
891         predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
892         for( i = 0; i < i_max; i++ )
893         {
894             int i_mode = predict_mode[i];
895             if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
896                 continue;
897             h->mb.i_intra16x16_pred_mode = i_mode;
898             i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
899             COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
900         }
901     }
902
903     /* RD selection for chroma prediction */
904     predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
905     if( i_max > 1 )
906     {
907         i_thresh = a->i_satd_i8x8chroma * 5/4;
908
909         for( i = j = 0; i < i_max; i++ )
910             if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
911                 predict_mode[i] != a->i_predict8x8chroma )
912             {
913                 predict_mode[j++] = predict_mode[i];
914             }
915         i_max = j;
916
917         if( i_max > 0 )
918         {
919             int i_cbp_chroma_best = h->mb.i_cbp_chroma;
920             int i_chroma_lambda = x264_lambda2_tab[h->mb.i_chroma_qp];
921             /* the previous thing encoded was x264_intra_rd(), so the pixels and
922              * coefs for the current chroma mode are still around, so we only
923              * have to recount the bits. */
924             i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
925             for( i = 0; i < i_max; i++ )
926             {
927                 i_mode = predict_mode[i];
928                 if( h->mb.b_lossless )
929                     x264_predict_lossless_8x8_chroma( h, i_mode );
930                 else
931                 {
932                     h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
933                     h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
934                 }
935                 /* if we've already found a mode that needs no residual, then
936                  * probably any mode with a residual will be worse.
937                  * so avoid dct on the remaining modes to improve speed. */
938                 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
939                 COPY3_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode, i_cbp_chroma_best, h->mb.i_cbp_chroma );
940             }
941             h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
942             h->mb.i_cbp_chroma = i_cbp_chroma_best;
943         }
944     }
945
946     if( h->mb.i_type == I_4x4 )
947     {
948         uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
949         int i_nnz = 0;
950         for( idx = 0; idx < 16; idx++ )
951         {
952             uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
953             i_best = COST_MAX64;
954
955             i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
956
957             predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
958
959             if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
960                 /* emulate missing topright samples */
961                 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
962
963             for( i = 0; i < i_max; i++ )
964             {
965                 i_mode = predict_mode[i];
966                 if( h->mb.b_lossless )
967                     x264_predict_lossless_4x4( h, p_dst_by, idx, i_mode );
968                 else
969                     h->predict_4x4[i_mode]( p_dst_by );
970                 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
971
972                 if( i_best > i_satd )
973                 {
974                     a->i_predict4x4[idx] = i_mode;
975                     i_best = i_satd;
976                     pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
977                     pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
978                     pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
979                     pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
980                     i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
981                 }
982             }
983
984             *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
985             *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
986             *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
987             *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
988             h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
989
990             h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
991         }
992     }
993     else if( h->mb.i_type == I_8x8 )
994     {
995         DECLARE_ALIGNED_16( uint8_t edge[33] );
996         for( idx = 0; idx < 4; idx++ )
997         {
998             uint64_t pels_h = 0;
999             uint8_t pels_v[7];
1000             uint16_t i_nnz[2];
1001             uint8_t *p_src_by;
1002             uint8_t *p_dst_by;
1003             int j;
1004             int cbp_luma_new = 0;
1005             i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
1006
1007             i_best = COST_MAX64;
1008             i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
1009             x = idx&1;
1010             y = idx>>1;
1011
1012             p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
1013             p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
1014             predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
1015             x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
1016
1017             for( i = 0; i < i_max; i++ )
1018             {
1019                 i_mode = predict_mode[i];
1020                 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
1021                     continue;
1022                 if( h->mb.b_lossless )
1023                     x264_predict_lossless_8x8( h, p_dst_by, idx, i_mode, edge );
1024                 else
1025                     h->predict_8x8[i_mode]( p_dst_by, edge );
1026                 h->mb.i_cbp_luma = a->i_cbp_i8x8_luma;
1027                 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
1028
1029                 if( i_best > i_satd )
1030                 {
1031                     a->i_predict8x8[idx] = i_mode;
1032                     cbp_luma_new = h->mb.i_cbp_luma;
1033                     i_best = i_satd;
1034
1035                     pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
1036                     if( !(idx&1) )
1037                         for( j=0; j<7; j++ )
1038                             pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
1039                     i_nnz[0] = *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[4*idx+0]];
1040                     i_nnz[1] = *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[4*idx+2]];
1041                 }
1042             }
1043             a->i_cbp_i8x8_luma = cbp_luma_new;
1044             *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
1045             if( !(idx&1) )
1046                 for( j=0; j<7; j++ )
1047                     p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
1048             *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[4*idx+0]] = i_nnz[0];
1049             *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[4*idx+2]] = i_nnz[1];
1050
1051             x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
1052         }
1053     }
1054 }
1055
1056 #define LOAD_FENC( m, src, xoff, yoff) \
1057     (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
1058     (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
1059     (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
1060     (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
1061     (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
1062
1063 #define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
1064     (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
1065     (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
1066     (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
1067     (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
1068     (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
1069     (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
1070     (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
1071
1072 #define REF_COST(list, ref) \
1073     (a->p_cost_ref##list[ref])
1074
1075 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
1076 {
1077     x264_me_t m;
1078     int i_ref, i_mvc;
1079     DECLARE_ALIGNED_4( int16_t mvc[8][2] );
1080     int i_halfpel_thresh = INT_MAX;
1081     int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1082
1083     /* 16x16 Search on all ref frame */
1084     m.i_pixel = PIXEL_16x16;
1085     m.p_cost_mv = a->p_cost_mv;
1086     LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1087
1088     a->l0.me16x16.cost = INT_MAX;
1089     for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1090     {
1091         const int i_ref_cost = REF_COST( 0, i_ref );
1092         i_halfpel_thresh -= i_ref_cost;
1093         m.i_ref_cost = i_ref_cost;
1094         m.i_ref = i_ref;
1095
1096         /* search with ref */
1097         LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1098         x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1099         x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1100         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1101
1102         /* early termination
1103          * SSD threshold would probably be better than SATD */
1104         if( i_ref == 0
1105             && a->b_try_pskip
1106             && m.cost-m.cost_mv < 300*a->i_lambda
1107             &&  abs(m.mv[0]-h->mb.cache.pskip_mv[0])
1108               + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
1109             && x264_macroblock_probe_pskip( h ) )
1110         {
1111             h->mb.i_type = P_SKIP;
1112             x264_analyse_update_cache( h, a );
1113             assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1114             return;
1115         }
1116
1117         m.cost += i_ref_cost;
1118         i_halfpel_thresh += i_ref_cost;
1119
1120         if( m.cost < a->l0.me16x16.cost )
1121             h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1122
1123         /* save mv for predicting neighbors */
1124         *(uint32_t*)a->l0.mvc[i_ref][0] =
1125         *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1126     }
1127
1128     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
1129     assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1130
1131     h->mb.i_type = P_L0;
1132     if( a->i_mbrd )
1133     {
1134         x264_mb_cache_fenc_satd( h );
1135         if( a->l0.me16x16.i_ref == 0 && *(uint32_t*)a->l0.me16x16.mv == *(uint32_t*)h->mb.cache.pskip_mv )
1136         {
1137             h->mb.i_partition = D_16x16;
1138             x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
1139             a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1140         }
1141     }
1142 }
1143
1144 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
1145 {
1146     x264_me_t m;
1147     int i_ref;
1148     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1149     int i_halfpel_thresh = INT_MAX;
1150     int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
1151     int i;
1152     int i_maxref = h->mb.pic.i_fref[0]-1;
1153
1154     h->mb.i_partition = D_8x8;
1155
1156     /* early termination: if 16x16 chose ref 0, then evalute no refs older
1157      * than those used by the neighbors */
1158     if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
1159         h->mb.i_mb_type_top && h->mb.i_mb_type_left )
1160     {
1161         i_maxref = 0;
1162         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
1163         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
1164         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
1165         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
1166         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
1167         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
1168     }
1169
1170     for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1171          *(uint32_t*)a->l0.mvc[i_ref][0] = *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy];
1172
1173     for( i = 0; i < 4; i++ )
1174     {
1175         x264_me_t *l0m = &a->l0.me8x8[i];
1176         const int x8 = i%2;
1177         const int y8 = i/2;
1178
1179         m.i_pixel = PIXEL_8x8;
1180         m.p_cost_mv = a->p_cost_mv;
1181
1182         LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
1183         l0m->cost = INT_MAX;
1184         for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
1185         {
1186             const int i_ref_cost = REF_COST( 0, i_ref );
1187             i_halfpel_thresh -= i_ref_cost;
1188             m.i_ref_cost = i_ref_cost;
1189             m.i_ref = i_ref;
1190
1191             LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
1192             x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
1193             x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1194             x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
1195
1196             m.cost += i_ref_cost;
1197             i_halfpel_thresh += i_ref_cost;
1198             *(uint32_t*)a->l0.mvc[i_ref][i+1] = *(uint32_t*)m.mv;
1199
1200             if( m.cost < l0m->cost )
1201                 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1202         }
1203         x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv );
1204         x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
1205
1206         /* mb type cost */
1207         l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1208     }
1209
1210     a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1211                       a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1212     /* P_8x8 ref0 has no ref cost */
1213     if( !h->param.b_cabac && !(a->l0.me8x8[0].i_ref | a->l0.me8x8[1].i_ref |
1214                                a->l0.me8x8[2].i_ref | a->l0.me8x8[3].i_ref) )
1215         a->l0.i_cost8x8 -= REF_COST( 0, 0 ) * 4;
1216     h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1217     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1218 }
1219
1220 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
1221 {
1222     const int i_ref = a->l0.me16x16.i_ref;
1223     const int i_ref_cost = h->param.b_cabac || i_ref ? REF_COST( 0, i_ref ) : 0;
1224     uint8_t  **p_fref = h->mb.pic.p_fref[0][i_ref];
1225     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1226     int i_mvc;
1227     int16_t (*mvc)[2] = a->l0.mvc[i_ref];
1228     int i;
1229
1230     /* XXX Needed for x264_mb_predict_mv */
1231     h->mb.i_partition = D_8x8;
1232
1233     i_mvc = 1;
1234     *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.me16x16.mv;
1235
1236     for( i = 0; i < 4; i++ )
1237     {
1238         x264_me_t *m = &a->l0.me8x8[i];
1239         const int x8 = i%2;
1240         const int y8 = i/2;
1241
1242         m->i_pixel = PIXEL_8x8;
1243         m->p_cost_mv = a->p_cost_mv;
1244         m->i_ref_cost = i_ref_cost;
1245         m->i_ref = i_ref;
1246
1247         LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
1248         LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
1249         x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1250         x264_me_search( h, m, mvc, i_mvc );
1251
1252         x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, m->mv );
1253
1254         *(uint32_t*)mvc[i_mvc] = *(uint32_t*)m->mv;
1255         i_mvc++;
1256
1257         /* mb type cost */
1258         m->cost += i_ref_cost;
1259         m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
1260     }
1261
1262     a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
1263                       a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
1264     /* theoretically this should include 4*ref_cost,
1265      * but 3 seems a better approximation of cabac. */
1266     if( h->param.b_cabac )
1267         a->l0.i_cost8x8 -= i_ref_cost;
1268     h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1269     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1270 }
1271
1272 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
1273 {
1274     x264_me_t m;
1275     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1276     DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1277     int i, j;
1278
1279     /* XXX Needed for x264_mb_predict_mv */
1280     h->mb.i_partition = D_16x8;
1281
1282     for( i = 0; i < 2; i++ )
1283     {
1284         x264_me_t *l0m = &a->l0.me16x8[i];
1285         const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
1286         const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1287
1288         m.i_pixel = PIXEL_16x8;
1289         m.p_cost_mv = a->p_cost_mv;
1290
1291         LOAD_FENC( &m, p_fenc, 0, 8*i );
1292         l0m->cost = INT_MAX;
1293         for( j = 0; j < i_ref8s; j++ )
1294         {
1295             const int i_ref = ref8[j];
1296             const int i_ref_cost = REF_COST( 0, i_ref );
1297             m.i_ref_cost = i_ref_cost;
1298             m.i_ref = i_ref;
1299
1300             /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
1301             *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1302             *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][2*i+1];
1303             *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][2*i+2];
1304
1305             LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
1306             x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
1307             x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
1308             x264_me_search( h, &m, mvc, 3 );
1309
1310             m.cost += i_ref_cost;
1311
1312             if( m.cost < l0m->cost )
1313                 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1314         }
1315         x264_macroblock_cache_mv_ptr( h, 0, 2*i, 4, 2, 0, l0m->mv );
1316         x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
1317     }
1318
1319     a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
1320 }
1321
1322 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
1323 {
1324     x264_me_t m;
1325     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1326     DECLARE_ALIGNED_4( int16_t mvc[3][2] );
1327     int i, j;
1328
1329     /* XXX Needed for x264_mb_predict_mv */
1330     h->mb.i_partition = D_8x16;
1331
1332     for( i = 0; i < 2; i++ )
1333     {
1334         x264_me_t *l0m = &a->l0.me8x16[i];
1335         const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
1336         const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
1337
1338         m.i_pixel = PIXEL_8x16;
1339         m.p_cost_mv = a->p_cost_mv;
1340
1341         LOAD_FENC( &m, p_fenc, 8*i, 0 );
1342         l0m->cost = INT_MAX;
1343         for( j = 0; j < i_ref8s; j++ )
1344         {
1345             const int i_ref = ref8[j];
1346             const int i_ref_cost = REF_COST( 0, i_ref );
1347             m.i_ref_cost = i_ref_cost;
1348             m.i_ref = i_ref;
1349
1350             *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
1351             *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][i+1];
1352             *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][i+3];
1353
1354             LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
1355             x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
1356             x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
1357             x264_me_search( h, &m, mvc, 3 );
1358
1359             m.cost += i_ref_cost;
1360
1361             if( m.cost < l0m->cost )
1362                 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
1363         }
1364         x264_macroblock_cache_mv_ptr( h, 2*i, 0, 2, 4, 0, l0m->mv );
1365         x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
1366     }
1367
1368     a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
1369 }
1370
1371 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
1372 {
1373     DECLARE_ALIGNED_8( uint8_t pix1[16*8] );
1374     uint8_t *pix2 = pix1+8;
1375     const int i_stride = h->mb.pic.i_stride[1];
1376     const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
1377     const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
1378
1379 #define CHROMA4x4MC( width, height, me, x, y ) \
1380     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 ); \
1381     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 );
1382
1383     if( pixel == PIXEL_4x4 )
1384     {
1385         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
1386         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 2,0 );
1387         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 0,2 );
1388         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
1389     }
1390     else if( pixel == PIXEL_8x4 )
1391     {
1392         CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
1393         CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
1394     }
1395     else
1396     {
1397         CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1398         CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1399     }
1400
1401     return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
1402          + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
1403 }
1404
1405 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1406 {
1407     uint8_t  **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1408     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1409     const int i_ref = a->l0.me8x8[i8x8].i_ref;
1410     int i4x4;
1411
1412     /* XXX Needed for x264_mb_predict_mv */
1413     h->mb.i_partition = D_8x8;
1414
1415     for( i4x4 = 0; i4x4 < 4; i4x4++ )
1416     {
1417         const int idx = 4*i8x8 + i4x4;
1418         const int x4 = block_idx_x[idx];
1419         const int y4 = block_idx_y[idx];
1420         const int i_mvc = (i4x4 == 0);
1421
1422         x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1423
1424         m->i_pixel = PIXEL_4x4;
1425         m->p_cost_mv = a->p_cost_mv;
1426
1427         LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1428         LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1429
1430         x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1431         x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1432
1433         x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 1, 0, m->mv );
1434     }
1435     a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1436                             a->l0.me4x4[i8x8][1].cost +
1437                             a->l0.me4x4[i8x8][2].cost +
1438                             a->l0.me4x4[i8x8][3].cost +
1439                             REF_COST( 0, i_ref ) +
1440                             a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1441     if( h->mb.b_chroma_me )
1442         a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1443 }
1444
1445 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1446 {
1447     uint8_t  **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1448     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1449     const int i_ref = a->l0.me8x8[i8x8].i_ref;
1450     int i8x4;
1451
1452     /* XXX Needed for x264_mb_predict_mv */
1453     h->mb.i_partition = D_8x8;
1454
1455     for( i8x4 = 0; i8x4 < 2; i8x4++ )
1456     {
1457         const int idx = 4*i8x8 + 2*i8x4;
1458         const int x4 = block_idx_x[idx];
1459         const int y4 = block_idx_y[idx];
1460         const int i_mvc = (i8x4 == 0);
1461
1462         x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1463
1464         m->i_pixel = PIXEL_8x4;
1465         m->p_cost_mv = a->p_cost_mv;
1466
1467         LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1468         LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1469
1470         x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1471         x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1472
1473         x264_macroblock_cache_mv_ptr( h, x4, y4, 2, 1, 0, m->mv );
1474     }
1475     a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1476                             REF_COST( 0, i_ref ) +
1477                             a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1478     if( h->mb.b_chroma_me )
1479         a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1480 }
1481
1482 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1483 {
1484     uint8_t  **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1485     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1486     const int i_ref = a->l0.me8x8[i8x8].i_ref;
1487     int i4x8;
1488
1489     /* XXX Needed for x264_mb_predict_mv */
1490     h->mb.i_partition = D_8x8;
1491
1492     for( i4x8 = 0; i4x8 < 2; i4x8++ )
1493     {
1494         const int idx = 4*i8x8 + i4x8;
1495         const int x4 = block_idx_x[idx];
1496         const int y4 = block_idx_y[idx];
1497         const int i_mvc = (i4x8 == 0);
1498
1499         x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1500
1501         m->i_pixel = PIXEL_4x8;
1502         m->p_cost_mv = a->p_cost_mv;
1503
1504         LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1505         LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
1506
1507         x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1508         x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1509
1510         x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 2, 0, m->mv );
1511     }
1512     a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1513                             REF_COST( 0, i_ref ) +
1514                             a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1515     if( h->mb.b_chroma_me )
1516         a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1517 }
1518
1519 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1520 {
1521     /* Assumes that fdec still contains the results of
1522      * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1523
1524     uint8_t **p_fenc = h->mb.pic.p_fenc;
1525     uint8_t **p_fdec = h->mb.pic.p_fdec;
1526     int i;
1527
1528     a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1529     for( i = 0; i < 4; i++ )
1530     {
1531         const int x = (i&1)*8;
1532         const int y = (i>>1)*8;
1533         a->i_cost16x16direct +=
1534         a->i_cost8x8direct[i] =
1535             h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
1536
1537         /* mb type cost */
1538         a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1539     }
1540 }
1541
1542 #define WEIGHTED_AVG( size, pix, stride, src1, stride1, src2, stride2 ) \
1543 { \
1544     h->mc.avg[size]( pix, stride, src1, stride1, src2, stride2, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1545 }
1546
1547 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1548 {
1549     DECLARE_ALIGNED_16( uint8_t pix0[16*16] );
1550     DECLARE_ALIGNED_16( uint8_t pix1[16*16] );
1551     uint8_t *src0, *src1;
1552     int stride0 = 16, stride1 = 16;
1553
1554     x264_me_t m;
1555     int i_ref, i_mvc;
1556     DECLARE_ALIGNED_4( int16_t mvc[9][2] );
1557     int i_halfpel_thresh = INT_MAX;
1558     int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1559
1560     /* 16x16 Search on all ref frame */
1561     m.i_pixel = PIXEL_16x16;
1562     m.p_cost_mv = a->p_cost_mv;
1563     LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1564
1565     /* ME for List 0 */
1566     a->l0.me16x16.cost = INT_MAX;
1567     for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1568     {
1569         /* search with ref */
1570         LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1571         x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1572         x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1573         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1574
1575         /* add ref cost */
1576         m.cost += REF_COST( 0, i_ref );
1577
1578         if( m.cost < a->l0.me16x16.cost )
1579         {
1580             a->l0.i_ref = i_ref;
1581             h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1582         }
1583
1584         /* save mv for predicting neighbors */
1585         *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1586     }
1587     /* subtract ref cost, so we don't have to add it for the other MB types */
1588     a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1589
1590     /* ME for list 1 */
1591     i_halfpel_thresh = INT_MAX;
1592     p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1593     a->l1.me16x16.cost = INT_MAX;
1594     for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1595     {
1596         /* search with ref */
1597         LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1598         x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1599         x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1600         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1601
1602         /* add ref cost */
1603         m.cost += REF_COST( 1, i_ref );
1604
1605         if( m.cost < a->l1.me16x16.cost )
1606         {
1607             a->l1.i_ref = i_ref;
1608             h->mc.memcpy_aligned( &a->l1.me16x16, &m, sizeof(x264_me_t) );
1609         }
1610
1611         /* save mv for predicting neighbors */
1612         *(uint32_t*)h->mb.mvr[1][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1613     }
1614     /* subtract ref cost, so we don't have to add it for the other MB types */
1615     a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1616
1617     /* Set global ref, needed for other modes? */
1618     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1619     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1620
1621     /* get cost of BI mode */
1622     src0 = h->mc.get_ref( pix0, &stride0,
1623                            h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1624                            a->l0.me16x16.mv[0], a->l0.me16x16.mv[1], 16, 16 );
1625     src1 = h->mc.get_ref( pix1, &stride1,
1626                            h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1627                            a->l1.me16x16.mv[0], a->l1.me16x16.mv[1], 16, 16 );
1628
1629     h->mc.avg[PIXEL_16x16]( pix0, 16, src0, stride0, src1, stride1, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
1630
1631     a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix0, 16 )
1632                      + REF_COST( 0, a->l0.i_ref )
1633                      + REF_COST( 1, a->l1.i_ref )
1634                      + a->l0.me16x16.cost_mv
1635                      + a->l1.me16x16.cost_mv;
1636
1637     /* mb type cost */
1638     a->i_cost16x16bi   += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1639     a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1640     a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1641 }
1642
1643 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1644 {
1645     const int x = 2*(i%2);
1646     const int y = 2*(i/2);
1647
1648     switch( h->mb.i_sub_partition[i] )
1649     {
1650         case D_L0_8x8:
1651             x264_macroblock_cache_mv_ptr( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv );
1652             break;
1653         case D_L0_8x4:
1654             x264_macroblock_cache_mv_ptr( h, x, y+0, 2, 1, 0, a->l0.me8x4[i][0].mv );
1655             x264_macroblock_cache_mv_ptr( h, x, y+1, 2, 1, 0, a->l0.me8x4[i][1].mv );
1656             break;
1657         case D_L0_4x8:
1658             x264_macroblock_cache_mv_ptr( h, x+0, y, 1, 2, 0, a->l0.me4x8[i][0].mv );
1659             x264_macroblock_cache_mv_ptr( h, x+1, y, 1, 2, 0, a->l0.me4x8[i][1].mv );
1660             break;
1661         case D_L0_4x4:
1662             x264_macroblock_cache_mv_ptr( h, x+0, y+0, 1, 1, 0, a->l0.me4x4[i][0].mv );
1663             x264_macroblock_cache_mv_ptr( h, x+1, y+0, 1, 1, 0, a->l0.me4x4[i][1].mv );
1664             x264_macroblock_cache_mv_ptr( h, x+0, y+1, 1, 1, 0, a->l0.me4x4[i][2].mv );
1665             x264_macroblock_cache_mv_ptr( h, x+1, y+1, 1, 1, 0, a->l0.me4x4[i][3].mv );
1666             break;
1667         default:
1668             x264_log( h, X264_LOG_ERROR, "internal error\n" );
1669             break;
1670     }
1671 }
1672
1673 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1674     if( x264_mb_partition_listX_table[0][part] ) \
1675     { \
1676         x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1677         x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 0, me0.mv ); \
1678     } \
1679     else \
1680     { \
1681         x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1682         x264_macroblock_cache_mv(  h, x,y,dx,dy, 0, 0 ); \
1683         if( b_mvd ) \
1684             x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0 ); \
1685     } \
1686     if( x264_mb_partition_listX_table[1][part] ) \
1687     { \
1688         x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1689         x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 1, me1.mv ); \
1690     } \
1691     else \
1692     { \
1693         x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1694         x264_macroblock_cache_mv(  h, x,y,dx,dy, 1, 0 ); \
1695         if( b_mvd ) \
1696             x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0 ); \
1697     }
1698
1699 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1700 {
1701     int x = (i%2)*2;
1702     int y = (i/2)*2;
1703     if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1704     {
1705         x264_mb_load_mv_direct8x8( h, i );
1706         if( b_mvd )
1707         {
1708             x264_macroblock_cache_mvd(  h, x, y, 2, 2, 0, 0 );
1709             x264_macroblock_cache_mvd(  h, x, y, 2, 2, 1, 0 );
1710             x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1711         }
1712     }
1713     else
1714     {
1715         CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1716     }
1717 }
1718 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1719 {
1720     CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1721 }
1722 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1723 {
1724     CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1725 }
1726 #undef CACHE_MV_BI
1727
1728 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1729 {
1730     uint8_t **p_fref[2] =
1731         { h->mb.pic.p_fref[0][a->l0.i_ref],
1732           h->mb.pic.p_fref[1][a->l1.i_ref] };
1733     DECLARE_ALIGNED_8( uint8_t pix[2][8*8] );
1734     int i, l;
1735
1736     /* XXX Needed for x264_mb_predict_mv */
1737     h->mb.i_partition = D_8x8;
1738
1739     a->i_cost8x8bi = 0;
1740
1741     for( i = 0; i < 4; i++ )
1742     {
1743         const int x8 = i%2;
1744         const int y8 = i/2;
1745         int i_part_cost;
1746         int i_part_cost_bi = 0;
1747         int stride[2] = {8,8};
1748         uint8_t *src[2];
1749
1750         for( l = 0; l < 2; l++ )
1751         {
1752             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1753             x264_me_t *m = &lX->me8x8[i];
1754
1755             m->i_pixel = PIXEL_8x8;
1756             m->p_cost_mv = a->p_cost_mv;
1757
1758             LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1759             LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1760
1761             x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1762             x264_me_search( h, m, &lX->me16x16.mv, 1 );
1763
1764             x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, l, m->mv );
1765
1766             /* BI mode */
1767             src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1768                                     m->mv[0], m->mv[1], 8, 8 );
1769             i_part_cost_bi += m->cost_mv;
1770             /* FIXME: ref cost */
1771         }
1772         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] );
1773         i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1774                         + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1775         a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1776         a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1777
1778         i_part_cost = a->l0.me8x8[i].cost;
1779         h->mb.i_sub_partition[i] = D_L0_8x8;
1780         COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1781         COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1782         COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1783         a->i_cost8x8bi += i_part_cost;
1784
1785         /* XXX Needed for x264_mb_predict_mv */
1786         x264_mb_cache_mv_b8x8( h, a, i, 0 );
1787     }
1788
1789     /* mb type cost */
1790     a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1791 }
1792
1793 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1794 {
1795     uint8_t **p_fref[2] =
1796         { h->mb.pic.p_fref[0][a->l0.i_ref],
1797           h->mb.pic.p_fref[1][a->l1.i_ref] };
1798     DECLARE_ALIGNED_16( uint8_t pix[2][16*8] );
1799     DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1800     int i, l;
1801
1802     h->mb.i_partition = D_16x8;
1803     a->i_cost16x8bi = 0;
1804
1805     for( i = 0; i < 2; i++ )
1806     {
1807         int i_part_cost;
1808         int i_part_cost_bi = 0;
1809         int stride[2] = {16,16};
1810         uint8_t *src[2];
1811
1812         /* TODO: check only the list(s) that were used in b8x8? */
1813         for( l = 0; l < 2; l++ )
1814         {
1815             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1816             x264_me_t *m = &lX->me16x8[i];
1817
1818             m->i_pixel = PIXEL_16x8;
1819             m->p_cost_mv = a->p_cost_mv;
1820
1821             LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1822             LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1823
1824             *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[2*i].mv;
1825             *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[2*i+1].mv;
1826
1827             x264_mb_predict_mv( h, l, 8*i, 2, m->mvp );
1828             x264_me_search( h, m, mvc, 2 );
1829
1830             /* BI mode */
1831             src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1832                                     m->mv[0], m->mv[1], 16, 8 );
1833             /* FIXME: ref cost */
1834             i_part_cost_bi += m->cost_mv;
1835         }
1836         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] );
1837         i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1838
1839         i_part_cost = a->l0.me16x8[i].cost;
1840         a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1841         if( a->l1.me16x8[i].cost < i_part_cost )
1842         {
1843             i_part_cost = a->l1.me16x8[i].cost;
1844             a->i_mb_partition16x8[i] = D_L1_8x8;
1845         }
1846         if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1847         {
1848             i_part_cost = i_part_cost_bi;
1849             a->i_mb_partition16x8[i] = D_BI_8x8;
1850         }
1851         a->i_cost16x8bi += i_part_cost;
1852
1853         x264_mb_cache_mv_b16x8( h, a, i, 0 );
1854     }
1855
1856     /* mb type cost */
1857     a->i_mb_type16x8 = B_L0_L0
1858         + (a->i_mb_partition16x8[0]>>2) * 3
1859         + (a->i_mb_partition16x8[1]>>2);
1860     a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1861 }
1862
1863 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1864 {
1865     uint8_t **p_fref[2] =
1866         { h->mb.pic.p_fref[0][a->l0.i_ref],
1867           h->mb.pic.p_fref[1][a->l1.i_ref] };
1868     DECLARE_ALIGNED_8( uint8_t pix[2][8*16] );
1869     DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1870     int i, l;
1871
1872     h->mb.i_partition = D_8x16;
1873     a->i_cost8x16bi = 0;
1874
1875     for( i = 0; i < 2; i++ )
1876     {
1877         int i_part_cost;
1878         int i_part_cost_bi = 0;
1879         int stride[2] = {8,8};
1880         uint8_t *src[2];
1881
1882         for( l = 0; l < 2; l++ )
1883         {
1884             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1885             x264_me_t *m = &lX->me8x16[i];
1886
1887             m->i_pixel = PIXEL_8x16;
1888             m->p_cost_mv = a->p_cost_mv;
1889
1890             LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1891             LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1892
1893             *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[i].mv;
1894             *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[i+2].mv;
1895
1896             x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1897             x264_me_search( h, m, mvc, 2 );
1898
1899             /* BI mode */
1900             src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref,  m->i_stride[0],
1901                                     m->mv[0], m->mv[1], 8, 16 );
1902             /* FIXME: ref cost */
1903             i_part_cost_bi += m->cost_mv;
1904         }
1905
1906         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] );
1907         i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1908
1909         i_part_cost = a->l0.me8x16[i].cost;
1910         a->i_mb_partition8x16[i] = D_L0_8x8;
1911         if( a->l1.me8x16[i].cost < i_part_cost )
1912         {
1913             i_part_cost = a->l1.me8x16[i].cost;
1914             a->i_mb_partition8x16[i] = D_L1_8x8;
1915         }
1916         if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1917         {
1918             i_part_cost = i_part_cost_bi;
1919             a->i_mb_partition8x16[i] = D_BI_8x8;
1920         }
1921         a->i_cost8x16bi += i_part_cost;
1922
1923         x264_mb_cache_mv_b8x16( h, a, i, 0 );
1924     }
1925
1926     /* mb type cost */
1927     a->i_mb_type8x16 = B_L0_L0
1928         + (a->i_mb_partition8x16[0]>>2) * 3
1929         + (a->i_mb_partition8x16[1]>>2);
1930     a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1931 }
1932
1933 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1934 {
1935     int thresh = i_satd * 5/4;
1936
1937     h->mb.i_type = P_L0;
1938     if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1939     {
1940         h->mb.i_partition = D_16x16;
1941         x264_analyse_update_cache( h, a );
1942         a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1943     }
1944     a->l0.me16x16.cost = a->l0.i_rd16x16;
1945
1946     if( a->l0.i_cost16x8 <= thresh )
1947     {
1948         h->mb.i_partition = D_16x8;
1949         x264_analyse_update_cache( h, a );
1950         a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1951     }
1952     else
1953         a->l0.i_cost16x8 = COST_MAX;
1954
1955     if( a->l0.i_cost8x16 <= thresh )
1956     {
1957         h->mb.i_partition = D_8x16;
1958         x264_analyse_update_cache( h, a );
1959         a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1960     }
1961     else
1962         a->l0.i_cost8x16 = COST_MAX;
1963
1964     if( a->l0.i_cost8x8 <= thresh )
1965     {
1966         h->mb.i_type = P_8x8;
1967         h->mb.i_partition = D_8x8;
1968         if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1969         {
1970             int i;
1971             x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
1972             x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
1973             x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
1974             x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
1975             /* FIXME: In the 8x8 blocks where RDO isn't run, the NNZ values used for context selection
1976              * for future blocks are those left over from previous RDO calls. */
1977             for( i = 0; i < 4; i++ )
1978             {
1979                 int costs[4] = {a->l0.i_cost4x4[i], a->l0.i_cost8x4[i], a->l0.i_cost4x8[i], a->l0.me8x8[i].cost};
1980                 int thresh = X264_MIN4( costs[0], costs[1], costs[2], costs[3] ) * 5 / 4;
1981                 int subtype, btype = D_L0_8x8;
1982                 uint64_t bcost = COST_MAX64;
1983                 for( subtype = D_L0_4x4; subtype <= D_L0_8x8; subtype++ )
1984                 {
1985                     uint64_t cost;
1986                     if( costs[subtype] > thresh || (subtype == D_L0_8x8 && bcost == COST_MAX64) )
1987                         continue;
1988                     h->mb.i_sub_partition[i] = subtype;
1989                     x264_mb_cache_mv_p8x8( h, a, i );
1990                     cost = x264_rd_cost_part( h, a->i_lambda2, i<<2, PIXEL_8x8 );
1991                     COPY2_IF_LT( bcost, cost, btype, subtype );
1992                 }
1993                 h->mb.i_sub_partition[i] = btype;
1994                 x264_mb_cache_mv_p8x8( h, a, i );
1995             }
1996         }
1997         else
1998             x264_analyse_update_cache( h, a );
1999         a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
2000     }
2001     else
2002         a->l0.i_cost8x8 = COST_MAX;
2003 }
2004
2005 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
2006 {
2007     int thresh = i_satd_inter * (17 + (!!h->mb.i_psy_rd))/16;
2008
2009     if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
2010     {
2011         h->mb.i_type = B_DIRECT;
2012         /* Assumes direct/skip MC is still in fdec */
2013         /* Requires b-rdo to be done before intra analysis */
2014         h->mb.b_skip_mc = 1;
2015         x264_analyse_update_cache( h, a );
2016         a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
2017         h->mb.b_skip_mc = 0;
2018     }
2019
2020     //FIXME not all the update_cache calls are needed
2021     h->mb.i_partition = D_16x16;
2022     /* L0 */
2023     if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
2024     {
2025         h->mb.i_type = B_L0_L0;
2026         x264_analyse_update_cache( h, a );
2027         a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
2028     }
2029
2030     /* L1 */
2031     if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
2032     {
2033         h->mb.i_type = B_L1_L1;
2034         x264_analyse_update_cache( h, a );
2035         a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
2036     }
2037
2038     /* BI */
2039     if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
2040     {
2041         h->mb.i_type = B_BI_BI;
2042         x264_analyse_update_cache( h, a );
2043         a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
2044     }
2045
2046     /* 8x8 */
2047     if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
2048     {
2049         h->mb.i_type = B_8x8;
2050         h->mb.i_partition = D_8x8;
2051         x264_analyse_update_cache( h, a );
2052         a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
2053         x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
2054     }
2055
2056     /* 16x8 */
2057     if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
2058     {
2059         h->mb.i_type = a->i_mb_type16x8;
2060         h->mb.i_partition = D_16x8;
2061         x264_analyse_update_cache( h, a );
2062         a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
2063     }
2064
2065     /* 8x16 */
2066     if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
2067     {
2068         h->mb.i_type = a->i_mb_type8x16;
2069         h->mb.i_partition = D_8x16;
2070         x264_analyse_update_cache( h, a );
2071         a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
2072     }
2073 }
2074
2075 static void x264_refine_bidir( x264_t *h, x264_mb_analysis_t *a )
2076 {
2077     const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
2078     int i;
2079
2080     if( IS_INTRA(h->mb.i_type) )
2081         return;
2082
2083     switch( h->mb.i_partition )
2084     {
2085     case D_16x16:
2086         if( h->mb.i_type == B_BI_BI )
2087             x264_me_refine_bidir_satd( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
2088         break;
2089     case D_16x8:
2090         for( i=0; i<2; i++ )
2091             if( a->i_mb_partition16x8[i] == D_BI_8x8 )
2092                 x264_me_refine_bidir_satd( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
2093         break;
2094     case D_8x16:
2095         for( i=0; i<2; i++ )
2096             if( a->i_mb_partition8x16[i] == D_BI_8x8 )
2097                 x264_me_refine_bidir_satd( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
2098         break;
2099     case D_8x8:
2100         for( i=0; i<4; i++ )
2101             if( h->mb.i_sub_partition[i] == D_BI_8x8 )
2102                 x264_me_refine_bidir_satd( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
2103         break;
2104     }
2105 }
2106
2107 static inline void x264_mb_analyse_transform( x264_t *h )
2108 {
2109     if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 && !h->mb.b_lossless )
2110     {
2111         int i_cost4, i_cost8;
2112         /* Only luma MC is really needed, but the full MC is re-used in macroblock_encode. */
2113         x264_mb_mc( h );
2114
2115         i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2116                                              h->mb.pic.p_fdec[0], FDEC_STRIDE );
2117         i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2118                                              h->mb.pic.p_fdec[0], FDEC_STRIDE );
2119
2120         h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2121         h->mb.b_skip_mc = 1;
2122     }
2123 }
2124
2125 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2126 {
2127     if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2128     {
2129         int i_rd8;
2130         x264_analyse_update_cache( h, a );
2131         h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2132         /* FIXME only luma is needed, but the score for comparison already includes chroma */
2133         i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2134
2135         if( *i_rd >= i_rd8 )
2136         {
2137             if( *i_rd > 0 )
2138                 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2139             *i_rd = i_rd8;
2140         }
2141         else
2142             h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2143     }
2144 }
2145
2146
2147 /*****************************************************************************
2148  * x264_macroblock_analyse:
2149  *****************************************************************************/
2150 void x264_macroblock_analyse( x264_t *h )
2151 {
2152     x264_mb_analysis_t analysis;
2153     int i_cost = COST_MAX;
2154     int i;
2155
2156     h->mb.i_qp = x264_ratecontrol_qp( h );
2157     if( h->param.rc.i_aq_mode )
2158         x264_adaptive_quant( h );
2159
2160     x264_mb_analyse_init( h, &analysis, h->mb.i_qp );
2161
2162     /*--------------------------- Do the analysis ---------------------------*/
2163     if( h->sh.i_type == SLICE_TYPE_I )
2164     {
2165         if( analysis.i_mbrd )
2166             x264_mb_cache_fenc_satd( h );
2167         x264_mb_analyse_intra( h, &analysis, COST_MAX );
2168         if( analysis.i_mbrd )
2169             x264_intra_rd( h, &analysis, COST_MAX );
2170
2171         i_cost = analysis.i_satd_i16x16;
2172         h->mb.i_type = I_16x16;
2173         COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, h->mb.i_type, I_4x4 );
2174         COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, h->mb.i_type, I_8x8 );
2175         if( analysis.i_satd_pcm < i_cost )
2176             h->mb.i_type = I_PCM;
2177
2178         else if( analysis.i_mbrd >= 2 )
2179             x264_intra_rd_refine( h, &analysis );
2180     }
2181     else if( h->sh.i_type == SLICE_TYPE_P )
2182     {
2183         int b_skip = 0;
2184
2185         h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2186
2187         /* Fast P_SKIP detection */
2188         analysis.b_try_pskip = 0;
2189         if( h->param.analyse.b_fast_pskip )
2190         {
2191             if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2192                 // FIXME don't need to check this if the reference frame is done
2193                 {}
2194             else if( h->param.analyse.i_subpel_refine >= 3 )
2195                 analysis.b_try_pskip = 1;
2196             else if( h->mb.i_mb_type_left == P_SKIP ||
2197                      h->mb.i_mb_type_top == P_SKIP ||
2198                      h->mb.i_mb_type_topleft == P_SKIP ||
2199                      h->mb.i_mb_type_topright == P_SKIP )
2200                 b_skip = x264_macroblock_probe_pskip( h );
2201         }
2202
2203         h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2204
2205         if( b_skip )
2206         {
2207             h->mb.i_type = P_SKIP;
2208             h->mb.i_partition = D_16x16;
2209             assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2210         }
2211         else
2212         {
2213             const unsigned int flags = h->param.analyse.inter;
2214             int i_type;
2215             int i_partition;
2216             int i_thresh16x8;
2217             int i_satd_inter, i_satd_intra;
2218
2219             x264_mb_analyse_load_costs( h, &analysis );
2220
2221             x264_mb_analyse_inter_p16x16( h, &analysis );
2222
2223             if( h->mb.i_type == P_SKIP )
2224                 return;
2225
2226             if( flags & X264_ANALYSE_PSUB16x16 )
2227             {
2228                 if( h->param.analyse.b_mixed_references )
2229                     x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2230                 else
2231                     x264_mb_analyse_inter_p8x8( h, &analysis );
2232             }
2233
2234             /* Select best inter mode */
2235             i_type = P_L0;
2236             i_partition = D_16x16;
2237             i_cost = analysis.l0.me16x16.cost;
2238
2239             if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2240                 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2241             {
2242                 i_type = P_8x8;
2243                 i_partition = D_8x8;
2244                 i_cost = analysis.l0.i_cost8x8;
2245
2246                 /* Do sub 8x8 */
2247                 if( flags & X264_ANALYSE_PSUB8x8 )
2248                 {
2249                     for( i = 0; i < 4; i++ )
2250                     {
2251                         x264_mb_analyse_inter_p4x4( h, &analysis, i );
2252                         if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2253                         {
2254                             int i_cost8x8 = analysis.l0.i_cost4x4[i];
2255                             h->mb.i_sub_partition[i] = D_L0_4x4;
2256
2257                             x264_mb_analyse_inter_p8x4( h, &analysis, i );
2258                             COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2259                                          h->mb.i_sub_partition[i], D_L0_8x4 );
2260
2261                             x264_mb_analyse_inter_p4x8( h, &analysis, i );
2262                             COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2263                                          h->mb.i_sub_partition[i], D_L0_4x8 );
2264
2265                             i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2266                         }
2267                         x264_mb_cache_mv_p8x8( h, &analysis, i );
2268                     }
2269                     analysis.l0.i_cost8x8 = i_cost;
2270                 }
2271             }
2272
2273             /* Now do 16x8/8x16 */
2274             i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2275             if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2276                 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2277             {
2278                 x264_mb_analyse_inter_p16x8( h, &analysis );
2279                 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2280
2281                 x264_mb_analyse_inter_p8x16( h, &analysis );
2282                 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2283             }
2284
2285             h->mb.i_partition = i_partition;
2286
2287             /* refine qpel */
2288             //FIXME mb_type costs?
2289             if( analysis.i_mbrd )
2290             {
2291                 /* refine later */
2292             }
2293             else if( i_partition == D_16x16 )
2294             {
2295                 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2296                 i_cost = analysis.l0.me16x16.cost;
2297             }
2298             else if( i_partition == D_16x8 )
2299             {
2300                 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2301                 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2302                 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2303             }
2304             else if( i_partition == D_8x16 )
2305             {
2306                 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2307                 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2308                 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2309             }
2310             else if( i_partition == D_8x8 )
2311             {
2312                 int i8x8;
2313                 i_cost = 0;
2314                 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2315                 {
2316                     switch( h->mb.i_sub_partition[i8x8] )
2317                     {
2318                         case D_L0_8x8:
2319                             x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2320                             i_cost += analysis.l0.me8x8[i8x8].cost;
2321                             break;
2322                         case D_L0_8x4:
2323                             x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2324                             x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2325                             i_cost += analysis.l0.me8x4[i8x8][0].cost +
2326                                       analysis.l0.me8x4[i8x8][1].cost;
2327                             break;
2328                         case D_L0_4x8:
2329                             x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2330                             x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2331                             i_cost += analysis.l0.me4x8[i8x8][0].cost +
2332                                       analysis.l0.me4x8[i8x8][1].cost;
2333                             break;
2334
2335                         case D_L0_4x4:
2336                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2337                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2338                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2339                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2340                             i_cost += analysis.l0.me4x4[i8x8][0].cost +
2341                                       analysis.l0.me4x4[i8x8][1].cost +
2342                                       analysis.l0.me4x4[i8x8][2].cost +
2343                                       analysis.l0.me4x4[i8x8][3].cost;
2344                             break;
2345                         default:
2346                             x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2347                             break;
2348                     }
2349                 }
2350             }
2351
2352             if( h->mb.b_chroma_me )
2353             {
2354                 x264_mb_analyse_intra_chroma( h, &analysis );
2355                 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2356                 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2357                 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2358                 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2359             }
2360             else
2361                 x264_mb_analyse_intra( h, &analysis, i_cost );
2362
2363             i_satd_inter = i_cost;
2364             i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2365                                       analysis.i_satd_i8x8,
2366                                       analysis.i_satd_i4x4 );
2367
2368             if( analysis.i_mbrd )
2369             {
2370                 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2371                 i_type = P_L0;
2372                 i_partition = D_16x16;
2373                 i_cost = analysis.l0.me16x16.cost;
2374                 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2375                 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2376                 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2377                 h->mb.i_type = i_type;
2378                 h->mb.i_partition = i_partition;
2379                 if( i_cost < COST_MAX )
2380                     x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2381                 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2382             }
2383
2384             COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2385             COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2386             COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2387             COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
2388
2389             h->mb.i_type = i_type;
2390
2391             if( analysis.i_mbrd >= 2 && h->mb.i_type != I_PCM )
2392             {
2393                 if( IS_INTRA( h->mb.i_type ) )
2394                 {
2395                     x264_intra_rd_refine( h, &analysis );
2396                 }
2397                 else if( i_partition == D_16x16 )
2398                 {
2399                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2400                     x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 );
2401                 }
2402                 else if( i_partition == D_16x8 )
2403                 {
2404                     h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2405                     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2406                     x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2407                     x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2408                     x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0, 0 );
2409                     x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 8, 0 );
2410                 }
2411                 else if( i_partition == D_8x16 )
2412                 {
2413                     h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2414                     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2415                     x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2416                     x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2417                     x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0, 0 );
2418                     x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 4, 0 );
2419                 }
2420                 else if( i_partition == D_8x8 )
2421                 {
2422                     int i8x8;
2423                     x264_analyse_update_cache( h, &analysis );
2424                     for( i8x8 = 0; i8x8 < 4; i8x8++ )
2425                     {
2426                         if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2427                         {
2428                             x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8*4, 0 );
2429                         }
2430                         else if( h->mb.i_sub_partition[i8x8] == D_L0_8x4 )
2431                         {
2432                            x264_me_refine_qpel_rd( h, &analysis.l0.me8x4[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
2433                            x264_me_refine_qpel_rd( h, &analysis.l0.me8x4[i8x8][1], analysis.i_lambda2, i8x8*4+2, 0 );
2434                         }
2435                         else if( h->mb.i_sub_partition[i8x8] == D_L0_4x8 )
2436                         {
2437                            x264_me_refine_qpel_rd( h, &analysis.l0.me4x8[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
2438                            x264_me_refine_qpel_rd( h, &analysis.l0.me4x8[i8x8][1], analysis.i_lambda2, i8x8*4+1, 0 );
2439                         }
2440                         else if( h->mb.i_sub_partition[i8x8] == D_L0_4x4 )
2441                         {
2442                            x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
2443                            x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][1], analysis.i_lambda2, i8x8*4+1, 0 );
2444                            x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][2], analysis.i_lambda2, i8x8*4+2, 0 );
2445                            x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][3], analysis.i_lambda2, i8x8*4+3, 0 );
2446                         }
2447                     }
2448                 }
2449             }
2450         }
2451     }
2452     else if( h->sh.i_type == SLICE_TYPE_B )
2453     {
2454         int i_bskip_cost = COST_MAX;
2455         int b_skip = 0;
2456
2457         if( analysis.i_mbrd )
2458             x264_mb_cache_fenc_satd( h );
2459
2460         h->mb.i_type = B_SKIP;
2461         if( h->mb.b_direct_auto_write )
2462         {
2463             /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2464             for( i = 0; i < 2; i++ )
2465             {
2466                 int b_changed = 1;
2467                 h->sh.b_direct_spatial_mv_pred ^= 1;
2468                 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2469                 if( analysis.b_direct_available )
2470                 {
2471                     if( b_changed )
2472                     {
2473                         x264_mb_mc( h );
2474                         b_skip = x264_macroblock_probe_bskip( h );
2475                     }
2476                     h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2477                 }
2478                 else
2479                     b_skip = 0;
2480             }
2481         }
2482         else
2483             analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2484
2485         if( analysis.b_direct_available )
2486         {
2487             if( !h->mb.b_direct_auto_write )
2488                 x264_mb_mc( h );
2489             if( analysis.i_mbrd )
2490             {
2491                 i_bskip_cost = ssd_mb( h );
2492                 /* 6 = minimum cavlc cost of a non-skipped MB */
2493                 b_skip = h->mb.b_skip_mc = i_bskip_cost <= ((6 * analysis.i_lambda2 + 128) >> 8);
2494             }
2495             else if( !h->mb.b_direct_auto_write )
2496             {
2497                 /* Conditioning the probe on neighboring block types
2498                  * doesn't seem to help speed or quality. */
2499                 b_skip = x264_macroblock_probe_bskip( h );
2500             }
2501         }
2502
2503         if( !b_skip )
2504         {
2505             const unsigned int flags = h->param.analyse.inter;
2506             int i_type;
2507             int i_partition;
2508             int i_satd_inter = 0; // shut up uninitialized warning
2509             h->mb.b_skip_mc = 0;
2510
2511             x264_mb_analyse_load_costs( h, &analysis );
2512
2513             /* select best inter mode */
2514             /* direct must be first */
2515             if( analysis.b_direct_available )
2516                 x264_mb_analyse_inter_direct( h, &analysis );
2517
2518             x264_mb_analyse_inter_b16x16( h, &analysis );
2519
2520             i_type = B_L0_L0;
2521             i_partition = D_16x16;
2522             i_cost = analysis.l0.me16x16.cost;
2523             COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2524             COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2525             COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2526
2527             if( analysis.i_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2528             {
2529                 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2530                 if( i_bskip_cost < analysis.i_rd16x16direct &&
2531                     i_bskip_cost < analysis.i_rd16x16bi &&
2532                     i_bskip_cost < analysis.l0.i_rd16x16 &&
2533                     i_bskip_cost < analysis.l1.i_rd16x16 )
2534                 {
2535                     h->mb.i_type = B_SKIP;
2536                     x264_analyse_update_cache( h, &analysis );
2537                     return;
2538                 }
2539             }
2540
2541             if( flags & X264_ANALYSE_BSUB16x16 )
2542             {
2543                 x264_mb_analyse_inter_b8x8( h, &analysis );
2544                 if( analysis.i_cost8x8bi < i_cost )
2545                 {
2546                     i_type = B_8x8;
2547                     i_partition = D_8x8;
2548                     i_cost = analysis.i_cost8x8bi;
2549
2550                     if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2551                         h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2552                     {
2553                         x264_mb_analyse_inter_b16x8( h, &analysis );
2554                         COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2555                                      i_type, analysis.i_mb_type16x8,
2556                                      i_partition, D_16x8 );
2557                     }
2558                     if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2559                         h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2560                     {
2561                         x264_mb_analyse_inter_b8x16( h, &analysis );
2562                         COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2563                                      i_type, analysis.i_mb_type8x16,
2564                                      i_partition, D_8x16 );
2565                     }
2566                 }
2567             }
2568
2569             if( analysis.i_mbrd )
2570             {
2571                 /* refine later */
2572             }
2573             /* refine qpel */
2574             else if( i_partition == D_16x16 )
2575             {
2576                 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2577                 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2578                 if( i_type == B_L0_L0 )
2579                 {
2580                     x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2581                     i_cost = analysis.l0.me16x16.cost
2582                            + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2583                 }
2584                 else if( i_type == B_L1_L1 )
2585                 {
2586                     x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2587                     i_cost = analysis.l1.me16x16.cost
2588                            + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2589                 }
2590                 else if( i_type == B_BI_BI )
2591                 {
2592                     x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2593                     x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2594                 }
2595             }
2596             else if( i_partition == D_16x8 )
2597             {
2598                 for( i=0; i<2; i++ )
2599                 {
2600                     if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2601                         x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2602                     if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2603                         x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2604                 }
2605             }
2606             else if( i_partition == D_8x16 )
2607             {
2608                 for( i=0; i<2; i++ )
2609                 {
2610                     if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2611                         x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2612                     if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2613                         x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2614                 }
2615             }
2616             else if( i_partition == D_8x8 )
2617             {
2618                 for( i=0; i<4; i++ )
2619                 {
2620                     x264_me_t *m;
2621                     int i_part_cost_old;
2622                     int i_type_cost;
2623                     int i_part_type = h->mb.i_sub_partition[i];
2624                     int b_bidir = (i_part_type == D_BI_8x8);
2625
2626                     if( i_part_type == D_DIRECT_8x8 )
2627                         continue;
2628                     if( x264_mb_partition_listX_table[0][i_part_type] )
2629                     {
2630                         m = &analysis.l0.me8x8[i];
2631                         i_part_cost_old = m->cost;
2632                         i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2633                         m->cost -= i_type_cost;
2634                         x264_me_refine_qpel( h, m );
2635                         if( !b_bidir )
2636                             analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2637                     }
2638                     if( x264_mb_partition_listX_table[1][i_part_type] )
2639                     {
2640                         m = &analysis.l1.me8x8[i];
2641                         i_part_cost_old = m->cost;
2642                         i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2643                         m->cost -= i_type_cost;
2644                         x264_me_refine_qpel( h, m );
2645                         if( !b_bidir )
2646                             analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2647                     }
2648                     /* TODO: update mvp? */
2649                 }
2650             }
2651
2652             if( analysis.i_mbrd )
2653             {
2654                 i_satd_inter = i_cost;
2655                 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2656                 i_type = B_SKIP;
2657                 i_cost = i_bskip_cost;
2658                 i_partition = D_16x16;
2659                 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2660                 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2661                 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2662                 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2663                 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2664                 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2665                 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2666
2667                 h->mb.i_type = i_type;
2668                 h->mb.i_partition = i_partition;
2669             }
2670
2671             x264_mb_analyse_intra( h, &analysis, i_satd_inter );
2672
2673             if( analysis.i_mbrd )
2674             {
2675                 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2676                 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2677             }
2678
2679             COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2680             COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2681             COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2682             COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
2683
2684             h->mb.i_type = i_type;
2685             h->mb.i_partition = i_partition;
2686
2687             if( analysis.i_mbrd >= 2 && IS_INTRA( i_type ) && i_type != I_PCM )
2688                 x264_intra_rd_refine( h, &analysis );
2689             if( h->mb.i_subpel_refine >= 5 )
2690                 x264_refine_bidir( h, &analysis );
2691
2692             if( analysis.i_mbrd >= 2 && i_type > B_DIRECT && i_type < B_SKIP )
2693             {
2694                 const int i_biweight = h->mb.bipred_weight[analysis.l0.i_ref][analysis.l1.i_ref];
2695                 x264_analyse_update_cache( h, &analysis );
2696
2697                 if( i_partition == D_16x16 )
2698                 {
2699                     if( i_type == B_L0_L0 )
2700                         x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 );
2701                     else if( i_type == B_L1_L1 )
2702                         x264_me_refine_qpel_rd( h, &analysis.l1.me16x16, analysis.i_lambda2, 0, 1 );
2703                     else if( i_type == B_BI_BI )
2704                         x264_me_refine_bidir_rd( h, &analysis.l0.me16x16, &analysis.l1.me16x16, i_biweight, 0, analysis.i_lambda2 );
2705                 }
2706                 else if( i_partition == D_16x8 )
2707                 {
2708                     for( i = 0; i < 2; i++ )
2709                     {
2710                         h->mb.i_sub_partition[i*2] = h->mb.i_sub_partition[i*2+1] = analysis.i_mb_partition16x8[i];
2711                         if( analysis.i_mb_partition16x8[i] == D_L0_8x8 )
2712                             x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[i], analysis.i_lambda2, i*8, 0 );
2713                         else if( analysis.i_mb_partition16x8[i] == D_L1_8x8 )
2714                             x264_me_refine_qpel_rd( h, &analysis.l1.me16x8[i], analysis.i_lambda2, i*8, 1 );
2715                         else if( analysis.i_mb_partition16x8[i] == D_BI_8x8 )
2716                             x264_me_refine_bidir_rd( h, &analysis.l0.me16x8[i], &analysis.l1.me16x8[i], i_biweight, i*2, analysis.i_lambda2 );
2717                     }
2718                 }
2719                 else if( i_partition == D_8x16 )
2720                 {
2721                     for( i = 0; i < 2; i++ )
2722                     {
2723                         h->mb.i_sub_partition[i] = h->mb.i_sub_partition[i+2] = analysis.i_mb_partition8x16[i];
2724                         if( analysis.i_mb_partition8x16[i] == D_L0_8x8 )
2725                             x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[i], analysis.i_lambda2, i*4, 0 );
2726                         else if( analysis.i_mb_partition8x16[i] == D_L1_8x8 )
2727                             x264_me_refine_qpel_rd( h, &analysis.l1.me8x16[i], analysis.i_lambda2, i*4, 1 );
2728                         else if( analysis.i_mb_partition8x16[i] == D_BI_8x8 )
2729                             x264_me_refine_bidir_rd( h, &analysis.l0.me8x16[i], &analysis.l1.me8x16[i], i_biweight, i, analysis.i_lambda2 );
2730                     }
2731                 }
2732                 else if( i_partition == D_8x8 )
2733                 {
2734                     for( i = 0; i < 4; i++ )
2735                     {
2736                         if( h->mb.i_sub_partition[i] == D_L0_8x8 )
2737                             x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i], analysis.i_lambda2, i*4, 0 );
2738                         else if( h->mb.i_sub_partition[i] == D_L1_8x8 )
2739                             x264_me_refine_qpel_rd( h, &analysis.l1.me8x8[i], analysis.i_lambda2, i*4, 1 );
2740                         else if( h->mb.i_sub_partition[i] == D_BI_8x8 )
2741                             x264_me_refine_bidir_rd( h, &analysis.l0.me8x8[i], &analysis.l1.me8x8[i], i_biweight, i, analysis.i_lambda2 );
2742                     }
2743                 }
2744             }
2745         }
2746     }
2747
2748     x264_analyse_update_cache( h, &analysis );
2749
2750     if( !analysis.i_mbrd )
2751         x264_mb_analyse_transform( h );
2752
2753     h->mb.b_trellis = h->param.analyse.i_trellis;
2754     h->mb.b_noise_reduction = !!h->param.analyse.i_noise_reduction;
2755     if( !IS_SKIP(h->mb.i_type) && h->mb.i_psy_trellis && h->param.analyse.i_trellis == 1 )
2756         x264_psy_trellis_init( h, 0 );
2757     if( h->mb.b_trellis == 1 || h->mb.b_noise_reduction )
2758         h->mb.i_skip_intra = 0;
2759 }
2760
2761 /*-------------------- Update MB from the analysis ----------------------*/
2762 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a  )
2763 {
2764     int i;
2765
2766     switch( h->mb.i_type )
2767     {
2768         case I_4x4:
2769             for( i = 0; i < 16; i++ )
2770                 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2771
2772             x264_mb_analyse_intra_chroma( h, a );
2773             break;
2774         case I_8x8:
2775             for( i = 0; i < 4; i++ )
2776                 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2777
2778             x264_mb_analyse_intra_chroma( h, a );
2779             break;
2780         case I_16x16:
2781             h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2782             x264_mb_analyse_intra_chroma( h, a );
2783             break;
2784
2785         case I_PCM:
2786             break;
2787
2788         case P_L0:
2789             switch( h->mb.i_partition )
2790             {
2791                 case D_16x16:
2792                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2793                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2794                     break;
2795
2796                 case D_16x8:
2797                     x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2798                     x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2799                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv );
2800                     x264_macroblock_cache_mv_ptr( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv );
2801                     break;
2802
2803                 case D_8x16:
2804                     x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2805                     x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2806                     x264_macroblock_cache_mv_ptr( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv );
2807                     x264_macroblock_cache_mv_ptr( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv );
2808                     break;
2809
2810                 default:
2811                     x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2812                     break;
2813             }
2814             break;
2815
2816         case P_8x8:
2817             x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2818             x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2819             x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2820             x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2821             for( i = 0; i < 4; i++ )
2822                 x264_mb_cache_mv_p8x8( h, a, i );
2823             break;
2824
2825         case P_SKIP:
2826         {
2827             h->mb.i_partition = D_16x16;
2828             x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2829             x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv );
2830             break;
2831         }
2832
2833         case B_SKIP:
2834         case B_DIRECT:
2835             x264_mb_load_mv_direct8x8( h, 0 );
2836             x264_mb_load_mv_direct8x8( h, 1 );
2837             x264_mb_load_mv_direct8x8( h, 2 );
2838             x264_mb_load_mv_direct8x8( h, 3 );
2839             break;
2840
2841         case B_8x8:
2842             /* optimize: cache might not need to be rewritten */
2843             for( i = 0; i < 4; i++ )
2844                 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2845             break;
2846
2847         default: /* the rest of the B types */
2848             switch( h->mb.i_partition )
2849             {
2850             case D_16x16:
2851                 switch( h->mb.i_type )
2852                 {
2853                 case B_L0_L0:
2854                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2855                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2856
2857                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2858                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0 );
2859                     x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0 );
2860                     break;
2861                 case B_L1_L1:
2862                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2863                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0 );
2864                     x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0 );
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                 case B_BI_BI:
2870                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2871                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2872
2873                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2874                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2875                     break;
2876                 }
2877                 break;
2878             case D_16x8:
2879                 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2880                 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2881                 break;
2882             case D_8x16:
2883                 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2884                 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2885                 break;
2886             default:
2887                 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2888                 break;
2889             }
2890     }
2891
2892 #ifndef NDEBUG
2893     if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2894     {
2895         int l;
2896         for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2897         {
2898             int completed;
2899             int ref = h->mb.cache.ref[l][x264_scan8[0]];
2900             if( ref < 0 )
2901                 continue;
2902             completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2903             if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2904             {
2905                 x264_log( h, X264_LOG_WARNING, "internal error (MV out of thread range)\n");
2906                 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2907                 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2908                                 h->mb.cache.mv[l][x264_scan8[15]][0],
2909                                 h->mb.cache.mv[l][x264_scan8[15]][1] );
2910                 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2911                 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2912                 fprintf(stderr, "completed: %d \n", completed );
2913                 x264_log( h, X264_LOG_WARNING, "recovering by using intra mode\n");
2914                 x264_mb_analyse_intra( h, a, COST_MAX );
2915                 h->mb.i_type = I_16x16;
2916                 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2917                 x264_mb_analyse_intra_chroma( h, a );
2918             }
2919         }
2920     }
2921 #endif
2922 }
2923
2924 #include "slicetype.c"
2925