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