analyse.c 109 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
#include <limits.h>
28
#ifndef _MSC_VER
Loren Merritt's avatar
Loren Merritt committed
29
#include <unistd.h>
30
#endif
Laurent Aimar's avatar
Laurent Aimar committed
31

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

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

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

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


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

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

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

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

105 106
    int i_satd_pcm;

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

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

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

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

} x264_mb_analysis_t;

137
/* lambda = pow(2,qp/6-2) */
138
const int x264_lambda_tab[52] = {
Laurent Aimar's avatar
Laurent Aimar committed
139 140 141 142 143 144 145 146 147
   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
148
/* lambda2 = pow(lambda,2) * .9 * 256 */
149
const int x264_lambda2_tab[52] = {
Fiona Glaser's avatar
Fiona Glaser committed
150 151 152 153 154 155 156
    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 */
157 158
};

159
/* TODO: calculate CABAC costs */
Loren Merritt's avatar
Loren Merritt committed
160
static const int i_mb_b_cost_table[X264_MBTYPE_MAX] = {
161
    9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
162
};
163
static const int i_mb_b16x8_cost_table[17] = {
164
    0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
165 166 167 168
};
static const int i_sub_mb_b_cost_table[13] = {
    7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
};
169 170 171
static const int i_sub_mb_p_cost_table[4] = {
    5, 3, 3, 1
};
172

173 174
static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );

175 176 177 178
/* Indexed by lambda instead of qp because, due to rounding,
 * some quantizers share lambdas.  This saves memory. */
uint16_t *x264_cost_mv_fpel[92][4];
uint16_t x264_cost_ref[92][3][33];
179

180 181 182
/* 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 )
{
183
    static int16_t *p_cost_mv[92];
184
    int i, j;
185

186
    if( !p_cost_mv[a->i_lambda] )
187
    {
188
        x264_emms();
189
        /* could be faster, but isn't called many times */
190
        /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
191 192
        p_cost_mv[a->i_lambda] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) );
        p_cost_mv[a->i_lambda] += 2*4*2048;
193
        for( i = 0; i <= 2*4*2048; i++ )
194
        {
195 196
            p_cost_mv[a->i_lambda][-i] =
            p_cost_mv[a->i_lambda][i]  = a->i_lambda * (log2f(i+1)*2 + 0.718f + !!i) + .5f;
197
        }
198 199
        for( i = 0; i < 3; i++ )
            for( j = 0; j < 33; j++ )
200
                x264_cost_ref[a->i_lambda][i][j] = i ? a->i_lambda * bs_size_te( i, j ) : 0;
201
    }
202 203 204
    a->p_cost_mv = p_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)];
205 206

    /* FIXME is this useful for all me methods? */
207
    if( h->param.analyse.i_me_method >= X264_ME_ESA && !x264_cost_mv_fpel[a->i_lambda][0] )
208 209 210
    {
        for( j=0; j<4; j++ )
        {
211 212
            x264_cost_mv_fpel[a->i_lambda][j] = x264_malloc( (4*2048 + 1) * sizeof(int16_t) );
            x264_cost_mv_fpel[a->i_lambda][j] += 2*2048;
213
            for( i = -2*2048; i < 2*2048; i++ )
214
                x264_cost_mv_fpel[a->i_lambda][j][i] = p_cost_mv[a->i_lambda][i*4+j];
215 216
        }
    }
217 218
}

Laurent Aimar's avatar
Laurent Aimar committed
219 220
static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
{
221 222 223 224
    int i = h->param.analyse.i_subpel_refine - (h->sh.i_type == SLICE_TYPE_B);
    /* mbrd == 1 -> RD mode decision */
    /* mbrd == 2 -> RD refinement */
    a->i_mbrd = (i>=6) + (i>=8);
Laurent Aimar's avatar
Laurent Aimar committed
225
    /* conduct the analysis using this lamda and QP */
226
    a->i_qp = h->mb.i_qp = i_qp;
227
    h->mb.i_chroma_qp = h->chroma_qp_table[i_qp];
228 229
    a->i_lambda = x264_lambda_tab[i_qp];
    a->i_lambda2 = x264_lambda2_tab[i_qp];
230
    h->mb.i_me_method = h->param.analyse.i_me_method;
Loren Merritt's avatar
Loren Merritt committed
231 232 233
    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;
234
    h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->i_mbrd;
235
    h->mb.b_transform_8x8 = 0;
236
    h->mb.b_noise_reduction = 0;
237

Laurent Aimar's avatar
Laurent Aimar committed
238
    /* I: Intra part */
239 240 241 242
    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
243

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

247
    a->b_fast_intra = 0;
248 249
    h->mb.i_skip_intra =
        h->mb.b_lossless ? 0 :
250
        a->i_mbrd ? 2 :
251
        !h->param.analyse.i_trellis && !h->param.analyse.i_noise_reduction;
252

Laurent Aimar's avatar
Laurent Aimar committed
253 254 255
    /* II: Inter part P/B frame */
    if( h->sh.i_type != SLICE_TYPE_I )
    {
Loren Merritt's avatar
Loren Merritt committed
256 257
        int i, j;
        int i_fmv_range = 4 * h->param.analyse.i_mv_range;
258 259 260 261
        // limit motion search to a slightly smaller range than the theoretical limit,
        // since the search may go a few iterations past its given range
        int i_fpel_border = 5; // umh unconditional radius
        int i_spel_border = 8; // 1.5 for subpel_satd, 1.5 for subpel_rd, 2 for bime, round up
Laurent Aimar's avatar
Laurent Aimar committed
262

263
        /* Calculate max allowed MV range */
264
#define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range-1 )
265 266
        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
267 268 269 270
        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;
271 272
        if( h->mb.i_mb_x == 0)
        {
273 274
            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
275 276 277 278 279 280 281 282 283 284 285 286
            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++ )
                    {
287
                        x264_frame_cond_wait( fref[j], thresh );
Loren Merritt's avatar
Loren Merritt committed
288 289 290 291 292 293 294 295 296
                        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;
            }

297 298
            h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
            h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
299
            h->mb.mv_min_spel[1] = x264_clip3( h->mb.mv_min[1], X264_MAX(4*(-512+i_spel_border), -i_fmv_range), i_fmv_range );
Loren Merritt's avatar
Loren Merritt committed
300 301 302 303
            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;
304
        }
305
#undef CLIP_FMV
306

307
        a->l0.me16x16.cost =
308
        a->l0.i_rd16x16    =
309
        a->l0.i_cost8x8    = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
310 311 312

        for( i = 0; i < 4; i++ )
        {
313 314 315
            a->l0.i_cost4x4[i] =
            a->l0.i_cost8x4[i] =
            a->l0.i_cost4x8[i] = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
316 317
        }

318 319
        a->l0.i_cost16x8   =
        a->l0.i_cost8x16   = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
320 321
        if( h->sh.i_type == SLICE_TYPE_B )
        {
322
            a->l1.me16x16.cost =
323
            a->l1.i_rd16x16    =
324
            a->l1.i_cost8x8    = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
325 326 327

            for( i = 0; i < 4; i++ )
            {
328 329 330 331
                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
332 333
            }

334 335
            a->l1.i_cost16x8   =
            a->l1.i_cost8x16   =
336 337 338 339 340
            a->i_rd16x16bi     =
            a->i_rd16x16direct =
            a->i_rd8x8bi       =
            a->i_rd16x8bi      =
            a->i_rd8x16bi      =
341 342 343 344 345
            a->i_cost16x16bi   =
            a->i_cost16x16direct =
            a->i_cost8x8bi     =
            a->i_cost16x8bi    =
            a->i_cost8x16bi    = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
346
        }
347 348

        /* Fast intra decision */
Loren Merritt's avatar
Loren Merritt committed
349
        if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
350
        {
351
            if(   IS_INTRA( h->mb.i_mb_type_left )
Loren Merritt's avatar
Loren Merritt committed
352 353 354
               || IS_INTRA( h->mb.i_mb_type_top )
               || IS_INTRA( h->mb.i_mb_type_topleft )
               || IS_INTRA( h->mb.i_mb_type_topright )
355
               || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
356
               || (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])) )
357 358 359 360 361 362
            { /* intra is likely */ }
            else
            {
                a->b_fast_intra = 1;
            }
        }
363
        h->mb.b_skip_mc = 0;
Laurent Aimar's avatar
Laurent Aimar committed
364 365 366 367 368 369 370 371 372 373 374
    }
}



/*
 * Handle intra mb
 */
/* Max = 4 */
static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
{
Loren Merritt's avatar
Loren Merritt committed
375
    if( i_neighbour & MB_TOPLEFT )
Laurent Aimar's avatar
Laurent Aimar committed
376
    {
Loren Merritt's avatar
Loren Merritt committed
377
        /* top and left available */
Laurent Aimar's avatar
Laurent Aimar committed
378 379 380 381 382 383
        *mode++ = I_PRED_16x16_V;
        *mode++ = I_PRED_16x16_H;
        *mode++ = I_PRED_16x16_DC;
        *mode++ = I_PRED_16x16_P;
        *pi_count = 4;
    }
Loren Merritt's avatar
Loren Merritt committed
384
    else if( i_neighbour & MB_LEFT )
Laurent Aimar's avatar
Laurent Aimar committed
385 386 387 388 389 390
    {
        /* left available*/
        *mode++ = I_PRED_16x16_DC_LEFT;
        *mode++ = I_PRED_16x16_H;
        *pi_count = 2;
    }
Loren Merritt's avatar
Loren Merritt committed
391
    else if( i_neighbour & MB_TOP )
Laurent Aimar's avatar
Laurent Aimar committed
392 393 394 395 396 397 398 399
    {
        /* top available*/
        *mode++ = I_PRED_16x16_DC_TOP;
        *mode++ = I_PRED_16x16_V;
        *pi_count = 2;
    }
    else
    {
Loren Merritt's avatar
Loren Merritt committed
400
        /* none available */
Laurent Aimar's avatar
Laurent Aimar committed
401 402 403 404 405 406
        *mode = I_PRED_16x16_DC_128;
        *pi_count = 1;
    }
}

/* Max = 4 */
407
static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
Laurent Aimar's avatar
Laurent Aimar committed
408
{
Loren Merritt's avatar
Loren Merritt committed
409
    if( i_neighbour & MB_TOPLEFT )
Laurent Aimar's avatar
Laurent Aimar committed
410
    {
Loren Merritt's avatar
Loren Merritt committed
411
        /* top and left available */
Laurent Aimar's avatar
Laurent Aimar committed
412 413 414 415 416 417
        *mode++ = I_PRED_CHROMA_V;
        *mode++ = I_PRED_CHROMA_H;
        *mode++ = I_PRED_CHROMA_DC;
        *mode++ = I_PRED_CHROMA_P;
        *pi_count = 4;
    }
Loren Merritt's avatar
Loren Merritt committed
418
    else if( i_neighbour & MB_LEFT )
Laurent Aimar's avatar
Laurent Aimar committed
419 420 421 422 423 424
    {
        /* left available*/
        *mode++ = I_PRED_CHROMA_DC_LEFT;
        *mode++ = I_PRED_CHROMA_H;
        *pi_count = 2;
    }
Loren Merritt's avatar
Loren Merritt committed
425
    else if( i_neighbour & MB_TOP )
Laurent Aimar's avatar
Laurent Aimar committed
426 427 428 429 430 431 432 433
    {
        /* top available*/
        *mode++ = I_PRED_CHROMA_DC_TOP;
        *mode++ = I_PRED_CHROMA_V;
        *pi_count = 2;
    }
    else
    {
Loren Merritt's avatar
Loren Merritt committed
434
        /* none available */
Laurent Aimar's avatar
Laurent Aimar committed
435 436 437 438 439
        *mode = I_PRED_CHROMA_DC_128;
        *pi_count = 1;
    }
}

440 441 442
/* MAX = 9 */
static void predict_4x4_mode_available( unsigned int i_neighbour,
                                        int *mode, int *pi_count )
Laurent Aimar's avatar
Laurent Aimar committed
443
{
444 445
    int b_l = i_neighbour & MB_LEFT;
    int b_t = i_neighbour & MB_TOP;
Laurent Aimar's avatar
Laurent Aimar committed
446

447
    if( b_l && b_t )
Laurent Aimar's avatar
Laurent Aimar committed
448
    {
449
        *pi_count = 6;
Laurent Aimar's avatar
Laurent Aimar committed
450 451 452
        *mode++ = I_PRED_4x4_DC;
        *mode++ = I_PRED_4x4_H;
        *mode++ = I_PRED_4x4_V;
453
        *mode++ = I_PRED_4x4_DDL;
454 455 456 457 458 459 460
        if( i_neighbour & MB_TOPLEFT )
        {
            *mode++ = I_PRED_4x4_DDR;
            *mode++ = I_PRED_4x4_VR;
            *mode++ = I_PRED_4x4_HD;
            *pi_count += 3;
        }
461
        *mode++ = I_PRED_4x4_VL;
Laurent Aimar's avatar
Laurent Aimar committed
462 463
        *mode++ = I_PRED_4x4_HU;
    }
464
    else if( b_l )
Laurent Aimar's avatar
Laurent Aimar committed
465 466 467
    {
        *mode++ = I_PRED_4x4_DC_LEFT;
        *mode++ = I_PRED_4x4_H;
468 469
        *mode++ = I_PRED_4x4_HU;
        *pi_count = 3;
Laurent Aimar's avatar
Laurent Aimar committed
470
    }
471
    else if( b_t )
Laurent Aimar's avatar
Laurent Aimar committed
472 473 474
    {
        *mode++ = I_PRED_4x4_DC_TOP;
        *mode++ = I_PRED_4x4_V;
475 476 477
        *mode++ = I_PRED_4x4_DDL;
        *mode++ = I_PRED_4x4_VL;
        *pi_count = 4;
Laurent Aimar's avatar
Laurent Aimar committed
478 479 480 481 482 483 484 485
    }
    else
    {
        *mode++ = I_PRED_4x4_DC_128;
        *pi_count = 1;
    }
}

486 487 488 489 490
/* 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 )
{
    DECLARE_ALIGNED_16( int16_t dct8x8[4][8][8] );
    DECLARE_ALIGNED_16( int16_t dct4x4[16][4][4] );
491
    DECLARE_ALIGNED_16( static uint8_t zero[16*FDEC_STRIDE] ) = {0};
492 493 494 495 496 497 498 499
    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
500
    if( do_both_dct || !h->mb.b_transform_8x8 )
501 502 503 504 505 506 507 508 509 510
    {
        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 )
{
511
    DECLARE_ALIGNED_16( static uint8_t zero[16] ) = {0};
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    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;
}

538 539 540 541 542
static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
{
    int i;

    int i_max;
543
    int predict_mode[4];
Fiona Glaser's avatar
Fiona Glaser committed
544
    int b_merged_satd = !!h->pixf.intra_mbcmp_x3_8x8c && !h->mb.b_lossless;
545 546 547

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

548
    if( a->i_satd_i8x8chroma < COST_MAX )
549 550 551 552 553 554 555 556 557
        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 );
558
    a->i_satd_i8x8chroma = COST_MAX;
Fiona Glaser's avatar
Fiona Glaser committed
559
    if( i_max == 4 && b_merged_satd )
560
    {
561
        int satdu[4], satdv[4];
Fiona Glaser's avatar
Fiona Glaser committed
562 563
        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 );
564 565 566 567 568 569
        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
570

571 572 573 574 575
        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);
576 577

            a->i_satd_i8x8chroma_dir[i] = i_satd;
578 579 580 581 582 583 584 585 586
            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];
587

588
            /* we do the prediction */
589 590 591 592 593 594 595
            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] );
            }
596

597 598 599 600 601 602
            /* 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] );
603

604
            a->i_satd_i8x8chroma_dir[i] = i_satd;
605 606
            COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
        }
607 608 609 610 611
    }

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

612
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
613 614 615 616 617 618 619 620
{
    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];
621
    int b_merged_satd = !!h->pixf.intra_mbcmp_x3_16x16 && !h->mb.b_lossless;
622

Laurent Aimar's avatar
Laurent Aimar committed
623 624 625 626
    /*---------------- Try all mode and calculate their score ---------------*/

    /* 16x16 prediction selection */
    predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
627 628 629

    if( b_merged_satd && i_max == 4 )
    {
630
        h->pixf.intra_mbcmp_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
631 632 633 634 635 636 637 638 639 640
        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
641
    {
642 643 644 645
        for( i = 0; i < i_max; i++ )
        {
            int i_satd;
            int i_mode = predict_mode[i];
646 647 648 649 650

            if( h->mb.b_lossless )
                x264_predict_lossless_16x16( h, i_mode );
            else
                h->predict_16x16[i_mode]( p_dst );
651 652 653 654 655 656

            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
657 658
    }

659 660 661 662 663
    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;
664

665 666
    /* 8x8 prediction selection */
    if( flags & X264_ANALYSE_I8x8 )
Laurent Aimar's avatar
Laurent Aimar committed
667
    {
Loren Merritt's avatar
Loren Merritt committed
668
        DECLARE_ALIGNED_16( uint8_t edge[33] );
669
        x264_pixel_cmp_t sa8d = (h->pixf.mbcmp[0] == h->pixf.satd[0]) ? h->pixf.sa8d[PIXEL_8x8] : h->pixf.mbcmp[PIXEL_8x8];
670
        int i_satd_thresh = a->i_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
671
        int i_cost = 0;
672
        h->mb.i_cbp_luma = 0;
Fiona Glaser's avatar
Fiona Glaser committed
673
        b_merged_satd = h->pixf.intra_mbcmp_x3_8x8 && !h->mb.b_lossless;
Laurent Aimar's avatar
Laurent Aimar committed
674

675 676 677
        // 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];
678

679 680 681 682 683 684 685 686
        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 );
687

688
            predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
689
            h->predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
690 691 692

            if( b_merged_satd && i_max == 9 )
            {
693
                int satd[9];
Fiona Glaser's avatar
Fiona Glaser committed
694
                h->pixf.intra_mbcmp_x3_8x8( p_src_by, edge, satd );
695
                satd[i_pred_mode] -= 3 * a->i_lambda;
696 697 698 699 700 701 702 703 704 705 706
                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
707
            {
708 709
                int i_satd;
                int i_mode = predict_mode[i];
Laurent Aimar's avatar
Laurent Aimar committed
710

711 712 713 714
                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
715

716 717
                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
718

719 720
                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
721
            }
722 723 724 725
            i_cost += i_best;

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

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

731
            x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
732 733
        }

734
        if( idx == 3 )
735
        {
736
            a->i_satd_i8x8 = i_cost;
737 738 739
            if( h->mb.i_skip_intra )
            {
                h->mc.copy[PIXEL_16x16]( h->mb.pic.i8x8_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
740 741 742 743 744
                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;
745 746 747 748
                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) );
            }
        }
749 750
        else
        {
751
            static const uint16_t cost_div_fix8[3] = {1024,512,341};
752
            a->i_satd_i8x8 = COST_MAX;
753
            i_cost = (i_cost * cost_div_fix8[idx]) >> 8;
Laurent Aimar's avatar
Laurent Aimar committed
754
        }
755
        if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+!!a->i_mbrd)/4 )
756
            return;
Laurent Aimar's avatar
Laurent Aimar committed
757
    }
758

759 760
    /* 4x4 prediction selection */
    if( flags & X264_ANALYSE_I4x4 )
761
    {
762 763
        int i_cost;
        int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
764
        h->mb.i_cbp_luma = 0;
Fiona Glaser's avatar
Fiona Glaser committed
765
        b_merged_satd = h->pixf.intra_mbcmp_x3_4x4 && !h->mb.b_lossless;
766
        if( a->i_mbrd )
767 768 769 770 771 772 773
            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++ )
774
        {
775 776
            uint8_t *p_src_by = p_src + block_idx_xy_fenc[idx];
            uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
777 778
            int i_best = COST_MAX;
            int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
779

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

782 783 784
            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;
785

786 787
            if( b_merged_satd && i_max >= 6 )
            {
788
                int satd[9];
789
                h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
790
                satd[i_pred_mode] -= 3 * a->i_lambda;
791 792 793 794 795 796 797 798 799
                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++ )
800
            {
801
                int i_satd;
802
                int i_mode = predict_mode[i];
803 804 805 806
                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 );
807

808
                i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
809
                                                   p_src_by, FENC_STRIDE )
810
                       + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
811

812
                COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
813
            }
814
            i_cost += i_best;
815

816 817
            if( i_cost > i_satd_thresh || idx == 15 )
                break;
818

819 820 821
            /* we need to encode this block now (for next ones) */
            h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
            x264_mb_encode_i4x4( h, idx, a->i_qp );
Laurent Aimar's avatar
Laurent Aimar committed
822

823
            h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
824
        }
825
        if( idx == 15 )
826
        {
827
            a->i_satd_i4x4 = i_cost;
828 829 830
            if( h->mb.i_skip_intra )
            {
                h->mc.copy[PIXEL_16x16]( h->mb.pic.i4x4_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
831 832 833 834 835
                h->mb.pic.i4x4_nnz_buf[0] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[ 0]];
                h->mb.pic.i4x4_nnz_buf[1] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[ 2]];
                h->mb.pic.i4x4_nnz_buf[2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[ 8]];
                h->mb.pic.i4x4_nnz_buf[3] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[10]];
                h->mb.pic.i4x4_cbp = h->mb.i_cbp_luma;
836
                if( h->mb.i_skip_intra == 2 )
837
                    h->mc.memcpy_aligned( h->mb.pic.i4x4_dct_buf, h->dct.luma4x4, sizeof(h->mb.pic.i4x4_dct_buf) );
838 839
            }
        }
840
        else
841
            a->i_satd_i4x4 = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
842 843 844
    }
}

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
{
    if( a->i_satd_i16x16 <= i_satd_thresh )
    {
        h->mb.i_type = I_16x16;
        x264_analyse_update_cache( h, a );
        a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
    }
    else
        a->i_satd_i16x16 = COST_MAX;

    if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
    {
        h->mb.i_type = I_4x4;
        x264_analyse_update_cache( h, a );
        a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
    }
    else
        a->i_satd_i4x4 = COST_MAX;

    if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
    {
        h->mb.i_type = I_8x8;
        x264_analyse_update_cache( h, a );
        a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
870
        a->i_cbp_i8x8_luma = h->mb.i_cbp_luma;
871 872 873 874 875
    }
    else
        a->i_satd_i8x8 = COST_MAX;
}

876 877 878 879
static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
{
    uint8_t  *p_dst = h->mb.pic.p_fdec[0];

880
    int i, j, idx, x, y;
881 882
    int i_max, i_mode, i_thresh;
    uint64_t i_satd, i_best;
883
    int predict_mode[9];
884
    h->mb.i_skip_intra = 0;
885 886 887 888

    if( h->mb.i_type == I_16x16 )
    {
        int old_pred_mode = a->i_predict16x16;
889
        i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
890
        i_best = a->i_satd_i16x16;
891 892 893
        predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
        for( i = 0; i < i_max; i++ )
        {
894 895
            int i_mode = predict_mode[i];
            if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
896
                continue;
897 898 899
            h->mb.i_intra16x16_pred_mode = i_mode;
            i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
            COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
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

    /* RD selection for chroma prediction */
    predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
    if( i_max > 1 )
    {
        i_thresh = a->i_satd_i8x8chroma * 5/4;

        for( i = j = 0; i < i_max; i++ )
            if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
                predict_mode[i] != a->i_predict8x8chroma )
            {
                predict_mode[j++] = predict_mode[i];
            }
        i_max = j;

        if( i_max > 0 )
        {
            int i_cbp_chroma_best = h->mb.i_cbp_chroma;
            int i_chroma_lambda = x264_lambda2_tab[h->mb.i_chroma_qp];
            /* the previous thing encoded was x264_intra_rd(), so the pixels and
             * coefs for the current chroma mode are still around, so we only
             * have to recount the bits. */
            i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
            for( i = 0; i < i_max; i++ )
            {
                i_mode = predict_mode[i];
                if( h->mb.b_lossless )
                    x264_predict_lossless_8x8_chroma( h, i_mode );
                else
                {
                    h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
                    h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
                }
                /* if we've already found a mode that needs no residual, then
                 * probably any mode with a residual will be worse.
                 * so avoid dct on the remaining modes to improve speed. */
                i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
                COPY3_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode, i_cbp_chroma_best, h->mb.i_cbp_chroma );
            }
            h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
            h->mb.i_cbp_chroma = i_cbp_chroma_best;
        }
    }

    if( h->mb.i_type == I_4x4 )
947
    {
Loren Merritt's avatar
Loren Merritt committed
948 949
        uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
        int i_nnz = 0;
950 951
        for( idx = 0; idx < 16; idx++ )
        {
952
            uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
953
            i_best = COST_MAX64;
954 955 956 957 958 959 960 961 962 963

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

            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;

            for( i = 0; i < i_max; i++ )
            {
                i_mode = predict_mode[i];
964 965 966 967
                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 );
968
                i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
969

970
                if( i_best > i_satd<