analyse.c 55.8 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*****************************************************************************
 * analyse.c: h264 encoder library
 *****************************************************************************
 * Copyright (C) 2003 Laurent Aimar
 * $Id: analyse.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
28
#include <limits.h>
Laurent Aimar's avatar
Laurent Aimar committed
29

30
31
#include "common/common.h"
#include "common/macroblock.h"
Laurent Aimar's avatar
Laurent Aimar committed
32
33
#include "macroblock.h"
#include "me.h"
34
#include "ratecontrol.h"
Laurent Aimar's avatar
Laurent Aimar committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

typedef struct
{
    /* 16x16 */
    int i_ref;
    x264_me_t me16x16;

    /* 8x8 */
    int       i_cost8x8;
    x264_me_t me8x8[4];

    /* Sub 4x4 */
    int       i_cost4x4[4]; /* cost per 8x8 partition */
    x264_me_t me4x4[4][4];

    /* Sub 8x4 */
    int       i_cost8x4[4]; /* cost per 8x8 partition */
    x264_me_t me8x4[4][2];

    /* Sub 4x8 */
    int       i_cost4x8[4]; /* cost per 8x8 partition */
    x264_me_t me4x8[4][4];

    /* 16x8 */
    int       i_cost16x8;
    x264_me_t me16x8[2];

    /* 8x16 */
    int       i_cost8x16;
    x264_me_t me8x16[2];

} x264_mb_analysis_list_t;

typedef struct
{
    /* conduct the analysis using this lamda and QP */
    int i_lambda;
    int i_qp;


    /* I: Intra part */
    /* Luma part 16x16 and 4x4 modes stats */
    int i_sad_i16x16;
    int i_predict16x16;

    int i_sad_i4x4;
    int i_predict4x4[4][4];

    /* Chroma part */
    int i_sad_i8x8;
    int i_predict8x8;

    /* II: Inter part P/B frame */
    int i_mv_range;

    x264_mb_analysis_list_t l0;
    x264_mb_analysis_list_t l1;

    int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
94
95
96
    int i_cost16x16direct;
    int i_cost8x8bi;
    int i_cost8x8direct[4];
97
98
99
100
101
102
103
    int i_cost16x8bi;
    int i_cost8x16bi;

    int i_mb_partition16x8[2]; /* mb_partition_e */
    int i_mb_partition8x16[2];
    int i_mb_type16x8; /* mb_class_e */
    int i_mb_type8x16;
104
105

    int b_direct_available;
Laurent Aimar's avatar
Laurent Aimar committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

} x264_mb_analysis_t;

static const int i_qp0_cost_table[52] = {
   1, 1, 1, 1, 1, 1, 1, 1,  /*  0-7 */
   1, 1, 1, 1,              /*  8-11 */
   1, 1, 1, 1, 2, 2, 2, 2,  /* 12-19 */
   3, 3, 3, 4, 4, 4, 5, 6,  /* 20-27 */
   6, 7, 8, 9,10,11,13,14,  /* 28-35 */
  16,18,20,23,25,29,32,36,  /* 36-43 */
  40,45,51,57,64,72,81,91   /* 44-51 */
};

static const uint8_t block_idx_x[16] = {
    0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
};
static const uint8_t block_idx_y[16] = {
    0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
};

126
127
128
129
130
131
132
133
134
135
/* TODO: calculate CABAC costs */
static const int i_mb_b_cost_table[18] = {
    9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
};
static const int i_mb_b16x8_cost_table[16] = {
    0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
};
static const int i_sub_mb_b_cost_table[13] = {
    7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
};
136
137
138
static const int i_sub_mb_p_cost_table[4] = {
    5, 3, 3, 1
};
139

Laurent Aimar's avatar
Laurent Aimar committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
{
    memset( a, 0, sizeof( x264_mb_analysis_t ) );

    /* conduct the analysis using this lamda and QP */
    a->i_qp = i_qp;
    a->i_lambda = i_qp0_cost_table[i_qp];

    /* I: Intra part */
    a->i_sad_i16x16 = -1;
    a->i_sad_i4x4   = -1;
    a->i_sad_i8x8   = -1;

    /* II: Inter part P/B frame */
    if( h->sh.i_type != SLICE_TYPE_I )
    {
        int dmb;
        int i;

        /* Calculate max start MV range */
        dmb = h->mb.i_mb_x;
        if( h->mb.i_mb_y < dmb )
            dmb = h->mb.i_mb_y;
        if( h->sps->i_mb_width - h->mb.i_mb_x < dmb )
            dmb = h->sps->i_mb_width - h->mb.i_mb_x;
        if( h->sps->i_mb_height - h->mb.i_mb_y < dmb )
            dmb = h->sps->i_mb_height - h->mb.i_mb_y;

        a->i_mv_range = 16*dmb + 8;

        a->l0.me16x16.cost = -1;
        a->l0.i_cost8x8    = -1;

        for( i = 0; i < 4; i++ )
        {
            a->l0.i_cost4x4[i] = -1;
            a->l0.i_cost8x4[i] = -1;
            a->l0.i_cost4x8[i] = -1;
        }

        a->l0.i_cost16x8   = -1;
        a->l0.i_cost8x16   = -1;
        if( h->sh.i_type == SLICE_TYPE_B )
        {
            a->l1.me16x16.cost = -1;
            a->l1.i_cost8x8    = -1;

            for( i = 0; i < 4; i++ )
            {
                a->l1.i_cost4x4[i] = -1;
                a->l1.i_cost8x4[i] = -1;
                a->l1.i_cost4x8[i] = -1;
192
                a->i_cost8x8direct[i] = -1;
Laurent Aimar's avatar
Laurent Aimar committed
193
194
195
196
197
198
            }

            a->l1.i_cost16x8   = -1;
            a->l1.i_cost8x16   = -1;

            a->i_cost16x16bi   = -1;
199
200
            a->i_cost16x16direct = -1;
            a->i_cost8x8bi     = -1;
201
202
            a->i_cost16x8bi    = -1;
            a->i_cost8x16bi    = -1;
Laurent Aimar's avatar
Laurent Aimar committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
        }
    }
}



/*
 * Handle intra mb
 */
/* Max = 4 */
static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
{
    if( ( i_neighbour & (MB_LEFT|MB_TOP) ) == (MB_LEFT|MB_TOP) )
    {
        /* top and left avaible */
        *mode++ = I_PRED_16x16_V;
        *mode++ = I_PRED_16x16_H;
        *mode++ = I_PRED_16x16_DC;
        *mode++ = I_PRED_16x16_P;
        *pi_count = 4;
    }
    else if( ( i_neighbour & MB_LEFT ) )
    {
        /* left available*/
        *mode++ = I_PRED_16x16_DC_LEFT;
        *mode++ = I_PRED_16x16_H;
        *pi_count = 2;
    }
    else if( ( i_neighbour & MB_TOP ) )
    {
        /* top available*/
        *mode++ = I_PRED_16x16_DC_TOP;
        *mode++ = I_PRED_16x16_V;
        *pi_count = 2;
    }
    else
    {
        /* none avaible */
        *mode = I_PRED_16x16_DC_128;
        *pi_count = 1;
    }
}

/* Max = 4 */
static void predict_8x8_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
{
    if( ( i_neighbour & (MB_LEFT|MB_TOP) ) == (MB_LEFT|MB_TOP) )
    {
        /* top and left avaible */
        *mode++ = I_PRED_CHROMA_V;
        *mode++ = I_PRED_CHROMA_H;
        *mode++ = I_PRED_CHROMA_DC;
        *mode++ = I_PRED_CHROMA_P;
        *pi_count = 4;
    }
    else if( ( i_neighbour & MB_LEFT ) )
    {
        /* left available*/
        *mode++ = I_PRED_CHROMA_DC_LEFT;
        *mode++ = I_PRED_CHROMA_H;
        *pi_count = 2;
    }
    else if( ( i_neighbour & MB_TOP ) )
    {
        /* top available*/
        *mode++ = I_PRED_CHROMA_DC_TOP;
        *mode++ = I_PRED_CHROMA_V;
        *pi_count = 2;
    }
    else
    {
        /* none avaible */
        *mode = I_PRED_CHROMA_DC_128;
        *pi_count = 1;
    }
}

/* MAX = 8 */
static void predict_4x4_mode_available( unsigned int i_neighbour, int idx, int *mode, int *pi_count )
{
    int b_a, b_b, b_c;
    static const unsigned int needmb[16] =
    {
        MB_LEFT|MB_TOP, MB_TOP,
        MB_LEFT,        MB_PRIVATE,
        MB_TOP,         MB_TOP|MB_TOPRIGHT,
        0,              MB_PRIVATE,
        MB_LEFT,        0,
        MB_LEFT,        MB_PRIVATE,
        0,              MB_PRIVATE,
        0,              MB_PRIVATE
    };

    /* FIXME even when b_c == 0 there is some case where missing pixels
     * are emulated and thus more mode are available TODO
     * analysis and encode should be fixed too */
    b_a = (needmb[idx]&i_neighbour&MB_LEFT) == (needmb[idx]&MB_LEFT);
    b_b = (needmb[idx]&i_neighbour&MB_TOP) == (needmb[idx]&MB_TOP);
    b_c = (needmb[idx]&i_neighbour&(MB_TOPRIGHT|MB_PRIVATE)) == (needmb[idx]&(MB_TOPRIGHT|MB_PRIVATE));

    if( b_a && b_b )
    {
        *mode++ = I_PRED_4x4_DC;
        *mode++ = I_PRED_4x4_H;
        *mode++ = I_PRED_4x4_V;
        *mode++ = I_PRED_4x4_DDR;
        *mode++ = I_PRED_4x4_VR;
        *mode++ = I_PRED_4x4_HD;
        *mode++ = I_PRED_4x4_HU;

        *pi_count = 7;

        if( b_c )
        {
            *mode++ = I_PRED_4x4_DDL;
            *mode++ = I_PRED_4x4_VL;
            (*pi_count) += 2;
        }
    }
    else if( b_a && !b_b )
    {
        *mode++ = I_PRED_4x4_DC_LEFT;
        *mode++ = I_PRED_4x4_H;
326
327
        *mode++ = I_PRED_4x4_HU;
        *pi_count = 3;
Laurent Aimar's avatar
Laurent Aimar committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
    }
    else if( !b_a && b_b )
    {
        *mode++ = I_PRED_4x4_DC_TOP;
        *mode++ = I_PRED_4x4_V;
        *pi_count = 2;
    }
    else
    {
        *mode++ = I_PRED_4x4_DC_128;
        *pi_count = 1;
    }
}

static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *res )
{
    const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
    const int i_stride = h->mb.pic.i_stride[0];
    uint8_t  *p_src = h->mb.pic.p_fenc[0];
    uint8_t  *p_dst = h->mb.pic.p_fdec[0];

    int i, idx;

    int i_max;
    int predict_mode[9];

    /*---------------- Try all mode and calculate their score ---------------*/

    /* 16x16 prediction selection */
    predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
    for( i = 0; i < i_max; i++ )
    {
        int i_sad;
        int i_mode;

        i_mode = predict_mode[i];

        /* we do the prediction */
        h->predict_16x16[i_mode]( p_dst, i_stride );

        /* we calculate the diff and get the square sum of the diff */
        i_sad = h->pixf.satd[PIXEL_16x16]( p_dst, i_stride, p_src, i_stride ) +
                res->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
        /* if i_score is lower it is better */
        if( res->i_sad_i16x16 == -1 || res->i_sad_i16x16 > i_sad )
        {
            res->i_predict16x16 = i_mode;
            res->i_sad_i16x16     = i_sad;
        }
    }
378
379
380
    /* cavlc mb type prefix */
    if( h->sh.i_type == SLICE_TYPE_B )
        res->i_sad_i16x16 += res->i_lambda * i_mb_b_cost_table[I_16x16];
Laurent Aimar's avatar
Laurent Aimar committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436

    /* 4x4 prediction selection */
    if( flags & X264_ANALYSE_I4x4 )
    {
        res->i_sad_i4x4 = 0;
        for( idx = 0; idx < 16; idx++ )
        {
            uint8_t *p_src_by;
            uint8_t *p_dst_by;
            int     i_best;
            int x, y;
            int i_pred_mode;

            i_pred_mode= x264_mb_predict_intra4x4_mode( h, idx );
            x = block_idx_x[idx];
            y = block_idx_y[idx];

            p_src_by = p_src + 4 * x + 4 * y * i_stride;
            p_dst_by = p_dst + 4 * x + 4 * y * i_stride;

            i_best = -1;
            predict_4x4_mode_available( h->mb.i_neighbour, idx, predict_mode, &i_max );
            for( i = 0; i < i_max; i++ )
            {
                int i_sad;
                int i_mode;

                i_mode = predict_mode[i];

                /* we do the prediction */
                h->predict_4x4[i_mode]( p_dst_by, i_stride );

                /* we calculate diff and get the square sum of the diff */
                i_sad = h->pixf.satd[PIXEL_4x4]( p_dst_by, i_stride,
                                                 p_src_by, i_stride );

                i_sad += res->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix[i_mode] ? 1 : 4);

                /* if i_score is lower it is better */
                if( i_best == -1 || i_best > i_sad )
                {
                    res->i_predict4x4[x][y] = i_mode;
                    i_best = i_sad;
                }
            }
            res->i_sad_i4x4 += i_best;

            /* we need to encode this mb now (for next ones) */
            h->predict_4x4[res->i_predict4x4[x][y]]( p_dst_by, i_stride );
            x264_mb_encode_i4x4( h, idx, res->i_qp );

            /* we need to store the 'fixed' version */
            h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] =
                x264_mb_pred_mode4x4_fix[res->i_predict4x4[x][y]];
        }
        res->i_sad_i4x4 += res->i_lambda * 24;    /* from JVT (SATD0) */
437
438
        if( h->sh.i_type == SLICE_TYPE_B )
            res->i_sad_i4x4 += res->i_lambda * i_mb_b_cost_table[I_4x4];
Laurent Aimar's avatar
Laurent Aimar committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
    }
}

static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *res )
{
    int i;

    int i_max;
    int predict_mode[9];

    uint8_t *p_dstc[2], *p_srcc[2];
    int      i_stride[2];

    /* 8x8 prediction selection for chroma */
    p_dstc[0] = h->mb.pic.p_fdec[1];
    p_dstc[1] = h->mb.pic.p_fdec[2];
    p_srcc[0] = h->mb.pic.p_fenc[1];
    p_srcc[1] = h->mb.pic.p_fenc[2];

    i_stride[0] = h->mb.pic.i_stride[1];
    i_stride[1] = h->mb.pic.i_stride[2];

    predict_8x8_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
    res->i_sad_i8x8 = -1;
    for( i = 0; i < i_max; i++ )
    {
        int i_sad;
        int i_mode;

        i_mode = predict_mode[i];

        /* we do the prediction */
        h->predict_8x8[i_mode]( p_dstc[0], i_stride[0] );
        h->predict_8x8[i_mode]( p_dstc[1], i_stride[1] );

        /* we calculate the cost */
        i_sad = h->pixf.satd[PIXEL_8x8]( p_dstc[0], i_stride[0],
                                         p_srcc[0], i_stride[0] ) +
                h->pixf.satd[PIXEL_8x8]( p_dstc[1], i_stride[1],
                                         p_srcc[1], i_stride[1] ) +
                res->i_lambda * bs_size_ue( x264_mb_pred_mode8x8_fix[i_mode] );

        /* if i_score is lower it is better */
        if( res->i_sad_i8x8 == -1 || res->i_sad_i8x8 > i_sad )
        {
            res->i_predict8x8 = i_mode;
            res->i_sad_i8x8     = i_sad;
        }
    }
}

static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
{
    x264_me_t m;
    int i_ref;
494
    int mvc[4][2], i_mvc;
495
496
    int i_fullpel_thresh = INT_MAX;
    int *p_fullpel_thresh = h->i_ref0>1 ? &i_fullpel_thresh : NULL;
Laurent Aimar's avatar
Laurent Aimar committed
497
498
499
500
501
502
503
504

    /* 16x16 Search on all ref frame */
    m.i_pixel = PIXEL_16x16;
    m.lm      = a->i_lambda;
    m.p_fenc  = h->mb.pic.p_fenc[0];
    m.i_stride= h->mb.pic.i_stride[0];
    m.i_mv_range = a->i_mv_range;

505
506
    a->l0.me16x16.cost = INT_MAX;
    for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
Laurent Aimar's avatar
Laurent Aimar committed
507
    {
508
509
510
        const int i_ref_cost = m.lm * bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, i_ref );
        i_fullpel_thresh -= i_ref_cost;

Laurent Aimar's avatar
Laurent Aimar committed
511
512
513
        /* search with ref */
        m.p_fref = h->mb.pic.p_fref[0][i_ref][0];
        x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
514
        x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
515
        x264_me_search_ref( h, &m, mvc, i_mvc, p_fullpel_thresh );
Laurent Aimar's avatar
Laurent Aimar committed
516

517
518
        m.cost += i_ref_cost;
        i_fullpel_thresh += i_ref_cost;
Laurent Aimar's avatar
Laurent Aimar committed
519
520
521
522
523
524

        if( m.cost < a->l0.me16x16.cost )
        {
            a->l0.i_ref = i_ref;
            a->l0.me16x16 = m;
        }
525
526
527
528

        /* save mv for predicting neighbors */
        h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
        h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
Laurent Aimar's avatar
Laurent Aimar committed
529
530
    }

531
532
533
    /* subtract ref cost, so we don't have to add it for the other P types */
    a->l0.me16x16.cost -= m.lm * bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, a->l0.i_ref );

Laurent Aimar's avatar
Laurent Aimar committed
534
535
536
537
538
539
540
541
    /* Set global ref, needed for all others modes */
    x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
}

static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
{
    uint8_t  *p_fref = h->mb.pic.p_fref[0][a->l0.i_ref][0];
    uint8_t  *p_fenc = h->mb.pic.p_fenc[0];
542
    int mvc[5][2], i_mvc;
Laurent Aimar's avatar
Laurent Aimar committed
543
544
545
546
547
    int i;

    /* XXX Needed for x264_mb_predict_mv */
    h->mb.i_partition = D_8x8;

548
549
550
551
    i_mvc = 1;
    mvc[0][0] = a->l0.me16x16.mv[0];
    mvc[0][1] = a->l0.me16x16.mv[1];

Laurent Aimar's avatar
Laurent Aimar committed
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
    for( i = 0; i < 4; i++ )
    {
        x264_me_t *m = &a->l0.me8x8[i];
        const int x8 = i%2;
        const int y8 = i/2;

        m->i_pixel = PIXEL_8x8;
        m->lm      = a->i_lambda;

        m->p_fenc = &p_fenc[8*(y8*h->mb.pic.i_stride[0]+x8)];
        m->p_fref = &p_fref[8*(y8*h->mb.pic.i_stride[0]+x8)];
        m->i_stride= h->mb.pic.i_stride[0];
        m->i_mv_range = a->i_mv_range;

        x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
567
        x264_me_search( h, m, mvc, i_mvc );
Laurent Aimar's avatar
Laurent Aimar committed
568
569

        x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
570
571
572
573

        mvc[i_mvc][0] = m->mv[0];
        mvc[i_mvc][1] = m->mv[1];
        i_mvc++;
574
575
576

        /* mb type cost */
        m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
Laurent Aimar's avatar
Laurent Aimar committed
577
578
579
580
581
582
583
584
585
586
    }

    a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
                   a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
}

static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
{
    uint8_t  *p_fref = h->mb.pic.p_fref[0][a->l0.i_ref][0];
    uint8_t  *p_fenc = h->mb.pic.p_fenc[0];
587
    int mvc[2][2];
Laurent Aimar's avatar
Laurent Aimar committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
    int i;

    /* XXX Needed for x264_mb_predict_mv */
    h->mb.i_partition = D_16x8;

    for( i = 0; i < 2; i++ )
    {
        x264_me_t *m = &a->l0.me16x8[i];

        m->i_pixel = PIXEL_16x8;
        m->lm      = a->i_lambda;

        m->p_fenc = &p_fenc[8*i*h->mb.pic.i_stride[0]];
        m->p_fref = &p_fref[8*i*h->mb.pic.i_stride[0]];
        m->i_stride= h->mb.pic.i_stride[0];
        m->i_mv_range = a->i_mv_range;

605
606
607
608
        mvc[0][0] = a->l0.me8x8[2*i].mv[0];
        mvc[0][1] = a->l0.me8x8[2*i].mv[1];
        mvc[1][0] = a->l0.me8x8[2*i+1].mv[0];
        mvc[1][1] = a->l0.me8x8[2*i+1].mv[1];
Laurent Aimar's avatar
Laurent Aimar committed
609
610

        x264_mb_predict_mv( h, 0, 8*i, 4, m->mvp );
611
        x264_me_search( h, m, mvc, 2 );
Laurent Aimar's avatar
Laurent Aimar committed
612
613
614
615
616
617
618
619
620
621
622

        x264_macroblock_cache_mv( h, 0, 2*i, 4, 2, 0, m->mv[0], m->mv[1] );
    }

    a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
}

static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
{
    uint8_t  *p_fref = h->mb.pic.p_fref[0][a->l0.i_ref][0];
    uint8_t  *p_fenc = h->mb.pic.p_fenc[0];
623
    int mvc[2][2];
Laurent Aimar's avatar
Laurent Aimar committed
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
    int i;

    /* XXX Needed for x264_mb_predict_mv */
    h->mb.i_partition = D_8x16;

    for( i = 0; i < 2; i++ )
    {
        x264_me_t *m = &a->l0.me8x16[i];

        m->i_pixel = PIXEL_8x16;
        m->lm      = a->i_lambda;

        m->p_fenc  = &p_fenc[8*i];
        m->p_fref  = &p_fref[8*i];
        m->i_stride= h->mb.pic.i_stride[0];
        m->i_mv_range = a->i_mv_range;

641
642
643
644
        mvc[0][0] = a->l0.me8x8[i].mv[0];
        mvc[0][1] = a->l0.me8x8[i].mv[1];
        mvc[1][0] = a->l0.me8x8[i+2].mv[0];
        mvc[1][1] = a->l0.me8x8[i+2].mv[1];
Laurent Aimar's avatar
Laurent Aimar committed
645
646

        x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
647
        x264_me_search( h, m, mvc, 2 );
Laurent Aimar's avatar
Laurent Aimar committed
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669

        x264_macroblock_cache_mv( h, 2*i, 0, 2, 4, 0, m->mv[0], m->mv[1] );
    }

    a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
}

static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
{
    uint8_t  *p_fref = h->mb.pic.p_fref[0][a->l0.i_ref][0];
    uint8_t  *p_fenc = h->mb.pic.p_fenc[0];

    int i4x4;

    /* XXX Needed for x264_mb_predict_mv */
    h->mb.i_partition = D_8x8;

    for( i4x4 = 0; i4x4 < 4; i4x4++ )
    {
        const int idx = 4*i8x8 + i4x4;
        const int x4 = block_idx_x[idx];
        const int y4 = block_idx_y[idx];
670
        const int i_mvc = (i4x4 == 0);
Laurent Aimar's avatar
Laurent Aimar committed
671
672
673
674
675
676
677
678
679
680
681
682

        x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];

        m->i_pixel = PIXEL_4x4;
        m->lm      = a->i_lambda;

        m->p_fenc  = &p_fenc[4*(y4*h->mb.pic.i_stride[0]+x4)];
        m->p_fref  = &p_fref[4*(y4*h->mb.pic.i_stride[0]+x4)];
        m->i_stride= h->mb.pic.i_stride[0];
        m->i_mv_range = a->i_mv_range;

        x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
683
        x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
Laurent Aimar's avatar
Laurent Aimar committed
684
685
686
687
688
689
690

        x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, m->mv[0], m->mv[1] );
    }

    a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
                         a->l0.me4x4[i8x8][1].cost +
                         a->l0.me4x4[i8x8][2].cost +
691
692
                         a->l0.me4x4[i8x8][3].cost +
                         a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
Laurent Aimar's avatar
Laurent Aimar committed
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
}

static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
{
    uint8_t  *p_fref = h->mb.pic.p_fref[0][a->l0.i_ref][0];
    uint8_t  *p_fenc = h->mb.pic.p_fenc[0];

    int i8x4;

    /* XXX Needed for x264_mb_predict_mv */
    h->mb.i_partition = D_8x8;

    for( i8x4 = 0; i8x4 < 2; i8x4++ )
    {
        const int idx = 4*i8x8 + 2*i8x4;
        const int x4 = block_idx_x[idx];
        const int y4 = block_idx_y[idx];
710
        const int i_mvc = (i8x4 == 0);
Laurent Aimar's avatar
Laurent Aimar committed
711
712
713
714
715
716
717
718
719
720
721
722

        x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];

        m->i_pixel = PIXEL_8x4;
        m->lm      = a->i_lambda;

        m->p_fenc  = &p_fenc[4*(y4*h->mb.pic.i_stride[0]+x4)];
        m->p_fref  = &p_fref[4*(y4*h->mb.pic.i_stride[0]+x4)];
        m->i_stride= h->mb.pic.i_stride[0];
        m->i_mv_range = a->i_mv_range;

        x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
723
        x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
Laurent Aimar's avatar
Laurent Aimar committed
724
725
726
727

        x264_macroblock_cache_mv( h, x4, y4, 2, 1, 0, m->mv[0], m->mv[1] );
    }

728
729
    a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
                            a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
Laurent Aimar's avatar
Laurent Aimar committed
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
}

static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
{
    uint8_t  *p_fref = h->mb.pic.p_fref[0][a->l0.i_ref][0];
    uint8_t  *p_fenc = h->mb.pic.p_fenc[0];

    int i4x8;

    /* XXX Needed for x264_mb_predict_mv */
    h->mb.i_partition = D_8x8;

    for( i4x8 = 0; i4x8 < 2; i4x8++ )
    {
        const int idx = 4*i8x8 + i4x8;
        const int x4 = block_idx_x[idx];
        const int y4 = block_idx_y[idx];
747
        const int i_mvc = (i4x8 == 0);
Laurent Aimar's avatar
Laurent Aimar committed
748
749
750
751
752
753
754
755
756
757
758
759

        x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];

        m->i_pixel = PIXEL_4x8;
        m->lm      = a->i_lambda;

        m->p_fenc  = &p_fenc[4*(y4*h->mb.pic.i_stride[0]+x4)];
        m->p_fref  = &p_fref[4*(y4*h->mb.pic.i_stride[0]+x4)];
        m->i_stride= h->mb.pic.i_stride[0];
        m->i_mv_range = a->i_mv_range;

        x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
760
        x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
Laurent Aimar's avatar
Laurent Aimar committed
761
762
763
764

        x264_macroblock_cache_mv( h, x4, y4, 1, 2, 0, m->mv[0], m->mv[1] );
    }

765
766
    a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
                            a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
Laurent Aimar's avatar
Laurent Aimar committed
767
768
}

769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
{
    /* Assumes that fdec still contains the results of
     * x264_mb_predict_mv_direct16x16 and x264_mb_mc */

    uint8_t *p_fenc = h->mb.pic.p_fenc[0];
    uint8_t *p_fdec = h->mb.pic.p_fdec[0];
    int i_stride= h->mb.pic.i_stride[0];
    int i;

    a->i_cost16x16direct = 0;
    for( i = 0; i < 4; i++ )
    {
        const int x8 = i%2;
        const int y8 = i/2;
        const int off = 8 * x8 + 8 * i_stride * y8;
        a->i_cost16x16direct +=
        a->i_cost8x8direct[i] =
            h->pixf.satd[PIXEL_8x8]( &p_fenc[off], i_stride, &p_fdec[off], i_stride );
788
789
790

        /* mb type cost */
        a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
791
    }
792
793

    a->i_cost16x16direct += a->i_lambda * i_mb_b_cost_table[B_DIRECT];
794
}
Laurent Aimar's avatar
Laurent Aimar committed
795
796
797
798
799
800
801

static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
{
    uint8_t pix1[16*16], pix2[16*16];

    x264_me_t m;
    int i_ref;
802
803
804
    int mvc[5][2], i_mvc;
    int i_fullpel_thresh = INT_MAX;
    int *p_fullpel_thresh = h->i_ref0>1 ? &i_fullpel_thresh : NULL;
Laurent Aimar's avatar
Laurent Aimar committed
805
806
807
808
809
810
811
812

    /* 16x16 Search on all ref frame */
    m.i_pixel = PIXEL_16x16;
    m.lm      = a->i_lambda;
    m.p_fenc  = h->mb.pic.p_fenc[0];
    m.i_stride= h->mb.pic.i_stride[0];
    m.i_mv_range = a->i_mv_range;

813
814
815
    /* ME for List 0 */
    a->l0.me16x16.cost = INT_MAX;
    for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
Laurent Aimar's avatar
Laurent Aimar committed
816
817
818
819
    {
        /* search with ref */
        m.p_fref = h->mb.pic.p_fref[0][i_ref][0];
        x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
820
821
        x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
        x264_me_search_ref( h, &m, mvc, i_mvc, p_fullpel_thresh );
Laurent Aimar's avatar
Laurent Aimar committed
822
823
824
825
826
827
828
829
830

        /* add ref cost */
        m.cost += m.lm * bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, i_ref );

        if( m.cost < a->l0.me16x16.cost )
        {
            a->l0.i_ref = i_ref;
            a->l0.me16x16 = m;
        }
831
832
833
834

        /* save mv for predicting neighbors */
        h->mb.mvr[0][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
        h->mb.mvr[0][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
Laurent Aimar's avatar
Laurent Aimar committed
835
    }
836
837
    /* subtract ref cost, so we don't have to add it for the other MB types */
    a->l0.me16x16.cost -= m.lm * bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, a->l0.i_ref );
Laurent Aimar's avatar
Laurent Aimar committed
838

839
    /* ME for list 1 */
840
    /* not using fullpel_thresh since we don't yet do more than 1 list 1 ref */
841
842
    a->l1.me16x16.cost = INT_MAX;
    for( i_ref = 0; i_ref < h->i_ref1; i_ref++ )
Laurent Aimar's avatar
Laurent Aimar committed
843
844
845
846
    {
        /* search with ref */
        m.p_fref = h->mb.pic.p_fref[1][i_ref][0];
        x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
847
848
        x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
        x264_me_search( h, &m, mvc, i_mvc );
Laurent Aimar's avatar
Laurent Aimar committed
849
850
851
852
853
854
855
856
857

        /* add ref cost */
        m.cost += m.lm * bs_size_te( h->sh.i_num_ref_idx_l1_active - 1, i_ref );

        if( m.cost < a->l1.me16x16.cost )
        {
            a->l1.i_ref = i_ref;
            a->l1.me16x16 = m;
        }
858
859
860
861

        /* save mv for predicting neighbors */
        h->mb.mvr[1][i_ref][h->mb.i_mb_xy][0] = m.mv[0];
        h->mb.mvr[1][i_ref][h->mb.i_mb_xy][1] = m.mv[1];
Laurent Aimar's avatar
Laurent Aimar committed
862
    }
863
864
    /* subtract ref cost, so we don't have to add it for the other MB types */
    a->l1.me16x16.cost -= m.lm * bs_size_te( h->sh.i_num_ref_idx_l1_active - 1, a->l1.i_ref );
Laurent Aimar's avatar
Laurent Aimar committed
865

866
    /* Set global ref, needed for other modes? */
Laurent Aimar's avatar
Laurent Aimar committed
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
    x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
    x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );

    /* get cost of BI mode */
    h->mc[MC_LUMA]( h->mb.pic.p_fref[0][a->l0.i_ref][0], h->mb.pic.i_stride[0],
                    pix1, 16,
                    a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
                    16, 16 );
    h->mc[MC_LUMA]( h->mb.pic.p_fref[1][a->l1.i_ref][0], h->mb.pic.i_stride[0],
                    pix2, 16,
                    a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
                    16, 16 );
    h->pixf.avg[PIXEL_16x16]( pix1, 16, pix2, 16 );

    a->i_cost16x16bi = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], h->mb.pic.i_stride[0], pix1, 16 ) +
                       a->i_lambda * ( bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, a->l0.i_ref ) +
                                       bs_size_te( h->sh.i_num_ref_idx_l1_active - 1, a->l1.i_ref ) +
                                       bs_size_se( a->l0.me16x16.mv[0] - a->l0.me16x16.mvp[0] ) +
                                       bs_size_se( a->l0.me16x16.mv[1] - a->l0.me16x16.mvp[1] ) +
                                       bs_size_se( a->l1.me16x16.mv[0] - a->l1.me16x16.mvp[0] ) +
                                       bs_size_se( a->l1.me16x16.mv[1] - a->l1.me16x16.mvp[1] ) );
888
889
890
891
892

    /* mb type cost */
    a->i_cost16x16bi   += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
    a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
    a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
Laurent Aimar's avatar
Laurent Aimar committed
893
894
}

895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
#define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \
    if( x264_mb_partition_listX_table[0][part] ) \
    { \
        x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \
        x264_macroblock_cache_mv(  h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \
    } \
    else \
    { \
        x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \
        x264_macroblock_cache_mv(  h, x,y,dx,dy, 0, 0, 0 ); \
        if( b_mvd ) \
            x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \
    } \
    if( x264_mb_partition_listX_table[1][part] ) \
    { \
        x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \
        x264_macroblock_cache_mv(  h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \
    } \
    else \
    { \
        x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \
        x264_macroblock_cache_mv(  h, x,y,dx,dy, 1, 0, 0 ); \
        if( b_mvd ) \
            x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \
    }

static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
{
    int x = (i%2)*2;
    int y = (i/2)*2;
    if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
    {
        x264_mb_load_mv_direct8x8( h, i );
        if( b_mvd )
        {
            x264_macroblock_cache_mvd(  h, x, y, 2, 2, 0, 0, 0 );
            x264_macroblock_cache_mvd(  h, x, y, 2, 2, 1, 0, 0 );
            x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
        }
    }
    else
    {
        CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
    }
}
static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
{
    CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
}
static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
{
    CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
}
#undef CACHE_MV_BI

950
951
952
953
954
static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
{
    uint8_t *p_fref[2] = { h->mb.pic.p_fref[0][a->l0.i_ref][0],
                           h->mb.pic.p_fref[1][a->l1.i_ref][0] };
    uint8_t *p_fenc = h->mb.pic.p_fenc[0];
955
956
    uint8_t pix[2][8*8];
    int i, l;
957
958
959
960
961
962
963
964
965
966
967
968
969
970

    /* XXX Needed for x264_mb_predict_mv */
    h->mb.i_partition = D_8x8;

    a->i_cost8x8bi = 0;

    for( i = 0; i < 4; i++ )
    {
        const int x8 = i%2;
        const int y8 = i/2;
        uint8_t *p_fenc_i = &p_fenc[8*(y8*h->mb.pic.i_stride[0]+x8)];
        int i_part_cost;
        int i_part_cost_bi = 0;

971
        for( l = 0; l < 2; l++ )
972
        {
973
974
            x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
            x264_me_t *m = &lX->me8x8[i];
975
976
977
978
979

            m->i_pixel = PIXEL_8x8;
            m->lm      = a->i_lambda;

            m->p_fenc = p_fenc_i;
980
            m->p_fref = &p_fref[l][8*(y8*h->mb.pic.i_stride[0]+x8)];
981
982
983
            m->i_stride = h->mb.pic.i_stride[0];
            m->i_mv_range = a->i_mv_range;

984
985
            x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
            x264_me_search( h, m, &lX->me16x16.mv, 1 );
986

987
988
            x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
            lX->i_cost8x8 += m->cost;
989
990

            /* BI mode */
991
            h->mc[MC_LUMA]( m->p_fref, m->i_stride, pix[l], 8,
992
                            m->mv[0], m->mv[1], 8, 8 );
993
            /* FIXME: ref cost */
994
            i_part_cost_bi += a->i_lambda * ( bs_size_se( m->mv[0] - m->mvp[0] ) +
995
996
                                              bs_size_se( m->mv[1] - m->mvp[1] ) +
                                              i_sub_mb_b_cost_table[D_L0_8x8] );
997
998
999
        }

        h->pixf.avg[PIXEL_8x8]( pix[0], 8, pix[1], 8 );
1000
1001
        i_part_cost_bi += h->pixf.satd[PIXEL_8x8]( p_fenc_i, h->mb.pic.i_stride[0], pix[0], 8 )
                        + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022

        i_part_cost = a->l0.me8x8[i].cost;
        h->mb.i_sub_partition[i] = D_L0_8x8;
        if( a->l1.me8x8[i].cost < i_part_cost )
        {
            i_part_cost = a->l1.me8x8[i].cost;
            h->mb.i_sub_partition[i] = D_L1_8x8;
        }
        if( i_part_cost_bi < i_part_cost )
        {
            i_part_cost = i_part_cost_bi;
            h->mb.i_sub_partition[i] = D_BI_8x8;
        }
        if( a->i_cost8x8direct[i] < i_part_cost && a->i_cost8x8direct[i] >= 0)
        {
            i_part_cost = a->i_cost8x8direct[i];
            h->mb.i_sub_partition[i] = D_DIRECT_8x8;
        }
        a->i_cost8x8bi += i_part_cost;

        /* XXX Needed for x264_mb_predict_mv */
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
        x264_mb_cache_mv_b8x8( h, a, i, 0 );
    }

    /* mb type cost */
    a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
}

static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
{
    uint8_t *p_fref[2] = { h->mb.pic.p_fref[0][a->l0.i_ref][0],
                           h->mb.pic.p_fref[1][a->l1.i_ref][0] };
    uint8_t *p_fenc = h->mb.pic.p_fenc[0];
    uint8_t pix[2][8*8];
    int i_ref_stride = h->mb.pic.i_stride[0];
    int mvc[2][2];
    int i, l;

    h->mb.i_partition = D_16x8;
    a->i_cost16x8bi = 0;

    for( i = 0; i < 2; i++ )
    {
        uint8_t *p_fenc_i = &p_fenc[8*i*i_ref_stride];
        int i_part_cost;
        int i_part_cost_bi = 0;

        /* TODO: check only the list(s) that were used in b8x8? */
        for( l = 0; l < 2; l++ )
1051
        {
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
            x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
            x264_me_t *m = &lX->me16x8[i];

            m->i_pixel = PIXEL_16x8;
            m->lm      = a->i_lambda;

            m->p_fenc  = p_fenc_i;
            m->i_stride= i_ref_stride;
            m->p_fref  = &p_fref[l][8*i*i_ref_stride];
            m->i_mv_range = a->i_mv_range;

            mvc[0][0] = lX->me8x8[2*i].mv[0];
            mvc[0][1] = lX->me8x8[2*i].mv[1];
            mvc[1][0] = lX->me8x8[2*i+1].mv[0];
            mvc[1][1] = lX->me8x8[2*i+1].mv[1];

            x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp );
            x264_me_search( h, m, mvc, 2 );

            /* BI mode */
            h->mc[MC_LUMA]( m->p_fref, m->i_stride, pix[l], 8,
                            m->mv[0], m->mv[1], 8, 8 );
            /* FIXME: ref cost */
            i_part_cost_bi += a->i_lambda * ( bs_size_se( m->mv[0] - m->mvp[0] ) +
                                              bs_size_se( m->mv[1] - m->mvp[1] ) );
1077
        }
1078
1079
1080
1081
1082
1083
1084

        h->pixf.avg[PIXEL_16x8]( pix[0], 8, pix[1], 8 );
        i_part_cost_bi += h->pixf.satd[PIXEL_16x8]( p_fenc_i, h->mb.pic.i_stride[0], pix[0], 8 );

        i_part_cost = a->l0.me16x8[i].cost;
        a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
        if( a->l1.me16x8[i].cost < i_part_cost )
1085
        {
1086
1087
1088
1089
1090
1091
1092
1093
1094
            i_part_cost = a->l1.me16x8[i].cost;
            a->i_mb_partition16x8[i] = D_L1_8x8;
        }
        if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
        {
            i_part_cost = i_part_cost_bi;
            a->i_mb_partition16x8[i] = D_BI_8x8;
        }
        a->i_cost16x8bi += i_part_cost;
1095

1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
        if( i == 0 )
            x264_mb_cache_mv_b16x8( h, a, i, 0 );
    }

    /* mb type cost */
    a->i_mb_type16x8 = B_L0_L0
        + (a->i_mb_partition16x8[0]>>2) * 3
        + (a->i_mb_partition16x8[1]>>2);
    a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
}
static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
{
    uint8_t *p_fref[2] = { h->mb.pic.p_fref[0][a->l0.i_ref][0],
                           h->mb.pic.p_fref[1][a->l1.i_ref][0] };
    uint8_t *p_fenc = h->mb.pic.p_fenc[0];
    uint8_t pix[2][8*8];
    int i_ref_stride = h->mb.pic.i_stride[0];
    int mvc[2][2];
    int i, l;

    h->mb.i_partition = D_8x16;
    a->i_cost8x16bi = 0;

    for( i = 0; i < 2; i++ )
    {
        uint8_t *p_fenc_i = &p_fenc[8*i];
        int i_part_cost;
        int i_part_cost_bi = 0;

        for( l = 0; l < 2; l++ )
        {
            x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
            x264_me_t *m = &lX->me8x16[i];

            m->i_pixel = PIXEL_8x16;
            m->lm      = a->i_lambda;

            m->p_fenc  = p_fenc_i;
            m->p_fref  = &p_fref[l][8*i];
            m->i_stride= i_ref_stride;
            m->i_mv_range = a->i_mv_range;

            mvc[0][0] = lX->me8x8[i].mv[0];
            mvc[0][1] = lX->me8x8[i].mv[1];
            mvc[1][0] = lX->me8x8[i+2].mv[0];
            mvc[1][1] = lX->me8x8[i+2].mv[1];

            x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
            x264_me_search( h, m, mvc, 2 );

            /* BI mode */
            h->mc[MC_LUMA]( m->p_fref, m->i_stride, pix[l], 8,
                            m->mv[0], m->mv[1], 8, 8 );
            /* FIXME: ref cost */
            i_part_cost_bi += a->i_lambda * ( bs_size_se( m->mv[0] - m->mvp[0] ) +
                                              bs_size_se( m->mv[1] - m->mvp[1] ) );
        }

        h->pixf.avg[PIXEL_8x16]( pix[0], 8, pix[1], 8 );
        i_part_cost_bi += h->pixf.satd[PIXEL_8x16]( p_fenc_i, h->mb.pic.i_stride[0], pix[0], 8 );

        i_part_cost = a->l0.me8x16[i].cost;
        a->i_mb_partition8x16[i] = D_L0_8x8;
        if( a->l1.me8x16[i].cost < i_part_cost )
        {
            i_part_cost = a->l1.me8x16[i].cost;
            a->i_mb_partition8x16[i] = D_L1_8x8;
1163
        }
1164
1165
1166
1167
1168
1169
1170
1171
1172
        if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
        {
            i_part_cost = i_part_cost_bi;
            a->i_mb_partition8x16[i] = D_BI_8x8;
        }
        a->i_cost8x16bi += i_part_cost;

        if( i == 0 )
            x264_mb_cache_mv_b8x16( h, a, i, 0 );
1173
    }
1174
1175
1176
1177
1178
1179

    /* mb type cost */
    a->i_mb_type8x16 = B_L0_L0
        + (a->i_mb_partition8x16[0]>>2) * 3
        + (a->i_mb_partition8x16[1]>>2);
    a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
1180
1181
}

Laurent Aimar's avatar
Laurent Aimar committed
1182
1183
1184
1185
1186
1187
1188
1189
/*****************************************************************************
 * x264_macroblock_analyse:
 *****************************************************************************/
void x264_macroblock_analyse( x264_t *h )
{
    x264_mb_analysis_t analysis;
    int i;

1190
    h->mb.qp[h->mb.i_mb_xy] = x264_ratecontrol_qp(h);
Laurent Aimar's avatar
Laurent Aimar committed
1191

1192
1193
1194
1195
1196
1197
    /* FIXME check if it's 12 */
    if( h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp < -12 )
        h->mb.qp[h->mb.i_mb_xy] = h->mb.i_last_qp - 12;
    else if( h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp > 12 )
        h->mb.qp[h->mb.i_mb_xy] = h->mb.i_last_qp + 12;

Laurent Aimar's avatar
Laurent Aimar committed
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
    /* init analysis */
    x264_mb_analyse_init( h, &analysis, h->mb.qp[h->mb.i_mb_xy] );

    /*--------------------------- Do the analysis ---------------------------*/
    if( h->sh.i_type == SLICE_TYPE_I )
    {
        x264_mb_analyse_intra( h, &analysis );

        if( analysis.i_sad_i4x4 >= 0 &&  analysis.i_sad_i4x4 < analysis.i_sad_i16x16 )
            h->mb.i_type = I_4x4;
        else
            h->mb.i_type = I_16x16;
    }
    else if( h->sh.i_type == SLICE_TYPE_P )
    {
        const unsigned int i_neighbour = h->mb.i_neighbour;

        int b_skip = 0;
        int i_cost;
1217
        int i_intra_cost, i_intra_type;
Laurent Aimar's avatar
Laurent Aimar committed
1218
1219

        /* Fast P_SKIP detection */
1220
1221
1222
1223
        if( ( (i_neighbour&MB_LEFT) && h->mb.type[h->mb.i_mb_xy - 1] == P_SKIP ) ||
            ( (i_neighbour&MB_TOP) && h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride] == P_SKIP ) ||
            ( ((i_neighbour&(MB_TOP|MB_LEFT)) == (MB_TOP|MB_LEFT) ) && h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride-1 ] == P_SKIP ) ||
            ( (i_neighbour&MB_TOPRIGHT) && h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride+1 ] == P_SKIP ) )
Laurent Aimar's avatar
Laurent Aimar committed
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
        {
            b_skip = x264_macroblock_probe_pskip( h );
        }

        if( b_skip )
        {
            h->mb.i_type = P_SKIP;
            h->mb.i_partition = D_16x16;
        }
        else
        {
            const unsigned int flags = h->param.analyse.inter;
            int i_type;
            int i_partition;

            x264_mb_analyse_inter_p16x16( h, &analysis );
            if( flags & X264_ANALYSE_PSUB16x16 )
                x264_mb_analyse_inter_p8x8( h, &analysis );

            /* Select best inter mode */
            i_type = P_L0;
            i_partition = D_16x16;
            i_cost = analysis.l0.me16x16.cost;

            if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
                analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
            {
                int i;

                i_type = P_8x8;
                i_partition = D_8x8;
                h->mb.i_sub_partition[0] = D_L0_8x8;
                h->mb.i_sub_partition[1] = D_L0_8x8;
                h->mb.i_sub_partition[2] = D_L0_8x8;
                h->mb.i_sub_partition[3] = D_L0_8x8;

                i_cost = analysis.l0.i_cost8x8;

                /* Do sub 8x8 */
                if( flags & X264_ANALYSE_PSUB8x8 )
                {
                    for( i = 0; i < 4; i++ )
                    {
                        x264_mb_analyse_inter_p4x4( h, &analysis, i );
                        if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
                        {
                            int i_cost8x8;

                            h->mb.i_sub_partition[i] = D_L0_4x4;
                            i_cost8x8 = analysis.l0.i_cost4x4[i];

                            x264_mb_analyse_inter_p8x4( h, &analysis, i );
                            if( analysis.l0.i_cost8x4[i] < analysis.l0.i_cost4x4[i] )
                            {
                                h->mb.i_sub_partition[i] = D_L0_8x4;
                                i_cost8x8 = analysis.l0.i_cost8x4[i];
                            }

                            x264_mb_analyse_inter_p4x8( h, &analysis, i );
                            if( analysis.l0.i_cost4x8[i] < analysis.l0.i_cost4x4[i] )
                            {
                                h->mb.i_sub_partition[i] = D_L0_4x8;
                                i_cost8x8 = analysis.l0.i_cost4x8[i];
                            }

                            i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
                        }
                    }
                }

                /* Now do sub 16x8/8x16 */
                x264_mb_analyse_inter_p16x8( h, &analysis );
                if( analysis.l0.i_cost16x8 < i_cost )
                {
                    i_type = P_L0;
                    i_partition = D_16x8;
                    i_cost = analysis.l0.i_cost16x8;
                }

                x264_mb_analyse_inter_p8x16( h, &analysis );
                if( analysis.l0.i_cost8x16 < i_cost )
                {
                    i_type = P_L0;
                    i_partition = D_8x16;
                    i_cost = analysis.l0.i_cost8x16;
                }
            }

            h->mb.i_type = i_type;
            h->mb.i_partition = i_partition;

            /* refine qpel */
            if( h->mb.i_partition == D_16x16 )
            {
                x264_me_refine_qpel( h, &analysis.l0.me16x16 );
                i_cost = analysis.l0.me16x16.cost;
            }
            else if( h->mb.i_partition == D_16x8 )
            {
                x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
                x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
                i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
            }
            else if( h->mb.i_partition == D_8x16 )
            {
                x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
                x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
                i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
            }
            else if( h->mb.i_partition == D_8x8 )
            {
                int i8x8;
                i_cost = 0;
                for( i8x8 = 0; i8x8 < 4; i8x8++ )
                {
                    switch( h->mb.i_sub_partition[i8x8] )
                    {
                        case D_L0_8x8:
                            x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
                            i_cost += analysis.l0.me8x8[i8x8].cost;
                            break;
                        case D_L0_8x4:
                            x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
                            x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
                            i_cost += analysis.l0.me8x4[i8x8][0].cost +
                                      analysis.l0.me8x4[i8x8][1].cost;
                            break;
                        case D_L0_4x8:
                            x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
                            x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
                            i_cost += analysis.l0.me4x8[i8x8][0].cost +
                                      analysis.l0.me4x8[i8x8][1].cost;
                            break;

                        case D_L0_4x4:
                            x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
                            x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
                            x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
                            x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
                            i_cost += analysis.l0.me4x4[i8x8][0].cost +
                                      analysis.l0.me4x4[i8x8][1].cost +
                                      analysis.l0.me4x4[i8x8][2].cost +
                                      analysis.l0.me4x4[i8x8][3].cost;
                            break;
                        default:
                            fprintf( stderr, "internal error (!8x8 && !4x4)" );
                            break;
                    }
                }
            }

            x264_mb_analyse_intra( h, &analysis );
1376
1377
1378
1379
            i_intra_type = I_16x16;
            i_intra_cost = analysis.i_sad_i16x16;

            if( analysis.i_sad_i4x4 >=0 && analysis.i_sad_i4x4 < i_intra_cost )
Laurent Aimar's avatar
Laurent Aimar committed
1380
            {
1381
1382
                i_intra_type = I_4x4;
                i_intra_cost = analysis.i_sad_i4x4;
Laurent Aimar's avatar
Laurent Aimar committed
1383
1384
            }

1385
            if( i_intra_cost >= 0 && i_intra_cost < i_cost )
Laurent Aimar's avatar
Laurent Aimar committed
1386
            {
1387
1388
                h->mb.i_type = i_intra_type;
                i_cost = i_intra_cost;
Laurent Aimar's avatar
Laurent Aimar committed
1389
            }
1390
1391
1392

            h->stat.frame.i_intra_cost += i_intra_cost;
            h->stat.frame.i_inter_cost += i_cost;
Laurent Aimar's avatar
Laurent Aimar committed
1393
1394
1395
1396
        }
    }
    else if( h->sh.i_type == SLICE_TYPE_B )
    {
1397
        int b_skip = 0;
Laurent Aimar's avatar
Laurent Aimar committed
1398

1399
1400
        analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h );
        if( analysis.b_direct_available )
Laurent Aimar's avatar
Laurent Aimar committed
1401
        {
1402
1403
            h->mb.i_type = B_SKIP;
            x264_mb_mc( h );
Laurent Aimar's avatar
Laurent Aimar committed
1404

1405
1406
1407
            /* Conditioning the probe on neighboring block types
             * doesn't seem to help speed or quality. */
            b_skip = x264_macroblock_probe_bskip( h );
Laurent Aimar's avatar
Laurent Aimar committed
1408
        }
1409
1410

        if( !b_skip )
Laurent Aimar's avatar
Laurent Aimar committed
1411
        {
1412
1413
1414
1415
1416
            const unsigned int flags = h->param.analyse.inter;
            int i_partition;
            int i_cost;

            /* select best inter mode */
1417
1418
1419
1420
1421
1422
1423
            /* direct must be first */
            if( analysis.b_direct_available )
                x264_mb_analyse_inter_direct( h, &analysis );

            x264_mb_analyse_inter_b16x16( h, &analysis );

            h->mb.i_type = B_L0_L0;
1424
            i_partition = D_16x16;
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
            i_cost = analysis.l0.me16x16.cost;
            if( analysis.l1.me16x16.cost < i_cost )
            {
                h->mb.i_type = B_L1_L1;
                i_cost = analysis.l1.me16x16.cost;
            }
            if( analysis.i_cost16x16bi < i_cost )
            {
                h->mb.i_type = B_BI_BI;
                i_cost = analysis.i_cost16x16bi;
            }
            if( analysis.i_cost16x16direct < i_cost && analysis.i_cost16x16direct >= 0 )
            {
                h->mb.i_type = B_DIRECT;
                i_cost = analysis.i_cost16x16direct;
            }
1441
1442
            
            if( flags & X264_ANALYSE_BSUB16x16 )
1443
            {
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
                x264_mb_analyse_inter_b8x8( h, &analysis );
                if( analysis.i_cost8x8bi < i_cost )
                {
                    h->mb.i_type = B_8x8;
                    i_partition = D_8x8;
                    i_cost = analysis.i_cost8x8bi;

                    if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
                        h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
                    {
                        x264_mb_analyse_inter_b16x8( h, &analysis );
                        if( analysis.i_cost16x8bi < i_cost )
                        {
                            i_partition = D_16x8;
                            i_cost = analysis.i_cost16x8bi;
                            h->mb.i_type = analysis.i_mb_type16x8;
                        }
                    }
                    if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
                        h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
                    {
                        x264_mb_analyse_inter_b8x16( h, &analysis );
                        if( analysis.i_cost8x16bi < i_cost )
                        {
                            i_partition = D_8x16;
                            i_cost = analysis.i_cost8x16bi;
                            h->mb.i_type = analysis.i_mb_type8x16;
                        }
                    }
                }
1474
1475
            }

1476
1477
            h->mb.i_partition = i_partition;

1478
            /* refine qpel */
1479
            if( i_partition == D_16x16 )
1480
1481
1482
            {
                if( h->mb.i_type == B_L0_L0 )
                {
1483
                    analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
1484
                    x264_me_refine_qpel( h, &analysis.l0.me16x16 );
1485
                    analysis.l0.me16x16.cost += analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
1486
1487
1488
1489
                    i_cost = analysis.l0.me16x16.cost;
                }
                else if( h->mb.i_type == B_L1_L1 )
                {
1490
                    analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
1491
                    x264_me_refine_qpel( h, &analysis.l1.me16x16 );
1492
                    analysis.l1.me16x16.cost += analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
1493
1494
1495
1496
1497
1498
1499
                    i_cost = analysis.l1.me16x16.cost;
                }
            }
            /* TODO: refine bidir, 8x8 */

            /* best intra mode */
            x264_mb_analyse_intra( h, &analysis );
1500

1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
            if( analysis.i_sad_i16x16 >= 0 && analysis.i_sad_i16x16 < i_cost )
            {
                h->mb.i_type = I_16x16;
                i_cost = analysis.i_sad_i16x16;
            }
            if( analysis.i_sad_i4x4 >=0 && analysis.i_sad_i4x4 < i_cost )
            {
                h->mb.i_type = I_4x4;
                i_cost = analysis.i_sad_i4x4;
            }
Laurent Aimar's avatar
Laurent Aimar committed
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
        }
    }

    /*-------------------- Update MB from the analysis ----------------------*/
    h->mb.type[h->mb.i_mb_xy] = h->mb.i_type;
    switch( h->mb.i_type )
    {
        case I_4x4:
            for( i = 0; i < 16; i++ )
            {
                h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] =
                    analysis.i_predict4x4[block_idx_x[i]][block_idx_y[i]];
            }

            x264_mb_analyse_intra_chroma( h, &analysis );
            h->mb.i_chroma_pred_mode = analysis.i_predict8x8;
            break;
        case I_16x16:
            h->mb.i_intra16x16_pred_mode = analysis.i_predict16x16;

            x264_mb_analyse_intra_chroma( h, &analysis );
            h->mb.i_chroma_pred_mode = analysis.i_predict8x8;
            break;

        case P_L0:
            x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.i_ref );
            switch( h->mb.i_partition )
            {
                case D_16x16:
                    x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.mv[0], analysis.l0.me16x16.mv[1] );
                    break;

                case D_16x8:
                    x264_macroblock_cache_mv ( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].mv[0], analysis.l0.me16x8[0].mv[1] );
                    x264_macroblock_cache_mv ( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].mv[0], analysis.l0.me16x8[1].mv[1] );
                    break;

                case D_8x16:
                    x264_macroblock_cache_mv ( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].mv[0], analysis.l0.me8x16[0].mv[1] );
                    x264_macroblock_cache_mv ( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].mv[0], analysis.l0.me8x16[1].mv[1] );
                    break;

                default:
                    fprintf( stderr, "internal error P_L0 and partition=%d\n", h->mb.i_partition );
                    break;
            }
            break;

        case P_8x8:
            x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.i_ref );
            for( i = 0; i < 4; i++ )
            {
                const int x = 2*(i%2);
                const int y = 2*(i/2);

                switch( h->mb.i_sub_partition[i] )
                {
                    case D_L0_8x8:
                        x264_macroblock_cache_mv( h, x, y, 2, 2, 0, analysis.l0.me8x8[i].mv[0], analysis.l0.me8x8[i].mv[1] );
                        break;
                    case D_L0_8x4:
                        x264_macroblock_cache_mv( h, x, y+0, 2, 1, 0, analysis.l0.me8x4[i][0].mv[0], analysis.l0.me8x4[i][0].mv[1] );
                        x264_macroblock_cache_mv( h, x, y+1, 2, 1, 0, analysis.l0.me8x4[i][1].mv[0], analysis.l0.me8x4[i][1].mv[1] );
                        break;
                    case D_L0_4x8:
                        x264_macroblock_cache_mv( h, x+0, y, 1, 2, 0, analysis.l0.me4x8[i][0].mv[0], analysis.l0.me4x8[i][0].mv[1] );
                        x264_macroblock_cache_mv( h, x+1, y, 1, 2, 0, analysis.l0.me4x8[i][1].mv[0], analysis.l0.me4x8[i][1].mv[1] );
                        break;
                    case D_L0_4x4:
                        x264_macroblock_cache_mv( h, x+0, y+0, 1, 1, 0, analysis.l0.me4x4[i][0].mv[0], analysis.l0.me4x4[i][0].mv[1] );
                        x264_macroblock_cache_mv( h, x+1, y+0, 1, 1, 0, analysis.l0.me4x4[i][1].mv[0], analysis.l0.me4x4[i][1].mv[1] );
                        x264_macroblock_cache_mv( h, x+0, y+1, 1, 1, 0, analysis.l0.me4x4[i][2].mv[0], analysis.l0.me4x4[i][2].mv[1] );
                        x264_macroblock_cache_mv( h, x+1, y+1, 1, 1, 0, analysis.l0.me4x4[i][3].mv[0], analysis.l0.me4x4[i][3].mv[1] );
                        break;
                    default:
                        fprintf( stderr, "internal error\n" );
                        break;
                }
            }
            break;

        case P_SKIP:
        {
            int mvp[2];
            x264_mb_predict_mv_pskip( h, mvp );
            /* */
            h->mb.i_partition = D_16x16;
            x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
            x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, mvp[0], mvp[1] );
            break;
        }

1603
        case B_SKIP:
1604
1605
            /* nothing has changed since x264_macroblock_probe_bskip */
            break;
1606
1607
1608
1609
1610
1611
1612
        case B_DIRECT:
            x264_mb_load_mv_direct8x8( h, 0 );
            x264_mb_load_mv_direct8x8( h, 1 );
            x264_mb_load_mv_direct8x8( h, 2 );
            x264_mb_load_mv_direct8x8( h, 3 );
            break;

1613
1614
1615
1616
1617
1618
1619
        case B_8x8:
            /* optimize: cache might not need to be rewritten */
            for( i = 0; i < 4; i++ )
                x264_mb_cache_mv_b8x8( h, &analysis, i, 1 );
            break;

        default: /* the rest of the B types */
Laurent Aimar's avatar
Laurent Aimar committed
1620
1621
            switch( h->mb.i_partition )
            {
1622
1623
1624
1625
            case D_16x16:
                switch( h->mb.i_type )
                {
                case B_L0_L0:
Laurent Aimar's avatar
Laurent Aimar committed
1626
1627
1628
1629
1630
1631
1632
                    x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.i_ref );
                    x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.mv[0], analysis.l0.me16x16.mv[1] );

                    x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
                    x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1,  0, 0 );
                    x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1,  0, 0 );
                    break;
1633
                case B_L1_L1:
Laurent Aimar's avatar
Laurent Aimar committed
1634
1635
1636
1637
1638
1639
1640
                    x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
                    x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0,  0, 0 );
                    x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0,  0, 0 );

                    x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, analysis.l1.i_ref );
                    x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, analysis.l1.me16x16.mv[0], analysis.l1.me16x16.mv[1] );
                    break;
1641
                case B_BI_BI:
Laurent Aimar's avatar
Laurent Aimar committed
1642
1643
1644
1645
1646
1647
                    x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.i_ref );
                    x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.mv[0], analysis.l0.me16x16.mv[1] );

                    x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, analysis.l1.i_ref );
                    x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, analysis.l1.me16x16.mv[0], analysis.l1.me16x16.mv[1] );
                    break;
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
                }
                break;
            case D_16x8:
                x264_mb_cache_mv_b16x8( h, &analysis, 0, 1 );
                x264_mb_cache_mv_b16x8( h, &analysis, 1, 1 );
                break;
            case D_8x16:
                x264_mb_cache_mv_b8x16( h, &analysis, 0, 1 );
                x264_mb_cache_mv_b8x16( h, &analysis, 1, 1 );
                break;
            default:
                fprintf( stderr, "internal error (invalid MB type)\n" );
                break;
Laurent Aimar's avatar
Laurent Aimar committed
1661
1662
1663
1664
            }
    }
}