]> git.sesse.net Git - x264/blob - encoder/analyse.c
Trellis RD quantization.
[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 <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <math.h>
29 #include <limits.h>
30
31 #include "common/common.h"
32 #include "macroblock.h"
33 #include "me.h"
34 #include "ratecontrol.h"
35 #include "analyse.h"
36 #include "rdo.c"
37
38 typedef struct
39 {
40     /* 16x16 */
41     int i_ref;
42     x264_me_t me16x16;
43
44     /* 8x8 */
45     int       i_cost8x8;
46     int       mvc[16][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][4];
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 i_best_satd;
86
87     /* Luma part */
88     int i_sad_i16x16;
89     int i_predict16x16;
90
91     int i_sad_i8x8;
92     int i_predict8x8[2][2];
93
94     int i_sad_i4x4;
95     int i_predict4x4[4][4];
96
97     /* Chroma part */
98     int i_sad_i8x8chroma;
99     int i_predict8x8chroma;
100
101     /* II: Inter part P/B frame */
102     x264_mb_analysis_list_t l0;
103     x264_mb_analysis_list_t l1;
104
105     int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
106     int i_cost16x16direct;
107     int i_cost8x8bi;
108     int i_cost8x8direct[4];
109     int i_cost16x8bi;
110     int i_cost8x16bi;
111
112     int i_mb_partition16x8[2]; /* mb_partition_e */
113     int i_mb_partition8x16[2];
114     int i_mb_type16x8; /* mb_class_e */
115     int i_mb_type8x16;
116
117     int b_direct_available;
118
119 } x264_mb_analysis_t;
120
121 /* lambda = pow(2,qp/6-2) */
122 static const int i_qp0_cost_table[52] = {
123    1, 1, 1, 1, 1, 1, 1, 1,  /*  0-7 */
124    1, 1, 1, 1,              /*  8-11 */
125    1, 1, 1, 1, 2, 2, 2, 2,  /* 12-19 */
126    3, 3, 3, 4, 4, 4, 5, 6,  /* 20-27 */
127    6, 7, 8, 9,10,11,13,14,  /* 28-35 */
128   16,18,20,23,25,29,32,36,  /* 36-43 */
129   40,45,51,57,64,72,81,91   /* 44-51 */
130 };
131
132 /* pow(lambda,2) * .9 */
133 static const int i_qp0_cost2_table[52] = {
134    1,   1,   1,   1,   1,   1, /*  0-5  */
135    1,   1,   1,   1,   1,   1, /*  6-11 */
136    1,   1,   1,   2,   2,   3, /* 12-17 */
137    4,   5,   6,   7,   9,  11, /* 18-23 */
138   14,  18,  23,  29,  36,  46, /* 24-29 */
139   58,  73,  91, 115, 145, 183, /* 30-35 */
140  230, 290, 366, 461, 581, 731, /* 36-41 */
141  922,1161,1463,1843,2322,2926, /* 42-47 */
142 3686,4645,5852,7373
143 };
144
145 /* TODO: calculate CABAC costs */
146 static const int i_mb_b_cost_table[19] = {
147     9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
148 };
149 static const int i_mb_b16x8_cost_table[17] = {
150     0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
151 };
152 static const int i_sub_mb_b_cost_table[13] = {
153     7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
154 };
155 static const int i_sub_mb_p_cost_table[4] = {
156     5, 3, 3, 1
157 };
158
159 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
160
161 /* initialize an array of lambda*nbits for all possible mvs */
162 static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
163 {
164     static int16_t *p_cost_mv[52];
165
166     if( !p_cost_mv[a->i_qp] )
167     {
168         /* could be faster, but isn't called many times */
169         /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
170         int i;
171         p_cost_mv[a->i_qp] = x264_malloc( (4*4*h->param.analyse.i_mv_range + 1) * sizeof(int16_t) );
172         p_cost_mv[a->i_qp] += 2*4*h->param.analyse.i_mv_range;
173         for( i = 0; i <= 2*4*h->param.analyse.i_mv_range; i++ )
174         {
175             p_cost_mv[a->i_qp][-i] =
176             p_cost_mv[a->i_qp][i]  = a->i_lambda * bs_size_se( i );
177         }
178     }
179
180     a->p_cost_mv = p_cost_mv[a->i_qp];
181 }
182
183 static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
184 {
185     memset( a, 0, sizeof( x264_mb_analysis_t ) );
186
187     /* conduct the analysis using this lamda and QP */
188     a->i_qp = h->mb.i_qp = i_qp;
189     a->i_lambda = i_qp0_cost_table[i_qp];
190     a->i_lambda2 = i_qp0_cost2_table[i_qp];
191     a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 && h->sh.i_type != SLICE_TYPE_B;
192
193     h->mb.i_me_method = h->param.analyse.i_me_method;
194     h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
195     h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
196                         && h->mb.i_subpel_refine >= 5;
197     h->mb.b_trellis = h->param.analyse.i_trellis > 1;
198     h->mb.b_transform_8x8 = 0;
199
200     /* I: Intra part */
201     a->i_sad_i16x16 =
202     a->i_sad_i8x8   =
203     a->i_sad_i4x4   =
204     a->i_sad_i8x8chroma = COST_MAX;
205
206     a->b_fast_intra = 0;
207     a->i_best_satd = COST_MAX;
208
209     /* II: Inter part P/B frame */
210     if( h->sh.i_type != SLICE_TYPE_I )
211     {
212         int i;
213         int i_fmv_range = h->param.analyse.i_mv_range - 16;
214
215         /* Calculate max allowed MV range */
216 #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range )
217         h->mb.mv_min_fpel[0] = CLIP_FMV( -16*h->mb.i_mb_x - 8 );
218         h->mb.mv_max_fpel[0] = CLIP_FMV( 16*( h->sps->i_mb_width - h->mb.i_mb_x ) - 8 );
219         h->mb.mv_min[0] = 4*( h->mb.mv_min_fpel[0] - 16 );
220         h->mb.mv_max[0] = 4*( h->mb.mv_max_fpel[0] + 16 );
221         if( h->mb.i_mb_x == 0)
222         {
223             h->mb.mv_min_fpel[1] = CLIP_FMV( -16*h->mb.i_mb_y - 8 );
224             h->mb.mv_max_fpel[1] = CLIP_FMV( 16*( h->sps->i_mb_height - h->mb.i_mb_y ) - 8 );
225             h->mb.mv_min[1] = 4*( h->mb.mv_min_fpel[1] - 16 );
226             h->mb.mv_max[1] = 4*( h->mb.mv_max_fpel[1] + 16 );
227         }
228 #undef CLIP_FMV
229
230         a->l0.me16x16.cost =
231         a->l0.i_cost8x8    = COST_MAX;
232
233         for( i = 0; i < 4; i++ )
234         {
235             a->l0.i_cost4x4[i] =
236             a->l0.i_cost8x4[i] =
237             a->l0.i_cost4x8[i] = COST_MAX;
238         }
239
240         a->l0.i_cost16x8   =
241         a->l0.i_cost8x16   = COST_MAX;
242         if( h->sh.i_type == SLICE_TYPE_B )
243         {
244             a->l1.me16x16.cost =
245             a->l1.i_cost8x8    = COST_MAX;
246
247             for( i = 0; i < 4; i++ )
248             {
249                 a->l1.i_cost4x4[i] =
250                 a->l1.i_cost8x4[i] =
251                 a->l1.i_cost4x8[i] =
252                 a->i_cost8x8direct[i] = COST_MAX;
253             }
254
255             a->l1.i_cost16x8   =
256             a->l1.i_cost8x16   =
257
258             a->i_cost16x16bi   =
259             a->i_cost16x16direct =
260             a->i_cost8x8bi     =
261             a->i_cost16x8bi    =
262             a->i_cost8x16bi    = COST_MAX;
263         }
264
265         /* Fast intra decision */
266         if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
267         {
268             if( a->b_mbrd
269                || IS_INTRA( h->mb.i_mb_type_left )
270                || IS_INTRA( h->mb.i_mb_type_top )
271                || IS_INTRA( h->mb.i_mb_type_topleft )
272                || IS_INTRA( h->mb.i_mb_type_topright )
273                || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
274                || (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])) )
275             { /* intra is likely */ }
276             else
277             {
278                 a->b_fast_intra = 1;
279             }
280         }
281     }
282 }
283
284
285
286 /*
287  * Handle intra mb
288  */
289 /* Max = 4 */
290 static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
291 {
292     if( i_neighbour & MB_TOPLEFT )
293     {
294         /* top and left avaible */
295         *mode++ = I_PRED_16x16_V;
296         *mode++ = I_PRED_16x16_H;
297         *mode++ = I_PRED_16x16_DC;
298         *mode++ = I_PRED_16x16_P;
299         *pi_count = 4;
300     }
301     else if( i_neighbour & MB_LEFT )
302     {
303         /* left available*/
304         *mode++ = I_PRED_16x16_DC_LEFT;
305         *mode++ = I_PRED_16x16_H;
306         *pi_count = 2;
307     }
308     else if( i_neighbour & MB_TOP )
309     {
310         /* top available*/
311         *mode++ = I_PRED_16x16_DC_TOP;
312         *mode++ = I_PRED_16x16_V;
313         *pi_count = 2;
314     }
315     else
316     {
317         /* none avaible */
318         *mode = I_PRED_16x16_DC_128;
319         *pi_count = 1;
320     }
321 }
322
323 /* Max = 4 */
324 static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
325 {
326     if( i_neighbour & MB_TOPLEFT )
327     {
328         /* top and left avaible */
329         *mode++ = I_PRED_CHROMA_V;
330         *mode++ = I_PRED_CHROMA_H;
331         *mode++ = I_PRED_CHROMA_DC;
332         *mode++ = I_PRED_CHROMA_P;
333         *pi_count = 4;
334     }
335     else if( i_neighbour & MB_LEFT )
336     {
337         /* left available*/
338         *mode++ = I_PRED_CHROMA_DC_LEFT;
339         *mode++ = I_PRED_CHROMA_H;
340         *pi_count = 2;
341     }
342     else if( i_neighbour & MB_TOP )
343     {
344         /* top available*/
345         *mode++ = I_PRED_CHROMA_DC_TOP;
346         *mode++ = I_PRED_CHROMA_V;
347         *pi_count = 2;
348     }
349     else
350     {
351         /* none avaible */
352         *mode = I_PRED_CHROMA_DC_128;
353         *pi_count = 1;
354     }
355 }
356
357 /* MAX = 9 */
358 static void predict_4x4_mode_available( unsigned int i_neighbour,
359                                         int *mode, int *pi_count )
360 {
361     int b_l = i_neighbour & MB_LEFT;
362     int b_t = i_neighbour & MB_TOP;
363
364     if( b_l && b_t )
365     {
366         *mode++ = I_PRED_4x4_DC;
367         *mode++ = I_PRED_4x4_H;
368         *mode++ = I_PRED_4x4_V;
369         *mode++ = I_PRED_4x4_DDL;
370         *mode++ = I_PRED_4x4_DDR;
371         *mode++ = I_PRED_4x4_VR;
372         *mode++ = I_PRED_4x4_HD;
373         *mode++ = I_PRED_4x4_VL;
374         *mode++ = I_PRED_4x4_HU;
375         *pi_count = 9;
376     }
377     else if( b_l )
378     {
379         *mode++ = I_PRED_4x4_DC_LEFT;
380         *mode++ = I_PRED_4x4_H;
381         *mode++ = I_PRED_4x4_HU;
382         *pi_count = 3;
383     }
384     else if( b_t )
385     {
386         *mode++ = I_PRED_4x4_DC_TOP;
387         *mode++ = I_PRED_4x4_V;
388         *mode++ = I_PRED_4x4_DDL;
389         *mode++ = I_PRED_4x4_VL;
390         *pi_count = 4;
391     }
392     else
393     {
394         *mode++ = I_PRED_4x4_DC_128;
395         *pi_count = 1;
396     }
397 }
398
399 static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
400 {
401     int i;
402
403     int i_max;
404     int predict_mode[9];
405
406     uint8_t *p_dstc[2], *p_srcc[2];
407     int      i_stride[2];
408
409     if( a->i_sad_i8x8chroma < COST_MAX )
410         return;
411
412     /* 8x8 prediction selection for chroma */
413     p_dstc[0] = h->mb.pic.p_fdec[1];
414     p_dstc[1] = h->mb.pic.p_fdec[2];
415     p_srcc[0] = h->mb.pic.p_fenc[1];
416     p_srcc[1] = h->mb.pic.p_fenc[2];
417
418     i_stride[0] = h->mb.pic.i_stride[1];
419     i_stride[1] = h->mb.pic.i_stride[2];
420
421     predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
422     a->i_sad_i8x8chroma = COST_MAX;
423     for( i = 0; i < i_max; i++ )
424     {
425         int i_sad;
426         int i_mode;
427
428         i_mode = predict_mode[i];
429
430         /* we do the prediction */
431         h->predict_8x8c[i_mode]( p_dstc[0], i_stride[0] );
432         h->predict_8x8c[i_mode]( p_dstc[1], i_stride[1] );
433
434         /* we calculate the cost */
435         i_sad = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], i_stride[0],
436                                           p_srcc[0], i_stride[0] ) +
437                 h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], i_stride[1],
438                                           p_srcc[1], i_stride[1] ) +
439                 a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
440
441         /* if i_score is lower it is better */
442         if( a->i_sad_i8x8chroma > i_sad )
443         {
444             a->i_predict8x8chroma = i_mode;
445             a->i_sad_i8x8chroma   = i_sad;
446         }
447     }
448
449     h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
450 }
451
452 static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_cost_inter )
453 {
454     const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
455     const int i_stride = h->mb.pic.i_stride[0];
456     uint8_t  *p_src = h->mb.pic.p_fenc[0];
457     uint8_t  *p_dst = h->mb.pic.p_fdec[0];
458     int      f8_satd_rd_ratio = 0;
459
460     int i, idx;
461     int i_max;
462     int predict_mode[9];
463
464     const int i_satd_thresh = a->i_best_satd * 5/4 + a->i_lambda * 10;
465
466     /*---------------- Try all mode and calculate their score ---------------*/
467
468     /* 16x16 prediction selection */
469     predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
470     for( i = 0; i < i_max; i++ )
471     {
472         int i_sad;
473         int i_mode;
474
475         i_mode = predict_mode[i];
476         h->predict_16x16[i_mode]( p_dst, i_stride );
477
478         i_sad = h->pixf.mbcmp[PIXEL_16x16]( p_dst, i_stride, p_src, i_stride ) +
479                 a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
480         if( a->i_sad_i16x16 > i_sad )
481         {
482             a->i_predict16x16 = i_mode;
483             a->i_sad_i16x16   = i_sad;
484         }
485     }
486
487     if( a->b_mbrd )
488     {
489         f8_satd_rd_ratio = ((unsigned)i_cost_inter << 8) / a->i_best_satd + 1;
490         x264_mb_analyse_intra_chroma( h, a );
491         if( h->mb.b_chroma_me )
492             a->i_sad_i16x16 += a->i_sad_i8x8chroma;
493         if( a->i_sad_i16x16 < i_satd_thresh )
494         {
495             h->mb.i_type = I_16x16;
496             h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
497             a->i_sad_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
498         }
499         else
500             a->i_sad_i16x16 = a->i_sad_i16x16 * f8_satd_rd_ratio >> 8;
501     }
502     else
503     {
504         if( h->sh.i_type == SLICE_TYPE_B )
505             /* cavlc mb type prefix */
506             a->i_sad_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
507         if( a->b_fast_intra && a->i_sad_i16x16 > 2*i_cost_inter )
508             return;
509     }
510
511     /* 4x4 prediction selection */
512     if( flags & X264_ANALYSE_I4x4 )
513     {
514         a->i_sad_i4x4 = 0;
515         for( idx = 0; idx < 16; idx++ )
516         {
517             uint8_t *p_src_by;
518             uint8_t *p_dst_by;
519             int     i_best;
520             int x, y;
521             int i_pred_mode;
522
523             i_pred_mode= x264_mb_predict_intra4x4_mode( h, idx );
524             x = block_idx_x[idx];
525             y = block_idx_y[idx];
526
527             p_src_by = p_src + 4 * x + 4 * y * i_stride;
528             p_dst_by = p_dst + 4 * x + 4 * y * i_stride;
529
530             i_best = COST_MAX;
531             predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
532
533             if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
534                 /* emulate missing topright samples */
535                 *(uint32_t*) &p_dst_by[4 - i_stride] = p_dst_by[3 - i_stride] * 0x01010101U;
536
537             for( i = 0; i < i_max; i++ )
538             {
539                 int i_sad;
540                 int i_mode;
541
542                 i_mode = predict_mode[i];
543                 h->predict_4x4[i_mode]( p_dst_by, i_stride );
544
545                 i_sad = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, i_stride,
546                                                   p_src_by, i_stride )
547                       + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
548
549                 if( i_best > i_sad )
550                 {
551                     a->i_predict4x4[x][y] = i_mode;
552                     i_best = i_sad;
553                 }
554             }
555             a->i_sad_i4x4 += i_best;
556
557             /* we need to encode this block now (for next ones) */
558             h->predict_4x4[a->i_predict4x4[x][y]]( p_dst_by, i_stride );
559             x264_mb_encode_i4x4( h, idx, a->i_qp );
560
561             h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[x][y];
562         }
563
564         a->i_sad_i4x4 += a->i_lambda * 24;    /* from JVT (SATD0) */
565         if( a->b_mbrd )
566         {
567             if( h->mb.b_chroma_me )
568                 a->i_sad_i4x4 += a->i_sad_i8x8chroma;
569             if( a->i_sad_i4x4 < i_satd_thresh )
570             {
571                 h->mb.i_type = I_4x4;
572                 a->i_sad_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
573             }
574             else
575                 a->i_sad_i4x4 = a->i_sad_i4x4 * f8_satd_rd_ratio >> 8;
576         }
577         else
578         {
579             if( h->sh.i_type == SLICE_TYPE_B )
580                 a->i_sad_i4x4 += a->i_lambda * i_mb_b_cost_table[I_4x4];
581         }
582     }
583
584     /* 8x8 prediction selection */
585     if( flags & X264_ANALYSE_I8x8 )
586     {
587         a->i_sad_i8x8 = 0;
588         for( idx = 0; idx < 4; idx++ )
589         {
590             uint8_t *p_src_by;
591             uint8_t *p_dst_by;
592             int     i_best;
593             int x, y;
594             int i_pred_mode;
595
596             i_pred_mode= x264_mb_predict_intra4x4_mode( h, 4*idx );
597             x = idx&1;
598             y = idx>>1;
599
600             p_src_by = p_src + 8 * x + 8 * y * i_stride;
601             p_dst_by = p_dst + 8 * x + 8 * y * i_stride;
602
603             i_best = COST_MAX;
604             predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
605             for( i = 0; i < i_max; i++ )
606             {
607                 int i_sad;
608                 int i_mode;
609
610                 i_mode = predict_mode[i];
611                 h->predict_8x8[i_mode]( p_dst_by, i_stride, h->mb.i_neighbour );
612
613                 /* could use sa8d, but it doesn't seem worth the speed cost (without mmx at least) */
614                 i_sad = h->pixf.mbcmp[PIXEL_8x8]( p_dst_by, i_stride,
615                                                   p_src_by, i_stride )
616                       + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
617
618                 if( i_best > i_sad )
619                 {
620                     a->i_predict8x8[x][y] = i_mode;
621                     i_best = i_sad;
622                 }
623             }
624             a->i_sad_i8x8 += i_best;
625
626             /* we need to encode this block now (for next ones) */
627             h->predict_8x8[a->i_predict8x8[x][y]]( p_dst_by, i_stride, h->mb.i_neighbour );
628             x264_mb_encode_i8x8( h, idx, a->i_qp );
629
630             x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[x][y] );
631         }
632
633         if( a->b_mbrd )
634         {
635             if( h->mb.b_chroma_me )
636                 a->i_sad_i8x8 += a->i_sad_i8x8chroma;
637             if( a->i_sad_i8x8 < i_satd_thresh )
638             {
639                 h->mb.i_type = I_8x8;
640                 a->i_sad_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
641             }
642             else
643                 a->i_sad_i8x8 = a->i_sad_i8x8 * f8_satd_rd_ratio >> 8;
644         }
645         else
646         {
647             // FIXME some bias like in i4x4?
648             if( h->sh.i_type == SLICE_TYPE_B )
649                 a->i_sad_i8x8 += a->i_lambda * i_mb_b_cost_table[I_8x8];
650         }
651     }
652 }
653
654 #define LOAD_FENC( m, src, xoff, yoff) \
655     (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
656     (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
657     (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
658     (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
659     (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]];
660
661 #define LOAD_HPELS(m, src, xoff, yoff) \
662     (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
663     (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
664     (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
665     (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
666     (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
667     (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]];
668
669 #define REF_COST(list, ref) \
670     (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
671
672 static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
673 {
674     x264_me_t m;
675     int i_ref;
676     int mvc[7][2], i_mvc;
677     int i_halfpel_thresh = INT_MAX;
678     int *p_halfpel_thresh = h->i_ref0>1 ? &i_halfpel_thresh : NULL;
679
680     /* 16x16 Search on all ref frame */
681     m.i_pixel = PIXEL_16x16;
682     m.p_cost_mv = a->p_cost_mv;
683     LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
684
685     a->l0.me16x16.cost = INT_MAX;
686     for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
687     {
688         const int i_ref_cost = REF_COST( 0, i_ref );
689         i_halfpel_thresh -= i_ref_cost;
690         m.i_ref_cost = i_ref_cost;
691         m.i_ref = i_ref;
692
693         /* search with ref */
694         LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, 0 );
695         x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
696         x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
697         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
698
699         m.cost += i_ref_cost;
700         i_halfpel_thresh += i_ref_cost;
701
702         if( m.cost < a->l0.me16x16.cost )
703             a->l0.me16x16 = m;
704
705         /* save mv for predicting neighbors */
706         a->l0.mvc[i_ref][0][0] =
707         h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
708         a->l0.mvc[i_ref][0][1] =
709         h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
710     }
711
712     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
713
714     if( a->b_mbrd )
715     {
716         a->i_best_satd = a->l0.me16x16.cost;
717         h->mb.i_type = P_L0;
718         h->mb.i_partition = D_16x16;
719         x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
720         a->l0.me16x16.cost = x264_rd_cost_mb( h, a->i_lambda2 );
721     }
722 }
723
724 static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
725 {
726     x264_me_t m;
727     int i_ref;
728     uint8_t  **p_fenc = h->mb.pic.p_fenc;
729     int i_halfpel_thresh = INT_MAX;
730     int *p_halfpel_thresh = /*h->i_ref0>1 ? &i_halfpel_thresh : */NULL;
731     int i;
732     int i_maxref = h->i_ref0-1;
733
734     h->mb.i_partition = D_8x8;
735
736     /* early termination: if 16x16 chose ref 0, then evalute no refs older
737      * than those used by the neighbors */
738     if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
739         h->mb.i_mb_type_top && h->mb.i_mb_type_left )
740     {
741         i_maxref = 0;
742         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
743         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
744         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
745         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
746         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
747         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
748     }
749
750     for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
751     {
752          a->l0.mvc[i_ref][0][0] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0];
753          a->l0.mvc[i_ref][0][1] = h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1];
754     }
755
756     for( i = 0; i < 4; i++ )
757     {
758         x264_me_t *l0m = &a->l0.me8x8[i];
759         const int x8 = i%2;
760         const int y8 = i/2;
761
762         m.i_pixel = PIXEL_8x8;
763         m.p_cost_mv = a->p_cost_mv;
764
765         LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
766         l0m->cost = INT_MAX;
767         for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
768         {
769              const int i_ref_cost = REF_COST( 0, i_ref );
770              i_halfpel_thresh -= i_ref_cost;
771              m.i_ref_cost = i_ref_cost;
772              m.i_ref = i_ref;
773
774              LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 8*x8, 8*y8 );
775              x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
776              x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
777              x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
778
779              m.cost += i_ref_cost;
780              i_halfpel_thresh += i_ref_cost;
781              *(uint64_t*)a->l0.mvc[i_ref][i+1] = *(uint64_t*)m.mv;
782
783              if( m.cost < l0m->cost )
784                  *l0m = m;
785         }
786         x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv[0], l0m->mv[1] );
787         x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
788
789         /* mb type cost */
790         l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
791     }
792
793     a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
794                       a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
795     if( a->b_mbrd )
796     {
797         if( a->i_best_satd > a->l0.i_cost8x8 )
798             a->i_best_satd = a->l0.i_cost8x8;
799         h->mb.i_type = P_8x8;
800         h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
801         h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
802         a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
803     }
804 }
805
806 static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
807 {
808     const int i_ref = a->l0.me16x16.i_ref;
809     const int i_ref_cost = REF_COST( 0, i_ref );
810     uint8_t  **p_fref = h->mb.pic.p_fref[0][i_ref];
811     uint8_t  **p_fenc = h->mb.pic.p_fenc;
812     int i_mvc;
813     int (*mvc)[2] = a->l0.mvc[i_ref];
814     int i;
815
816     /* XXX Needed for x264_mb_predict_mv */
817     h->mb.i_partition = D_8x8;
818
819     i_mvc = 1;
820     *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.me16x16.mv;
821
822     for( i = 0; i < 4; i++ )
823     {
824         x264_me_t *m = &a->l0.me8x8[i];
825         const int x8 = i%2;
826         const int y8 = i/2;
827
828         m->i_pixel = PIXEL_8x8;
829         m->p_cost_mv = a->p_cost_mv;
830         m->i_ref_cost = i_ref_cost;
831         m->i_ref = i_ref;
832
833         LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
834         LOAD_HPELS( m, p_fref, 8*x8, 8*y8 );
835         x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
836         x264_me_search( h, m, mvc, i_mvc );
837
838         x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
839
840         *(uint64_t*)mvc[i_mvc] = *(uint64_t*)m->mv;
841         i_mvc++;
842
843         /* mb type cost */
844         m->cost += i_ref_cost;
845         m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
846     }
847
848     /* theoretically this should include 4*ref_cost,
849      * but 3 seems a better approximation of cabac. */
850     a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
851                       a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
852                       REF_COST( 0, a->l0.me16x16.i_ref );
853     if( a->b_mbrd )
854     {
855         if( a->i_best_satd > a->l0.i_cost8x8 )
856             a->i_best_satd = a->l0.i_cost8x8;
857         h->mb.i_type = P_8x8;
858         h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
859         h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
860         a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
861     }
862 }
863
864 static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
865 {
866     x264_me_t m;
867     uint8_t  **p_fenc = h->mb.pic.p_fenc;
868     int mvc[3][2];
869     int i, j;
870
871     /* XXX Needed for x264_mb_predict_mv */
872     h->mb.i_partition = D_16x8;
873
874     for( i = 0; i < 2; i++ )
875     {
876         x264_me_t *l0m = &a->l0.me16x8[i];
877         const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
878         const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
879
880         m.i_pixel = PIXEL_16x8;
881         m.p_cost_mv = a->p_cost_mv;
882
883         LOAD_FENC( &m, p_fenc, 0, 8*i );
884         l0m->cost = INT_MAX;
885         for( j = 0; j < i_ref8s; j++ )
886         {
887              const int i_ref = ref8[j];
888              const int i_ref_cost = REF_COST( 0, i_ref );
889              m.i_ref_cost = i_ref_cost;
890              m.i_ref = i_ref;
891
892              /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
893              *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
894              *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][2*i+1];
895              *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][2*i+2];
896
897              LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, 8*i );
898              x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
899              x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
900              x264_me_search( h, &m, mvc, 3 );
901
902              m.cost += i_ref_cost;
903
904              if( m.cost < l0m->cost )
905                  *l0m = m;
906         }
907         x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, l0m->mv[0], l0m->mv[1] );
908         x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
909     }
910
911     a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
912     if( a->b_mbrd )
913     {
914         if( a->i_best_satd > a->l0.i_cost16x8 )
915             a->i_best_satd = a->l0.i_cost16x8;
916         h->mb.i_type = P_L0;
917         a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
918     }
919 }
920
921 static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
922 {
923     x264_me_t m;
924     uint8_t  **p_fenc = h->mb.pic.p_fenc;
925     int mvc[3][2];
926     int i, j;
927
928     /* XXX Needed for x264_mb_predict_mv */
929     h->mb.i_partition = D_8x16;
930
931     for( i = 0; i < 2; i++ )
932     {
933         x264_me_t *l0m = &a->l0.me8x16[i];
934         const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
935         const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
936
937         m.i_pixel = PIXEL_8x16;
938         m.p_cost_mv = a->p_cost_mv;
939
940         LOAD_FENC( &m, p_fenc, 8*i, 0 );
941         l0m->cost = INT_MAX;
942         for( j = 0; j < i_ref8s; j++ )
943         {
944              const int i_ref = ref8[j];
945              const int i_ref_cost = REF_COST( 0, i_ref );
946              m.i_ref_cost = i_ref_cost;
947              m.i_ref = i_ref;
948
949              *(uint64_t*)mvc[0] = *(uint64_t*)a->l0.mvc[i_ref][0];
950              *(uint64_t*)mvc[1] = *(uint64_t*)a->l0.mvc[i_ref][i+1];
951              *(uint64_t*)mvc[2] = *(uint64_t*)a->l0.mvc[i_ref][i+3];
952
953              LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 8*i, 0 );
954              x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
955              x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
956              x264_me_search( h, &m, mvc, 3 );
957
958              m.cost += i_ref_cost;
959
960              if( m.cost < l0m->cost )
961                  *l0m = m;
962         }
963         x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, l0m->mv[0], l0m->mv[1] );
964         x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
965     }
966
967     a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
968     if( a->b_mbrd )
969     {
970         if( a->i_best_satd > a->l0.i_cost8x16 )
971             a->i_best_satd = a->l0.i_cost8x16;
972         h->mb.i_type = P_L0;
973         a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
974     }
975 }
976
977 static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
978 {
979     uint8_t pix1[8*8], pix2[8*8];
980     const int i_stride = h->mb.pic.i_stride[1];
981     const int off = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
982
983 #define CHROMA4x4MC( width, height, me, x, y ) \
984     h->mc.mc_chroma( &p_fref[4][off+x+y*i_stride], i_stride, &pix1[x+y*8], 8, (me).mv[0], (me).mv[1], width, height ); \
985     h->mc.mc_chroma( &p_fref[5][off+x+y*i_stride], i_stride, &pix2[x+y*8], 8, (me).mv[0], (me).mv[1], width, height );
986
987     if( pixel == PIXEL_4x4 )
988     {
989         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
990         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 0,2 );
991         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 2,0 );
992         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
993     }
994     else if( pixel == PIXEL_8x4 )
995     {
996         CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
997         CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
998     }
999     else
1000     {
1001         CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
1002         CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
1003     }
1004
1005     return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][off], i_stride, pix1, 8 )
1006          + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][off], i_stride, pix2, 8 );
1007 }
1008
1009 static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1010 {
1011     uint8_t  **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1012     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1013
1014     int i4x4;
1015
1016     /* XXX Needed for x264_mb_predict_mv */
1017     h->mb.i_partition = D_8x8;
1018
1019     for( i4x4 = 0; i4x4 < 4; i4x4++ )
1020     {
1021         const int idx = 4*i8x8 + i4x4;
1022         const int x4 = block_idx_x[idx];
1023         const int y4 = block_idx_y[idx];
1024         const int i_mvc = (i4x4 == 0);
1025
1026         x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
1027
1028         m->i_pixel = PIXEL_4x4;
1029         m->p_cost_mv = a->p_cost_mv;
1030
1031         LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1032         LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
1033
1034         x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1035         x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
1036
1037         x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
1038     }
1039     a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
1040                             a->l0.me4x4[i8x8][1].cost +
1041                             a->l0.me4x4[i8x8][2].cost +
1042                             a->l0.me4x4[i8x8][3].cost +
1043                             REF_COST( 0, a->l0.me8x8[i8x8].i_ref ) +
1044                             a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
1045     if( h->mb.b_chroma_me )
1046         a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
1047 }
1048
1049 static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1050 {
1051     uint8_t  **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1052     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1053
1054     int i8x4;
1055
1056     /* XXX Needed for x264_mb_predict_mv */
1057     h->mb.i_partition = D_8x8;
1058
1059     for( i8x4 = 0; i8x4 < 2; i8x4++ )
1060     {
1061         const int idx = 4*i8x8 + 2*i8x4;
1062         const int x4 = block_idx_x[idx];
1063         const int y4 = block_idx_y[idx];
1064         const int i_mvc = (i8x4 == 0);
1065
1066         x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
1067
1068         m->i_pixel = PIXEL_8x4;
1069         m->p_cost_mv = a->p_cost_mv;
1070
1071         LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1072         LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
1073
1074         x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
1075         x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1076
1077         x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
1078     }
1079     a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
1080                             REF_COST( 0, a->l0.me8x8[i8x8].i_ref ) +
1081                             a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
1082     if( h->mb.b_chroma_me )
1083         a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
1084 }
1085
1086 static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
1087 {
1088     uint8_t  **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
1089     uint8_t  **p_fenc = h->mb.pic.p_fenc;
1090
1091     int i4x8;
1092
1093     /* XXX Needed for x264_mb_predict_mv */
1094     h->mb.i_partition = D_8x8;
1095
1096     for( i4x8 = 0; i4x8 < 2; i4x8++ )
1097     {
1098         const int idx = 4*i8x8 + i4x8;
1099         const int x4 = block_idx_x[idx];
1100         const int y4 = block_idx_y[idx];
1101         const int i_mvc = (i4x8 == 0);
1102
1103         x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
1104
1105         m->i_pixel = PIXEL_4x8;
1106         m->p_cost_mv = a->p_cost_mv;
1107
1108         LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
1109         LOAD_HPELS( m, p_fref, 4*x4, 4*y4 );
1110
1111         x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
1112         x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
1113
1114         x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
1115     }
1116     a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
1117                             REF_COST( 0, a->l0.me8x8[i8x8].i_ref ) +
1118                             a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
1119     if( h->mb.b_chroma_me )
1120         a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
1121 }
1122
1123 static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
1124 {
1125     /* Assumes that fdec still contains the results of
1126      * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
1127
1128     uint8_t **p_fenc = h->mb.pic.p_fenc;
1129     uint8_t **p_fdec = h->mb.pic.p_fdec;
1130     int i_stride= h->mb.pic.i_stride[0];
1131     int i;
1132
1133     a->i_cost16x16direct = 0;
1134     for( i = 0; i < 4; i++ )
1135     {
1136         const int x8 = i%2;
1137         const int y8 = i/2;
1138         const int off = 8 * x8 + 8 * i_stride * y8;
1139         a->i_cost16x16direct +=
1140         a->i_cost8x8direct[i] =
1141             h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][off], i_stride, &p_fdec[0][off], i_stride );
1142
1143         /* mb type cost */
1144         a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
1145     }
1146
1147     a->i_cost16x16direct += a->i_lambda * i_mb_b_cost_table[B_DIRECT];
1148 }
1149
1150 #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) \
1151     { \
1152         if( h->param.analyse.b_weighted_bipred ) \
1153             h->mc.avg_weight[size]( pix1, stride1, src2, stride2, \
1154                     h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); \
1155         else \
1156             h->mc.avg[size]( pix1, stride1, src2, stride2 ); \
1157     }
1158
1159 static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
1160 {
1161     uint8_t pix1[16*16], pix2[16*16];
1162     uint8_t *src2;
1163     int stride2 = 16;
1164     int src2_ref, pix1_ref;
1165
1166     x264_me_t m;
1167     int i_ref;
1168     int mvc[8][2], i_mvc;
1169     int i_halfpel_thresh = INT_MAX;
1170     int *p_halfpel_thresh = h->i_ref0>1 ? &i_halfpel_thresh : NULL;
1171
1172     /* 16x16 Search on all ref frame */
1173     m.i_pixel = PIXEL_16x16;
1174     m.p_cost_mv = a->p_cost_mv;
1175     LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
1176
1177     /* ME for List 0 */
1178     a->l0.me16x16.cost = INT_MAX;
1179     for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
1180     {
1181         /* search with ref */
1182         LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, 0 );
1183         x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
1184         x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
1185         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1186
1187         /* add ref cost */
1188         m.cost += REF_COST( 0, i_ref );
1189
1190         if( m.cost < a->l0.me16x16.cost )
1191         {
1192             a->l0.i_ref = i_ref;
1193             a->l0.me16x16 = m;
1194         }
1195
1196         /* save mv for predicting neighbors */
1197         h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1198         h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1199     }
1200     /* subtract ref cost, so we don't have to add it for the other MB types */
1201     a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
1202
1203     /* ME for list 1 */
1204     i_halfpel_thresh = INT_MAX;
1205     p_halfpel_thresh = h->i_ref1>1 ? &i_halfpel_thresh : NULL;
1206     a->l1.me16x16.cost = INT_MAX;
1207     for( i_ref = 0; i_ref < h->i_ref1; i_ref++ )
1208     {
1209         /* search with ref */
1210         LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 0, 0 );
1211         x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
1212         x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
1213         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
1214
1215         /* add ref cost */
1216         m.cost += REF_COST( 1, i_ref );
1217
1218         if( m.cost < a->l1.me16x16.cost )
1219         {
1220             a->l1.i_ref = i_ref;
1221             a->l1.me16x16 = m;
1222         }
1223
1224         /* save mv for predicting neighbors */
1225         h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
1226         h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
1227     }
1228     /* subtract ref cost, so we don't have to add it for the other MB types */
1229     a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
1230
1231     /* Set global ref, needed for other modes? */
1232     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
1233     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
1234
1235     /* get cost of BI mode */
1236     if ( ((a->l0.me16x16.mv[0] | a->l0.me16x16.mv[1]) & 1) == 0 )
1237     {
1238         /* l0 reference is halfpel, so get_ref on it will make it faster */
1239         src2 = h->mc.get_ref( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1240                         pix2, &stride2,
1241                         a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1242                         16, 16 );
1243         h->mc.mc_luma( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1244                         pix1, 16,
1245                         a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1246                         16, 16 );
1247         src2_ref = a->l0.i_ref;
1248         pix1_ref = a->l1.i_ref;
1249     } 
1250     else
1251     {
1252         /* if l0 was qpel, we'll use get_ref on l1 instead */
1253         h->mc.mc_luma( h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
1254                         pix1, 16,
1255                         a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
1256                         16, 16 );
1257         src2 = h->mc.get_ref( h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
1258                         pix2, &stride2,
1259                         a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
1260                         16, 16 );
1261         src2_ref = a->l1.i_ref;
1262         pix1_ref = a->l0.i_ref;
1263     }
1264
1265     if( h->param.analyse.b_weighted_bipred )
1266         h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2,
1267                 h->mb.bipred_weight[pix1_ref][src2_ref] );
1268     else
1269         h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
1270
1271     a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0], pix1, 16 )
1272                      + REF_COST( 0, a->l0.i_ref )
1273                      + REF_COST( 1, a->l1.i_ref )
1274                      + a->l0.me16x16.cost_mv
1275                      + a->l1.me16x16.cost_mv;
1276
1277     /* mb type cost */
1278     a->i_cost16x16bi   += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
1279     a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
1280     a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
1281 }
1282
1283 static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
1284 {
1285     const int x = 2*(i%2);
1286     const int y = 2*(i/2);
1287
1288     switch( h->mb.i_sub_partition[i] )
1289     {
1290         case D_L0_8x8:
1291             x264_macroblock_cache_mv( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv[0], a->l0.me8x8[i].mv[1] );
1292             break;
1293         case D_L0_8x4:
1294             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] );
1295             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] );
1296             break;
1297         case D_L0_4x8:
1298             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] );
1299             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] );
1300             break;
1301         case D_L0_4x4:
1302             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] );
1303             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] );
1304             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] );
1305             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] );
1306             break;
1307         default:
1308             x264_log( h, X264_LOG_ERROR, "internal error\n" );
1309             break;
1310     }
1311 }
1312
1313 #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
1314     if( x264_mb_partition_listX_table[0][part] ) \
1315     { \
1316         x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
1317         x264_macroblock_cache_mv(  h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
1318     } \
1319     else \
1320     { \
1321         x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
1322         x264_macroblock_cache_mv(  h, x,y,dx,dy, 0, 0, 0 ); \
1323         if( b_mvd ) \
1324             x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
1325     } \
1326     if( x264_mb_partition_listX_table[1][part] ) \
1327     { \
1328         x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
1329         x264_macroblock_cache_mv(  h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
1330     } \
1331     else \
1332     { \
1333         x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
1334         x264_macroblock_cache_mv(  h, x,y,dx,dy, 1, 0, 0 ); \
1335         if( b_mvd ) \
1336             x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
1337     }
1338
1339 static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1340 {
1341     int x = (i%2)*2;
1342     int y = (i/2)*2;
1343     if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
1344     {
1345         x264_mb_load_mv_direct8x8( h, i );
1346         if( b_mvd )
1347         {
1348             x264_macroblock_cache_mvd(  h, x, y, 2, 2, 0, 0, 0 );
1349             x264_macroblock_cache_mvd(  h, x, y, 2, 2, 1, 0, 0 );
1350             x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
1351         }
1352     }
1353     else
1354     {
1355         CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
1356     }
1357 }
1358 static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1359 {
1360     CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
1361 }
1362 static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
1363 {
1364     CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
1365 }
1366 #undef CACHE_MV_BI
1367
1368 static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
1369 {
1370     uint8_t **p_fref[2] =
1371         { h->mb.pic.p_fref[0][a->l0.i_ref],
1372           h->mb.pic.p_fref[1][a->l1.i_ref] };
1373     uint8_t pix[2][8*8];
1374     int i, l;
1375
1376     /* XXX Needed for x264_mb_predict_mv */
1377     h->mb.i_partition = D_8x8;
1378
1379     a->i_cost8x8bi = 0;
1380
1381     for( i = 0; i < 4; i++ )
1382     {
1383         const int x8 = i%2;
1384         const int y8 = i/2;
1385         int i_part_cost;
1386         int i_part_cost_bi = 0;
1387
1388         for( l = 0; l < 2; l++ )
1389         {
1390             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1391             x264_me_t *m = &lX->me8x8[i];
1392
1393             m->i_pixel = PIXEL_8x8;
1394             m->p_cost_mv = a->p_cost_mv;
1395
1396             LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
1397             LOAD_HPELS( m, p_fref[l], 8*x8, 8*y8 );
1398
1399             x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
1400             x264_me_search( h, m, &lX->me16x16.mv, 1 );
1401
1402             x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
1403
1404             /* BI mode */
1405             h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1406                             m->mv[0], m->mv[1], 8, 8 );
1407             i_part_cost_bi += m->cost_mv;
1408             /* FIXME: ref cost */
1409         }
1410
1411         WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
1412         i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], h->mb.pic.i_stride[0], pix[0], 8 )
1413                         + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1414         a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
1415         a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
1416
1417         i_part_cost = a->l0.me8x8[i].cost;
1418         h->mb.i_sub_partition[i] = D_L0_8x8;
1419         if( a->l1.me8x8[i].cost < i_part_cost )
1420         {
1421             i_part_cost = a->l1.me8x8[i].cost;
1422             h->mb.i_sub_partition[i] = D_L1_8x8;
1423         }
1424         if( i_part_cost_bi < i_part_cost )
1425         {
1426             i_part_cost = i_part_cost_bi;
1427             h->mb.i_sub_partition[i] = D_BI_8x8;
1428         }
1429         if( a->i_cost8x8direct[i] < i_part_cost )
1430         {
1431             i_part_cost = a->i_cost8x8direct[i];
1432             h->mb.i_sub_partition[i] = D_DIRECT_8x8;
1433         }
1434         a->i_cost8x8bi += i_part_cost;
1435
1436         /* XXX Needed for x264_mb_predict_mv */
1437         x264_mb_cache_mv_b8x8( h, a, i, 0 );
1438     }
1439
1440     /* mb type cost */
1441     a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
1442 }
1443
1444 static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
1445 {
1446     uint8_t **p_fref[2] =
1447         { h->mb.pic.p_fref[0][a->l0.i_ref],
1448           h->mb.pic.p_fref[1][a->l1.i_ref] };
1449     uint8_t pix[2][16*8];
1450     int mvc[2][2];
1451     int i, l;
1452
1453     h->mb.i_partition = D_16x8;
1454     a->i_cost16x8bi = 0;
1455
1456     for( i = 0; i < 2; i++ )
1457     {
1458         int i_part_cost;
1459         int i_part_cost_bi = 0;
1460
1461         /* TODO: check only the list(s) that were used in b8x8? */
1462         for( l = 0; l < 2; l++ )
1463         {
1464             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1465             x264_me_t *m = &lX->me16x8[i];
1466
1467             m->i_pixel = PIXEL_16x8;
1468             m->p_cost_mv = a->p_cost_mv;
1469
1470             LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
1471             LOAD_HPELS( m, p_fref[l], 0, 8*i );
1472
1473             mvc[0][0] = lX->me8x8[2*i].mv[0];
1474             mvc[0][1] = lX->me8x8[2*i].mv[1];
1475             mvc[1][0] = lX->me8x8[2*i+1].mv[0];
1476             mvc[1][1] = lX->me8x8[2*i+1].mv[1];
1477
1478             x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp );
1479             x264_me_search( h, m, mvc, 2 );
1480
1481             /* BI mode */
1482             h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 16,
1483                             m->mv[0], m->mv[1], 16, 8 );
1484             /* FIXME: ref cost */
1485             i_part_cost_bi += m->cost_mv;
1486         }
1487
1488         WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
1489         i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], h->mb.pic.i_stride[0], pix[0], 16 );
1490
1491         i_part_cost = a->l0.me16x8[i].cost;
1492         a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
1493         if( a->l1.me16x8[i].cost < i_part_cost )
1494         {
1495             i_part_cost = a->l1.me16x8[i].cost;
1496             a->i_mb_partition16x8[i] = D_L1_8x8;
1497         }
1498         if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1499         {
1500             i_part_cost = i_part_cost_bi;
1501             a->i_mb_partition16x8[i] = D_BI_8x8;
1502         }
1503         a->i_cost16x8bi += i_part_cost;
1504
1505         if( i == 0 )
1506             x264_mb_cache_mv_b16x8( h, a, i, 0 );
1507     }
1508
1509     /* mb type cost */
1510     a->i_mb_type16x8 = B_L0_L0
1511         + (a->i_mb_partition16x8[0]>>2) * 3
1512         + (a->i_mb_partition16x8[1]>>2);
1513     a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
1514 }
1515 static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
1516 {
1517     uint8_t **p_fref[2] =
1518         { h->mb.pic.p_fref[0][a->l0.i_ref],
1519           h->mb.pic.p_fref[1][a->l1.i_ref] };
1520     uint8_t pix[2][8*16];
1521     int mvc[2][2];
1522     int i, l;
1523
1524     h->mb.i_partition = D_8x16;
1525     a->i_cost8x16bi = 0;
1526
1527     for( i = 0; i < 2; i++ )
1528     {
1529         int i_part_cost;
1530         int i_part_cost_bi = 0;
1531
1532         for( l = 0; l < 2; l++ )
1533         {
1534             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
1535             x264_me_t *m = &lX->me8x16[i];
1536
1537             m->i_pixel = PIXEL_8x16;
1538             m->p_cost_mv = a->p_cost_mv;
1539
1540             LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
1541             LOAD_HPELS( m, p_fref[l], 8*i, 0 );
1542
1543             mvc[0][0] = lX->me8x8[i].mv[0];
1544             mvc[0][1] = lX->me8x8[i].mv[1];
1545             mvc[1][0] = lX->me8x8[i+2].mv[0];
1546             mvc[1][1] = lX->me8x8[i+2].mv[1];
1547
1548             x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
1549             x264_me_search( h, m, mvc, 2 );
1550
1551             /* BI mode */
1552             h->mc.mc_luma( m->p_fref, m->i_stride[0], pix[l], 8,
1553                             m->mv[0], m->mv[1], 8, 16 );
1554             /* FIXME: ref cost */
1555             i_part_cost_bi += m->cost_mv;
1556         }
1557
1558         WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
1559         i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], h->mb.pic.i_stride[0], pix[0], 8 );
1560
1561         i_part_cost = a->l0.me8x16[i].cost;
1562         a->i_mb_partition8x16[i] = D_L0_8x8;
1563         if( a->l1.me8x16[i].cost < i_part_cost )
1564         {
1565             i_part_cost = a->l1.me8x16[i].cost;
1566             a->i_mb_partition8x16[i] = D_L1_8x8;
1567         }
1568         if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
1569         {
1570             i_part_cost = i_part_cost_bi;
1571             a->i_mb_partition8x16[i] = D_BI_8x8;
1572         }
1573         a->i_cost8x16bi += i_part_cost;
1574
1575         if( i == 0 )
1576             x264_mb_cache_mv_b8x16( h, a, i, 0 );
1577     }
1578
1579     /* mb type cost */
1580     a->i_mb_type8x16 = B_L0_L0
1581         + (a->i_mb_partition8x16[0]>>2) * 3
1582         + (a->i_mb_partition8x16[1]>>2);
1583     a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1584 }
1585
1586 static inline void x264_mb_analyse_transform( x264_t *h )
1587 {
1588     h->mb.cache.b_transform_8x8_allowed =
1589         h->param.analyse.b_transform_8x8
1590         && !IS_INTRA( h->mb.i_type ) && x264_mb_transform_8x8_allowed( h );
1591
1592     if( h->mb.cache.b_transform_8x8_allowed )
1593     {
1594         int i_cost4, i_cost8;
1595         /* FIXME only luma mc is needed */
1596         x264_mb_mc( h );
1597
1598         i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0],
1599                                              h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] );
1600         i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0],
1601                                              h->mb.pic.p_fdec[0], h->mb.pic.i_stride[0] );
1602
1603         h->mb.b_transform_8x8 = i_cost8 < i_cost4;
1604     }
1605 }
1606
1607 static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_cost )
1608 {
1609     h->mb.cache.b_transform_8x8_allowed =
1610         h->param.analyse.b_transform_8x8 && x264_mb_transform_8x8_allowed( h );
1611
1612     if( h->mb.cache.b_transform_8x8_allowed )
1613     {
1614         int i_cost8;
1615         x264_analyse_update_cache( h, a );
1616         h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1617         /* FIXME only luma is needed, but the score for comparison already includes chroma */
1618         i_cost8 = x264_rd_cost_mb( h, a->i_lambda2 );
1619
1620         if( *i_cost >= i_cost8 )
1621         {
1622             if( *i_cost > 0 )
1623                 a->i_best_satd = (int64_t)a->i_best_satd * i_cost8 / *i_cost;
1624             *i_cost = i_cost8;
1625         }
1626         else
1627             h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
1628     }
1629 }
1630
1631
1632 /*****************************************************************************
1633  * x264_macroblock_analyse:
1634  *****************************************************************************/
1635 void x264_macroblock_analyse( x264_t *h )
1636 {
1637     x264_mb_analysis_t analysis;
1638     int i_cost = COST_MAX;
1639     int i;
1640
1641     /* init analysis */
1642     x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
1643
1644     /*--------------------------- Do the analysis ---------------------------*/
1645     if( h->sh.i_type == SLICE_TYPE_I )
1646     {
1647         x264_mb_analyse_intra( h, &analysis, COST_MAX );
1648
1649         i_cost = analysis.i_sad_i16x16;
1650         h->mb.i_type = I_16x16;
1651         if( analysis.i_sad_i4x4 < i_cost )
1652         {
1653             i_cost = analysis.i_sad_i4x4;
1654             h->mb.i_type = I_4x4;
1655         }
1656         if( analysis.i_sad_i8x8 < i_cost )
1657             h->mb.i_type = I_8x8;
1658     }
1659     else if( h->sh.i_type == SLICE_TYPE_P )
1660     {
1661         int b_skip = 0;
1662         int i_intra_cost, i_intra_type;
1663
1664         /* Fast P_SKIP detection */
1665         if( !h->mb.b_lossless &&
1666            (( h->mb.i_mb_type_left == P_SKIP ) ||
1667             ( h->mb.i_mb_type_top == P_SKIP ) ||
1668             ( h->mb.i_mb_type_topleft == P_SKIP ) ||
1669             ( h->mb.i_mb_type_topright == P_SKIP )))
1670         {
1671             b_skip = x264_macroblock_probe_pskip( h );
1672         }
1673
1674         if( b_skip )
1675         {
1676             h->mb.i_type = P_SKIP;
1677             h->mb.i_partition = D_16x16;
1678         }
1679         else
1680         {
1681             const unsigned int flags = h->param.analyse.inter;
1682             int i_type;
1683             int i_partition;
1684             int i_thresh16x8;
1685
1686             x264_mb_analyse_load_costs( h, &analysis );
1687
1688             x264_mb_analyse_inter_p16x16( h, &analysis );
1689             if( flags & X264_ANALYSE_PSUB16x16 )
1690             {
1691                 if( h->param.analyse.b_mixed_references )
1692                     x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
1693                 else
1694                     x264_mb_analyse_inter_p8x8( h, &analysis );
1695             }
1696
1697             /* Select best inter mode */
1698             i_type = P_L0;
1699             i_partition = D_16x16;
1700             i_cost = analysis.l0.me16x16.cost;
1701
1702             if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
1703                 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
1704             {
1705                 int i;
1706
1707                 i_type = P_8x8;
1708                 i_partition = D_8x8;
1709                 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1710                 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1711
1712                 i_cost = analysis.l0.i_cost8x8;
1713
1714                 /* Do sub 8x8 */
1715                 if( flags & X264_ANALYSE_PSUB8x8 )
1716                 {
1717                     int i_cost_bak = i_cost;
1718                     int b_sub8x8 = 0;
1719                     for( i = 0; i < 4; i++ )
1720                     {
1721                         x264_mb_analyse_inter_p4x4( h, &analysis, i );
1722                         if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
1723                         {
1724                             int i_cost8x8 = analysis.l0.i_cost4x4[i];
1725                             h->mb.i_sub_partition[i] = D_L0_4x4;
1726
1727                             x264_mb_analyse_inter_p8x4( h, &analysis, i );
1728                             if( analysis.l0.i_cost8x4[i] < i_cost8x8 )
1729                             {
1730                                 h->mb.i_sub_partition[i] = D_L0_8x4;
1731                                 i_cost8x8 = analysis.l0.i_cost8x4[i];
1732                             }
1733
1734                             x264_mb_analyse_inter_p4x8( h, &analysis, i );
1735                             if( analysis.l0.i_cost4x8[i] < i_cost8x8 )
1736                             {
1737                                 h->mb.i_sub_partition[i] = D_L0_4x8;
1738                                 i_cost8x8 = analysis.l0.i_cost4x8[i];
1739                             }
1740
1741                             i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
1742                             b_sub8x8 = 1;
1743                         }
1744                         x264_mb_cache_mv_p8x8( h, &analysis, i );
1745                     }
1746                     /* TODO: RD per subpartition */
1747                     if( b_sub8x8 && analysis.b_mbrd )
1748                     {
1749                         i_cost = x264_rd_cost_mb( h, analysis.i_lambda2 );
1750                         if( i_cost > i_cost_bak )
1751                         {
1752                             i_cost = i_cost_bak;
1753                             h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
1754                             h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
1755                         }
1756                     }
1757                 }
1758             }
1759
1760             /* Now do 16x8/8x16 */
1761             i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
1762             if( analysis.b_mbrd )
1763                 i_thresh16x8 = i_thresh16x8 * analysis.i_lambda2 / analysis.i_lambda;
1764             if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
1765                 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
1766             {
1767                 x264_mb_analyse_inter_p16x8( h, &analysis );
1768                 if( analysis.l0.i_cost16x8 < i_cost )
1769                 {
1770                     i_type = P_L0;
1771                     i_partition = D_16x8;
1772                     i_cost = analysis.l0.i_cost16x8;
1773                 }
1774
1775                 x264_mb_analyse_inter_p8x16( h, &analysis );
1776                 if( analysis.l0.i_cost8x16 < i_cost )
1777                 {
1778                     i_type = P_L0;
1779                     i_partition = D_8x16;
1780                     i_cost = analysis.l0.i_cost8x16;
1781                 }
1782             }
1783
1784             h->mb.i_partition = i_partition;
1785
1786             /* refine qpel */
1787             //FIXME mb_type costs?
1788             if( analysis.b_mbrd )
1789             {
1790                 h->mb.i_type = i_type;
1791                 x264_mb_analyse_transform_rd( h, &analysis, &i_cost );
1792             }
1793             else if( i_partition == D_16x16 )
1794             {
1795                 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1796                 i_cost = analysis.l0.me16x16.cost;
1797             }
1798             else if( i_partition == D_16x8 )
1799             {
1800                 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
1801                 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
1802                 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
1803             }
1804             else if( i_partition == D_8x16 )
1805             {
1806                 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
1807                 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
1808                 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
1809             }
1810             else if( i_partition == D_8x8 )
1811             {
1812                 int i8x8;
1813                 i_cost = 0;
1814                 for( i8x8 = 0; i8x8 < 4; i8x8++ )
1815                 {
1816                     switch( h->mb.i_sub_partition[i8x8] )
1817                     {
1818                         case D_L0_8x8:
1819                             x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
1820                             i_cost += analysis.l0.me8x8[i8x8].cost;
1821                             break;
1822                         case D_L0_8x4:
1823                             x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
1824                             x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
1825                             i_cost += analysis.l0.me8x4[i8x8][0].cost +
1826                                       analysis.l0.me8x4[i8x8][1].cost;
1827                             break;
1828                         case D_L0_4x8:
1829                             x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
1830                             x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
1831                             i_cost += analysis.l0.me4x8[i8x8][0].cost +
1832                                       analysis.l0.me4x8[i8x8][1].cost;
1833                             break;
1834
1835                         case D_L0_4x4:
1836                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
1837                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
1838                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
1839                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
1840                             i_cost += analysis.l0.me4x4[i8x8][0].cost +
1841                                       analysis.l0.me4x4[i8x8][1].cost +
1842                                       analysis.l0.me4x4[i8x8][2].cost +
1843                                       analysis.l0.me4x4[i8x8][3].cost;
1844                             break;
1845                         default:
1846                             x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
1847                             break;
1848                     }
1849                 }
1850             }
1851
1852             x264_mb_analyse_intra( h, &analysis, i_cost );
1853             if( h->mb.b_chroma_me && !analysis.b_mbrd &&
1854                 ( analysis.i_sad_i16x16 < i_cost
1855                || analysis.i_sad_i8x8 < i_cost
1856                || analysis.i_sad_i4x4 < i_cost ))
1857             {
1858                 x264_mb_analyse_intra_chroma( h, &analysis );
1859                 analysis.i_sad_i16x16 += analysis.i_sad_i8x8chroma;
1860                 analysis.i_sad_i8x8 += analysis.i_sad_i8x8chroma;
1861                 analysis.i_sad_i4x4 += analysis.i_sad_i8x8chroma;
1862             }
1863
1864             i_intra_type = I_16x16;
1865             i_intra_cost = analysis.i_sad_i16x16;
1866
1867             if( analysis.i_sad_i8x8 < i_intra_cost )
1868             {
1869                 i_intra_type = I_8x8;
1870                 i_intra_cost = analysis.i_sad_i8x8;
1871             }
1872             if( analysis.i_sad_i4x4 < i_intra_cost )
1873             {
1874                 i_intra_type = I_4x4;
1875                 i_intra_cost = analysis.i_sad_i4x4;
1876             }
1877
1878             if( i_intra_cost < i_cost )
1879             {
1880                 i_type = i_intra_type;
1881                 i_cost = i_intra_cost;
1882             }
1883
1884             h->mb.i_type = i_type;
1885             h->stat.frame.i_intra_cost += i_intra_cost;
1886             h->stat.frame.i_inter_cost += i_cost;
1887         }
1888     }
1889     else if( h->sh.i_type == SLICE_TYPE_B )
1890     {
1891         int b_skip = 0;
1892
1893         analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h );
1894         if( analysis.b_direct_available )
1895         {
1896             h->mb.i_type = B_SKIP;
1897             x264_mb_mc( h );
1898
1899             /* Conditioning the probe on neighboring block types
1900              * doesn't seem to help speed or quality. */
1901             b_skip = !h->mb.b_lossless && x264_macroblock_probe_bskip( h );
1902         }
1903
1904         if( !b_skip )
1905         {
1906             const unsigned int flags = h->param.analyse.inter;
1907             int i_partition;
1908
1909             x264_mb_analyse_load_costs( h, &analysis );
1910
1911             /* select best inter mode */
1912             /* direct must be first */
1913             if( analysis.b_direct_available )
1914                 x264_mb_analyse_inter_direct( h, &analysis );
1915
1916             x264_mb_analyse_inter_b16x16( h, &analysis );
1917
1918             h->mb.i_type = B_L0_L0;
1919             i_partition = D_16x16;
1920             i_cost = analysis.l0.me16x16.cost;
1921             if( analysis.l1.me16x16.cost < i_cost )
1922             {
1923                 h->mb.i_type = B_L1_L1;
1924                 i_cost = analysis.l1.me16x16.cost;
1925             }
1926             if( analysis.i_cost16x16bi < i_cost )
1927             {
1928                 h->mb.i_type = B_BI_BI;
1929                 i_cost = analysis.i_cost16x16bi;
1930             }
1931             if( analysis.i_cost16x16direct < i_cost )
1932             {
1933                 h->mb.i_type = B_DIRECT;
1934                 i_cost = analysis.i_cost16x16direct;
1935             }
1936             
1937             if( flags & X264_ANALYSE_BSUB16x16 )
1938             {
1939                 x264_mb_analyse_inter_b8x8( h, &analysis );
1940                 if( analysis.i_cost8x8bi < i_cost )
1941                 {
1942                     h->mb.i_type = B_8x8;
1943                     i_partition = D_8x8;
1944                     i_cost = analysis.i_cost8x8bi;
1945
1946                     if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
1947                         h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
1948                     {
1949                         x264_mb_analyse_inter_b16x8( h, &analysis );
1950                         if( analysis.i_cost16x8bi < i_cost )
1951                         {
1952                             i_partition = D_16x8;
1953                             i_cost = analysis.i_cost16x8bi;
1954                             h->mb.i_type = analysis.i_mb_type16x8;
1955                         }
1956                     }
1957                     if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
1958                         h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
1959                     {
1960                         x264_mb_analyse_inter_b8x16( h, &analysis );
1961                         if( analysis.i_cost8x16bi < i_cost )
1962                         {
1963                             i_partition = D_8x16;
1964                             i_cost = analysis.i_cost8x16bi;
1965                             h->mb.i_type = analysis.i_mb_type8x16;
1966                         }
1967                     }
1968                 }
1969             }
1970
1971             h->mb.i_partition = i_partition;
1972
1973             /* refine qpel */
1974             if( i_partition == D_16x16 )
1975             {
1976                 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
1977                 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
1978                 if( h->mb.i_type == B_L0_L0 )
1979                 {
1980                     x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1981                     i_cost = analysis.l0.me16x16.cost
1982                            + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
1983                 }
1984                 else if( h->mb.i_type == B_L1_L1 )
1985                 {
1986                     x264_me_refine_qpel( h, &analysis.l1.me16x16 );
1987                     i_cost = analysis.l1.me16x16.cost
1988                            + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
1989                 }
1990                 else if( h->mb.i_type == B_BI_BI )
1991                 {
1992                     x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1993                     x264_me_refine_qpel( h, &analysis.l1.me16x16 );
1994                 }
1995             }
1996             else if( i_partition == D_16x8 )
1997             {
1998                 for( i=0; i<2; i++ )
1999                 {
2000                     if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
2001                         x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
2002                     if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
2003                         x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
2004                 }
2005             }
2006             else if( i_partition == D_8x16 )
2007             {
2008                 for( i=0; i<2; i++ )
2009                 {
2010                     if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
2011                         x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
2012                     if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
2013                         x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
2014                 }
2015             }
2016             else if( i_partition == D_8x8 )
2017             {
2018                 for( i=0; i<4; i++ )
2019                 {
2020                     x264_me_t *m;
2021                     int i_part_cost_old;
2022                     int i_type_cost;
2023                     int i_part_type = h->mb.i_sub_partition[i];
2024                     int b_bidir = (i_part_type == D_BI_8x8);
2025
2026                     if( i_part_type == D_DIRECT_8x8 )
2027                         continue;
2028                     if( x264_mb_partition_listX_table[0][i_part_type] )
2029                     {
2030                         m = &analysis.l0.me8x8[i];
2031                         i_part_cost_old = m->cost;
2032                         i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
2033                         m->cost -= i_type_cost;
2034                         x264_me_refine_qpel( h, m );
2035                         if( !b_bidir )
2036                             analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2037                     }
2038                     if( x264_mb_partition_listX_table[1][i_part_type] )
2039                     {
2040                         m = &analysis.l1.me8x8[i];
2041                         i_part_cost_old = m->cost;
2042                         i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
2043                         m->cost -= i_type_cost;
2044                         x264_me_refine_qpel( h, m );
2045                         if( !b_bidir )
2046                             analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
2047                     }
2048                     /* TODO: update mvp? */
2049                 }
2050             }
2051
2052             /* best intra mode */
2053             x264_mb_analyse_intra( h, &analysis, i_cost );
2054
2055             if( analysis.i_sad_i16x16 < i_cost )
2056             {
2057                 h->mb.i_type = I_16x16;
2058                 i_cost = analysis.i_sad_i16x16;
2059             }
2060             if( analysis.i_sad_i8x8 < i_cost )
2061             {
2062                 h->mb.i_type = I_8x8;
2063                 i_cost = analysis.i_sad_i8x8;
2064             }
2065             if( analysis.i_sad_i4x4 < i_cost )
2066             {
2067                 h->mb.i_type = I_4x4;
2068                 i_cost = analysis.i_sad_i4x4;
2069             }
2070         }
2071     }
2072
2073     x264_analyse_update_cache( h, &analysis );
2074
2075     if( !analysis.b_mbrd )
2076         x264_mb_analyse_transform( h );
2077
2078     h->mb.b_trellis = h->param.analyse.i_trellis;
2079 }
2080
2081 /*-------------------- Update MB from the analysis ----------------------*/
2082 static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a  )
2083 {
2084     int i;
2085
2086     switch( h->mb.i_type )
2087     {
2088         case I_4x4:
2089             for( i = 0; i < 16; i++ )
2090             {
2091                 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] =
2092                     a->i_predict4x4[block_idx_x[i]][block_idx_y[i]];
2093             }
2094
2095             x264_mb_analyse_intra_chroma( h, a );
2096             break;
2097         case I_8x8:
2098             for( i = 0; i < 4; i++ )
2099                 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1),
2100                     a->i_predict8x8[i&1][i>>1] );
2101
2102             x264_mb_analyse_intra_chroma( h, a );
2103             break;
2104         case I_16x16:
2105             h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
2106             x264_mb_analyse_intra_chroma( h, a );
2107             break;
2108
2109         case P_L0:
2110             switch( h->mb.i_partition )
2111             {
2112                 case D_16x16:
2113                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
2114                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2115                     break;
2116
2117                 case D_16x8:
2118                     x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
2119                     x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
2120                     x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv[0], a->l0.me16x8[0].mv[1] );
2121                     x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv[0], a->l0.me16x8[1].mv[1] );
2122                     break;
2123
2124                 case D_8x16:
2125                     x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
2126                     x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
2127                     x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv[0], a->l0.me8x16[0].mv[1] );
2128                     x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv[0], a->l0.me8x16[1].mv[1] );
2129                     break;
2130
2131                 default:
2132                     x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
2133                     break;
2134             }
2135             break;
2136
2137         case P_8x8:
2138             x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
2139             x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
2140             x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
2141             x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
2142             for( i = 0; i < 4; i++ )
2143                 x264_mb_cache_mv_p8x8( h, a, i );
2144             break;
2145
2146         case P_SKIP:
2147         {
2148             int mvp[2];
2149             x264_mb_predict_mv_pskip( h, mvp );
2150             /* */
2151             h->mb.i_partition = D_16x16;
2152             x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
2153             x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, mvp[0], mvp[1] );
2154             break;
2155         }
2156
2157         case B_SKIP:
2158             /* nothing has changed since x264_macroblock_probe_bskip */
2159             break;
2160         case B_DIRECT:
2161             x264_mb_load_mv_direct8x8( h, 0 );
2162             x264_mb_load_mv_direct8x8( h, 1 );
2163             x264_mb_load_mv_direct8x8( h, 2 );
2164             x264_mb_load_mv_direct8x8( h, 3 );
2165             break;
2166
2167         case B_8x8:
2168             /* optimize: cache might not need to be rewritten */
2169             for( i = 0; i < 4; i++ )
2170                 x264_mb_cache_mv_b8x8( h, a, i, 1 );
2171             break;
2172
2173         default: /* the rest of the B types */
2174             switch( h->mb.i_partition )
2175             {
2176             case D_16x16:
2177                 switch( h->mb.i_type )
2178                 {
2179                 case B_L0_L0:
2180                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2181                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2182
2183                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
2184                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1,  0, 0 );
2185                     x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1,  0, 0 );
2186                     break;
2187                 case B_L1_L1:
2188                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
2189                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0,  0, 0 );
2190                     x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0,  0, 0 );
2191
2192                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2193                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2194                     break;
2195                 case B_BI_BI:
2196                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
2197                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv[0], a->l0.me16x16.mv[1] );
2198
2199                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
2200                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv[0], a->l1.me16x16.mv[1] );
2201                     break;
2202                 }
2203                 break;
2204             case D_16x8:
2205                 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
2206                 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
2207                 break;
2208             case D_8x16:
2209                 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
2210                 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
2211                 break;
2212             default:
2213                 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)\n" );
2214                 break;
2215             }
2216     }
2217 }
2218
2219 #include "slicetype_decision.c"
2220