]> git.sesse.net Git - x264/blob - encoder/analyse.c
Merge avg and avg_weight
[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[8][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, pix, stride, src1, stride1, src2, stride2 ) \
1478 { \
1479     h->mc.avg[size]( pix, stride, src1, stride1, src2, stride2, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1480 }
1481
1482 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1483 {
1484     DECLARE_ALIGNED_16( uint8_t pix0[16*16] );
1485     DECLARE_ALIGNED_16( uint8_t pix1[16*16] );
1486     uint8_t *src0, *src1;
1487     int stride0 = 16, stride1 = 16;
1488
1489     x264_me_t m;
1490     int i_ref, i_mvc;
1491     DECLARE_ALIGNED_4( int16_t mvc[9][2] );
1492     int i_halfpel_thresh = INT_MAX;
1493     int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
1494
1495     /* 16x16 Search on all ref frame */
1496     m.i_pixel = PIXEL_16x16;
1497     m.p_cost_mv = a->p_cost_mv;
1498     LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1499
1500     /* ME for List 0 */
1501     a->l0.me16x16.cost = INT_MAX;
1502     for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
1503     {
1504         /* search with ref */
1505         LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
1506         x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1507         x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1508         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1509
1510         /* add ref cost */
1511         m.cost += REF_COST( 0, i_ref );
1512
1513         if( m.cost < a->l0.me16x16.cost )
1514         {
1515             a->l0.i_ref = i_ref;
1516             h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
1517         }
1518
1519         /* save mv for predicting neighbors */
1520         *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1521     }
1522     /* subtract ref cost, so we don't have to add it for the other MB types */
1523     a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1524
1525     /* ME for list 1 */
1526     i_halfpel_thresh = INT_MAX;
1527     p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
1528     a->l1.me16x16.cost = INT_MAX;
1529     for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
1530     {
1531         /* search with ref */
1532         LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
1533         x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1534         x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1535         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1536
1537         /* add ref cost */
1538         m.cost += REF_COST( 1, i_ref );
1539
1540         if( m.cost < a->l1.me16x16.cost )
1541         {
1542             a->l1.i_ref = i_ref;
1543             h->mc.memcpy_aligned( &a->l1.me16x16, &m, sizeof(x264_me_t) );
1544         }
1545
1546         /* save mv for predicting neighbors */
1547         *(uint32_t*)h->mb.mvr[1][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
1548     }
1549     /* subtract ref cost, so we don't have to add it for the other MB types */
1550     a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1551
1552     /* Set global ref, needed for other modes? */
1553     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1554     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1555
1556     /* get cost of BI mode */
1557     src0 = h->mc.get_ref( pix0, &stride0,
1558                            h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1559                            a->l0.me16x16.mv[0], a->l0.me16x16.mv[1], 16, 16 );
1560     src1 = h->mc.get_ref( pix1, &stride1,
1561                            h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1562                            a->l1.me16x16.mv[0], a->l1.me16x16.mv[1], 16, 16 );
1563
1564     h->mc.avg[PIXEL_16x16]( pix0, 16, src0, stride0, src1, stride1, h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
1565
1566     a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix0, 16 )
1567                      + REF_COST( 0, a->l0.i_ref )
1568                      + REF_COST( 1, a->l1.i_ref )
1569                      + a->l0.me16x16.cost_mv
1570                      + a->l1.me16x16.cost_mv;
1571
1572     /* mb type cost */
1573     a->i_cost16x16bi   += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1574     a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1575     a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1576 }
1577
1578 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1579 {
1580     const int x = 2*(i%2);
1581     const int y = 2*(i/2);
1582
1583     switch( h->mb.i_sub_partition[i] )
1584     {
1585         case D_L0_8x8:
1586             x264_macroblock_cache_mv_ptr( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv );
1587             break;
1588         case D_L0_8x4:
1589             x264_macroblock_cache_mv_ptr( h, x, y+0, 2, 1, 0, a->l0.me8x4[i][0].mv );
1590             x264_macroblock_cache_mv_ptr( h, x, y+1, 2, 1, 0, a->l0.me8x4[i][1].mv );
1591             break;
1592         case D_L0_4x8:
1593             x264_macroblock_cache_mv_ptr( h, x+0, y, 1, 2, 0, a->l0.me4x8[i][0].mv );
1594             x264_macroblock_cache_mv_ptr( h, x+1, y, 1, 2, 0, a->l0.me4x8[i][1].mv );
1595             break;
1596         case D_L0_4x4:
1597             x264_macroblock_cache_mv_ptr( h, x+0, y+0, 1, 1, 0, a->l0.me4x4[i][0].mv );
1598             x264_macroblock_cache_mv_ptr( h, x+1, y+0, 1, 1, 0, a->l0.me4x4[i][1].mv );
1599             x264_macroblock_cache_mv_ptr( h, x+0, y+1, 1, 1, 0, a->l0.me4x4[i][2].mv );
1600             x264_macroblock_cache_mv_ptr( h, x+1, y+1, 1, 1, 0, a->l0.me4x4[i][3].mv );
1601             break;
1602         default:
1603             x264_log( h, X264_LOG_ERROR, "internal error\n" );
1604             break;
1605     }
1606 }
1607
1608 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1609     if( x264_mb_partition_listX_table[0][part] ) \
1610     { \
1611         x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1612         x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 0, me0.mv ); \
1613     } \
1614     else \
1615     { \
1616         x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1617         x264_macroblock_cache_mv(  h, x,y,dx,dy, 0, 0 ); \
1618         if( b_mvd ) \
1619             x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0 ); \
1620     } \
1621     if( x264_mb_partition_listX_table[1][part] ) \
1622     { \
1623         x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1624         x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 1, me1.mv ); \
1625     } \
1626     else \
1627     { \
1628         x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1629         x264_macroblock_cache_mv(  h, x,y,dx,dy, 1, 0 ); \
1630         if( b_mvd ) \
1631             x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0 ); \
1632     }
1633
1634 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1635 {
1636     int x = (i%2)*2;
1637     int y = (i/2)*2;
1638     if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1639     {
1640         x264_mb_load_mv_direct8x8( h, i );
1641         if( b_mvd )
1642         {
1643             x264_macroblock_cache_mvd(  h, x, y, 2, 2, 0, 0 );
1644             x264_macroblock_cache_mvd(  h, x, y, 2, 2, 1, 0 );
1645             x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1646         }
1647     }
1648     else
1649     {
1650         CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1651     }
1652 }
1653 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1654 {
1655     CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1656 }
1657 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1658 {
1659     CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1660 }
1661 #undef CACHE_MV_BI
1662
1663 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1664 {
1665     uint8_t **p_fref[2] =
1666         { h->mb.pic.p_fref[0][a->l0.i_ref],
1667           h->mb.pic.p_fref[1][a->l1.i_ref] };
1668     DECLARE_ALIGNED_8( uint8_t pix[2][8*8] );
1669     int i, l;
1670
1671     /* XXX Needed for x264_mb_predict_mv */
1672     h->mb.i_partition = D_8x8;
1673
1674     a->i_cost8x8bi = 0;
1675
1676     for( i = 0; i < 4; i++ )
1677     {
1678         const int x8 = i%2;
1679         const int y8 = i/2;
1680         int i_part_cost;
1681         int i_part_cost_bi = 0;
1682         int stride[2] = {8,8};
1683         uint8_t *src[2];
1684
1685         for( l = 0; l < 2; l++ )
1686         {
1687             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1688             x264_me_t *m = &lX->me8x8[i];
1689
1690             m->i_pixel = PIXEL_8x8;
1691             m->p_cost_mv = a->p_cost_mv;
1692
1693             LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1694             LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
1695
1696             x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1697             x264_me_search( h, m, &lX->me16x16.mv, 1 );
1698
1699             x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, l, m->mv );
1700
1701             /* BI mode */
1702             src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1703                                     m->mv[0], m->mv[1], 8, 8 );
1704             i_part_cost_bi += m->cost_mv;
1705             /* FIXME: ref cost */
1706         }
1707         h->mc.avg[PIXEL_8x8]( pix[0], 8, src[0], stride[0], src[1], stride[1], h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
1708         i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
1709                         + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1710         a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1711         a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1712
1713         i_part_cost = a->l0.me8x8[i].cost;
1714         h->mb.i_sub_partition[i] = D_L0_8x8;
1715         COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
1716         COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
1717         COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
1718         a->i_cost8x8bi += i_part_cost;
1719
1720         /* XXX Needed for x264_mb_predict_mv */
1721         x264_mb_cache_mv_b8x8( h, a, i, 0 );
1722     }
1723
1724     /* mb type cost */
1725     a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1726 }
1727
1728 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1729 {
1730     uint8_t **p_fref[2] =
1731         { h->mb.pic.p_fref[0][a->l0.i_ref],
1732           h->mb.pic.p_fref[1][a->l1.i_ref] };
1733     DECLARE_ALIGNED_16( uint8_t pix[2][16*8] );
1734     DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1735     int i, l;
1736
1737     h->mb.i_partition = D_16x8;
1738     a->i_cost16x8bi = 0;
1739
1740     for( i = 0; i < 2; i++ )
1741     {
1742         int i_part_cost;
1743         int i_part_cost_bi = 0;
1744         int stride[2] = {16,16};
1745         uint8_t *src[2];
1746
1747         /* TODO: check only the list(s) that were used in b8x8? */
1748         for( l = 0; l < 2; l++ )
1749         {
1750             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1751             x264_me_t *m = &lX->me16x8[i];
1752
1753             m->i_pixel = PIXEL_16x8;
1754             m->p_cost_mv = a->p_cost_mv;
1755
1756             LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1757             LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
1758
1759             *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[2*i].mv;
1760             *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[2*i+1].mv;
1761
1762             x264_mb_predict_mv( h, l, 8*i, 2, m->mvp );
1763             x264_me_search( h, m, mvc, 2 );
1764
1765             /* BI mode */
1766             src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref, m->i_stride[0],
1767                                     m->mv[0], m->mv[1], 16, 8 );
1768             /* FIXME: ref cost */
1769             i_part_cost_bi += m->cost_mv;
1770         }
1771         h->mc.avg[PIXEL_16x8]( pix[0], 16, src[0], stride[0], src[1], stride[1], h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
1772         i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
1773
1774         i_part_cost = a->l0.me16x8[i].cost;
1775         a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1776         if( a->l1.me16x8[i].cost < i_part_cost )
1777         {
1778             i_part_cost = a->l1.me16x8[i].cost;
1779             a->i_mb_partition16x8[i] = D_L1_8x8;
1780         }
1781         if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1782         {
1783             i_part_cost = i_part_cost_bi;
1784             a->i_mb_partition16x8[i] = D_BI_8x8;
1785         }
1786         a->i_cost16x8bi += i_part_cost;
1787
1788         x264_mb_cache_mv_b16x8( h, a, i, 0 );
1789     }
1790
1791     /* mb type cost */
1792     a->i_mb_type16x8 = B_L0_L0
1793         + (a->i_mb_partition16x8[0]>>2) * 3
1794         + (a->i_mb_partition16x8[1]>>2);
1795     a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1796 }
1797
1798 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1799 {
1800     uint8_t **p_fref[2] =
1801         { h->mb.pic.p_fref[0][a->l0.i_ref],
1802           h->mb.pic.p_fref[1][a->l1.i_ref] };
1803     DECLARE_ALIGNED_8( uint8_t pix[2][8*16] );
1804     DECLARE_ALIGNED_4( int16_t mvc[2][2] );
1805     int i, l;
1806
1807     h->mb.i_partition = D_8x16;
1808     a->i_cost8x16bi = 0;
1809
1810     for( i = 0; i < 2; i++ )
1811     {
1812         int i_part_cost;
1813         int i_part_cost_bi = 0;
1814         int stride[2] = {8,8};
1815         uint8_t *src[2];
1816
1817         for( l = 0; l < 2; l++ )
1818         {
1819             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1820             x264_me_t *m = &lX->me8x16[i];
1821
1822             m->i_pixel = PIXEL_8x16;
1823             m->p_cost_mv = a->p_cost_mv;
1824
1825             LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1826             LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
1827
1828             *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[i].mv;
1829             *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[i+2].mv;
1830
1831             x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1832             x264_me_search( h, m, mvc, 2 );
1833
1834             /* BI mode */
1835             src[l] = h->mc.get_ref( pix[l], &stride[l], m->p_fref,  m->i_stride[0],
1836                                     m->mv[0], m->mv[1], 8, 16 );
1837             /* FIXME: ref cost */
1838             i_part_cost_bi += m->cost_mv;
1839         }
1840
1841         h->mc.avg[PIXEL_8x16]( pix[0], 8, src[0], stride[0], src[1], stride[1], h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] );
1842         i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
1843
1844         i_part_cost = a->l0.me8x16[i].cost;
1845         a->i_mb_partition8x16[i] = D_L0_8x8;
1846         if( a->l1.me8x16[i].cost < i_part_cost )
1847         {
1848             i_part_cost = a->l1.me8x16[i].cost;
1849             a->i_mb_partition8x16[i] = D_L1_8x8;
1850         }
1851         if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1852         {
1853             i_part_cost = i_part_cost_bi;
1854             a->i_mb_partition8x16[i] = D_BI_8x8;
1855         }
1856         a->i_cost8x16bi += i_part_cost;
1857
1858         x264_mb_cache_mv_b8x16( h, a, i, 0 );
1859     }
1860
1861     /* mb type cost */
1862     a->i_mb_type8x16 = B_L0_L0
1863         + (a->i_mb_partition8x16[0]>>2) * 3
1864         + (a->i_mb_partition8x16[1]>>2);
1865     a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1866 }
1867
1868 static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
1869 {
1870     int thresh = i_satd * 5/4;
1871
1872     h->mb.i_type = P_L0;
1873     if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
1874     {
1875         h->mb.i_partition = D_16x16;
1876         x264_analyse_update_cache( h, a );
1877         a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1878     }
1879     a->l0.me16x16.cost = a->l0.i_rd16x16;
1880
1881     if( a->l0.i_cost16x8 <= thresh )
1882     {
1883         h->mb.i_partition = D_16x8;
1884         x264_analyse_update_cache( h, a );
1885         a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1886     }
1887     else
1888         a->l0.i_cost16x8 = COST_MAX;
1889
1890     if( a->l0.i_cost8x16 <= thresh )
1891     {
1892         h->mb.i_partition = D_8x16;
1893         x264_analyse_update_cache( h, a );
1894         a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1895     }
1896     else
1897         a->l0.i_cost8x16 = COST_MAX;
1898
1899     if( a->l0.i_cost8x8 <= thresh )
1900     {
1901         h->mb.i_type = P_8x8;
1902         h->mb.i_partition = D_8x8;
1903         x264_analyse_update_cache( h, a );
1904         a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
1905
1906         if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
1907         {
1908             /* FIXME: RD per subpartition */
1909             int part_bak[4];
1910             int i, i_cost;
1911             int b_sub8x8 = 0;
1912             for( i=0; i<4; i++ )
1913             {
1914                 part_bak[i] = h->mb.i_sub_partition[i];
1915                 b_sub8x8 |= (part_bak[i] != D_L0_8x8);
1916             }
1917             if( b_sub8x8 )
1918             {
1919                 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1920                 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1921                 x264_analyse_update_cache( h, a );
1922                 i_cost = x264_rd_cost_mb( h, a->i_lambda2 );
1923                 if( a->l0.i_cost8x8 < i_cost )
1924                 {
1925                     for( i=0; i<4; i++ )
1926                         h->mb.i_sub_partition[i] = part_bak[i];
1927                 }
1928                 else
1929                    a->l0.i_cost8x8 = i_cost;
1930             }
1931         }
1932     }
1933     else
1934         a->l0.i_cost8x8 = COST_MAX;
1935 }
1936
1937 static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
1938 {
1939     int thresh = i_satd_inter * (17 + (!!h->mb.i_psy_rd))/16;
1940
1941     if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
1942     {
1943         h->mb.i_type = B_DIRECT;
1944         /* Assumes direct/skip MC is still in fdec */
1945         /* Requires b-rdo to be done before intra analysis */
1946         h->mb.b_skip_mc = 1;
1947         x264_analyse_update_cache( h, a );
1948         a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
1949         h->mb.b_skip_mc = 0;
1950     }
1951
1952     //FIXME not all the update_cache calls are needed
1953     h->mb.i_partition = D_16x16;
1954     /* L0 */
1955     if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
1956     {
1957         h->mb.i_type = B_L0_L0;
1958         x264_analyse_update_cache( h, a );
1959         a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1960     }
1961
1962     /* L1 */
1963     if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
1964     {
1965         h->mb.i_type = B_L1_L1;
1966         x264_analyse_update_cache( h, a );
1967         a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
1968     }
1969
1970     /* BI */
1971     if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
1972     {
1973         h->mb.i_type = B_BI_BI;
1974         x264_analyse_update_cache( h, a );
1975         a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
1976     }
1977
1978     /* 8x8 */
1979     if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
1980     {
1981         h->mb.i_type = B_8x8;
1982         h->mb.i_partition = D_8x8;
1983         x264_analyse_update_cache( h, a );
1984         a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1985         x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1986     }
1987
1988     /* 16x8 */
1989     if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
1990     {
1991         h->mb.i_type = a->i_mb_type16x8;
1992         h->mb.i_partition = D_16x8;
1993         x264_analyse_update_cache( h, a );
1994         a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
1995     }
1996
1997     /* 8x16 */
1998     if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
1999     {
2000         h->mb.i_type = a->i_mb_type8x16;
2001         h->mb.i_partition = D_8x16;
2002         x264_analyse_update_cache( h, a );
2003         a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
2004     }
2005 }
2006
2007 static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
2008 {
2009     const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
2010     int i;
2011
2012     switch( h->mb.i_partition )
2013     {
2014     case D_16x16:
2015         if( h->mb.i_type == B_BI_BI )
2016             x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
2017         break;
2018     case D_16x8:
2019         for( i=0; i<2; i++ )
2020             if( a->i_mb_partition16x8[i] == D_BI_8x8 )
2021                 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
2022         break;
2023     case D_8x16:
2024         for( i=0; i<2; i++ )
2025             if( a->i_mb_partition8x16[i] == D_BI_8x8 )
2026                 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
2027         break;
2028     case D_8x8:
2029         for( i=0; i<4; i++ )
2030             if( h->mb.i_sub_partition[i] == D_BI_8x8 )
2031                 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
2032         break;
2033     }
2034 }
2035
2036 static inline void x264_mb_analyse_transform( x264_t *h )
2037 {
2038     if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2039     {
2040         int i_cost4, i_cost8;
2041         /* Only luma MC is really needed, but the full MC is re-used in macroblock_encode. */
2042         x264_mb_mc( h );
2043
2044         i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2045                                              h->mb.pic.p_fdec[0], FDEC_STRIDE );
2046         i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
2047                                              h->mb.pic.p_fdec[0], FDEC_STRIDE );
2048
2049         h->mb.b_transform_8x8 = i_cost8 < i_cost4;
2050         h->mb.b_skip_mc = 1;
2051     }
2052 }
2053
2054 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
2055 {
2056     if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
2057     {
2058         int i_rd8;
2059         x264_analyse_update_cache( h, a );
2060         h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2061         /* FIXME only luma is needed, but the score for comparison already includes chroma */
2062         i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
2063
2064         if( *i_rd >= i_rd8 )
2065         {
2066             if( *i_rd > 0 )
2067                 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
2068             /* prevent a rare division by zero in estimated intra cost */
2069             if( *i_satd == 0 )
2070                 *i_satd = 1;
2071
2072             *i_rd = i_rd8;
2073         }
2074         else
2075             h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
2076     }
2077 }
2078
2079
2080 /*****************************************************************************
2081  * x264_macroblock_analyse:
2082  *****************************************************************************/
2083 void x264_macroblock_analyse( x264_t *h )
2084 {
2085     x264_mb_analysis_t analysis;
2086     int i_cost = COST_MAX;
2087     int i;
2088
2089     h->mb.i_qp = x264_ratecontrol_qp( h );
2090     if( h->param.rc.i_aq_mode )
2091         x264_adaptive_quant( h );
2092
2093     x264_mb_analyse_init( h, &analysis, h->mb.i_qp );
2094
2095     /*--------------------------- Do the analysis ---------------------------*/
2096     if( h->sh.i_type == SLICE_TYPE_I )
2097     {
2098         if( analysis.b_mbrd )
2099             x264_mb_cache_fenc_satd( h );
2100         x264_mb_analyse_intra( h, &analysis, COST_MAX );
2101         if( analysis.b_mbrd )
2102             x264_intra_rd( h, &analysis, COST_MAX );
2103
2104         i_cost = analysis.i_satd_i16x16;
2105         h->mb.i_type = I_16x16;
2106         COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, h->mb.i_type, I_4x4 );
2107         COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, h->mb.i_type, I_8x8 );
2108         if( analysis.i_satd_pcm < i_cost )
2109             h->mb.i_type = I_PCM;
2110
2111         else if( h->mb.i_subpel_refine >= 7 )
2112             x264_intra_rd_refine( h, &analysis );
2113     }
2114     else if( h->sh.i_type == SLICE_TYPE_P )
2115     {
2116         int b_skip = 0;
2117         int i_intra_cost, i_intra_type;
2118
2119         h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
2120
2121         /* Fast P_SKIP detection */
2122         analysis.b_try_pskip = 0;
2123         if( h->param.analyse.b_fast_pskip )
2124         {
2125             if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
2126                 // FIXME don't need to check this if the reference frame is done
2127                 {}
2128             else if( h->param.analyse.i_subpel_refine >= 3 )
2129                 analysis.b_try_pskip = 1;
2130             else if( h->mb.i_mb_type_left == P_SKIP ||
2131                      h->mb.i_mb_type_top == P_SKIP ||
2132                      h->mb.i_mb_type_topleft == P_SKIP ||
2133                      h->mb.i_mb_type_topright == P_SKIP )
2134                 b_skip = x264_macroblock_probe_pskip( h );
2135         }
2136
2137         h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
2138
2139         if( b_skip )
2140         {
2141             h->mb.i_type = P_SKIP;
2142             h->mb.i_partition = D_16x16;
2143             assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
2144         }
2145         else
2146         {
2147             const unsigned int flags = h->param.analyse.inter;
2148             int i_type;
2149             int i_partition;
2150             int i_thresh16x8;
2151             int i_satd_inter, i_satd_intra;
2152
2153             x264_mb_analyse_load_costs( h, &analysis );
2154
2155             x264_mb_analyse_inter_p16x16( h, &analysis );
2156
2157             if( h->mb.i_type == P_SKIP )
2158                 return;
2159
2160             if( flags & X264_ANALYSE_PSUB16x16 )
2161             {
2162                 if( h->param.analyse.b_mixed_references )
2163                     x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
2164                 else
2165                     x264_mb_analyse_inter_p8x8( h, &analysis );
2166             }
2167
2168             /* Select best inter mode */
2169             i_type = P_L0;
2170             i_partition = D_16x16;
2171             i_cost = analysis.l0.me16x16.cost;
2172
2173             if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2174                 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
2175             {
2176                 i_type = P_8x8;
2177                 i_partition = D_8x8;
2178                 i_cost = analysis.l0.i_cost8x8;
2179
2180                 /* Do sub 8x8 */
2181                 if( flags & X264_ANALYSE_PSUB8x8 )
2182                 {
2183                     for( i = 0; i < 4; i++ )
2184                     {
2185                         x264_mb_analyse_inter_p4x4( h, &analysis, i );
2186                         if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
2187                         {
2188                             int i_cost8x8 = analysis.l0.i_cost4x4[i];
2189                             h->mb.i_sub_partition[i] = D_L0_4x4;
2190
2191                             x264_mb_analyse_inter_p8x4( h, &analysis, i );
2192                             COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
2193                                          h->mb.i_sub_partition[i], D_L0_8x4 );
2194
2195                             x264_mb_analyse_inter_p4x8( h, &analysis, i );
2196                             COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
2197                                          h->mb.i_sub_partition[i], D_L0_4x8 );
2198
2199                             i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
2200                         }
2201                         x264_mb_cache_mv_p8x8( h, &analysis, i );
2202                     }
2203                     analysis.l0.i_cost8x8 = i_cost;
2204                 }
2205             }
2206
2207             /* Now do 16x8/8x16 */
2208             i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
2209             if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
2210                 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
2211             {
2212                 x264_mb_analyse_inter_p16x8( h, &analysis );
2213                 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
2214
2215                 x264_mb_analyse_inter_p8x16( h, &analysis );
2216                 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
2217             }
2218
2219             h->mb.i_partition = i_partition;
2220
2221             /* refine qpel */
2222             //FIXME mb_type costs?
2223             if( analysis.b_mbrd )
2224             {
2225                 /* refine later */
2226             }
2227             else if( i_partition == D_16x16 )
2228             {
2229                 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2230                 i_cost = analysis.l0.me16x16.cost;
2231             }
2232             else if( i_partition == D_16x8 )
2233             {
2234                 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
2235                 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
2236                 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
2237             }
2238             else if( i_partition == D_8x16 )
2239             {
2240                 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
2241                 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
2242                 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
2243             }
2244             else if( i_partition == D_8x8 )
2245             {
2246                 int i8x8;
2247                 i_cost = 0;
2248                 for( i8x8 = 0; i8x8 < 4; i8x8++ )
2249                 {
2250                     switch( h->mb.i_sub_partition[i8x8] )
2251                     {
2252                         case D_L0_8x8:
2253                             x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
2254                             i_cost += analysis.l0.me8x8[i8x8].cost;
2255                             break;
2256                         case D_L0_8x4:
2257                             x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
2258                             x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
2259                             i_cost += analysis.l0.me8x4[i8x8][0].cost +
2260                                       analysis.l0.me8x4[i8x8][1].cost;
2261                             break;
2262                         case D_L0_4x8:
2263                             x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
2264                             x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
2265                             i_cost += analysis.l0.me4x8[i8x8][0].cost +
2266                                       analysis.l0.me4x8[i8x8][1].cost;
2267                             break;
2268
2269                         case D_L0_4x4:
2270                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
2271                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
2272                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
2273                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
2274                             i_cost += analysis.l0.me4x4[i8x8][0].cost +
2275                                       analysis.l0.me4x4[i8x8][1].cost +
2276                                       analysis.l0.me4x4[i8x8][2].cost +
2277                                       analysis.l0.me4x4[i8x8][3].cost;
2278                             break;
2279                         default:
2280                             x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
2281                             break;
2282                     }
2283                 }
2284             }
2285
2286             if( h->mb.b_chroma_me )
2287             {
2288                 x264_mb_analyse_intra_chroma( h, &analysis );
2289                 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
2290                 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
2291                 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
2292                 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
2293             }
2294             else
2295                 x264_mb_analyse_intra( h, &analysis, i_cost );
2296
2297             i_satd_inter = i_cost;
2298             i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
2299                                       analysis.i_satd_i8x8,
2300                                       analysis.i_satd_i4x4 );
2301
2302             if( analysis.b_mbrd )
2303             {
2304                 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
2305                 i_type = P_L0;
2306                 i_partition = D_16x16;
2307                 i_cost = analysis.l0.me16x16.cost;
2308                 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
2309                 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
2310                 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
2311                 h->mb.i_type = i_type;
2312                 h->mb.i_partition = i_partition;
2313                 if( i_cost < COST_MAX )
2314                     x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2315                 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
2316             }
2317
2318             i_intra_type = I_16x16;
2319             i_intra_cost = analysis.i_satd_i16x16;
2320             COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
2321             COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
2322             COPY2_IF_LT( i_intra_cost, analysis.i_satd_pcm, i_intra_type, I_PCM );
2323             COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
2324
2325             if( i_intra_cost == COST_MAX )
2326                 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
2327
2328             h->mb.i_type = i_type;
2329             h->stat.frame.i_intra_cost += i_intra_cost;
2330             h->stat.frame.i_inter_cost += i_cost;
2331             h->stat.frame.i_mbs_analysed++;
2332
2333             if( h->mb.i_subpel_refine >= 7 && h->mb.i_type != I_PCM )
2334             {
2335                 if( IS_INTRA( h->mb.i_type ) )
2336                 {
2337                     x264_intra_rd_refine( h, &analysis );
2338                 }
2339                 else if( i_partition == D_16x16 )
2340                 {
2341                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
2342                     x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
2343                 }
2344                 else if( i_partition == D_16x8 )
2345                 {
2346                     h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2347                     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2348                     x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
2349                     x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
2350                     x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
2351                     x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
2352                 }
2353                 else if( i_partition == D_8x16 )
2354                 {
2355                     h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
2356                     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
2357                     x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
2358                     x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
2359                     x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
2360                     x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
2361                 }
2362                 else if( i_partition == D_8x8 )
2363                 {
2364                     int i8x8;
2365                     x264_analyse_update_cache( h, &analysis );
2366                     for( i8x8 = 0; i8x8 < 4; i8x8++ )
2367                          if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
2368                              x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
2369                 }
2370             }
2371         }
2372     }
2373     else if( h->sh.i_type == SLICE_TYPE_B )
2374     {
2375         int i_bskip_cost = COST_MAX;
2376         int b_skip = 0;
2377
2378         if( analysis.b_mbrd )
2379             x264_mb_cache_fenc_satd( h );
2380
2381         h->mb.i_type = B_SKIP;
2382         if( h->mb.b_direct_auto_write )
2383         {
2384             /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
2385             for( i = 0; i < 2; i++ )
2386             {
2387                 int b_changed = 1;
2388                 h->sh.b_direct_spatial_mv_pred ^= 1;
2389                 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
2390                 if( analysis.b_direct_available )
2391                 {
2392                     if( b_changed )
2393                     {
2394                         x264_mb_mc( h );
2395                         b_skip = x264_macroblock_probe_bskip( h );
2396                     }
2397                     h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
2398                 }
2399                 else
2400                     b_skip = 0;
2401             }
2402         }
2403         else
2404             analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
2405
2406         if( analysis.b_direct_available )
2407         {
2408             if( !h->mb.b_direct_auto_write )
2409                 x264_mb_mc( h );
2410             if( h->mb.b_lossless )
2411             {
2412                 /* chance of skip is too small to bother */
2413             }
2414             else if( analysis.b_mbrd )
2415             {
2416                 i_bskip_cost = ssd_mb( h );
2417                 /* 6 = minimum cavlc cost of a non-skipped MB */
2418                 b_skip = h->mb.b_skip_mc = i_bskip_cost <= ((6 * analysis.i_lambda2 + 128) >> 8);
2419             }
2420             else if( !h->mb.b_direct_auto_write )
2421             {
2422                 /* Conditioning the probe on neighboring block types
2423                  * doesn't seem to help speed or quality. */
2424                 b_skip = x264_macroblock_probe_bskip( h );
2425             }
2426         }
2427
2428         if( !b_skip )
2429         {
2430             const unsigned int flags = h->param.analyse.inter;
2431             int i_type;
2432             int i_partition;
2433             int i_satd_inter = 0; // shut up uninitialized warning
2434             h->mb.b_skip_mc = 0;
2435
2436             x264_mb_analyse_load_costs( h, &analysis );
2437
2438             /* select best inter mode */
2439             /* direct must be first */
2440             if( analysis.b_direct_available )
2441                 x264_mb_analyse_inter_direct( h, &analysis );
2442
2443             x264_mb_analyse_inter_b16x16( h, &analysis );
2444
2445             i_type = B_L0_L0;
2446             i_partition = D_16x16;
2447             i_cost = analysis.l0.me16x16.cost;
2448             COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
2449             COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
2450             COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
2451
2452             if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
2453             {
2454                 x264_mb_analyse_b_rd( h, &analysis, i_cost );
2455                 if( i_bskip_cost < analysis.i_rd16x16direct &&
2456                     i_bskip_cost < analysis.i_rd16x16bi &&
2457                     i_bskip_cost < analysis.l0.i_rd16x16 &&
2458                     i_bskip_cost < analysis.l1.i_rd16x16 )
2459                 {
2460                     h->mb.i_type = B_SKIP;
2461                     x264_analyse_update_cache( h, &analysis );
2462                     return;
2463                 }
2464             }
2465
2466             if( flags & X264_ANALYSE_BSUB16x16 )
2467             {
2468                 x264_mb_analyse_inter_b8x8( h, &analysis );
2469                 if( analysis.i_cost8x8bi < i_cost )
2470                 {
2471                     i_type = B_8x8;
2472                     i_partition = D_8x8;
2473                     i_cost = analysis.i_cost8x8bi;
2474
2475                     if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
2476                         h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
2477                     {
2478                         x264_mb_analyse_inter_b16x8( h, &analysis );
2479                         COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
2480                                      i_type, analysis.i_mb_type16x8,
2481                                      i_partition, D_16x8 );
2482                     }
2483                     if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
2484                         h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
2485                     {
2486                         x264_mb_analyse_inter_b8x16( h, &analysis );
2487                         COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
2488                                      i_type, analysis.i_mb_type8x16,
2489                                      i_partition, D_8x16 );
2490                     }
2491                 }
2492             }
2493
2494             if( analysis.b_mbrd )
2495             {
2496                 /* refine later */
2497             }
2498             /* refine qpel */
2499             else if( i_partition == D_16x16 )
2500             {
2501                 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2502                 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2503                 if( i_type == B_L0_L0 )
2504                 {
2505                     x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2506                     i_cost = analysis.l0.me16x16.cost
2507                            + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
2508                 }
2509                 else if( i_type == B_L1_L1 )
2510                 {
2511                     x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2512                     i_cost = analysis.l1.me16x16.cost
2513                            + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
2514                 }
2515                 else if( i_type == B_BI_BI )
2516                 {
2517                     x264_me_refine_qpel( h, &analysis.l0.me16x16 );
2518                     x264_me_refine_qpel( h, &analysis.l1.me16x16 );
2519                 }
2520             }
2521             else if( i_partition == D_16x8 )
2522             {
2523                 for( i=0; i<2; i++ )
2524                 {
2525                     if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2526                         x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2527                     if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2528                         x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2529                 }
2530             }
2531             else if( i_partition == D_8x16 )
2532             {
2533                 for( i=0; i<2; i++ )
2534                 {
2535                     if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2536                         x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2537                     if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2538                         x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2539                 }
2540             }
2541             else if( i_partition == D_8x8 )
2542             {
2543                 for( i=0; i<4; i++ )
2544                 {
2545                     x264_me_t *m;
2546                     int i_part_cost_old;
2547                     int i_type_cost;
2548                     int i_part_type = h->mb.i_sub_partition[i];
2549                     int b_bidir = (i_part_type == D_BI_8x8);
2550
2551                     if( i_part_type == D_DIRECT_8x8 )
2552                         continue;
2553                     if( x264_mb_partition_listX_table[0][i_part_type] )
2554                     {
2555                         m = &analysis.l0.me8x8[i];
2556                         i_part_cost_old = m->cost;
2557                         i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2558                         m->cost -= i_type_cost;
2559                         x264_me_refine_qpel( h, m );
2560                         if( !b_bidir )
2561                             analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2562                     }
2563                     if( x264_mb_partition_listX_table[1][i_part_type] )
2564                     {
2565                         m = &analysis.l1.me8x8[i];
2566                         i_part_cost_old = m->cost;
2567                         i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2568                         m->cost -= i_type_cost;
2569                         x264_me_refine_qpel( h, m );
2570                         if( !b_bidir )
2571                             analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2572                     }
2573                     /* TODO: update mvp? */
2574                 }
2575             }
2576
2577             if( analysis.b_mbrd )
2578             {
2579                 i_satd_inter = i_cost;
2580                 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
2581                 i_type = B_SKIP;
2582                 i_cost = i_bskip_cost;
2583                 i_partition = D_16x16;
2584                 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
2585                 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
2586                 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
2587                 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
2588                 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
2589                 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
2590                 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
2591
2592                 h->mb.i_type = i_type;
2593                 h->mb.i_partition = i_partition;
2594             }
2595
2596             x264_mb_analyse_intra( h, &analysis, i_satd_inter );
2597
2598             if( analysis.b_mbrd )
2599             {
2600                 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
2601                 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
2602             }
2603
2604             COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
2605             COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
2606             COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
2607             COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
2608
2609             h->mb.i_type = i_type;
2610             h->mb.i_partition = i_partition;
2611
2612             if( analysis.b_mbrd && h->mb.i_subpel_refine >= 7 && IS_INTRA( i_type ) && i_type != I_PCM )
2613                 x264_intra_rd_refine( h, &analysis );
2614             else if( h->param.analyse.b_bidir_me )
2615                 refine_bidir( h, &analysis );
2616         }
2617     }
2618
2619     x264_analyse_update_cache( h, &analysis );
2620
2621     if( !analysis.b_mbrd )
2622         x264_mb_analyse_transform( h );
2623
2624     h->mb.b_trellis = h->param.analyse.i_trellis;
2625     h->mb.b_noise_reduction = !!h->param.analyse.i_noise_reduction;
2626     if( !IS_SKIP(h->mb.i_type) && h->mb.i_psy_trellis && h->param.analyse.i_trellis == 1 )
2627         x264_psy_trellis_init( h, 0 );
2628     if( h->mb.b_trellis == 1 || h->mb.b_noise_reduction )
2629         h->mb.i_skip_intra = 0;
2630 }
2631
2632 /*-------------------- Update MB from the analysis ----------------------*/
2633 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a  )
2634 {
2635     int i;
2636
2637     switch( h->mb.i_type )
2638     {
2639         case I_4x4:
2640             for( i = 0; i < 16; i++ )
2641                 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
2642
2643             x264_mb_analyse_intra_chroma( h, a );
2644             break;
2645         case I_8x8:
2646             for( i = 0; i < 4; i++ )
2647                 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
2648
2649             x264_mb_analyse_intra_chroma( h, a );
2650             break;
2651         case I_16x16:
2652             h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2653             x264_mb_analyse_intra_chroma( h, a );
2654             break;
2655
2656         case I_PCM:
2657             break;
2658
2659         case P_L0:
2660             switch( h->mb.i_partition )
2661             {
2662                 case D_16x16:
2663                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2664                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2665                     break;
2666
2667                 case D_16x8:
2668                     x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2669                     x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2670                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv );
2671                     x264_macroblock_cache_mv_ptr( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv );
2672                     break;
2673
2674                 case D_8x16:
2675                     x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2676                     x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2677                     x264_macroblock_cache_mv_ptr( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv );
2678                     x264_macroblock_cache_mv_ptr( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv );
2679                     break;
2680
2681                 default:
2682                     x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2683                     break;
2684             }
2685             break;
2686
2687         case P_8x8:
2688             x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2689             x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2690             x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2691             x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2692             for( i = 0; i < 4; i++ )
2693                 x264_mb_cache_mv_p8x8( h, a, i );
2694             break;
2695
2696         case P_SKIP:
2697         {
2698             h->mb.i_partition = D_16x16;
2699             x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2700             x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv );
2701             break;
2702         }
2703
2704         case B_SKIP:
2705         case B_DIRECT:
2706             x264_mb_load_mv_direct8x8( h, 0 );
2707             x264_mb_load_mv_direct8x8( h, 1 );
2708             x264_mb_load_mv_direct8x8( h, 2 );
2709             x264_mb_load_mv_direct8x8( h, 3 );
2710             break;
2711
2712         case B_8x8:
2713             /* optimize: cache might not need to be rewritten */
2714             for( i = 0; i < 4; i++ )
2715                 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2716             break;
2717
2718         default: /* the rest of the B types */
2719             switch( h->mb.i_partition )
2720             {
2721             case D_16x16:
2722                 switch( h->mb.i_type )
2723                 {
2724                 case B_L0_L0:
2725                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2726                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2727
2728                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2729                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0 );
2730                     x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0 );
2731                     break;
2732                 case B_L1_L1:
2733                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2734                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0 );
2735                     x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0 );
2736
2737                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2738                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2739                     break;
2740                 case B_BI_BI:
2741                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2742                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
2743
2744                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2745                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
2746                     break;
2747                 }
2748                 break;
2749             case D_16x8:
2750                 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2751                 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2752                 break;
2753             case D_8x16:
2754                 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2755                 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2756                 break;
2757             default:
2758                 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2759                 break;
2760             }
2761     }
2762
2763 #ifndef NDEBUG
2764     if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
2765     {
2766         int l;
2767         for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
2768         {
2769             int completed;
2770             int ref = h->mb.cache.ref[l][x264_scan8[0]];
2771             if( ref < 0 )
2772                 continue;
2773             completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
2774             if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
2775             {
2776                 x264_log( h, X264_LOG_WARNING, "internal error (MV out of thread range)\n");
2777                 fprintf(stderr, "mb type: %d \n", h->mb.i_type);
2778                 fprintf(stderr, "mv: l%dr%d (%d,%d) \n", l, ref,
2779                                 h->mb.cache.mv[l][x264_scan8[15]][0],
2780                                 h->mb.cache.mv[l][x264_scan8[15]][1] );
2781                 fprintf(stderr, "limit: %d \n", h->mb.mv_max_spel[1]);
2782                 fprintf(stderr, "mb_xy: %d,%d \n", h->mb.i_mb_x, h->mb.i_mb_y);
2783                 fprintf(stderr, "completed: %d \n", completed );
2784                 x264_log( h, X264_LOG_WARNING, "recovering by using intra mode\n");
2785                 x264_mb_analyse_intra( h, a, COST_MAX );
2786                 h->mb.i_type = I_16x16;
2787                 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2788                 x264_mb_analyse_intra_chroma( h, a );
2789             }
2790         }
2791     }
2792 #endif
2793 }
2794
2795 #include "slicetype.c"
2796