macroblock.c 79.7 KB
Newer Older
1
/*****************************************************************************
2
 * macroblock.c: macroblock common functions
3
 *****************************************************************************
Henrik Gramner's avatar
Henrik Gramner committed
4
 * Copyright (C) 2003-2019 x264 project
5
 *
6 7
 * Authors: Fiona Glaser <fiona@x264.com>
 *          Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Loren Merritt <lorenm@u.washington.edu>
Henrik Gramner's avatar
Henrik Gramner committed
9
 *          Henrik Gramner <henrik@gramner.com>
10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
24 25 26
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at licensing@x264.com.
27 28 29 30
 *****************************************************************************/

#include "common.h"

Fiona Glaser's avatar
Fiona Glaser committed
31 32 33 34
#define MC_LUMA(list,p) \
    h->mc.mc_luma( &h->mb.pic.p_fdec[p][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, \
                   &h->mb.pic.p_fref[list][i_ref][p*4], h->mb.pic.i_stride[p], \
                   mvx, mvy, 4*width, 4*height, \
Anton Mitrofanov's avatar
Anton Mitrofanov committed
35
                   list ? x264_weight_none : &h->sh.weight[i_ref][p] );
Fiona Glaser's avatar
Fiona Glaser committed
36

37
static NOINLINE void mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
38
{
39 40 41 42
    int i8    = x264_scan8[0]+x+8*y;
    int i_ref = h->mb.cache.ref[0][i8];
    int mvx   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
    int mvy   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
43

Fiona Glaser's avatar
Fiona Glaser committed
44
    MC_LUMA( 0, 0 );
45

Fiona Glaser's avatar
Fiona Glaser committed
46 47 48 49 50
    if( CHROMA444 )
    {
        MC_LUMA( 0, 1 );
        MC_LUMA( 0, 2 );
    }
51
    else if( CHROMA_FORMAT )
Fiona Glaser's avatar
Fiona Glaser committed
52
    {
53
        int v_shift = CHROMA_V_SHIFT;
Henrik Gramner's avatar
Henrik Gramner committed
54 55
        // Chroma in 4:2:0 is offset if MCing from a field of opposite parity
        if( v_shift & MB_INTERLACED & i_ref )
Fiona Glaser's avatar
Fiona Glaser committed
56 57
            mvy += (h->mb.i_mb_y & 1)*4 - 2;

Henrik Gramner's avatar
Henrik Gramner committed
58 59 60 61 62
        int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
        height = 4*height >> v_shift;

        h->mc.mc_chroma( &h->mb.pic.p_fdec[1][offset],
                         &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
Fiona Glaser's avatar
Fiona Glaser committed
63
                         h->mb.pic.p_fref[0][i_ref][4], h->mb.pic.i_stride[1],
Henrik Gramner's avatar
Henrik Gramner committed
64
                         mvx, 2*mvy>>v_shift, 2*width, height );
Fiona Glaser's avatar
Fiona Glaser committed
65 66

        if( h->sh.weight[i_ref][1].weightfn )
Henrik Gramner's avatar
Henrik Gramner committed
67 68 69
            h->sh.weight[i_ref][1].weightfn[width>>1]( &h->mb.pic.p_fdec[1][offset], FDEC_STRIDE,
                                                       &h->mb.pic.p_fdec[1][offset], FDEC_STRIDE,
                                                       &h->sh.weight[i_ref][1], height );
Fiona Glaser's avatar
Fiona Glaser committed
70
        if( h->sh.weight[i_ref][2].weightfn )
Henrik Gramner's avatar
Henrik Gramner committed
71 72 73
            h->sh.weight[i_ref][2].weightfn[width>>1]( &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
                                                       &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
                                                       &h->sh.weight[i_ref][2], height );
Fiona Glaser's avatar
Fiona Glaser committed
74
    }
75
}
76
static NOINLINE void mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
77
{
78 79 80 81
    int i8    = x264_scan8[0]+x+8*y;
    int i_ref = h->mb.cache.ref[1][i8];
    int mvx   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
    int mvy   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
82

Fiona Glaser's avatar
Fiona Glaser committed
83
    MC_LUMA( 1, 0 );
84

Fiona Glaser's avatar
Fiona Glaser committed
85 86 87 88 89
    if( CHROMA444 )
    {
        MC_LUMA( 1, 1 );
        MC_LUMA( 1, 2 );
    }
90
    else if( CHROMA_FORMAT )
Fiona Glaser's avatar
Fiona Glaser committed
91
    {
92
        int v_shift = CHROMA_V_SHIFT;
Henrik Gramner's avatar
Henrik Gramner committed
93
        if( v_shift & MB_INTERLACED & i_ref )
Fiona Glaser's avatar
Fiona Glaser committed
94
            mvy += (h->mb.i_mb_y & 1)*4 - 2;
95

Henrik Gramner's avatar
Henrik Gramner committed
96 97 98
        int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
        h->mc.mc_chroma( &h->mb.pic.p_fdec[1][offset],
                         &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
Fiona Glaser's avatar
Fiona Glaser committed
99
                         h->mb.pic.p_fref[1][i_ref][4], h->mb.pic.i_stride[1],
Henrik Gramner's avatar
Henrik Gramner committed
100
                         mvx, 2*mvy>>v_shift, 2*width, 4*height>>v_shift );
Fiona Glaser's avatar
Fiona Glaser committed
101
    }
102 103
}

Fiona Glaser's avatar
Fiona Glaser committed
104 105
#define MC_LUMA_BI(p) \
    src0 = h->mc.get_ref( tmp0, &i_stride0, &h->mb.pic.p_fref[0][i_ref0][p*4], h->mb.pic.i_stride[p], \
Anton Mitrofanov's avatar
Anton Mitrofanov committed
106
                          mvx0, mvy0, 4*width, 4*height, x264_weight_none ); \
Fiona Glaser's avatar
Fiona Glaser committed
107
    src1 = h->mc.get_ref( tmp1, &i_stride1, &h->mb.pic.p_fref[1][i_ref1][p*4], h->mb.pic.i_stride[p], \
Anton Mitrofanov's avatar
Anton Mitrofanov committed
108
                          mvx1, mvy1, 4*width, 4*height, x264_weight_none ); \
Fiona Glaser's avatar
Fiona Glaser committed
109 110 111
    h->mc.avg[i_mode]( &h->mb.pic.p_fdec[p][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, \
                       src0, i_stride0, src1, i_stride1, weight );

112
static NOINLINE void mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
113
{
114 115 116 117 118 119 120 121 122
    int i8 = x264_scan8[0]+x+8*y;
    int i_ref0 = h->mb.cache.ref[0][i8];
    int i_ref1 = h->mb.cache.ref[1][i8];
    int weight = h->mb.bipred_weight[i_ref0][i_ref1];
    int mvx0   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
    int mvx1   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
    int mvy0   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
    int mvy1   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
    int i_mode = x264_size2pixel[height][width];
123
    intptr_t i_stride0 = 16, i_stride1 = 16;
124 125
    ALIGNED_ARRAY_32( pixel, tmp0,[16*16] );
    ALIGNED_ARRAY_32( pixel, tmp1,[16*16] );
126
    pixel *src0, *src1;
127

Fiona Glaser's avatar
Fiona Glaser committed
128
    MC_LUMA_BI( 0 );
129

Fiona Glaser's avatar
Fiona Glaser committed
130 131 132 133 134
    if( CHROMA444 )
    {
        MC_LUMA_BI( 1 );
        MC_LUMA_BI( 2 );
    }
135
    else if( CHROMA_FORMAT )
Fiona Glaser's avatar
Fiona Glaser committed
136
    {
137
        int v_shift = CHROMA_V_SHIFT;
Henrik Gramner's avatar
Henrik Gramner committed
138
        if( v_shift & MB_INTERLACED & i_ref0 )
Fiona Glaser's avatar
Fiona Glaser committed
139
            mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
Henrik Gramner's avatar
Henrik Gramner committed
140
        if( v_shift & MB_INTERLACED & i_ref1 )
Fiona Glaser's avatar
Fiona Glaser committed
141 142 143
            mvy1 += (h->mb.i_mb_y & 1)*4 - 2;

        h->mc.mc_chroma( tmp0, tmp0+8, 16, h->mb.pic.p_fref[0][i_ref0][4], h->mb.pic.i_stride[1],
Henrik Gramner's avatar
Henrik Gramner committed
144
                         mvx0, 2*mvy0>>v_shift, 2*width, 4*height>>v_shift );
Fiona Glaser's avatar
Fiona Glaser committed
145
        h->mc.mc_chroma( tmp1, tmp1+8, 16, h->mb.pic.p_fref[1][i_ref1][4], h->mb.pic.i_stride[1],
Henrik Gramner's avatar
Henrik Gramner committed
146 147 148 149 150 151
                         mvx1, 2*mvy1>>v_shift, 2*width, 4*height>>v_shift );

        int chromapix = h->luma2chroma_pixel[i_mode];
        int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
        h->mc.avg[chromapix]( &h->mb.pic.p_fdec[1][offset], FDEC_STRIDE, tmp0,   16, tmp1,   16, weight );
        h->mc.avg[chromapix]( &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE, tmp0+8, 16, tmp1+8, 16, weight );
Fiona Glaser's avatar
Fiona Glaser committed
152
    }
153 154
}

Fiona Glaser's avatar
Fiona Glaser committed
155 156 157
#undef MC_LUMA
#undef MC_LUMA_BI

158 159
void x264_mb_mc_8x8( x264_t *h, int i8 )
{
160 161
    int x = 2*(i8&1);
    int y = 2*(i8>>1);
162 163 164 165 166 167

    if( h->sh.i_type == SLICE_TYPE_P )
    {
        switch( h->mb.i_sub_partition[i8] )
        {
            case D_L0_8x8:
168
                mb_mc_0xywh( h, x, y, 2, 2 );
169 170
                break;
            case D_L0_8x4:
171 172
                mb_mc_0xywh( h, x, y+0, 2, 1 );
                mb_mc_0xywh( h, x, y+1, 2, 1 );
173 174
                break;
            case D_L0_4x8:
175 176
                mb_mc_0xywh( h, x+0, y, 1, 2 );
                mb_mc_0xywh( h, x+1, y, 1, 2 );
177 178
                break;
            case D_L0_4x4:
179 180 181 182
                mb_mc_0xywh( h, x+0, y+0, 1, 1 );
                mb_mc_0xywh( h, x+1, y+0, 1, 1 );
                mb_mc_0xywh( h, x+0, y+1, 1, 1 );
                mb_mc_0xywh( h, x+1, y+1, 1, 1 );
183 184 185 186
                break;
        }
    }
    else
187
    {
188
        int scan8 = x264_scan8[0] + x + 8*y;
189

190 191
        if( h->mb.cache.ref[0][scan8] >= 0 )
            if( h->mb.cache.ref[1][scan8] >= 0 )
192
                mb_mc_01xywh( h, x, y, 2, 2 );
193
            else
194
                mb_mc_0xywh( h, x, y, 2, 2 );
195
        else
196
            mb_mc_1xywh( h, x, y, 2, 2 );
197 198 199
    }
}

200 201
void x264_mb_mc( x264_t *h )
{
202
    if( h->mb.i_partition == D_8x8 )
203
    {
204
        for( int i = 0; i < 4; i++ )
205
            x264_mb_mc_8x8( h, i );
206
    }
207
    else
208
    {
209 210 211 212
        int ref0a = h->mb.cache.ref[0][x264_scan8[ 0]];
        int ref0b = h->mb.cache.ref[0][x264_scan8[12]];
        int ref1a = h->mb.cache.ref[1][x264_scan8[ 0]];
        int ref1b = h->mb.cache.ref[1][x264_scan8[12]];
213 214 215

        if( h->mb.i_partition == D_16x16 )
        {
216
            if( ref0a >= 0 )
217 218 219
                if( ref1a >= 0 ) mb_mc_01xywh( h, 0, 0, 4, 4 );
                else             mb_mc_0xywh ( h, 0, 0, 4, 4 );
            else                 mb_mc_1xywh ( h, 0, 0, 4, 4 );
220 221 222
        }
        else if( h->mb.i_partition == D_16x8 )
        {
223
            if( ref0a >= 0 )
224 225 226
                if( ref1a >= 0 ) mb_mc_01xywh( h, 0, 0, 4, 2 );
                else             mb_mc_0xywh ( h, 0, 0, 4, 2 );
            else                 mb_mc_1xywh ( h, 0, 0, 4, 2 );
227 228

            if( ref0b >= 0 )
229 230 231
                if( ref1b >= 0 ) mb_mc_01xywh( h, 0, 2, 4, 2 );
                else             mb_mc_0xywh ( h, 0, 2, 4, 2 );
            else                 mb_mc_1xywh ( h, 0, 2, 4, 2 );
232 233 234
        }
        else if( h->mb.i_partition == D_8x16 )
        {
235
            if( ref0a >= 0 )
236 237 238
                if( ref1a >= 0 ) mb_mc_01xywh( h, 0, 0, 2, 4 );
                else             mb_mc_0xywh ( h, 0, 0, 2, 4 );
            else                 mb_mc_1xywh ( h, 0, 0, 2, 4 );
239 240

            if( ref0b >= 0 )
241 242 243
                if( ref1b >= 0 ) mb_mc_01xywh( h, 2, 0, 2, 4 );
                else             mb_mc_0xywh ( h, 2, 0, 2, 4 );
            else                 mb_mc_1xywh ( h, 2, 0, 2, 4 );
244 245 246 247
        }
    }
}

248
int x264_macroblock_cache_allocate( x264_t *h )
249
{
250
    int i_mb_count = h->mb.i_mb_count;
251

252 253 254
    h->mb.i_mb_stride = h->mb.i_mb_width;
    h->mb.i_b8_stride = h->mb.i_mb_width * 2;
    h->mb.i_b4_stride = h->mb.i_mb_width * 4;
255

256
    h->mb.b_interlaced = PARAM_INTERLACED;
257

258 259 260 261 262
    PREALLOC_INIT

    PREALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
    PREALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
    PREALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
263
    PREALLOC( h->mb.slice_table, i_mb_count * sizeof(uint32_t) );
264 265

    /* 0 -> 3 top(4), 4 -> 6 : left(3) */
266
    PREALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
267 268

    /* all coeffs */
269
    PREALLOC( h->mb.non_zero_count, i_mb_count * 48 * sizeof(uint8_t) );
270 271 272

    if( h->param.b_cabac )
    {
273 274 275
        PREALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
        PREALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
        PREALLOC( h->mb.mvd[0], i_mb_count * sizeof( **h->mb.mvd ) );
276
        if( h->param.i_bframe )
277
            PREALLOC( h->mb.mvd[1], i_mb_count * sizeof( **h->mb.mvd ) );
278 279
    }

280
    for( int i = 0; i < 2; i++ )
281
    {
282
        int i_refs = X264_MIN(X264_REF_MAX, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << PARAM_INTERLACED;
283
        if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
284
            i_refs = X264_MIN(X264_REF_MAX, i_refs + 1 + (BIT_DEPTH == 8)); //smart weights add two duplicate frames, one in >8-bit
285

286
        for( int j = !i; j < i_refs; j++ )
287
            PREALLOC( h->mb.mvr[i][j], 2 * (i_mb_count + 1) * sizeof(int16_t) );
288
    }
289

290 291
    if( h->param.analyse.i_weighted_pred )
    {
292
        int i_padv = PADV << PARAM_INTERLACED;
293
        int luma_plane_size = 0;
294 295 296 297
        int numweightbuf;

        if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_FAKE )
        {
Dylan Yudaken's avatar
Dylan Yudaken committed
298
            // only need buffer for lookahead
299 300 301
            if( !h->param.i_sync_lookahead || h == h->thread[h->param.i_threads] )
            {
                // Fake analysis only works on lowres
302
                luma_plane_size = h->fdec->i_stride_lowres * (h->mb.i_mb_height*8+2*i_padv);
303 304 305 306 307 308 309 310
                // Only need 1 buffer for analysis
                numweightbuf = 1;
            }
            else
                numweightbuf = 0;
        }
        else
        {
Henrik Gramner's avatar
Henrik Gramner committed
311 312 313
            /* Both ref and fenc is stored for 4:2:0 and 4:2:2 which means that 4:2:0 and 4:4:4
             * needs the same amount of space and 4:2:2 needs twice that much */
            luma_plane_size = h->fdec->i_stride[0] * (h->mb.i_mb_height*(16<<(CHROMA_FORMAT==CHROMA_422))+2*i_padv);
314 315

            if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
316 317
                //smart can weight one ref and one offset -1 in 8-bit
                numweightbuf = 1 + (BIT_DEPTH == 8);
318
            else
319
                //simple only has one weighted ref
320 321 322
                numweightbuf = 1;
        }

323
        for( int i = 0; i < numweightbuf; i++ )
324 325 326 327 328
            PREALLOC( h->mb.p_weight_buf[i], luma_plane_size * sizeof(pixel) );
    }

    PREALLOC_END( h->mb.base );

329
    memset( h->mb.slice_table, -1, i_mb_count * sizeof(uint32_t) );
330 331 332 333 334 335 336 337 338 339 340 341

    for( int i = 0; i < 2; i++ )
    {
        int i_refs = X264_MIN(X264_REF_MAX, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << PARAM_INTERLACED;
        if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
            i_refs = X264_MIN(X264_REF_MAX, i_refs + 1 + (BIT_DEPTH == 8)); //smart weights add two duplicate frames, one in >8-bit

        for( int j = !i; j < i_refs; j++ )
        {
            M32( h->mb.mvr[i][j][0] ) = 0;
            h->mb.mvr[i][j]++;
        }
342 343
    }

344
    return 0;
Anton Mitrofanov's avatar
Anton Mitrofanov committed
345 346
fail:
    return -1;
347
}
348
void x264_macroblock_cache_free( x264_t *h )
349
{
350
    x264_free( h->mb.base );
351
}
352 353 354 355

int x264_macroblock_thread_allocate( x264_t *h, int b_lookahead )
{
    if( !b_lookahead )
356
    {
357
        for( int i = 0; i < (PARAM_INTERLACED ? 5 : 2); i++ )
Fiona Glaser's avatar
Fiona Glaser committed
358
            for( int j = 0; j < (CHROMA444 ? 3 : 2); j++ )
359
            {
360
                CHECKED_MALLOC( h->intra_border_backup[i][j], (h->sps->i_mb_width*16+32) * sizeof(pixel) );
361
                h->intra_border_backup[i][j] += 16;
362
            }
363
        for( int i = 0; i <= PARAM_INTERLACED; i++ )
364
        {
365 366 367 368 369 370 371 372 373 374 375
            if( h->param.b_sliced_threads )
            {
                /* Only allocate the first one, and allocate it for the whole frame, because we
                 * won't be deblocking until after the frame is fully encoded. */
                if( h == h->thread[0] && !i )
                    CHECKED_MALLOC( h->deblock_strength[0], sizeof(**h->deblock_strength) * h->mb.i_mb_count );
                else
                    h->deblock_strength[i] = h->thread[0]->deblock_strength[0];
            }
            else
                CHECKED_MALLOC( h->deblock_strength[i], sizeof(**h->deblock_strength) * h->mb.i_mb_width );
376
            h->deblock_strength[1] = h->deblock_strength[i];
Fiona Glaser's avatar
Fiona Glaser committed
377
        }
378
    }
379 380 381 382 383

    /* Allocate scratch buffer */
    int scratch_size = 0;
    if( !b_lookahead )
    {
384
        int buf_hpel = (h->thread[0]->fdec->i_width[0]+48+32) * sizeof(int16_t);
385 386 387
        int buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int);
        int me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range);
        int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) *
Loren Merritt's avatar
Loren Merritt committed
388
            ((me_range*2+24) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t));
389
        scratch_size = X264_MAX3( buf_hpel, buf_ssim, buf_tesa );
390
    }
391
    int buf_mbtree = h->param.rc.b_mb_tree * ((h->mb.i_mb_width+15)&~15) * sizeof(int16_t);
392
    scratch_size = X264_MAX( scratch_size, buf_mbtree );
393 394 395 396
    if( scratch_size )
        CHECKED_MALLOC( h->scratch_buffer, scratch_size );
    else
        h->scratch_buffer = NULL;
397

Fiona Glaser's avatar
Fiona Glaser committed
398
    int buf_lookahead_threads = (h->mb.i_mb_height + (4 + 32) * h->param.i_lookahead_threads) * sizeof(int) * 2;
399 400 401
    int buf_mbtree2 = buf_mbtree * 12; /* size of the internal propagate_list asm buffer */
    scratch_size = X264_MAX( buf_lookahead_threads, buf_mbtree2 );
    CHECKED_MALLOC( h->scratch_buffer2, scratch_size );
Fiona Glaser's avatar
Fiona Glaser committed
402

403
    return 0;
Anton Mitrofanov's avatar
Anton Mitrofanov committed
404 405
fail:
    return -1;
406 407 408 409 410
}

void x264_macroblock_thread_free( x264_t *h, int b_lookahead )
{
    if( !b_lookahead )
411
    {
412
        for( int i = 0; i <= PARAM_INTERLACED; i++ )
413 414
            if( !h->param.b_sliced_threads || (h == h->thread[0] && !i) )
                x264_free( h->deblock_strength[i] );
415
        for( int i = 0; i < (PARAM_INTERLACED ? 5 : 2); i++ )
Fiona Glaser's avatar
Fiona Glaser committed
416
            for( int j = 0; j < (CHROMA444 ? 3 : 2); j++ )
417
                x264_free( h->intra_border_backup[i][j] - 16 );
418
    }
419
    x264_free( h->scratch_buffer );
Fiona Glaser's avatar
Fiona Glaser committed
420
    x264_free( h->scratch_buffer2 );
421 422
}

423 424 425 426
void x264_macroblock_slice_init( x264_t *h )
{
    h->mb.mv[0] = h->fdec->mv[0];
    h->mb.mv[1] = h->fdec->mv[1];
427
    h->mb.mvr[0][0] = h->fdec->mv16x16;
428 429
    h->mb.ref[0] = h->fdec->ref[0];
    h->mb.ref[1] = h->fdec->ref[1];
430
    h->mb.type = h->fdec->mb_type;
431
    h->mb.partition = h->fdec->mb_partition;
432
    h->mb.field = h->fdec->field;
433

434 435 436 437
    h->fdec->i_ref[0] = h->i_ref[0];
    h->fdec->i_ref[1] = h->i_ref[1];
    for( int i = 0; i < h->i_ref[0]; i++ )
        h->fdec->ref_poc[0][i] = h->fref[0][i]->i_poc;
438 439
    if( h->sh.i_type == SLICE_TYPE_B )
    {
440 441
        for( int i = 0; i < h->i_ref[1]; i++ )
            h->fdec->ref_poc[1][i] = h->fref[1][i]->i_poc;
442

443 444
        map_col_to_list0(-1) = -1;
        map_col_to_list0(-2) = -2;
445
        for( int i = 0; i < h->fref[1][0]->i_ref[0]; i++ )
446
        {
447
            int poc = h->fref[1][0]->ref_poc[0][i];
448
            map_col_to_list0(i) = -2;
449 450
            for( int j = 0; j < h->i_ref[0]; j++ )
                if( h->fref[0][j]->i_poc == poc )
451
                {
452
                    map_col_to_list0(i) = j;
453 454 455
                    break;
                }
        }
456
    }
457 458
    else if( h->sh.i_type == SLICE_TYPE_P )
    {
459
        if( h->sh.i_disable_deblocking_filter_idc != 1 && h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
460 461 462
        {
            deblock_ref_table(-2) = -2;
            deblock_ref_table(-1) = -1;
463
            for( int i = 0; i < h->i_ref[0] << SLICE_MBAFF; i++ )
464 465 466 467
            {
                /* Mask off high bits to avoid frame num collisions with -1/-2.
                 * In current x264 frame num values don't cover a range of more
                 * than 32, so 6 bits is enough for uniqueness. */
468
                if( !MB_INTERLACED )
469
                    deblock_ref_table(i) = h->fref[0][i]->i_frame_num&63;
470
                else
471
                    deblock_ref_table(i) = ((h->fref[0][i>>1]->i_frame_num&63)<<1) + (i&1);
472 473 474 475
            }
        }
    }

476
    /* init with not available (for top right idx=7,15) */
477
    memset( h->mb.cache.ref, -2, sizeof( h->mb.cache.ref ) );
478

479
    if( h->i_ref[0] > 0 )
480
        for( int field = 0; field <= SLICE_MBAFF; field++ )
481
        {
482 483
            int curpoc = h->fdec->i_poc + h->fdec->i_delta_poc[field];
            int refpoc = h->fref[0][0]->i_poc + h->fref[0][0]->i_delta_poc[field];
484 485 486 487
            int delta = curpoc - refpoc;

            h->fdec->inv_ref_poc[field] = (256 + delta/2) / delta;
        }
488

489 490 491 492 493 494 495 496 497 498 499 500 501 502
    h->mb.i_neighbour4[6] =
    h->mb.i_neighbour4[9] =
    h->mb.i_neighbour4[12] =
    h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
    h->mb.i_neighbour4[3] =
    h->mb.i_neighbour4[7] =
    h->mb.i_neighbour4[11] =
    h->mb.i_neighbour4[13] =
    h->mb.i_neighbour4[15] =
    h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
}

void x264_macroblock_thread_init( x264_t *h )
{
Fiona Glaser's avatar
Fiona Glaser committed
503 504 505 506
    h->mb.i_me_method = h->param.analyse.i_me_method;
    h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
    if( h->sh.i_type == SLICE_TYPE_B && (h->mb.i_subpel_refine == 6 || h->mb.i_subpel_refine == 8) )
        h->mb.i_subpel_refine--;
507 508 509
    h->mb.b_chroma_me = h->param.analyse.b_chroma_me &&
                        ((h->sh.i_type == SLICE_TYPE_P && h->mb.i_subpel_refine >= 5) ||
                         (h->sh.i_type == SLICE_TYPE_B && h->mb.i_subpel_refine >= 9));
Fiona Glaser's avatar
Fiona Glaser committed
510 511
    h->mb.b_dct_decimate = h->sh.i_type == SLICE_TYPE_B ||
                          (h->param.analyse.b_dct_decimate && h->sh.i_type != SLICE_TYPE_I);
512
    h->mb.i_mb_prev_xy = -1;
Fiona Glaser's avatar
Fiona Glaser committed
513

Henrik Gramner's avatar
Henrik Gramner committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
    /*          4:2:0                      4:2:2                      4:4:4
     * fdec            fenc       fdec            fenc       fdec            fenc
     * y y y y y y y   Y Y Y Y    y y y y y y y   Y Y Y Y    y y y y y y y   Y Y Y Y
     * y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y
     * y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y
     * y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y
     * y Y Y Y Y       U U V V    y Y Y Y Y       U U V V    y Y Y Y Y       U U U U
     * u u u   v v v   U U V V    u u u   v v v   U U V V    u u u u u u u   U U U U
     * u U U   v V V              u U U   v V V   U U V V    u U U U U       U U U U
     * u U U   v V V              u U U   v V V   U U V V    u U U U U       U U U U
     *                            u U U   v V V              u U U U U       V V V V
     *                            u U U   v V V              u U U U U       V V V V
     *                                                       v v v v v v v   V V V V
     *                                                       v V V V V       V V V V
     *                                                       v V V V V
     *                                                       v V V V V
     *                                                       v V V V V
     */
Fiona Glaser's avatar
Fiona Glaser committed
532 533
    h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
    h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
534
    if( CHROMA_FORMAT )
Fiona Glaser's avatar
Fiona Glaser committed
535
    {
536 537 538 539 540 541 542 543 544 545 546 547
        h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
        h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 20*FDEC_STRIDE;
        if( CHROMA444 )
        {
            h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 32*FENC_STRIDE;
            h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 38*FDEC_STRIDE;
        }
        else
        {
            h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
            h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 20*FDEC_STRIDE + 16;
        }
Fiona Glaser's avatar
Fiona Glaser committed
548
    }
549
}
550

551 552 553 554
void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
{
    int stride_y  = fenc->i_stride[0];
    int stride_uv = fenc->i_stride[1];
555
    int off_y  = 16 * i_mb_x + 16 * i_mb_y * stride_y;
556
    int off_uv = 16 * i_mb_x + (16 * i_mb_y * stride_uv >> CHROMA_V_SHIFT);
557
    h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
558
                         fenc->plane[1]+off_uv, stride_uv, i_mb_x );
559
}
560

561
NOINLINE void x264_copy_column8( pixel *dst, pixel *src )
562
{
563
    // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
564
    for( int i = -4; i < 4; i++ )
565 566 567
        dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
}

568
static ALWAYS_INLINE void macroblock_load_pic_pointers( x264_t *h, int mb_x, int mb_y, int i, int b_chroma, int b_mbaff )
569
{
Fiona Glaser's avatar
Fiona Glaser committed
570
    int mb_interlaced = b_mbaff && MB_INTERLACED;
571
    int height = b_chroma ? 16 >> CHROMA_V_SHIFT : 16;
572
    int i_stride = h->fdec->i_stride[i];
Fiona Glaser's avatar
Fiona Glaser committed
573 574
    int i_stride2 = i_stride << mb_interlaced;
    int i_pix_offset = mb_interlaced
Henrik Gramner's avatar
Henrik Gramner committed
575 576
                     ? 16 * mb_x + height * (mb_y&~1) * i_stride + (mb_y&1) * i_stride
                     : 16 * mb_x + height * mb_y * i_stride;
577
    pixel *plane_fdec = &h->fdec->plane[i][i_pix_offset];
578
    int fdec_idx = b_mbaff ? (mb_interlaced ? (3 + (mb_y&1)) : (mb_y&1) ? 2 : 4) : !(mb_y&1);
579
    pixel *intra_fdec = &h->intra_border_backup[fdec_idx][i][mb_x*16];
580
    int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
581
    /* ref_pix_offset[0] references the current field and [1] the opposite field. */
Fiona Glaser's avatar
Fiona Glaser committed
582
    if( mb_interlaced )
583
        ref_pix_offset[1] += (1-2*(mb_y&1)) * i_stride;
584
    h->mb.pic.i_stride[i] = i_stride2;
585
    h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
Fiona Glaser's avatar
Fiona Glaser committed
586
    if( b_chroma )
587
    {
Henrik Gramner's avatar
Henrik Gramner committed
588
        h->mc.load_deinterleave_chroma_fenc( h->mb.pic.p_fenc[1], h->mb.pic.p_fenc_plane[1], i_stride2, height );
589 590
        memcpy( h->mb.pic.p_fdec[1]-FDEC_STRIDE, intra_fdec, 8*sizeof(pixel) );
        memcpy( h->mb.pic.p_fdec[2]-FDEC_STRIDE, intra_fdec+8, 8*sizeof(pixel) );
591 592
        h->mb.pic.p_fdec[1][-FDEC_STRIDE-1] = intra_fdec[-1-8];
        h->mb.pic.p_fdec[2][-FDEC_STRIDE-1] = intra_fdec[-1];
593 594 595
    }
    else
    {
Fiona Glaser's avatar
Fiona Glaser committed
596 597
        h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE, h->mb.pic.p_fenc_plane[i], i_stride2, 16 );
        memcpy( h->mb.pic.p_fdec[i]-FDEC_STRIDE, intra_fdec, 24*sizeof(pixel) );
598
        h->mb.pic.p_fdec[i][-FDEC_STRIDE-1] = intra_fdec[-1];
599
    }
600
    if( b_mbaff || h->mb.b_reencode_mb )
601
    {
Henrik Gramner's avatar
Henrik Gramner committed
602
        for( int j = 0; j < height; j++ )
Fiona Glaser's avatar
Fiona Glaser committed
603
            if( b_chroma )
604 605 606 607 608
            {
                h->mb.pic.p_fdec[1][-1+j*FDEC_STRIDE] = plane_fdec[-2+j*i_stride2];
                h->mb.pic.p_fdec[2][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
            }
            else
Fiona Glaser's avatar
Fiona Glaser committed
609
                h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
610
    }
Simon Horlick's avatar
Simon Horlick committed
611
    pixel *plane_src, **filtered_src;
612
    for( int j = 0; j < h->mb.pic.i_fref[0]; j++ )
613
    {
Simon Horlick's avatar
Simon Horlick committed
614
        // Interpolate between pixels in same field.
Fiona Glaser's avatar
Fiona Glaser committed
615
        if( mb_interlaced )
Simon Horlick's avatar
Simon Horlick committed
616 617
        {
            plane_src = h->fref[0][j>>1]->plane_fld[i];
Fiona Glaser's avatar
Fiona Glaser committed
618
            filtered_src = h->fref[0][j>>1]->filtered_fld[i];
Simon Horlick's avatar
Simon Horlick committed
619 620 621 622
        }
        else
        {
            plane_src = h->fref[0][j]->plane[i];
Fiona Glaser's avatar
Fiona Glaser committed
623
            filtered_src = h->fref[0][j]->filtered[i];
Simon Horlick's avatar
Simon Horlick committed
624
        }
Fiona Glaser's avatar
Fiona Glaser committed
625
        h->mb.pic.p_fref[0][j][i*4] = plane_src + ref_pix_offset[j&1];
Simon Horlick's avatar
Simon Horlick committed
626

Fiona Glaser's avatar
Fiona Glaser committed
627
        if( !b_chroma )
628
        {
629
            for( int k = 1; k < 4; k++ )
Fiona Glaser's avatar
Fiona Glaser committed
630 631 632 633 634 635 636 637
                h->mb.pic.p_fref[0][j][i*4+k] = filtered_src[k] + ref_pix_offset[j&1];
            if( !i )
            {
                if( h->sh.weight[j][0].weightfn )
                    h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> mb_interlaced][ref_pix_offset[j&1]];
                else
                    h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
            }
638
        }
639 640
    }
    if( h->sh.i_type == SLICE_TYPE_B )
641
        for( int j = 0; j < h->mb.pic.i_fref[1]; j++ )
642
        {
Fiona Glaser's avatar
Fiona Glaser committed
643
            if( mb_interlaced )
Simon Horlick's avatar
Simon Horlick committed
644 645
            {
                plane_src = h->fref[1][j>>1]->plane_fld[i];
Fiona Glaser's avatar
Fiona Glaser committed
646
                filtered_src = h->fref[1][j>>1]->filtered_fld[i];
Simon Horlick's avatar
Simon Horlick committed
647 648 649 650
            }
            else
            {
                plane_src = h->fref[1][j]->plane[i];
Fiona Glaser's avatar
Fiona Glaser committed
651
                filtered_src = h->fref[1][j]->filtered[i];
Simon Horlick's avatar
Simon Horlick committed
652
            }
Fiona Glaser's avatar
Fiona Glaser committed
653
            h->mb.pic.p_fref[1][j][i*4] = plane_src + ref_pix_offset[j&1];
Simon Horlick's avatar
Simon Horlick committed
654

Fiona Glaser's avatar
Fiona Glaser committed
655
            if( !b_chroma )
656
                for( int k = 1; k < 4; k++ )
Fiona Glaser's avatar
Fiona Glaser committed
657
                    h->mb.pic.p_fref[1][j][i*4+k] = filtered_src[k] + ref_pix_offset[j&1];
658 659 660
        }
}

661
static const x264_left_table_t left_indices[4] =
662 663
{
    /* Current is progressive */
Fiona Glaser's avatar
Fiona Glaser committed
664 665
    {{ 4, 4, 5, 5}, { 3,  3,  7,  7}, {16+1, 16+1, 32+1, 32+1}, {0, 0, 1, 1}, {0, 0, 0, 0}},
    {{ 6, 6, 3, 3}, {11, 11, 15, 15}, {16+5, 16+5, 32+5, 32+5}, {2, 2, 3, 3}, {1, 1, 1, 1}},
666
    /* Current is interlaced */
Fiona Glaser's avatar
Fiona Glaser committed
667
    {{ 4, 6, 4, 6}, { 3, 11,  3, 11}, {16+1, 16+1, 32+1, 32+1}, {0, 2, 0, 2}, {0, 1, 0, 1}},
668
    /* Both same */
Fiona Glaser's avatar
Fiona Glaser committed
669
    {{ 4, 5, 6, 3}, { 3,  7, 11, 15}, {16+1, 16+5, 32+1, 32+5}, {0, 1, 2, 3}, {0, 0, 1, 1}}
670 671
};

672
static ALWAYS_INLINE void macroblock_cache_load_neighbours( x264_t *h, int mb_x, int mb_y, int b_interlaced )
673
{
674 675
    const int mb_interlaced = b_interlaced && MB_INTERLACED;
    int top_y = mb_y - (1 << mb_interlaced);
676
    int top = top_y * h->mb.i_mb_stride + mb_x;
677

678 679 680 681 682
    h->mb.i_mb_x = mb_x;
    h->mb.i_mb_y = mb_y;
    h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
    h->mb.i_b8_xy = 2*(mb_y * h->mb.i_b8_stride + mb_x);
    h->mb.i_b4_xy = 4*(mb_y * h->mb.i_b4_stride + mb_x);
683 684 685 686
    h->mb.left_b8[0] =
    h->mb.left_b8[1] = -1;
    h->mb.left_b4[0] =
    h->mb.left_b4[1] = -1;
687
    h->mb.i_neighbour = 0;
688
    h->mb.i_neighbour_intra = 0;
689 690
    h->mb.i_neighbour_frame = 0;
    h->mb.i_mb_top_xy = -1;
691
    h->mb.i_mb_top_y = -1;
692
    h->mb.i_mb_left_xy[0] = h->mb.i_mb_left_xy[1] = -1;
693 694 695
    h->mb.i_mb_topleft_xy = -1;
    h->mb.i_mb_topright_xy = -1;
    h->mb.i_mb_type_top = -1;
696
    h->mb.i_mb_type_left[0] = h->mb.i_mb_type_left[1] = -1;
697 698
    h->mb.i_mb_type_topleft = -1;
    h->mb.i_mb_type_topright = -1;
699
    h->mb.left_index_table = &left_indices[3];
700 701 702 703 704 705 706 707 708 709
    h->mb.topleft_partition = 0;

    int topleft_y = top_y;
    int topright_y = top_y;
    int left[2];

    left[0] = left[1] = h->mb.i_mb_xy - 1;
    h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2;
    h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4;

710
    if( b_interlaced )
711
    {
712 713 714 715
        h->mb.i_mb_top_mbpair_xy = h->mb.i_mb_xy - 2*h->mb.i_mb_stride;
        h->mb.i_mb_topleft_y = -1;
        h->mb.i_mb_topright_y = -1;

716 717
        if( mb_y&1 )
        {
718
            if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
719 720 721 722 723
            {
                left[0] = left[1] = h->mb.i_mb_xy - 1 - h->mb.i_mb_stride;
                h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2 - 2*h->mb.i_b8_stride;
                h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4 - 4*h->mb.i_b4_stride;

724
                if( mb_interlaced )
725 726 727 728 729 730 731 732 733 734 735 736 737
                {
                    h->mb.left_index_table = &left_indices[2];
                    left[1] += h->mb.i_mb_stride;
                    h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
                    h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
                }
                else
                {
                    h->mb.left_index_table = &left_indices[1];
                    topleft_y++;
                    h->mb.topleft_partition = 1;
                }
            }
738
            if( !mb_interlaced )
739 740 741 742
                topright_y = -1;
        }
        else
        {
743
            if( mb_interlaced && top >= 0 )
744 745 746 747 748 749 750 751 752 753 754
            {
                if( !h->mb.field[top] )
                {
                    top += h->mb.i_mb_stride;
                    top_y++;
                }
                if( mb_x )
                    topleft_y += !h->mb.field[h->mb.i_mb_stride*topleft_y + mb_x - 1];
                if( mb_x < h->mb.i_mb_width-1 )
                    topright_y += !h->mb.field[h->mb.i_mb_stride*topright_y + mb_x + 1];
            }
755
            if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
756
            {
757
                if( mb_interlaced )
758 759 760 761 762 763 764 765 766 767 768
                {
                    h->mb.left_index_table = &left_indices[2];
                    left[1] += h->mb.i_mb_stride;
                    h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
                    h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
                }
                else
                    h->mb.left_index_table = &left_indices[0];
            }
        }
    }
769 770

    if( mb_x > 0 )
771
    {
772
        h->mb.i_neighbour_frame |= MB_LEFT;
773 774
        h->mb.i_mb_left_xy[0] = left[0];
        h->mb.i_mb_left_xy[1] = left[1];
775
        h->mb.i_mb_type_left[0] = h->mb.type[h->mb.i_mb_left_xy[0]];
776
        h->mb.i_mb_type_left[1] = h->mb.type[h->mb.i_mb_left_xy[1]];
777
        if( h->mb.slice_table[left[0]] == h->sh.i_first_mb )
778 779 780
        {
            h->mb.i_neighbour |= MB_LEFT;

781
            // FIXME: We don't currently support constrained intra + mbaff.
782
            if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_left[0] ) )
783 784 785 786
                h->mb.i_neighbour_intra |= MB_LEFT;
        }
    }

787
    /* We can't predict from the previous threadslice since it hasn't been encoded yet. */
788
    if( (h->i_threadslice_start >> mb_interlaced) != (mb_y >> mb_interlaced) )
789
    {
790
        if( top >= 0 )
791
        {
792 793
            h->mb.i_neighbour_frame |= MB_TOP;
            h->mb.i_mb_top_xy = top;
794
            h->mb.i_mb_top_y = top_y;
795
            h->mb.i_mb_type_top = h->mb.type[h->mb.i_mb_top_xy];
796
            if( h->mb.slice_table[top] == h->sh.i_first_mb )
797 798 799 800 801
            {
                h->mb.i_neighbour |= MB_TOP;

                if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_top ) )
                    h->mb.i_neighbour_intra |= MB_TOP;
802 803 804 805 806 807 808 809 810 811

                /* We only need to prefetch the top blocks because the left was just written
                 * to as part of the previous cache_save.  Since most target CPUs use write-allocate
                 * caches, left blocks are near-guaranteed to be in L1 cache.  Top--not so much. */
                x264_prefetch( &h->mb.cbp[top] );
                x264_prefetch( h->mb.intra4x4_pred_mode[top] );
                x264_prefetch( &h->mb.non_zero_count[top][12] );
                /* These aren't always allocated, but prefetching an invalid address can't hurt. */
                x264_prefetch( &h->mb.mb_transform_size[top] );
                x264_prefetch( &h->mb.skipbp[top] );
812 813 814
            }
        }

815
        if( mb_x > 0 && topleft_y >= 0  )
816 817
        {
            h->mb.i_neighbour_frame |= MB_TOPLEFT;
818 819
            h->mb.i_mb_topleft_xy = h->mb.i_mb_stride*topleft_y + mb_x - 1;
            h->mb.i_mb_topleft_y = topleft_y;
820
            h->mb.i_mb_type_topleft = h->mb.type[h->mb.i_mb_topleft_xy];
821
            if( h->mb.slice_table[h->mb.i_mb_topleft_xy] == h->sh.i_first_mb )
822 823
            {
                h->mb.i_neighbour |= MB_TOPLEFT;
824

825 826 827
                if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
                    h->mb.i_neighbour_intra |= MB_TOPLEFT;
            }
828 829
        }

830
        if( mb_x < h->mb.i_mb_width - 1 && topright_y >= 0 )
831
        {
832
            h->mb.i_neighbour_frame |= MB_TOPRIGHT;
833 834
            h->mb.i_mb_topright_xy = h->mb.i_mb_stride*topright_y + mb_x + 1;
            h->mb.i_mb_topright_y = topright_y;
835
            h->mb.i_mb_type_topright = h->mb.type[h->mb.i_mb_topright_xy];
836
            if( h->mb.slice_table[h->mb.i_mb_topright_xy] == h->sh.i_first_mb )
837 838
            {
                h->mb.i_neighbour |= MB_TOPRIGHT;
839

840 841 842
                if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
                    h->mb.i_neighbour_intra |= MB_TOPRIGHT;
            }
843 844 845
        }
    }
}
846

847 848 849 850 851 852 853
#define LTOP 0
#if HAVE_INTERLACED
#   define LBOT 1
#else
#   define LBOT 0
#endif

854
static ALWAYS_INLINE void macroblock_cache_load( x264_t *h, int mb_x, int mb_y, int b_mbaff )
855
{
856
    macroblock_cache_load_neighbours( h, mb_x, mb_y, b_mbaff );
857

858
    int *left = h->mb.i_mb_left_xy;
859
    int top  = h->mb.i_mb_top_xy;
860
    int top_y = h->mb.i_mb_top_y;
861 862 863 864 865
    int s8x8 = h->mb.i_b8_stride;
    int s4x4 = h->mb.i_b4_stride;
    int top_8x8 = (2*top_y+1) * s8x8 + 2*mb_x;
    int top_4x4 = (4*top_y+3) * s4x4 + 4*mb_x;
    int lists = (1 << h->sh.i_type) & 3;
866

867 868 869
    /* GCC pessimizes direct loads from heap-allocated arrays due to aliasing. */
    /* By only dereferencing them once, we avoid this issue. */
    int8_t (*i4x4)[8] = h->mb.intra4x4_pred_mode;
Fiona Glaser's avatar
Fiona Glaser committed
870
    uint8_t (*nnz)[48] = h->mb.non_zero_count;
871 872
    int16_t *cbp = h->mb.cbp;

873
    const x264_left_table_t *left_index_table = h->mb.left_index_table;
874

875 876
    h->mb.cache.deblock_strength = h->deblock_strength[mb_y&1][h->param.b_sliced_threads?h->mb.i_mb_xy:mb_x];

877 878 879
    /* load cache */
    if( h->mb.i_neighbour & MB_TOP )
    {
880
        h->mb.cache.i_cbp_top = cbp[top];
881
        /* load intra4x4 */
882
        CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &i4x4[top][0] );
883 884

        /* load non_zero_count */
Fiona Glaser's avatar
Fiona Glaser committed
885
        CP32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8], &nnz[top][12] );
886 887
        CP32( &h->mb.cache.non_zero_count[x264_scan8[16] - 8], &nnz[top][16-4 + (16>>CHROMA_V_SHIFT)] );
        CP32( &h->mb.cache.non_zero_count[x264_scan8[32] - 8], &nnz[top][32-4 + (16>>CHROMA_V_SHIFT)] );
888 889 890 891 892 893 894 895 896 897 898

        /* Finish the prefetching */
        for( int l = 0; l < lists; l++ )
        {
            x264_prefetch( &h->mb.mv[l][top_4x4-1] );
            /* Top right being not in the same cacheline as top left will happen
             * once every 4 MBs, so one extra prefetch is worthwhile */
            x264_prefetch( &h->mb.mv[l][top_4x4+4] );
            x264_prefetch( &h->mb.ref[l][top_8x8-1] );
            x264_prefetch( &h->mb.mvd[l][top] );
        }
899 900 901
    }
    else
    {
902
        h->mb.cache.i_cbp_top = -1;
903

904
        /* load intra4x4 */
905
        M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
906 907

        /* load non_zero_count */
Fiona Glaser's avatar
Fiona Glaser committed
908 909 910
        M32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8] ) = 0x80808080U;
        M32( &h->mb.cache.non_zero_count[x264_scan8[16] - 8] ) = 0x80808080U;
        M32( &h->mb.cache.non_zero_count[x264_scan8[32] - 8] ) = 0x80808080U;
911 912
    }

913
    if( h->mb.i_neighbour & MB_LEFT )
914
    {
Fiona Glaser's avatar
Fiona Glaser committed
915 916
        int ltop = left[LTOP];
        int lbot = b_mbaff ? left[LBOT] : ltop;
917
        if( b_mbaff )
918
        {
Fiona Glaser's avatar
Fiona Glaser committed
919 920 921
            const int16_t top_luma = (cbp[ltop] >> (left_index_table->mv[0]&(~1))) & 2;
            const int16_t bot_luma = (cbp[lbot] >> (left_index_table->mv[2]&(~1))) & 2;
            h->mb.cache.i_cbp_left = (cbp[ltop] & 0xfff0) | (bot_luma<<2) | top_luma;
922 923
        }
        else
Fiona Glaser's avatar
Fiona Glaser committed
924 925 926 927 928 929 930 931 932 933 934 935 936 937
            h->mb.cache.i_cbp_left = cbp[ltop];

        /* load intra4x4 */
        h->mb.cache.intra4x4_pred_mode[x264_scan8[ 0] - 1] = i4x4[ltop][left_index_table->intra[0]];
        h->mb.cache.intra4x4_pred_mode[x264_scan8[ 2] - 1] = i4x4[ltop][left_index_table->intra[1]];
        h->mb.cache.intra4x4_pred_mode[x264_scan8[ 8] - 1] = i4x4[lbot][left_index_table->intra[2]];
        h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = i4x4[lbot][left_index_table->intra[3]];

        /* load non_zero_count */
        h->mb.cache.non_zero_count[x264_scan8[ 0] - 1] = nnz[ltop][left_index_table->nnz[0]];
        h->mb.cache.non_zero_count[x264_scan8[ 2] - 1] = nnz[ltop][left_index_table->nnz[1]];
        h->mb.cache.non_zero_count[x264_scan8[ 8] - 1] = nnz[lbot][left_index_table->nnz[2]];
        h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[lbot][left_index_table->nnz[3]];

Henrik Gramner's avatar
Henrik Gramner committed
938
        if( CHROMA_FORMAT >= CHROMA_422 )
939
        {
940
            int offset = (4>>CHROMA_H_SHIFT) - 4;
Henrik Gramner's avatar
Henrik Gramner committed
941 942 943 944 945 946 947 948
            h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] = nnz[ltop][left_index_table->nnz[0]+16+offset];
            h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] = nnz[ltop][left_index_table->nnz[1]+16+offset];
            h->mb.cache.non_zero_count[x264_scan8[16+ 8] - 1] = nnz[lbot][left_index_table->nnz[2]+16+offset];
            h->mb.cache.non_zero_count[x264_scan8[16+10] - 1] = nnz[lbot][left_index_table->nnz[3]+16+offset];
            h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] = nnz[ltop][left_index_table->nnz[0]+32+offset];
            h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = nnz[ltop][left_index_table->nnz[1]+32+offset];
            h->mb.cache.non_zero_count[x264_scan8[32+ 8] - 1] = nnz[lbot][left_index_table->nnz[2]+32+offset];
            h->mb.cache.non_zero_count[x264_scan8[32+10] - 1] = nnz[lbot][left_index_table->nnz[3]+32+offset];
949 950 951
        }
        else
        {
Fiona Glaser's avatar
Fiona Glaser committed
952 953 954 955
            h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] = nnz[ltop][left_index_table->nnz_chroma[0]];
            h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] = nnz[lbot][left_index_table->nnz_chroma[1]];
            h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] = nnz[ltop][left_index_table->nnz_chroma[2]];
            h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = nnz[lbot][left_index_table->nnz_chroma[3]];
956
        }
957 958 959
    }
    else
    {
960
        h->mb.cache.i_cbp_left = -1;
961

Fiona Glaser's avatar
Fiona Glaser committed
962 963 964
        h->mb.cache.intra4x4_pred_mode[x264_scan8[ 0] - 1] =
        h->mb.cache.intra4x4_pred_mode[x264_scan8[ 2] - 1] =
        h->mb.cache.intra4x4_pred_mode[x264_scan8[ 8] - 1] =
965 966 967
        h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;

        /* load non_zero_count */
Fiona Glaser's avatar
Fiona Glaser committed
968 969 970
        h->mb.cache.non_zero_count[x264_scan8[ 0] - 1] =
        h->mb.cache.non_zero_count[x264_scan8[ 2] - 1] =
        h->mb.cache.non_zero_count[x264_scan8[ 8] - 1] =
971
        h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
Fiona Glaser's avatar
Fiona Glaser committed
972 973 974 975
        h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] =
        h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] =
        h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] =
        h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = 0x80;
Henrik Gramner's avatar
Henrik Gramner committed
976
        if( CHROMA_FORMAT >= CHROMA_422 )
Fiona Glaser's avatar
Fiona Glaser committed
977 978 979 980 981 982
        {
            h->mb.cache.non_zero_count[x264_scan8[16+ 8] - 1] =
            h->mb.cache.non_zero_count[x264_scan8[16+10] - 1] =
            h->mb.cache.non_zero_count[x264_scan8[32+ 8] - 1] =
            h->mb.cache.non_zero_count[x264_scan8[32+10] - 1] = 0x80;
        }
983 984
    }

985
    if( h->pps->b_transform_8x8_mode )
986
    {
987
        h->mb.cache.i_neighbour_transform_size =
988
            ( (h->mb.i_neighbour & MB_LEFT) && h->mb.mb_transform_size[left[0]] )
989
          + ( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top]  );
990 991
    }

992
    if( b_mbaff )
993
    {
994 995
        h->mb.pic.i_fref[0] = h->i_ref[0] << MB_INTERLACED;
        h->mb.pic.i_fref[1] = h->i_ref[1] << MB_INTERLACED;
996 997
    }

998
    if( !b_mbaff )
999
    {
1000 1001
        x264_copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
        x264_copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
1002
        macroblock_load_pic_pointers( h, mb_x, mb_y, 0, 0, 0 );
Fiona Glaser's avatar
Fiona Glaser committed
1003 1004 1005 1006 1007 1008
        if( CHROMA444 )
        {
            x264_copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+15+ 4*FDEC_STRIDE );
            x264_copy_column8( h->mb.pic.p_fdec[1]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[1]+15+12*FDEC_STRIDE );
            x264_copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+15+ 4*FDEC_STRIDE );
            x264_copy_column8( h->mb.pic.p_fdec[2]-