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