analyse.c 54.9 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
378
379
380
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
437
438
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
    }
    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;
        }
    }

    /* 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) */
    }
}

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;
489
    int mvc[4][2], i_mvc;
Laurent Aimar's avatar
Laurent Aimar committed
490
491
492
493
494
495
496
497

    /* 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;

498
499
    a->l0.me16x16.cost = INT_MAX;
    for( i_ref = 0; i_ref < h->i_ref0; i_ref++ )
Laurent Aimar's avatar
Laurent Aimar committed
500
501
502
503
    {
        /* 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 );
504
505
        x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
        x264_me_search( h, &m, mvc, i_mvc );
Laurent Aimar's avatar
Laurent Aimar committed
506
507
508
509
510
511
512
513
514

        /* 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;
        }
515
516
517
518

        /* 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
519
520
    }

521
522
523
    /* 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
524
525
526
527
528
529
530
531
    /* 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];
532
    int mvc[5][2], i_mvc;
Laurent Aimar's avatar
Laurent Aimar committed
533
534
535
536
537
    int i;

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

538
539
540
541
    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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
    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 );
557
        x264_me_search( h, m, mvc, i_mvc );
Laurent Aimar's avatar
Laurent Aimar committed
558
559

        x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, m->mv[0], m->mv[1] );
560
561
562
563

        mvc[i_mvc][0] = m->mv[0];
        mvc[i_mvc][1] = m->mv[1];
        i_mvc++;
564
565
566

        /* mb type cost */
        m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
Laurent Aimar's avatar
Laurent Aimar committed
567
568
569
570
571
572
573
574
575
576
    }

    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];
577
    int mvc[2][2];
Laurent Aimar's avatar
Laurent Aimar committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
    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;

595
596
597
598
        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
599
600

        x264_mb_predict_mv( h, 0, 8*i, 4, m->mvp );
601
        x264_me_search( h, m, mvc, 2 );
Laurent Aimar's avatar
Laurent Aimar committed
602
603
604
605
606
607
608
609
610
611
612

        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];
613
    int mvc[2][2];
Laurent Aimar's avatar
Laurent Aimar committed
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
    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;

631
632
633
634
        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
635
636

        x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
637
        x264_me_search( h, m, mvc, 2 );
Laurent Aimar's avatar
Laurent Aimar committed
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659

        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];
660
        const int i_mvc = (i4x4 == 0);
Laurent Aimar's avatar
Laurent Aimar committed
661
662
663
664
665
666
667
668
669
670
671
672

        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 );
673
        x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
Laurent Aimar's avatar
Laurent Aimar committed
674
675
676
677
678
679
680

        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 +
681
682
                         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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
}

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];
700
        const int i_mvc = (i8x4 == 0);
Laurent Aimar's avatar
Laurent Aimar committed
701
702
703
704
705
706
707
708
709
710
711
712

        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 );
713
        x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
Laurent Aimar's avatar
Laurent Aimar committed
714
715
716
717

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

718
719
    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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
}

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];
737
        const int i_mvc = (i4x8 == 0);
Laurent Aimar's avatar
Laurent Aimar committed
738
739
740
741
742
743
744
745
746
747
748
749

        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 );
750
        x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
Laurent Aimar's avatar
Laurent Aimar committed
751
752
753
754

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

755
756
    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
757
758
}

759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
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 );
778
779
780

        /* mb type cost */
        a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
781
    }
782
783

    a->i_cost16x16direct += a->i_lambda * i_mb_b_cost_table[B_DIRECT];
784
}
Laurent Aimar's avatar
Laurent Aimar committed
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799

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;

    /* 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;

800
801
802
    /* 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
803
804
805
806
    {
        /* 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 );
807
        x264_me_search( h, &m, NULL, 0 );
Laurent Aimar's avatar
Laurent Aimar committed
808
809
810
811
812
813
814
815
816
817

        /* 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;
        }
    }
818
819
    /* 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
820

821
822
823
    /* ME for list 1 */
    a->l1.me16x16.cost = INT_MAX;
    for( i_ref = 0; i_ref < h->i_ref1; i_ref++ )
Laurent Aimar's avatar
Laurent Aimar committed
824
825
826
827
    {
        /* 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 );
828
        x264_me_search( h, &m, NULL, 0 );
Laurent Aimar's avatar
Laurent Aimar committed
829
830
831
832
833
834
835
836
837
838

        /* 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;
        }
    }
839
840
    /* 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
841

842
    /* Set global ref, needed for other modes? */
Laurent Aimar's avatar
Laurent Aimar committed
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
    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] ) );
864
865
866
867
868

    /* 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
869
870
}

871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
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
#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

926
927
928
929
930
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];
931
932
    uint8_t pix[2][8*8];
    int i, l;
933
934
935
936
937
938
939
940
941
942
943
944
945
946

    /* 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;

947
        for( l = 0; l < 2; l++ )
948
        {
949
950
            x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
            x264_me_t *m = &lX->me8x8[i];
951
952
953
954
955

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

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

960
961
            x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
            x264_me_search( h, m, &lX->me16x16.mv, 1 );
962

963
964
            x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] );
            lX->i_cost8x8 += m->cost;
965
966

            /* BI mode */
967
            h->mc[MC_LUMA]( m->p_fref, m->i_stride, pix[l], 8,
968
                            m->mv[0], m->mv[1], 8, 8 );
969
            /* FIXME: ref cost */
970
            i_part_cost_bi += a->i_lambda * ( bs_size_se( m->mv[0] - m->mvp[0] ) +
971
972
                                              bs_size_se( m->mv[1] - m->mvp[1] ) +
                                              i_sub_mb_b_cost_table[D_L0_8x8] );
973
974
975
        }

        h->pixf.avg[PIXEL_8x8]( pix[0], 8, pix[1], 8 );
976
977
        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];
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998

        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 */
999
1000
        x264_mb_cache_mv_b8x8( h, a, i, 0 );
    }