analyse.c 117 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1 2 3
/*****************************************************************************
 * analyse.c: h264 encoder library
 *****************************************************************************
4
 * Copyright (C) 2003-2008 x264 project
Laurent Aimar's avatar
Laurent Aimar committed
5 6
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *          Loren Merritt <lorenm@u.washington.edu>
8
 *          Fiona Glaser <fiona@x264.com>
Laurent Aimar's avatar
Laurent Aimar committed
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
Laurent Aimar's avatar
Laurent Aimar committed
23 24
 *****************************************************************************/

25
#define _ISOC99_SOURCE
Laurent Aimar's avatar
Laurent Aimar committed
26
#include <math.h>
27
#ifndef _MSC_VER
Loren Merritt's avatar
Loren Merritt committed
28
#include <unistd.h>
29
#endif
Laurent Aimar's avatar
Laurent Aimar committed
30

31
#include "common/common.h"
32
#include "common/cpu.h"
Laurent Aimar's avatar
Laurent Aimar committed
33 34
#include "macroblock.h"
#include "me.h"
35
#include "ratecontrol.h"
36 37
#include "analyse.h"
#include "rdo.c"
Laurent Aimar's avatar
Laurent Aimar committed
38 39 40 41 42

typedef struct
{
    /* 16x16 */
    int i_ref;
43
    int       i_rd16x16;
Laurent Aimar's avatar
Laurent Aimar committed
44 45 46 47
    x264_me_t me16x16;

    /* 8x8 */
    int       i_cost8x8;
Håkan Hjort's avatar
Håkan Hjort committed
48
    /* [ref][0] is 16x16 mv, [ref][1..4] are 8x8 mv from partition [0..3] */
49
    ALIGNED_4( int16_t mvc[32][5][2] );
Laurent Aimar's avatar
Laurent Aimar committed
50 51 52 53 54 55 56 57 58 59 60 61
    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 */
Loren Merritt's avatar
Loren Merritt committed
62
    x264_me_t me4x8[4][2];
Laurent Aimar's avatar
Laurent Aimar committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

    /* 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;
78
    int i_lambda2;
Laurent Aimar's avatar
Laurent Aimar committed
79
    int i_qp;
80
    uint16_t *p_cost_mv;
81 82
    uint16_t *p_cost_ref0;
    uint16_t *p_cost_ref1;
83
    int i_mbrd;
Laurent Aimar's avatar
Laurent Aimar committed
84 85 86


    /* I: Intra part */
87 88
    /* Take some shortcuts in intra search if intra is deemed unlikely */
    int b_fast_intra;
89
    int b_try_pskip;
90

91
    /* Luma part */
92 93
    int i_satd_i16x16;
    int i_satd_i16x16_dir[7];
Laurent Aimar's avatar
Laurent Aimar committed
94 95
    int i_predict16x16;

96
    int i_satd_i8x8;
97
    int i_cbp_i8x8_luma;
98 99
    int i_satd_i8x8_dir[12][4];
    int i_predict8x8[4];
100

101 102
    int i_satd_i4x4;
    int i_predict4x4[16];
Laurent Aimar's avatar
Laurent Aimar committed
103

104 105
    int i_satd_pcm;

Laurent Aimar's avatar
Laurent Aimar committed
106
    /* Chroma part */
107
    int i_satd_i8x8chroma;
108
    int i_satd_i8x8chroma_dir[4];
109
    int i_predict8x8chroma;
Laurent Aimar's avatar
Laurent Aimar committed
110 111 112 113 114 115

    /* II: Inter part P/B frame */
    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) */
116 117 118
    int i_cost16x16direct;
    int i_cost8x8bi;
    int i_cost8x8direct[4];
119 120
    int i_cost16x8bi;
    int i_cost8x16bi;
121 122 123 124 125
    int i_rd16x16bi;
    int i_rd16x16direct;
    int i_rd16x8bi;
    int i_rd8x16bi;
    int i_rd8x8bi;
126 127 128 129 130

    int i_mb_partition16x8[2]; /* mb_partition_e */
    int i_mb_partition8x16[2];
    int i_mb_type16x8; /* mb_class_e */
    int i_mb_type8x16;
131 132

    int b_direct_available;
Laurent Aimar's avatar
Laurent Aimar committed
133 134 135

} x264_mb_analysis_t;

136
/* lambda = pow(2,qp/6-2) */
137
const int x264_lambda_tab[52] = {
Laurent Aimar's avatar
Laurent Aimar committed
138 139 140 141 142 143 144 145 146
   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 */
};

Fiona Glaser's avatar
Fiona Glaser committed
147
/* lambda2 = pow(lambda,2) * .9 * 256 */
148
const int x264_lambda2_tab[52] = {
Fiona Glaser's avatar
Fiona Glaser committed
149 150 151 152 153 154 155
    14,      18,      22,      28,     36,     45,     57,     72, /*  0 -  7 */
    91,     115,     145,     182,    230,    290,    365,    460, /*  8 - 15 */
   580,     731,     921,    1161,   1462,   1843,   2322,   2925, /* 16 - 23 */
  3686,    4644,    5851,    7372,   9289,  11703,  14745,  18578, /* 24 - 31 */
 23407,   29491,   37156,   46814,  58982,  74313,  93628, 117964, /* 32 - 39 */
148626,  187257,  235929,  297252, 374514, 471859, 594505, 749029, /* 40 - 47 */
943718, 1189010, 1498059, 1887436                                  /* 48 - 51 */
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
const uint8_t x264_exp2_lut[64] = {
      1,   4,   7,  10,  13,  16,  19,  22,  25,  28,  31,  34,  37,  40,  44,  47,
     50,  53,  57,  60,  64,  67,  71,  74,  78,  81,  85,  89,  93,  96, 100, 104,
    108, 112, 116, 120, 124, 128, 132, 137, 141, 145, 150, 154, 159, 163, 168, 172,
    177, 182, 186, 191, 196, 201, 206, 211, 216, 221, 226, 232, 237, 242, 248, 253,
};

const float x264_log2_lut[128] = {
    0.00000, 0.01123, 0.02237, 0.03342, 0.04439, 0.05528, 0.06609, 0.07682,
    0.08746, 0.09803, 0.10852, 0.11894, 0.12928, 0.13955, 0.14975, 0.15987,
    0.16993, 0.17991, 0.18982, 0.19967, 0.20945, 0.21917, 0.22882, 0.23840,
    0.24793, 0.25739, 0.26679, 0.27612, 0.28540, 0.29462, 0.30378, 0.31288,
    0.32193, 0.33092, 0.33985, 0.34873, 0.35755, 0.36632, 0.37504, 0.38370,
    0.39232, 0.40088, 0.40939, 0.41785, 0.42626, 0.43463, 0.44294, 0.45121,
    0.45943, 0.46761, 0.47573, 0.48382, 0.49185, 0.49985, 0.50779, 0.51570,
    0.52356, 0.53138, 0.53916, 0.54689, 0.55459, 0.56224, 0.56986, 0.57743,
    0.58496, 0.59246, 0.59991, 0.60733, 0.61471, 0.62205, 0.62936, 0.63662,
    0.64386, 0.65105, 0.65821, 0.66534, 0.67243, 0.67948, 0.68650, 0.69349,
    0.70044, 0.70736, 0.71425, 0.72110, 0.72792, 0.73471, 0.74147, 0.74819,
    0.75489, 0.76155, 0.76818, 0.77479, 0.78136, 0.78790, 0.79442, 0.80090,
    0.80735, 0.81378, 0.82018, 0.82655, 0.83289, 0.83920, 0.84549, 0.85175,
    0.85798, 0.86419, 0.87036, 0.87652, 0.88264, 0.88874, 0.89482, 0.90087,
    0.90689, 0.91289, 0.91886, 0.92481, 0.93074, 0.93664, 0.94251, 0.94837,
    0.95420, 0.96000, 0.96578, 0.97154, 0.97728, 0.98299, 0.98868, 0.99435,
};

/* Avoid an int/float conversion. */
const float x264_log2_lz_lut[32] = {
    31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
};

Fiona Glaser's avatar
Fiona Glaser committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
// should the intra and inter lambdas be different?
// I'm just matching the behaviour of deadzone quant.
static const int x264_trellis_lambda2_tab[2][52] = {
    // inter lambda = .85 * .85 * 2**(qp/3. + 10 - LAMBDA_BITS)
    {    46,      58,      73,      92,     117,     147,
        185,     233,     294,     370,     466,     587,
        740,     932,    1174,    1480,    1864,    2349,
       2959,    3728,    4697,    5918,    7457,    9395,
      11837,   14914,   18790,   23674,   29828,   37581,
      47349,   59656,   75163,   94699,  119313,  150326,
     189399,  238627,  300652,  378798,  477255,  601304,
     757596,  954511, 1202608, 1515192, 1909022, 2405217,
    3030384, 3818045, 4810435, 6060769 },
    // intra lambda = .65 * .65 * 2**(qp/3. + 10 - LAMBDA_BITS)
    {    27,      34,      43,      54,      68,      86,
        108,     136,     172,     216,     273,     343,
        433,     545,     687,     865,    1090,    1374,
       1731,    2180,    2747,    3461,    4361,    5494,
       6922,    8721,   10988,   13844,   17442,   21976,
      27688,   34885,   43953,   55377,   69771,   87906,
     110755,  139543,  175813,  221511,  279087,  351627,
     443023,  558174,  703255,  886046, 1116348, 1406511,
    1772093, 2232697, 2813022, 3544186 }
};

static const uint16_t x264_chroma_lambda2_offset_tab[] = {
       16,    20,    25,    32,    40,    50,
       64,    80,   101,   128,   161,   203,
      256,   322,   406,   512,   645,   812,
     1024,  1290,  1625,  2048,  2580,  3250,
     4096,  5160,  6501,  8192, 10321, 13003,
    16384, 20642, 26007, 32768, 41285, 52015,
    65535
};

224
/* TODO: calculate CABAC costs */
Loren Merritt's avatar
Loren Merritt committed
225
static const int i_mb_b_cost_table[X264_MBTYPE_MAX] = {
226
    9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
227
};
228
static const int i_mb_b16x8_cost_table[17] = {
229
    0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
230 231 232 233
};
static const int i_sub_mb_b_cost_table[13] = {
    7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
};
234 235 236
static const int i_sub_mb_p_cost_table[4] = {
    5, 3, 3, 1
};
237

238 239
static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );

240 241
static uint16_t x264_cost_ref[92][3][33];
static x264_pthread_mutex_t cost_ref_mutex = X264_PTHREAD_MUTEX_INITIALIZER;
242

243
int x264_analyse_init_costs( x264_t *h, int qp )
244
{
245
    int i, j;
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    int lambda = x264_lambda_tab[qp];
    if( h->cost_mv[lambda] )
        return 0;
    /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
    CHECKED_MALLOC( h->cost_mv[lambda], (4*4*2048 + 1) * sizeof(uint16_t) );
    h->cost_mv[lambda] += 2*4*2048;
    for( i = 0; i <= 2*4*2048; i++ )
    {
        h->cost_mv[lambda][-i] =
        h->cost_mv[lambda][i]  = lambda * (log2f(i+1)*2 + 0.718f + !!i) + .5f;
    }
    x264_pthread_mutex_lock( &cost_ref_mutex );
    for( i = 0; i < 3; i++ )
        for( j = 0; j < 33; j++ )
            x264_cost_ref[lambda][i][j] = i ? lambda * bs_size_te( i, j ) : 0;
    x264_pthread_mutex_unlock( &cost_ref_mutex );
    if( h->param.analyse.i_me_method >= X264_ME_ESA && !h->cost_mv_fpel[lambda][0] )
263 264 265
    {
        for( j=0; j<4; j++ )
        {
266 267
            CHECKED_MALLOC( h->cost_mv_fpel[lambda][j], (4*2048 + 1) * sizeof(uint16_t) );
            h->cost_mv_fpel[lambda][j] += 2*2048;
268
            for( i = -2*2048; i < 2*2048; i++ )
269
                h->cost_mv_fpel[lambda][j][i] = h->cost_mv[lambda][i*4+j];
270 271
        }
    }
272 273 274
    return 0;
fail:
    return -1;
275 276
}

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
void x264_analyse_free_costs( x264_t *h )
{
    int i, j;
    for( i = 0; i < 92; i++ )
    {
        if( h->cost_mv[i] )
            x264_free( h->cost_mv[i] - 2*4*2048 );
        if( h->cost_mv_fpel[i][0] )
            for( j = 0; j < 4; j++ )
                x264_free( h->cost_mv_fpel[i][j] - 2*2048 );
    }
}

/* initialize an array of lambda*nbits for all possible mvs */
static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
{
    a->p_cost_mv = h->cost_mv[a->i_lambda];
    a->p_cost_ref0 = x264_cost_ref[a->i_lambda][x264_clip3(h->sh.i_num_ref_idx_l0_active-1,0,2)];
    a->p_cost_ref1 = x264_cost_ref[a->i_lambda][x264_clip3(h->sh.i_num_ref_idx_l1_active-1,0,2)];
}

Laurent Aimar's avatar
Laurent Aimar committed
298 299
static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
{
300
    int i = h->param.analyse.i_subpel_refine - (h->sh.i_type == SLICE_TYPE_B);
Fiona Glaser's avatar
Fiona Glaser committed
301

302 303
    /* mbrd == 1 -> RD mode decision */
    /* mbrd == 2 -> RD refinement */
Fiona Glaser's avatar
Fiona Glaser committed
304 305 306
    /* mbrd == 3 -> QPRD */
    a->i_mbrd = (i>=6) + (i>=8) + (h->param.analyse.i_subpel_refine>=10);

Laurent Aimar's avatar
Laurent Aimar committed
307
    /* conduct the analysis using this lamda and QP */
308
    a->i_qp = h->mb.i_qp = i_qp;
309
    h->mb.i_chroma_qp = h->chroma_qp_table[i_qp];
Fiona Glaser's avatar
Fiona Glaser committed
310

311 312
    a->i_lambda = x264_lambda_tab[i_qp];
    a->i_lambda2 = x264_lambda2_tab[i_qp];
Fiona Glaser's avatar
Fiona Glaser committed
313 314

    h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->i_mbrd;
Fiona Glaser's avatar
Fiona Glaser committed
315
    if( h->param.analyse.i_trellis )
Fiona Glaser's avatar
Fiona Glaser committed
316 317 318 319 320 321 322
    {
        h->mb.i_trellis_lambda2[0][0] = x264_trellis_lambda2_tab[0][h->mb.i_qp];
        h->mb.i_trellis_lambda2[0][1] = x264_trellis_lambda2_tab[1][h->mb.i_qp];
        h->mb.i_trellis_lambda2[1][0] = x264_trellis_lambda2_tab[0][h->mb.i_chroma_qp];
        h->mb.i_trellis_lambda2[1][1] = x264_trellis_lambda2_tab[1][h->mb.i_chroma_qp];
    }
    h->mb.i_psy_rd_lambda = a->i_lambda;
Fiona Glaser's avatar
Fiona Glaser committed
323 324
    /* Adjusting chroma lambda based on QP offset hurts PSNR but improves visual quality. */
    h->mb.i_chroma_lambda2_offset = h->param.analyse.b_psy ? x264_chroma_lambda2_offset_tab[h->mb.i_qp-h->mb.i_chroma_qp+12] : 256;
Fiona Glaser's avatar
Fiona Glaser committed
325

326
    h->mb.i_me_method = h->param.analyse.i_me_method;
Loren Merritt's avatar
Loren Merritt committed
327 328 329
    h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
    h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
                        && h->mb.i_subpel_refine >= 5;
Fiona Glaser's avatar
Fiona Glaser committed
330

331
    h->mb.b_transform_8x8 = 0;
332
    h->mb.b_noise_reduction = 0;
333

Laurent Aimar's avatar
Laurent Aimar committed
334
    /* I: Intra part */
335 336 337 338
    a->i_satd_i16x16 =
    a->i_satd_i8x8   =
    a->i_satd_i4x4   =
    a->i_satd_i8x8chroma = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
339

Fiona Glaser's avatar
Fiona Glaser committed
340
    /* non-RD PCM decision is inaccurate (as is psy-rd), so don't do it */
341
    a->i_satd_pcm = !h->mb.i_psy_rd && a->i_mbrd ? ((uint64_t)X264_PCM_COST*a->i_lambda2 + 128) >> 8 : COST_MAX;
342

343
    a->b_fast_intra = 0;
344 345
    h->mb.i_skip_intra =
        h->mb.b_lossless ? 0 :
346
        a->i_mbrd ? 2 :
347
        !h->param.analyse.i_trellis && !h->param.analyse.i_noise_reduction;
348

Laurent Aimar's avatar
Laurent Aimar committed
349 350 351
    /* II: Inter part P/B frame */
    if( h->sh.i_type != SLICE_TYPE_I )
    {
Loren Merritt's avatar
Loren Merritt committed
352 353
        int i, j;
        int i_fmv_range = 4 * h->param.analyse.i_mv_range;
354 355
        // limit motion search to a slightly smaller range than the theoretical limit,
        // since the search may go a few iterations past its given range
356
        int i_fpel_border = 6; // umh: 1 for diamond, 2 for octagon, 2 for hpel
Laurent Aimar's avatar
Laurent Aimar committed
357

358
        /* Calculate max allowed MV range */
359
#define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range-1 )
360 361
        h->mb.mv_min[0] = 4*( -16*h->mb.i_mb_x - 24 );
        h->mb.mv_max[0] = 4*( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 24 );
Loren Merritt's avatar
Loren Merritt committed
362 363 364 365
        h->mb.mv_min_spel[0] = CLIP_FMV( h->mb.mv_min[0] );
        h->mb.mv_max_spel[0] = CLIP_FMV( h->mb.mv_max[0] );
        h->mb.mv_min_fpel[0] = (h->mb.mv_min_spel[0]>>2) + i_fpel_border;
        h->mb.mv_max_fpel[0] = (h->mb.mv_max_spel[0]>>2) - i_fpel_border;
366 367
        if( h->mb.i_mb_x == 0)
        {
368 369
            int mb_y = h->mb.i_mb_y >> h->sh.b_mbaff;
            int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff;
Loren Merritt's avatar
Loren Merritt committed
370 371 372 373 374 375 376 377 378 379 380 381
            int thread_mvy_range = i_fmv_range;

            if( h->param.i_threads > 1 )
            {
                int pix_y = (h->mb.i_mb_y | h->mb.b_interlaced) * 16;
                int thresh = pix_y + h->param.analyse.i_mv_range_thread;
                for( i = (h->sh.i_type == SLICE_TYPE_B); i >= 0; i-- )
                {
                    x264_frame_t **fref = i ? h->fref1 : h->fref0;
                    int i_ref = i ? h->i_ref1 : h->i_ref0;
                    for( j=0; j<i_ref; j++ )
                    {
382
                        x264_frame_cond_wait( fref[j], thresh );
Loren Merritt's avatar
Loren Merritt committed
383 384 385 386 387 388 389 390 391
                        thread_mvy_range = X264_MIN( thread_mvy_range, fref[j]->i_lines_completed - pix_y );
                    }
                }
                if( h->param.b_deterministic )
                    thread_mvy_range = h->param.analyse.i_mv_range_thread;
                if( h->mb.b_interlaced )
                    thread_mvy_range >>= 1;
            }

392 393
            h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
            h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
394
            h->mb.mv_min_spel[1] = x264_clip3( h->mb.mv_min[1], -i_fmv_range, i_fmv_range );
Loren Merritt's avatar
Loren Merritt committed
395 396 397 398
            h->mb.mv_max_spel[1] = CLIP_FMV( h->mb.mv_max[1] );
            h->mb.mv_max_spel[1] = X264_MIN( h->mb.mv_max_spel[1], thread_mvy_range*4 );
            h->mb.mv_min_fpel[1] = (h->mb.mv_min_spel[1]>>2) + i_fpel_border;
            h->mb.mv_max_fpel[1] = (h->mb.mv_max_spel[1]>>2) - i_fpel_border;
399
        }
400
#undef CLIP_FMV
401

402
        a->l0.me16x16.cost =
403
        a->l0.i_rd16x16    =
404
        a->l0.i_cost8x8    = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
405 406 407

        for( i = 0; i < 4; i++ )
        {
408 409 410
            a->l0.i_cost4x4[i] =
            a->l0.i_cost8x4[i] =
            a->l0.i_cost4x8[i] = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
411 412
        }

413 414
        a->l0.i_cost16x8   =
        a->l0.i_cost8x16   = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
415 416
        if( h->sh.i_type == SLICE_TYPE_B )
        {
417
            a->l1.me16x16.cost =
418
            a->l1.i_rd16x16    =
419
            a->l1.i_cost8x8    = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
420 421 422

            for( i = 0; i < 4; i++ )
            {
423 424 425 426
                a->l1.i_cost4x4[i] =
                a->l1.i_cost8x4[i] =
                a->l1.i_cost4x8[i] =
                a->i_cost8x8direct[i] = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
427 428
            }

429 430
            a->l1.i_cost16x8   =
            a->l1.i_cost8x16   =
431 432 433 434 435
            a->i_rd16x16bi     =
            a->i_rd16x16direct =
            a->i_rd8x8bi       =
            a->i_rd16x8bi      =
            a->i_rd8x16bi      =
436 437 438 439 440
            a->i_cost16x16bi   =
            a->i_cost16x16direct =
            a->i_cost8x8bi     =
            a->i_cost16x8bi    =
            a->i_cost8x16bi    = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
441
        }
442 443

        /* Fast intra decision */
Loren Merritt's avatar
Loren Merritt committed
444
        if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
445
        {
446
            if(   IS_INTRA( h->mb.i_mb_type_left )
Loren Merritt's avatar
Loren Merritt committed
447 448 449
               || IS_INTRA( h->mb.i_mb_type_top )
               || IS_INTRA( h->mb.i_mb_type_topleft )
               || IS_INTRA( h->mb.i_mb_type_topright )
450
               || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
451
               || (h->mb.i_mb_xy - h->sh.i_first_mb < 3*(h->stat.frame.i_mb_count[I_4x4] + h->stat.frame.i_mb_count[I_8x8] + h->stat.frame.i_mb_count[I_16x16])) )
452 453 454 455 456 457
            { /* intra is likely */ }
            else
            {
                a->b_fast_intra = 1;
            }
        }
458
        h->mb.b_skip_mc = 0;
Laurent Aimar's avatar
Laurent Aimar committed
459 460 461 462 463 464 465 466 467 468 469
    }
}



/*
 * Handle intra mb
 */
/* Max = 4 */
static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
{
Fiona Glaser's avatar
Fiona Glaser committed
470 471 472
    int b_top = i_neighbour & MB_TOP;
    int b_left = i_neighbour & MB_LEFT;
    if( b_top && b_left )
Laurent Aimar's avatar
Laurent Aimar committed
473
    {
Loren Merritt's avatar
Loren Merritt committed
474
        /* top and left available */
Laurent Aimar's avatar
Laurent Aimar committed
475 476 477
        *mode++ = I_PRED_16x16_V;
        *mode++ = I_PRED_16x16_H;
        *mode++ = I_PRED_16x16_DC;
Fiona Glaser's avatar
Fiona Glaser committed
478 479 480 481 482 483 484
        *pi_count = 3;
        if( i_neighbour & MB_TOPLEFT )
        {
            /* top left available*/
            *mode++ = I_PRED_16x16_P;
            *pi_count = 4;
        }
Laurent Aimar's avatar
Laurent Aimar committed
485
    }
Fiona Glaser's avatar
Fiona Glaser committed
486
    else if( b_left )
Laurent Aimar's avatar
Laurent Aimar committed
487 488 489 490 491 492
    {
        /* left available*/
        *mode++ = I_PRED_16x16_DC_LEFT;
        *mode++ = I_PRED_16x16_H;
        *pi_count = 2;
    }
Fiona Glaser's avatar
Fiona Glaser committed
493
    else if( b_top )
Laurent Aimar's avatar
Laurent Aimar committed
494 495 496 497 498 499 500 501
    {
        /* top available*/
        *mode++ = I_PRED_16x16_DC_TOP;
        *mode++ = I_PRED_16x16_V;
        *pi_count = 2;
    }
    else
    {
Loren Merritt's avatar
Loren Merritt committed
502
        /* none available */
Laurent Aimar's avatar
Laurent Aimar committed
503 504 505 506 507 508
        *mode = I_PRED_16x16_DC_128;
        *pi_count = 1;
    }
}

/* Max = 4 */
509
static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
Laurent Aimar's avatar
Laurent Aimar committed
510
{
Fiona Glaser's avatar
Fiona Glaser committed
511 512 513
    int b_top = i_neighbour & MB_TOP;
    int b_left = i_neighbour & MB_LEFT;
    if( b_top && b_left )
Laurent Aimar's avatar
Laurent Aimar committed
514
    {
Loren Merritt's avatar
Loren Merritt committed
515
        /* top and left available */
Laurent Aimar's avatar
Laurent Aimar committed
516 517 518
        *mode++ = I_PRED_CHROMA_V;
        *mode++ = I_PRED_CHROMA_H;
        *mode++ = I_PRED_CHROMA_DC;
Fiona Glaser's avatar
Fiona Glaser committed
519 520 521 522 523 524 525
        *pi_count = 3;
        if( i_neighbour & MB_TOPLEFT )
        {
            /* top left available */
            *mode++ = I_PRED_CHROMA_P;
            *pi_count = 4;
        }
Laurent Aimar's avatar
Laurent Aimar committed
526
    }
Fiona Glaser's avatar
Fiona Glaser committed
527
    else if( b_left )
Laurent Aimar's avatar
Laurent Aimar committed
528 529 530 531 532 533
    {
        /* left available*/
        *mode++ = I_PRED_CHROMA_DC_LEFT;
        *mode++ = I_PRED_CHROMA_H;
        *pi_count = 2;
    }
Fiona Glaser's avatar
Fiona Glaser committed
534
    else if( b_top )
Laurent Aimar's avatar
Laurent Aimar committed
535 536 537 538 539 540 541 542
    {
        /* top available*/
        *mode++ = I_PRED_CHROMA_DC_TOP;
        *mode++ = I_PRED_CHROMA_V;
        *pi_count = 2;
    }
    else
    {
Loren Merritt's avatar
Loren Merritt committed
543
        /* none available */
Laurent Aimar's avatar
Laurent Aimar committed
544 545 546 547 548
        *mode = I_PRED_CHROMA_DC_128;
        *pi_count = 1;
    }
}

549 550 551
/* MAX = 9 */
static void predict_4x4_mode_available( unsigned int i_neighbour,
                                        int *mode, int *pi_count )
Laurent Aimar's avatar
Laurent Aimar committed
552
{
Fiona Glaser's avatar
Fiona Glaser committed
553 554 555
    int b_top = i_neighbour & MB_TOP;
    int b_left = i_neighbour & MB_LEFT;
    if( b_top && b_left )
Laurent Aimar's avatar
Laurent Aimar committed
556
    {
557
        *pi_count = 6;
Laurent Aimar's avatar
Laurent Aimar committed
558 559 560
        *mode++ = I_PRED_4x4_DC;
        *mode++ = I_PRED_4x4_H;
        *mode++ = I_PRED_4x4_V;
561
        *mode++ = I_PRED_4x4_DDL;
562 563 564 565 566 567 568
        if( i_neighbour & MB_TOPLEFT )
        {
            *mode++ = I_PRED_4x4_DDR;
            *mode++ = I_PRED_4x4_VR;
            *mode++ = I_PRED_4x4_HD;
            *pi_count += 3;
        }
569
        *mode++ = I_PRED_4x4_VL;
Laurent Aimar's avatar
Laurent Aimar committed
570 571
        *mode++ = I_PRED_4x4_HU;
    }
Fiona Glaser's avatar
Fiona Glaser committed
572
    else if( b_left )
Laurent Aimar's avatar
Laurent Aimar committed
573 574 575
    {
        *mode++ = I_PRED_4x4_DC_LEFT;
        *mode++ = I_PRED_4x4_H;
576 577
        *mode++ = I_PRED_4x4_HU;
        *pi_count = 3;
Laurent Aimar's avatar
Laurent Aimar committed
578
    }
Fiona Glaser's avatar
Fiona Glaser committed
579
    else if( b_top )
Laurent Aimar's avatar
Laurent Aimar committed
580 581 582
    {
        *mode++ = I_PRED_4x4_DC_TOP;
        *mode++ = I_PRED_4x4_V;
583 584 585
        *mode++ = I_PRED_4x4_DDL;
        *mode++ = I_PRED_4x4_VL;
        *pi_count = 4;
Laurent Aimar's avatar
Laurent Aimar committed
586 587 588 589 590 591 592 593
    }
    else
    {
        *mode++ = I_PRED_4x4_DC_128;
        *pi_count = 1;
    }
}

594 595 596
/* For trellis=2, we need to do this for both sizes of DCT, for trellis=1 we only need to use it on the chosen mode. */
static void inline x264_psy_trellis_init( x264_t *h, int do_both_dct )
{
597 598 599
    ALIGNED_ARRAY_16( int16_t, dct8x8,[4],[8][8] );
    ALIGNED_ARRAY_16( int16_t, dct4x4,[16],[4][4] );
    ALIGNED_16( static uint8_t zero[16*FDEC_STRIDE] ) = {0};
600 601 602 603 604 605 606 607
    int i;

    if( do_both_dct || h->mb.b_transform_8x8 )
    {
        h->dctf.sub16x16_dct8( dct8x8, h->mb.pic.p_fenc[0], zero );
        for( i = 0; i < 4; i++ )
            h->zigzagf.scan_8x8( h->mb.pic.fenc_dct8[i], dct8x8[i] );
    }
Loren Merritt's avatar
Loren Merritt committed
608
    if( do_both_dct || !h->mb.b_transform_8x8 )
609 610 611 612 613 614 615 616 617 618
    {
        h->dctf.sub16x16_dct( dct4x4, h->mb.pic.p_fenc[0], zero );
        for( i = 0; i < 16; i++ )
            h->zigzagf.scan_4x4( h->mb.pic.fenc_dct4[i], dct4x4[i] );
    }
}

/* Pre-calculate fenc satd scores for psy RD, minus DC coefficients */
static inline void x264_mb_cache_fenc_satd( x264_t *h )
{
619
    ALIGNED_16( static uint8_t zero[16] ) = {0};
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
    uint8_t *fenc;
    int x, y, satd_sum = 0, sa8d_sum = 0;
    if( h->param.analyse.i_trellis == 2 && h->mb.i_psy_trellis )
        x264_psy_trellis_init( h, h->param.analyse.b_transform_8x8 );
    if( !h->mb.i_psy_rd )
        return;
    for( y = 0; y < 4; y++ )
        for( x = 0; x < 4; x++ )
        {
            fenc = h->mb.pic.p_fenc[0]+x*4+y*4*FENC_STRIDE;
            h->mb.pic.fenc_satd[y][x] = h->pixf.satd[PIXEL_4x4]( zero, 0, fenc, FENC_STRIDE )
                                      - (h->pixf.sad[PIXEL_4x4]( zero, 0, fenc, FENC_STRIDE )>>1);
            satd_sum += h->mb.pic.fenc_satd[y][x];
        }
    for( y = 0; y < 2; y++ )
        for( x = 0; x < 2; x++ )
        {
            fenc = h->mb.pic.p_fenc[0]+x*8+y*8*FENC_STRIDE;
            h->mb.pic.fenc_sa8d[y][x] = h->pixf.sa8d[PIXEL_8x8]( zero, 0, fenc, FENC_STRIDE )
                                      - (h->pixf.sad[PIXEL_8x8]( zero, 0, fenc, FENC_STRIDE )>>2);
            sa8d_sum += h->mb.pic.fenc_sa8d[y][x];
        }
    h->mb.pic.fenc_satd_sum = satd_sum;
    h->mb.pic.fenc_sa8d_sum = sa8d_sum;
}

646 647 648 649 650
static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
{
    int i;

    int i_max;
651
    int predict_mode[4];
Fiona Glaser's avatar
Fiona Glaser committed
652
    int b_merged_satd = !!h->pixf.intra_mbcmp_x3_8x8c && !h->mb.b_lossless;
653 654 655

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

656
    if( a->i_satd_i8x8chroma < COST_MAX )
657 658 659 660 661 662 663 664 665
        return;

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

    predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
666
    a->i_satd_i8x8chroma = COST_MAX;
Fiona Glaser's avatar
Fiona Glaser committed
667
    if( i_max == 4 && b_merged_satd )
668
    {
669
        int satdu[4], satdv[4];
Fiona Glaser's avatar
Fiona Glaser committed
670 671
        h->pixf.intra_mbcmp_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
        h->pixf.intra_mbcmp_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
672 673 674 675 676 677
        h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
        h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
        satdu[I_PRED_CHROMA_P] =
            h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
        satdv[I_PRED_CHROMA_P] =
            h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
Loren Merritt's avatar
Loren Merritt committed
678

679 680 681 682 683
        for( i=0; i<i_max; i++ )
        {
            int i_mode = predict_mode[i];
            int i_satd = satdu[i_mode] + satdv[i_mode]
                       + a->i_lambda * bs_size_ue(i_mode);
684 685

            a->i_satd_i8x8chroma_dir[i] = i_satd;
686 687 688 689 690 691 692 693 694
            COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
        }
    }
    else
    {
        for( i=0; i<i_max; i++ )
        {
            int i_satd;
            int i_mode = predict_mode[i];
695

696
            /* we do the prediction */
697 698 699 700 701 702 703
            if( h->mb.b_lossless )
                x264_predict_lossless_8x8_chroma( h, i_mode );
            else
            {
                h->predict_8x8c[i_mode]( p_dstc[0] );
                h->predict_8x8c[i_mode]( p_dstc[1] );
            }
704

705 706 707 708 709 710
            /* we calculate the cost */
            i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
                                               p_srcc[0], FENC_STRIDE ) +
                     h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
                                               p_srcc[1], FENC_STRIDE ) +
                     a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
711

712
            a->i_satd_i8x8chroma_dir[i] = i_satd;
713 714
            COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
        }
715 716 717 718 719
    }

    h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
}

720
static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
Laurent Aimar's avatar
Laurent Aimar committed
721 722 723 724 725 726 727 728
{
    const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
    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];
729
    int b_merged_satd = !!h->pixf.intra_mbcmp_x3_16x16 && !h->mb.b_lossless;
730

Laurent Aimar's avatar
Laurent Aimar committed
731 732 733 734
    /*---------------- Try all mode and calculate their score ---------------*/

    /* 16x16 prediction selection */
    predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
735 736 737

    if( b_merged_satd && i_max == 4 )
    {
738
        h->pixf.intra_mbcmp_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
739 740 741 742 743 744 745 746 747 748
        h->predict_16x16[I_PRED_16x16_P]( p_dst );
        a->i_satd_i16x16_dir[I_PRED_16x16_P] =
            h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
        for( i=0; i<4; i++ )
        {
            int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
            COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
        }
    }
    else
Laurent Aimar's avatar
Laurent Aimar committed
749
    {
750 751 752 753
        for( i = 0; i < i_max; i++ )
        {
            int i_satd;
            int i_mode = predict_mode[i];
754 755 756 757 758

            if( h->mb.b_lossless )
                x264_predict_lossless_16x16( h, i_mode );
            else
                h->predict_16x16[i_mode]( p_dst );
759 760 761 762 763 764

            i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
                    a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
            COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
            a->i_satd_i16x16_dir[i_mode] = i_satd;
        }
Laurent Aimar's avatar
Laurent Aimar committed
765 766
    }

767 768 769 770 771
    if( h->sh.i_type == SLICE_TYPE_B )
        /* cavlc mb type prefix */
        a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
    if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
        return;
772

773 774
    /* 8x8 prediction selection */
    if( flags & X264_ANALYSE_I8x8 )
Laurent Aimar's avatar
Laurent Aimar committed
775
    {
776
        ALIGNED_ARRAY_16( uint8_t, edge,[33] );
777
        x264_pixel_cmp_t sa8d = (h->pixf.mbcmp[0] == h->pixf.satd[0]) ? h->pixf.sa8d[PIXEL_8x8] : h->pixf.mbcmp[PIXEL_8x8];
778
        int i_satd_thresh = a->i_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
779
        int i_cost = 0;
780
        h->mb.i_cbp_luma = 0;
Fiona Glaser's avatar
Fiona Glaser committed
781
        b_merged_satd = h->pixf.intra_mbcmp_x3_8x8 && !h->mb.b_lossless;
Laurent Aimar's avatar
Laurent Aimar committed
782

783 784 785
        // FIXME some bias like in i4x4?
        if( h->sh.i_type == SLICE_TYPE_B )
            i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
786

787 788 789 790 791 792 793 794
        for( idx = 0;; idx++ )
        {
            int x = idx&1;
            int y = idx>>1;
            uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
            uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
            int i_best = COST_MAX;
            int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
795

796
            predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
797
            h->predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
798 799 800

            if( b_merged_satd && i_max == 9 )
            {
801
                int satd[9];
Fiona Glaser's avatar
Fiona Glaser committed
802
                h->pixf.intra_mbcmp_x3_8x8( p_src_by, edge, satd );
803
                satd[i_pred_mode] -= 3 * a->i_lambda;
804 805 806 807 808 809 810 811 812 813 814
                for( i=2; i>=0; i-- )
                {
                    int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
                    COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
                }
                i = 3;
            }
            else
                i = 0;

            for( ; i<i_max; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
815
            {
816 817
                int i_satd;
                int i_mode = predict_mode[i];
Laurent Aimar's avatar
Laurent Aimar committed
818

819 820 821 822
                if( h->mb.b_lossless )
                    x264_predict_lossless_8x8( h, p_dst_by, idx, i_mode, edge );
                else
                    h->predict_8x8[i_mode]( p_dst_by, edge );
Laurent Aimar's avatar
Laurent Aimar committed
823

824 825
                i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
                       + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
Laurent Aimar's avatar
Laurent Aimar committed
826

827 828
                COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
                a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
Laurent Aimar's avatar
Laurent Aimar committed
829
            }
830 831 832 833
            i_cost += i_best;

            if( idx == 3 || i_cost > i_satd_thresh )
                break;
Laurent Aimar's avatar
Laurent Aimar committed
834

835
            /* we need to encode this block now (for next ones) */
836
            h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
837
            x264_mb_encode_i8x8( h, idx, a->i_qp );
Laurent Aimar's avatar
Laurent Aimar committed
838

839
            x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
840 841
        }

842
        if( idx == 3 )
843
        {
844
            a->i_satd_i8x8 = i_cost;
845 846 847
            if( h->mb.i_skip_intra )
            {
                h->mc.copy[PIXEL_16x16]( h->mb.pic.i8x8_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
848 849 850 851 852
                h->mb.pic.i8x8_nnz_buf[0] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[ 0]];
                h->mb.pic.i8x8_nnz_buf[1] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[ 2]];
                h->mb.pic.i8x8_nnz_buf[2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[ 8]];
                h->mb.pic.i8x8_nnz_buf[3] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[10]];
                h->mb.pic.i8x8_cbp = h->mb.i_cbp_luma;
853 854 855 856
                if( h->mb.i_skip_intra == 2 )
                    h->mc.memcpy_aligned( h->mb.pic.i8x8_dct_buf, h->dct.luma8x8, sizeof(h->mb.pic.i8x8_dct_buf) );
            }
        }
857 858
        else
        {
859
            static const uint16_t cost_div_fix8[3] = {1024,512,341};
860
            a->i_satd_i8x8 = COST_MAX;
861
            i_cost = (i_cost * cost_div_fix8[idx]) >> 8;
Laurent Aimar's avatar
Laurent Aimar committed
862
        }
863
        if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+!!a->i_mbrd)/4 )
864
            return;
Laurent Aimar's avatar
Laurent Aimar committed
865
    }
866

867 868
    /* 4x4 prediction selection */
    if( flags & X264_ANALYSE_I4x4 )
869
    {
870 871
        int i_cost;
        int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
872
        h->mb.i_cbp_luma = 0;
Fiona Glaser's avatar
Fiona Glaser committed
873
        b_merged_satd = h->pixf.intra_mbcmp_x3_4x4 && !h->mb.b_lossless;
874
        if( a->i_mbrd )
875 876 877 878 879 880 881
            i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;

        i_cost = a->i_lambda * 24;    /* from JVT (SATD0) */
        if( h->sh.i_type == SLICE_TYPE_B )
            i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];

        for( idx = 0;; idx++ )
882
        {
883 884
            uint8_t *p_src_by = p_src + block_idx_xy_fenc[idx];
            uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
885 886
            int i_best = COST_MAX;
            int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
887

888
            predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
889

890 891 892
            if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
                /* emulate missing topright samples */
                *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
893

894 895
            if( b_merged_satd && i_max >= 6 )
            {
896
                int satd[9];
897
                h->pixf.intra_mbcmp_x3_4x4( p_src_by, p_dst_by, satd );
898
                satd[i_pred_mode] -= 3 * a->i_lambda;
899 900 901 902 903 904 905 906 907
                for( i=2; i>=0; i-- )
                    COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
                                 a->i_predict4x4[idx], i );
                i = 3;
            }
            else
                i = 0;

            for( ; i<i_max; i++ )
908
            {
909
                int i_satd;
910
                int i_mode = predict_mode[i];
911 912 913 914
                if( h->mb.b_lossless )
                    x264_predict_lossless_4x4( h, p_dst_by, idx, i_mode );
                else
                    h->predict_4x4[i_mode]( p_dst_by );
915

916
                i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
917
                                                   p_src_by, FENC_STRIDE )
918
                       + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
919

920
                COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
921
            }
922
            i_cost += i_best;
923

924 925
            if( i_cost > i_satd_thresh || idx == 15 )
                break;