analyse.c 99.4 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1 2 3
/*****************************************************************************
 * analyse.c: h264 encoder library
 *****************************************************************************
4
 * Copyright (C) 2003 x264 project
Laurent Aimar's avatar
Laurent Aimar committed
5 6 7
 * $Id: analyse.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Loren Merritt <lorenm@u.washington.edu>
Laurent Aimar's avatar
Laurent Aimar committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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 <math.h>
26
#include <limits.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"
Laurent Aimar's avatar
Laurent Aimar committed
32 33
#include "macroblock.h"
#include "me.h"
34
#include "ratecontrol.h"
35 36
#include "analyse.h"
#include "rdo.c"
Laurent Aimar's avatar
Laurent Aimar committed
37 38 39 40 41

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

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

    /* 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;
77
    int i_lambda2;
Laurent Aimar's avatar
Laurent Aimar committed
78
    int i_qp;
79
    int16_t *p_cost_mv;
80
    int b_mbrd;
Laurent Aimar's avatar
Laurent Aimar committed
81 82 83


    /* I: Intra part */
84 85
    /* Take some shortcuts in intra search if intra is deemed unlikely */
    int b_fast_intra;
86
    int b_try_pskip;
87

88
    /* Luma part */
89 90
    int i_satd_i16x16;
    int i_satd_i16x16_dir[7];
Laurent Aimar's avatar
Laurent Aimar committed
91 92
    int i_predict16x16;

93 94 95
    int i_satd_i8x8;
    int i_satd_i8x8_dir[12][4];
    int i_predict8x8[4];
96

97 98
    int i_satd_i4x4;
    int i_predict4x4[16];
Laurent Aimar's avatar
Laurent Aimar committed
99 100

    /* Chroma part */
101
    int i_satd_i8x8chroma;
102
    int i_satd_i8x8chroma_dir[4];
103
    int i_predict8x8chroma;
Laurent Aimar's avatar
Laurent Aimar committed
104 105 106 107 108 109

    /* 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) */
110 111 112
    int i_cost16x16direct;
    int i_cost8x8bi;
    int i_cost8x8direct[4];
113 114
    int i_cost16x8bi;
    int i_cost8x16bi;
115 116 117 118 119
    int i_rd16x16bi;
    int i_rd16x16direct;
    int i_rd16x8bi;
    int i_rd8x16bi;
    int i_rd8x8bi;
120 121 122 123 124

    int i_mb_partition16x8[2]; /* mb_partition_e */
    int i_mb_partition8x16[2];
    int i_mb_type16x8; /* mb_class_e */
    int i_mb_type8x16;
125 126

    int b_direct_available;
Laurent Aimar's avatar
Laurent Aimar committed
127 128 129

} x264_mb_analysis_t;

130
/* lambda = pow(2,qp/6-2) */
Laurent Aimar's avatar
Laurent Aimar committed
131 132 133 134 135 136 137 138 139 140
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 */
};

Fiona Glaser's avatar
Fiona Glaser committed
141
/* lambda2 = pow(lambda,2) * .9 * 256 */
142
static const int i_qp0_cost2_table[52] = {
Fiona Glaser's avatar
Fiona Glaser committed
143 144 145 146 147 148 149
    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 */
150 151
};

152
/* TODO: calculate CABAC costs */
Loren Merritt's avatar
Loren Merritt committed
153
static const int i_mb_b_cost_table[X264_MBTYPE_MAX] = {
154
    9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
155
};
156
static const int i_mb_b16x8_cost_table[17] = {
157
    0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
158 159 160 161
};
static const int i_sub_mb_b_cost_table[13] = {
    7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
};
162 163 164
static const int i_sub_mb_p_cost_table[4] = {
    5, 3, 3, 1
};
165

166 167
static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );

168 169
uint16_t *x264_cost_mv_fpel[52][4];

170 171 172 173
/* 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 )
{
    static int16_t *p_cost_mv[52];
174
    int i, j;
175 176 177 178

    if( !p_cost_mv[a->i_qp] )
    {
        /* could be faster, but isn't called many times */
179
        /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
180 181 182
        p_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) );
        p_cost_mv[a->i_qp] += 2*4*2048;
        for( i = 0; i <= 2*4*2048; i++ )
183 184 185 186 187 188
        {
            p_cost_mv[a->i_qp][-i] =
            p_cost_mv[a->i_qp][i]  = a->i_lambda * bs_size_se( i );
        }
    }
    a->p_cost_mv = p_cost_mv[a->i_qp];
189 190

    /* FIXME is this useful for all me methods? */
191
    if( h->param.analyse.i_me_method >= X264_ME_ESA && !x264_cost_mv_fpel[a->i_qp][0] )
192 193 194 195 196
    {
        for( j=0; j<4; j++ )
        {
            x264_cost_mv_fpel[a->i_qp][j] = x264_malloc( (4*2048 + 1) * sizeof(int16_t) );
            x264_cost_mv_fpel[a->i_qp][j] += 2*2048;
197
            for( i = -2*2048; i < 2*2048; i++ )
198 199 200
                x264_cost_mv_fpel[a->i_qp][j][i] = p_cost_mv[a->i_qp][i*4+j];
        }
    }
201 202
}

Laurent Aimar's avatar
Laurent Aimar committed
203 204 205
static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
{
    /* conduct the analysis using this lamda and QP */
206
    a->i_qp = h->mb.i_qp = i_qp;
207
    h->mb.i_chroma_qp = i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )];
Laurent Aimar's avatar
Laurent Aimar committed
208
    a->i_lambda = i_qp0_cost_table[i_qp];
209
    a->i_lambda2 = i_qp0_cost2_table[i_qp];
210 211
    a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
                ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
Laurent Aimar's avatar
Laurent Aimar committed
212

Loren Merritt's avatar
Loren Merritt committed
213
    h->mb.i_me_method = h->param.analyse.i_me_method;
Loren Merritt's avatar
Loren Merritt committed
214 215 216
    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;
217
    h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
218
    h->mb.b_transform_8x8 = 0;
219
    h->mb.b_noise_reduction = 0;
220

Laurent Aimar's avatar
Laurent Aimar committed
221
    /* I: Intra part */
222 223 224 225
    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
226

227
    a->b_fast_intra = 0;
228 229 230 231
    h->mb.i_skip_intra =
        h->mb.b_lossless ? 0 :
        a->b_mbrd ? 2 :
        !h->param.analyse.i_trellis && !h->param.analyse.i_noise_reduction;
232

Laurent Aimar's avatar
Laurent Aimar committed
233 234 235
    /* II: Inter part P/B frame */
    if( h->sh.i_type != SLICE_TYPE_I )
    {
Loren Merritt's avatar
Loren Merritt committed
236 237
        int i, j;
        int i_fmv_range = 4 * h->param.analyse.i_mv_range;
238 239 240 241
        // 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
242

243
        /* Calculate max allowed MV range */
244
#define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range-1 )
245 246
        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
247 248 249 250
        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;
251 252
        if( h->mb.i_mb_x == 0)
        {
253 254
            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
255 256 257 258 259 260 261 262 263 264 265 266
            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++ )
                    {
267
                        x264_frame_cond_wait( fref[j], thresh );
Loren Merritt's avatar
Loren Merritt committed
268 269 270 271 272 273 274 275 276
                        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;
            }

277 278
            h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
            h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
279
            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
280 281 282 283
            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;
284
        }
285
#undef CLIP_FMV
286

287
        a->l0.me16x16.cost =
288
        a->l0.i_rd16x16    =
289
        a->l0.i_cost8x8    = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
290 291 292

        for( i = 0; i < 4; i++ )
        {
293 294 295
            a->l0.i_cost4x4[i] =
            a->l0.i_cost8x4[i] =
            a->l0.i_cost4x8[i] = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
296 297
        }

298 299
        a->l0.i_cost16x8   =
        a->l0.i_cost8x16   = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
300 301
        if( h->sh.i_type == SLICE_TYPE_B )
        {
302
            a->l1.me16x16.cost =
303
            a->l1.i_rd16x16    =
304
            a->l1.i_cost8x8    = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
305 306 307

            for( i = 0; i < 4; i++ )
            {
308 309 310 311
                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
312 313
            }

314 315
            a->l1.i_cost16x8   =
            a->l1.i_cost8x16   =
316 317 318 319 320
            a->i_rd16x16bi     =
            a->i_rd16x16direct =
            a->i_rd8x8bi       =
            a->i_rd16x8bi      =
            a->i_rd8x16bi      =
321 322 323 324 325
            a->i_cost16x16bi   =
            a->i_cost16x16direct =
            a->i_cost8x8bi     =
            a->i_cost16x8bi    =
            a->i_cost8x16bi    = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
326
        }
327 328

        /* Fast intra decision */
Loren Merritt's avatar
Loren Merritt committed
329
        if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
330
        {
331
            if(   IS_INTRA( h->mb.i_mb_type_left )
Loren Merritt's avatar
Loren Merritt committed
332 333 334
               || IS_INTRA( h->mb.i_mb_type_top )
               || IS_INTRA( h->mb.i_mb_type_topleft )
               || IS_INTRA( h->mb.i_mb_type_topright )
335
               || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
336
               || (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])) )
337 338 339 340 341 342
            { /* intra is likely */ }
            else
            {
                a->b_fast_intra = 1;
            }
        }
343
        h->mb.b_skip_mc = 0;
Laurent Aimar's avatar
Laurent Aimar committed
344 345 346 347 348 349 350 351 352 353 354
    }
}



/*
 * 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
355
    if( i_neighbour & MB_TOPLEFT )
Laurent Aimar's avatar
Laurent Aimar committed
356
    {
Loren Merritt's avatar
Loren Merritt committed
357
        /* top and left available */
Laurent Aimar's avatar
Laurent Aimar committed
358 359 360 361 362 363
        *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
364
    else if( i_neighbour & MB_LEFT )
Laurent Aimar's avatar
Laurent Aimar committed
365 366 367 368 369 370
    {
        /* left available*/
        *mode++ = I_PRED_16x16_DC_LEFT;
        *mode++ = I_PRED_16x16_H;
        *pi_count = 2;
    }
Loren Merritt's avatar
Loren Merritt committed
371
    else if( i_neighbour & MB_TOP )
Laurent Aimar's avatar
Laurent Aimar committed
372 373 374 375 376 377 378 379
    {
        /* top available*/
        *mode++ = I_PRED_16x16_DC_TOP;
        *mode++ = I_PRED_16x16_V;
        *pi_count = 2;
    }
    else
    {
Loren Merritt's avatar
Loren Merritt committed
380
        /* none available */
Laurent Aimar's avatar
Laurent Aimar committed
381 382 383 384 385 386
        *mode = I_PRED_16x16_DC_128;
        *pi_count = 1;
    }
}

/* Max = 4 */
387
static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
Laurent Aimar's avatar
Laurent Aimar committed
388
{
Loren Merritt's avatar
Loren Merritt committed
389
    if( i_neighbour & MB_TOPLEFT )
Laurent Aimar's avatar
Laurent Aimar committed
390
    {
Loren Merritt's avatar
Loren Merritt committed
391
        /* top and left available */
Laurent Aimar's avatar
Laurent Aimar committed
392 393 394 395 396 397
        *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
398
    else if( i_neighbour & MB_LEFT )
Laurent Aimar's avatar
Laurent Aimar committed
399 400 401 402 403 404
    {
        /* left available*/
        *mode++ = I_PRED_CHROMA_DC_LEFT;
        *mode++ = I_PRED_CHROMA_H;
        *pi_count = 2;
    }
Loren Merritt's avatar
Loren Merritt committed
405
    else if( i_neighbour & MB_TOP )
Laurent Aimar's avatar
Laurent Aimar committed
406 407 408 409 410 411 412 413
    {
        /* top available*/
        *mode++ = I_PRED_CHROMA_DC_TOP;
        *mode++ = I_PRED_CHROMA_V;
        *pi_count = 2;
    }
    else
    {
Loren Merritt's avatar
Loren Merritt committed
414
        /* none available */
Laurent Aimar's avatar
Laurent Aimar committed
415 416 417 418 419
        *mode = I_PRED_CHROMA_DC_128;
        *pi_count = 1;
    }
}

420 421 422
/* MAX = 9 */
static void predict_4x4_mode_available( unsigned int i_neighbour,
                                        int *mode, int *pi_count )
Laurent Aimar's avatar
Laurent Aimar committed
423
{
424 425
    int b_l = i_neighbour & MB_LEFT;
    int b_t = i_neighbour & MB_TOP;
Laurent Aimar's avatar
Laurent Aimar committed
426

427
    if( b_l && b_t )
Laurent Aimar's avatar
Laurent Aimar committed
428
    {
429
        *pi_count = 6;
Laurent Aimar's avatar
Laurent Aimar committed
430 431 432
        *mode++ = I_PRED_4x4_DC;
        *mode++ = I_PRED_4x4_H;
        *mode++ = I_PRED_4x4_V;
433
        *mode++ = I_PRED_4x4_DDL;
434 435 436 437 438 439 440
        if( i_neighbour & MB_TOPLEFT )
        {
            *mode++ = I_PRED_4x4_DDR;
            *mode++ = I_PRED_4x4_VR;
            *mode++ = I_PRED_4x4_HD;
            *pi_count += 3;
        }
441
        *mode++ = I_PRED_4x4_VL;
Laurent Aimar's avatar
Laurent Aimar committed
442 443
        *mode++ = I_PRED_4x4_HU;
    }
444
    else if( b_l )
Laurent Aimar's avatar
Laurent Aimar committed
445 446 447
    {
        *mode++ = I_PRED_4x4_DC_LEFT;
        *mode++ = I_PRED_4x4_H;
448 449
        *mode++ = I_PRED_4x4_HU;
        *pi_count = 3;
Laurent Aimar's avatar
Laurent Aimar committed
450
    }
451
    else if( b_t )
Laurent Aimar's avatar
Laurent Aimar committed
452 453 454
    {
        *mode++ = I_PRED_4x4_DC_TOP;
        *mode++ = I_PRED_4x4_V;
455 456 457
        *mode++ = I_PRED_4x4_DDL;
        *mode++ = I_PRED_4x4_VL;
        *pi_count = 4;
Laurent Aimar's avatar
Laurent Aimar committed
458 459 460 461 462 463 464 465
    }
    else
    {
        *mode++ = I_PRED_4x4_DC_128;
        *pi_count = 1;
    }
}

466 467 468 469 470
static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
{
    int i;

    int i_max;
471
    int predict_mode[4];
472 473 474

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

475
    if( a->i_satd_i8x8chroma < COST_MAX )
476 477 478 479 480 481 482 483 484
        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 );
485
    a->i_satd_i8x8chroma = COST_MAX;
486
    if( i_max == 4 && h->pixf.intra_satd_x3_8x8c && h->pixf.mbcmp[0] == h->pixf.satd[0] )
487
    {
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
        int satdu[4], satdv[4];
        h->pixf.intra_satd_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
        h->pixf.intra_satd_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
        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 );
        
        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);
503 504

            a->i_satd_i8x8chroma_dir[i] = i_satd;
505 506 507 508 509 510 511 512 513
            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];
514

515 516 517
            /* we do the prediction */
            h->predict_8x8c[i_mode]( p_dstc[0] );
            h->predict_8x8c[i_mode]( p_dstc[1] );
518

519 520 521 522 523 524
            /* 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] );
525

526
            a->i_satd_i8x8chroma_dir[i] = i_satd;
527 528
            COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
        }
529 530 531 532 533
    }

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

534
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
535 536 537 538 539 540 541 542
{
    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];
543
    int b_merged_satd = h->pixf.intra_satd_x3_16x16 && h->pixf.mbcmp[0] == h->pixf.satd[0];
544

Laurent Aimar's avatar
Laurent Aimar committed
545 546 547 548
    /*---------------- Try all mode and calculate their score ---------------*/

    /* 16x16 prediction selection */
    predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
549 550 551 552 553 554 555 556 557 558 559 560 561 562

    if( b_merged_satd && i_max == 4 )
    {
        h->pixf.intra_satd_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
        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
563
    {
564 565 566 567 568 569 570 571 572 573 574
        for( i = 0; i < i_max; i++ )
        {
            int i_satd;
            int i_mode = predict_mode[i];
            h->predict_16x16[i_mode]( p_dst );

            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
575 576
    }

577 578 579 580 581
    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;
582

583 584
    /* 8x8 prediction selection */
    if( flags & X264_ANALYSE_I8x8 )
Laurent Aimar's avatar
Laurent Aimar committed
585
    {
Loren Merritt's avatar
Loren Merritt committed
586
        DECLARE_ALIGNED_16( uint8_t edge[33] );
587
        x264_pixel_cmp_t sa8d = (*h->pixf.mbcmp == *h->pixf.sad) ? h->pixf.sad[PIXEL_8x8] : h->pixf.sa8d[PIXEL_8x8];
588 589
        int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
        int i_cost = 0;
590
        b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
Laurent Aimar's avatar
Laurent Aimar committed
591

592 593 594
        // 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];
595

596 597 598 599 600 601 602 603
        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 );
604

605
            predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
606
            x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
607 608 609 610

            if( b_merged_satd && i_max == 9 )
            {
                int satd[3];
611
                h->pixf.intra_sa8d_x3_8x8( p_src_by, edge, satd );
612 613 614 615 616 617 618 619 620 621 622 623 624
                if( i_pred_mode < 3 )
                    satd[i_pred_mode] -= 3 * a->i_lambda;
                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
625
            {
626 627
                int i_satd;
                int i_mode = predict_mode[i];
Laurent Aimar's avatar
Laurent Aimar committed
628

629
                h->predict_8x8[i_mode]( p_dst_by, edge );
Laurent Aimar's avatar
Laurent Aimar committed
630

631 632
                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
633

634 635
                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
636
            }
637 638 639 640
            i_cost += i_best;

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

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

646
            x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
647 648
        }

649
        if( idx == 3 )
650
        {
651
            a->i_satd_i8x8 = i_cost;
652 653 654 655 656 657 658
            if( h->mb.i_skip_intra )
            {
                h->mc.copy[PIXEL_16x16]( h->mb.pic.i8x8_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
                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) );
            }
        }
659 660
        else
        {
661 662
            a->i_satd_i8x8 = COST_MAX;
            i_cost = i_cost * 4/(idx+1);
Laurent Aimar's avatar
Laurent Aimar committed
663
        }
664 665
        if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 )
            return;
Laurent Aimar's avatar
Laurent Aimar committed
666
    }
667

668 669
    /* 4x4 prediction selection */
    if( flags & X264_ANALYSE_I4x4 )
670
    {
671 672
        int i_cost;
        int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
673
        b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
674 675 676 677 678 679 680 681
        if( a->b_mbrd )
            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++ )
682
        {
683 684 685 686 687 688
            int x = block_idx_x[idx];
            int y = block_idx_y[idx];
            uint8_t *p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
            uint8_t *p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
            int i_best = COST_MAX;
            int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
689

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

692 693 694
            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;
695

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
            if( b_merged_satd && i_max >= 6 )
            {
                int satd[3];
                h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
                if( i_pred_mode < 3 )
                    satd[i_pred_mode] -= 3 * a->i_lambda;
                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++ )
711
            {
712
                int i_satd;
713
                int i_mode = predict_mode[i];
714

715
                h->predict_4x4[i_mode]( p_dst_by );
716

717
                i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
718
                                                   p_src_by, FENC_STRIDE )
719
                       + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
720

721
                COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
722
            }
723
            i_cost += i_best;
724

725 726
            if( i_cost > i_satd_thresh || idx == 15 )
                break;
727

728 729 730
            /* 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
731

732
            h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
733
        }
734
        if( idx == 15 )
735
        {
736
            a->i_satd_i4x4 = i_cost;
737 738 739 740
            if( h->mb.i_skip_intra )
            {
                h->mc.copy[PIXEL_16x16]( h->mb.pic.i4x4_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
                if( h->mb.i_skip_intra == 2 )
741
                    h->mc.memcpy_aligned( h->mb.pic.i4x4_dct_buf, h->dct.luma4x4, sizeof(h->mb.pic.i4x4_dct_buf) );
742 743
            }
        }
744
        else
745
            a->i_satd_i4x4 = COST_MAX;
Laurent Aimar's avatar
Laurent Aimar committed
746 747 748
    }
}

749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
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 );
    }
    else
        a->i_satd_i8x8 = COST_MAX;
}

779 780 781 782 783
static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
{
    uint8_t  *p_src = h->mb.pic.p_fenc[0];
    uint8_t  *p_dst = h->mb.pic.p_fdec[0];

784 785
    int i, j, idx, x, y;
    int i_max, i_satd, i_best, i_mode, i_thresh;
786 787
    int i_pred_mode;
    int predict_mode[9];
788
    h->mb.i_skip_intra = 0;
789 790 791 792

    if( h->mb.i_type == I_16x16 )
    {
        int old_pred_mode = a->i_predict16x16;
793
        i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
794
        i_best = a->i_satd_i16x16;
795 796 797
        predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
        for( i = 0; i < i_max; i++ )
        {
798 799
            int i_mode = predict_mode[i];
            if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
800
                continue;
801 802 803
            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 );
804 805 806 807
        }
    }
    else if( h->mb.i_type == I_4x4 )
    {
Loren Merritt's avatar
Loren Merritt committed
808 809
        uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
        int i_nnz = 0;
810 811 812 813 814 815
        for( idx = 0; idx < 16; idx++ )
        {
            uint8_t *p_src_by;
            uint8_t *p_dst_by;
            i_best = COST_MAX;

816
            i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
            x = block_idx_x[idx];
            y = block_idx_y[idx];

            p_src_by = p_src + 4*x + 4*y*FENC_STRIDE;
            p_dst_by = p_dst + 4*x + 4*y*FDEC_STRIDE;
            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];
                h->predict_4x4[i_mode]( p_dst_by );
832
                i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
833

834
                if( i_best > i_satd )
835
                {
836 837
                    a->i_predict4x4[idx] = i_mode;
                    i_best = i_satd;
838 839 840 841 842 843 844 845 846 847 848 849 850 851
                    pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
                    pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
                    pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
                    pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
                    i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
                }
            }

            *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
            *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
            *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
            *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
            h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;

852
            h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
853 854 855 856
        }
    }
    else if( h->mb.i_type == I_8x8 )
    {
Loren Merritt's avatar
Loren Merritt committed
857
        DECLARE_ALIGNED_16( uint8_t edge[33] );
858 859 860 861 862 863 864 865
        for( idx = 0; idx < 4; idx++ )
        {
            uint64_t pels_h = 0;
            uint8_t pels_v[7];
            int i_nnz[3];
            uint8_t *p_src_by;
            uint8_t *p_dst_by;
            int j;
866
            i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
867

868 869
            i_best = COST_MAX;
            i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
870 871 872 873 874 875
            x = idx&1;
            y = idx>>1;

            p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
            p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
            predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
876 877
            x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );

878 879 880
            for( i = 0; i < i_max; i++ )
            {
                i_mode = predict_mode[i];
881 882
                if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
                    continue;
883
                h->predict_8x8[i_mode]( p_dst_by, edge );
884
                i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
885

886
                if( i_best > i_satd )
887
                {
888 889
                    a->i_predict8x8[idx] = i_mode;
                    i_best = i_satd;
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906

                    pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
                    if( !(idx&1) )
                        for( j=0; j<7; j++ )
                            pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
                    for( j=0; j<3; j++ )
                        i_nnz[j] = h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]];
                }
            }

            *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
            if( !(idx&1) )
                for( j=0; j<7; j++ )
                    p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
            for( j=0; j<3; j++ )
                h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]] = i_nnz[j];

907
            x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
908 909
        }
    }
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926

    /* 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 )
        {
927
            int i_chroma_lambda = i_qp0_cost2_table[h->mb.i_chroma_qp];
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
            /* 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];
                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 );
                COPY2_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode );
            }
            h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
        }
    }
946 947
}

Loren Merritt's avatar
Loren Merritt committed
948 949 950
#define LOAD_FENC( m, src, xoff, yoff) \
    (m)->i_stride[0] = h->mb.pic.i_stride[0]; \
    (m)->i_stride[1] = h->mb.pic.i_stride[1]; \
951 952 953
    (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; \
    (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; \
    (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
Loren Merritt's avatar
Loren Merritt committed
954

955
#define LOAD_HPELS(m, src, list, ref, xoff, yoff) \
Loren Merritt's avatar
Loren Merritt committed
956 957 958 959 960
    (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; \
    (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; \
    (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; \
    (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; \
    (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
961 962
    (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; \
    (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
963

Loren Merritt's avatar
Loren Merritt committed
964 965 966
#define REF_COST(list, ref) \
    (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))

Laurent Aimar's avatar
Laurent Aimar committed
967 968 969
static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
{
    x264_me_t m;
970 971
    int i_ref, i_mvc;
    DECLARE_ALIGNED_4( int16_t mvc[7][2] );
972
    int i_halfpel_thresh = INT_MAX;
973
    int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
Laurent Aimar's avatar
Laurent Aimar committed
974 975 976

    /* 16x16 Search on all ref frame */
    m.i_pixel = PIXEL_16x16;
977
    m.p_cost_mv = a->p_cost_mv;
Loren Merritt's avatar
Loren Merritt committed
978
    LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
Laurent Aimar's avatar
Laurent Aimar committed
979

980
    a->l0.me16x16.cost = INT_MAX;
981
    for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
Laurent Aimar's avatar
Laurent Aimar committed
982
    {
Loren Merritt's avatar
Loren Merritt committed
983
        const int i_ref_cost = REF_COST( 0, i_ref );
984
        i_halfpel_thresh -= i_ref_cost;
Loren Merritt's avatar
Loren Merritt committed
985 986
        m.i_ref_cost = i_ref_cost;
        m.i_ref = i_ref;
987

Laurent Aimar's avatar
Laurent Aimar committed
988
        /* search with ref */
989
        LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
Laurent Aimar's avatar
Laurent Aimar committed
990
        x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
991
        x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
992
        x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
Laurent Aimar's avatar
Laurent Aimar committed
993

994 995
        /* early termination
         * SSD threshold would probably be better than SATD */
996 997 998 999 1000 1001
        if( i_ref == 0
            && a->b_try_pskip
            && m.cost-m.cost_mv < 300*a->i_lambda
            &&  abs(m.mv[0]-h->mb.cache.pskip_mv[0])
              + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
            && x264_macroblock_probe_pskip( h ) )
1002
        {
1003 1004
            h->mb.i_type = P_SKIP;
            x264_analyse_update_cache( h, a );
Loren Merritt's avatar
Loren Merritt committed
1005
            assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
1006
            return;
1007 1008
        }

1009
        m.cost += i_ref_cost