Coverage Report

Created: 2026-04-01 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/x264/encoder/me.c
Line
Count
Source
1
/*****************************************************************************
2
 * me.c: motion estimation
3
 *****************************************************************************
4
 * Copyright (C) 2003-2025 x264 project
5
 *
6
 * Authors: Loren Merritt <lorenm@u.washington.edu>
7
 *          Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Fiona Glaser <fiona@x264.com>
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23
 *
24
 * This program is also available under a commercial proprietary license.
25
 * For more information, contact us at licensing@x264.com.
26
 *****************************************************************************/
27
28
#include "common/common.h"
29
#include "macroblock.h"
30
#include "me.h"
31
32
/* presets selected from good points on the speed-vs-quality curve of several test videos
33
 * subpel_iters[i_subpel_refine] = { refine_hpel, refine_qpel, me_hpel, me_qpel }
34
 * where me_* are the number of EPZS iterations run on all candidate block types,
35
 * and refine_* are run only on the winner.
36
 * the subme=8,9 values are much higher because any amount of satd search makes
37
 * up its time by reducing the number of qpel-rd iterations. */
38
static const uint8_t subpel_iterations[][4] =
39
   {{0,0,0,0},
40
    {1,1,0,0},
41
    {0,1,1,0},
42
    {0,2,1,0},
43
    {0,2,1,1},
44
    {0,2,1,2},
45
    {0,0,2,2},
46
    {0,0,2,2},
47
    {0,0,4,10},
48
    {0,0,4,10},
49
    {0,0,4,10},
50
    {0,0,4,10}};
51
52
/* (x-1)%6 */
53
static const uint8_t mod6m1[8] = {5,0,1,2,3,4,5,0};
54
/* radius 2 hexagon. repeated entries are to avoid having to compute mod6 every time. */
55
static const int8_t hex2[8][2] = {{-1,-2}, {-2,0}, {-1,2}, {1,2}, {2,0}, {1,-2}, {-1,-2}, {-2,0}};
56
static const int8_t square1[9][2] = {{0,0}, {0,-1}, {0,1}, {-1,0}, {1,0}, {-1,-1}, {-1,1}, {1,-1}, {1,1}};
57
58
static void refine_subpel( x264_t *h, x264_me_t *m, int hpel_iters, int qpel_iters, int *p_halfpel_thresh, int b_refine_qpel );
59
60
#define BITS_MVD( mx, my )\
61
0
    (p_cost_mvx[(mx)*4] + p_cost_mvy[(my)*4])
62
63
0
#define COST_MV( mx, my )\
64
0
do\
65
0
{\
66
0
    int cost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE,\
67
0
                   &p_fref_w[(my)*stride+(mx)], stride )\
68
0
             + BITS_MVD(mx,my);\
69
0
    COPY3_IF_LT( bcost, cost, bmx, mx, bmy, my );\
70
0
} while( 0 )
71
72
0
#define COST_MV_HPEL( mx, my, cost )\
73
0
do\
74
0
{\
75
0
    intptr_t stride2 = 16;\
76
0
    pixel *src = h->mc.get_ref( pix, &stride2, m->p_fref, stride, mx, my, bw, bh, &m->weight[0] );\
77
0
    cost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE, src, stride2 )\
78
0
         + p_cost_mvx[ mx ] + p_cost_mvy[ my ];\
79
0
} while( 0 )
80
81
0
#define COST_MV_X3_DIR( m0x, m0y, m1x, m1y, m2x, m2y, costs )\
82
0
{\
83
0
    pixel *pix_base = p_fref_w + bmx + bmy*stride;\
84
0
    h->pixf.fpelcmp_x3[i_pixel]( p_fenc,\
85
0
        pix_base + (m0x) + (m0y)*stride,\
86
0
        pix_base + (m1x) + (m1y)*stride,\
87
0
        pix_base + (m2x) + (m2y)*stride,\
88
0
        stride, costs );\
89
0
    (costs)[0] += BITS_MVD( bmx+(m0x), bmy+(m0y) );\
90
0
    (costs)[1] += BITS_MVD( bmx+(m1x), bmy+(m1y) );\
91
0
    (costs)[2] += BITS_MVD( bmx+(m2x), bmy+(m2y) );\
92
0
}
93
94
0
#define COST_MV_X4_DIR( m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y, costs )\
95
0
{\
96
0
    pixel *pix_base = p_fref_w + bmx + bmy*stride;\
97
0
    h->pixf.fpelcmp_x4[i_pixel]( p_fenc,\
98
0
        pix_base + (m0x) + (m0y)*stride,\
99
0
        pix_base + (m1x) + (m1y)*stride,\
100
0
        pix_base + (m2x) + (m2y)*stride,\
101
0
        pix_base + (m3x) + (m3y)*stride,\
102
0
        stride, costs );\
103
0
    (costs)[0] += BITS_MVD( bmx+(m0x), bmy+(m0y) );\
104
0
    (costs)[1] += BITS_MVD( bmx+(m1x), bmy+(m1y) );\
105
0
    (costs)[2] += BITS_MVD( bmx+(m2x), bmy+(m2y) );\
106
0
    (costs)[3] += BITS_MVD( bmx+(m3x), bmy+(m3y) );\
107
0
}
108
109
0
#define COST_MV_X4( m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y )\
110
0
{\
111
0
    pixel *pix_base = p_fref_w + omx + omy*stride;\
112
0
    h->pixf.fpelcmp_x4[i_pixel]( p_fenc,\
113
0
        pix_base + (m0x) + (m0y)*stride,\
114
0
        pix_base + (m1x) + (m1y)*stride,\
115
0
        pix_base + (m2x) + (m2y)*stride,\
116
0
        pix_base + (m3x) + (m3y)*stride,\
117
0
        stride, costs );\
118
0
    costs[0] += BITS_MVD( omx+(m0x), omy+(m0y) );\
119
0
    costs[1] += BITS_MVD( omx+(m1x), omy+(m1y) );\
120
0
    costs[2] += BITS_MVD( omx+(m2x), omy+(m2y) );\
121
0
    costs[3] += BITS_MVD( omx+(m3x), omy+(m3y) );\
122
0
    COPY3_IF_LT( bcost, costs[0], bmx, omx+(m0x), bmy, omy+(m0y) );\
123
0
    COPY3_IF_LT( bcost, costs[1], bmx, omx+(m1x), bmy, omy+(m1y) );\
124
0
    COPY3_IF_LT( bcost, costs[2], bmx, omx+(m2x), bmy, omy+(m2y) );\
125
0
    COPY3_IF_LT( bcost, costs[3], bmx, omx+(m3x), bmy, omy+(m3y) );\
126
0
}
127
128
0
#define COST_MV_X3_ABS( m0x, m0y, m1x, m1y, m2x, m2y )\
129
0
{\
130
0
    h->pixf.fpelcmp_x3[i_pixel]( p_fenc,\
131
0
        p_fref_w + (m0x) + (m0y)*stride,\
132
0
        p_fref_w + (m1x) + (m1y)*stride,\
133
0
        p_fref_w + (m2x) + (m2y)*stride,\
134
0
        stride, costs );\
135
0
    costs[0] += p_cost_mvx[(m0x)*4]; /* no cost_mvy */\
136
0
    costs[1] += p_cost_mvx[(m1x)*4];\
137
0
    costs[2] += p_cost_mvx[(m2x)*4];\
138
0
    COPY3_IF_LT( bcost, costs[0], bmx, m0x, bmy, m0y );\
139
0
    COPY3_IF_LT( bcost, costs[1], bmx, m1x, bmy, m1y );\
140
0
    COPY3_IF_LT( bcost, costs[2], bmx, m2x, bmy, m2y );\
141
0
}
142
143
/*  1  */
144
/* 101 */
145
/*  1  */
146
0
#define DIA1_ITER( mx, my )\
147
0
{\
148
0
    omx = mx; omy = my;\
149
0
    COST_MV_X4( 0,-1, 0,1, -1,0, 1,0 );\
150
0
}
151
152
0
#define CROSS( start, x_max, y_max )\
153
0
{\
154
0
    int i = start;\
155
0
    if( (x_max) <= X264_MIN(mv_x_max-omx, omx-mv_x_min) )\
156
0
        for( ; i < (x_max)-2; i+=4 )\
157
0
            COST_MV_X4( i,0, -i,0, i+2,0, -i-2,0 );\
158
0
    for( ; i < (x_max); i+=2 )\
159
0
    {\
160
0
        if( omx+i <= mv_x_max )\
161
0
            COST_MV( omx+i, omy );\
162
0
        if( omx-i >= mv_x_min )\
163
0
            COST_MV( omx-i, omy );\
164
0
    }\
165
0
    i = start;\
166
0
    if( (y_max) <= X264_MIN(mv_y_max-omy, omy-mv_y_min) )\
167
0
        for( ; i < (y_max)-2; i+=4 )\
168
0
            COST_MV_X4( 0,i, 0,-i, 0,i+2, 0,-i-2 );\
169
0
    for( ; i < (y_max); i+=2 )\
170
0
    {\
171
0
        if( omy+i <= mv_y_max )\
172
0
            COST_MV( omx, omy+i );\
173
0
        if( omy-i >= mv_y_min )\
174
0
            COST_MV( omx, omy-i );\
175
0
    }\
176
0
}
177
178
0
#define FPEL(mv) (((mv)+2)>>2) /* Convert subpel MV to fullpel with rounding... */
179
0
#define SPEL(mv) ((mv)*4)      /* ... and the reverse. */
180
0
#define SPELx2(mv) (SPEL(mv)&0xFFFCFFFC) /* for two packed MVs */
181
182
void x264_me_search_ref( x264_t *h, x264_me_t *m, int16_t (*mvc)[2], int i_mvc, int *p_halfpel_thresh )
183
0
{
184
0
    const int bw = x264_pixel_size[m->i_pixel].w;
185
0
    const int bh = x264_pixel_size[m->i_pixel].h;
186
0
    const int i_pixel = m->i_pixel;
187
0
    const int stride = m->i_stride[0];
188
0
    int i_me_range = h->param.analyse.i_me_range;
189
0
    int bmx, bmy, bcost = COST_MAX;
190
0
    int bpred_cost = COST_MAX;
191
0
    int omx, omy, pmx, pmy;
192
0
    pixel *p_fenc = m->p_fenc[0];
193
0
    pixel *p_fref_w = m->p_fref_w;
194
0
    ALIGNED_ARRAY_32( pixel, pix,[16*16] );
195
0
    ALIGNED_ARRAY_8( int16_t, mvc_temp,[16],[2] );
196
197
0
    ALIGNED_ARRAY_16( int, costs,[16] );
198
199
0
    int mv_x_min = h->mb.mv_limit_fpel[0][0];
200
0
    int mv_y_min = h->mb.mv_limit_fpel[0][1];
201
0
    int mv_x_max = h->mb.mv_limit_fpel[1][0];
202
0
    int mv_y_max = h->mb.mv_limit_fpel[1][1];
203
/* Special version of pack to allow shortcuts in CHECK_MVRANGE */
204
0
#define pack16to32_mask2(mx,my) (((uint32_t)(mx)<<16)|((uint32_t)(my)&0x7FFF))
205
0
    uint32_t mv_min = pack16to32_mask2( -mv_x_min, -mv_y_min );
206
0
    uint32_t mv_max = pack16to32_mask2( mv_x_max, mv_y_max )|0x8000;
207
0
    uint32_t pmv, bpred_mv = 0;
208
209
0
#define CHECK_MVRANGE(mx,my) (!(((pack16to32_mask2(mx,my) + mv_min) | (mv_max - pack16to32_mask2(mx,my))) & 0x80004000))
210
211
0
    const uint16_t *p_cost_mvx = m->p_cost_mv - m->mvp[0];
212
0
    const uint16_t *p_cost_mvy = m->p_cost_mv - m->mvp[1];
213
214
    /* Try extra predictors if provided.  If subme >= 3, check subpel predictors,
215
     * otherwise round them to fullpel. */
216
0
    if( h->mb.i_subpel_refine >= 3 )
217
0
    {
218
        /* Calculate and check the MVP first */
219
0
        int bpred_mx = x264_clip3( m->mvp[0], SPEL(mv_x_min), SPEL(mv_x_max) );
220
0
        int bpred_my = x264_clip3( m->mvp[1], SPEL(mv_y_min), SPEL(mv_y_max) );
221
0
        pmv = pack16to32_mask( bpred_mx, bpred_my );
222
0
        pmx = FPEL( bpred_mx );
223
0
        pmy = FPEL( bpred_my );
224
225
0
        COST_MV_HPEL( bpred_mx, bpred_my, bpred_cost );
226
0
        int pmv_cost = bpred_cost;
227
228
0
        if( i_mvc > 0 )
229
0
        {
230
            /* Clip MV candidates and eliminate those equal to zero and pmv. */
231
0
            int valid_mvcs = x264_predictor_clip( mvc_temp+2, mvc, i_mvc, h->mb.mv_limit_fpel, pmv );
232
0
            if( valid_mvcs > 0 )
233
0
            {
234
0
                int i = 1, cost;
235
                /* We stuff pmv here to branchlessly pick between pmv and the various
236
                 * MV candidates. [0] gets skipped in order to maintain alignment for
237
                 * x264_predictor_clip. */
238
0
                M32( mvc_temp[1] ) = pmv;
239
0
                bpred_cost <<= 4;
240
0
                do
241
0
                {
242
0
                    int mx = mvc_temp[i+1][0];
243
0
                    int my = mvc_temp[i+1][1];
244
0
                    COST_MV_HPEL( mx, my, cost );
245
0
                    COPY1_IF_LT( bpred_cost, (cost << 4) + i );
246
0
                } while( ++i <= valid_mvcs );
247
0
                bpred_mx = mvc_temp[(bpred_cost&15)+1][0];
248
0
                bpred_my = mvc_temp[(bpred_cost&15)+1][1];
249
0
                bpred_cost >>= 4;
250
0
            }
251
0
        }
252
253
        /* Round the best predictor back to fullpel and get the cost, since this is where
254
         * we'll be starting the fullpel motion search. */
255
0
        bmx = FPEL( bpred_mx );
256
0
        bmy = FPEL( bpred_my );
257
0
        bpred_mv = pack16to32_mask(bpred_mx, bpred_my);
258
0
        if( bpred_mv&0x00030003 ) /* Only test if the tested predictor is actually subpel... */
259
0
            COST_MV( bmx, bmy );
260
0
        else                          /* Otherwise just copy the cost (we already know it) */
261
0
            bcost = bpred_cost;
262
263
        /* Test the zero vector if it hasn't been tested yet. */
264
0
        if( pmv )
265
0
        {
266
0
            if( bmx|bmy ) COST_MV( 0, 0 );
267
0
        }
268
        /* If a subpel mv candidate was better than the zero vector, the previous
269
         * fullpel check won't have gotten it even if the pmv was zero. So handle
270
         * that possibility here. */
271
0
        else
272
0
        {
273
0
            COPY3_IF_LT( bcost, pmv_cost, bmx, 0, bmy, 0 );
274
0
        }
275
0
    }
276
0
    else
277
0
    {
278
        /* Calculate and check the fullpel MVP first */
279
0
        bmx = pmx = x264_clip3( FPEL(m->mvp[0]), mv_x_min, mv_x_max );
280
0
        bmy = pmy = x264_clip3( FPEL(m->mvp[1]), mv_y_min, mv_y_max );
281
0
        pmv = pack16to32_mask( bmx, bmy );
282
283
        /* Because we are rounding the predicted motion vector to fullpel, there will be
284
         * an extra MV cost in 15 out of 16 cases.  However, when the predicted MV is
285
         * chosen as the best predictor, it is often the case that the subpel search will
286
         * result in a vector at or next to the predicted motion vector.  Therefore, we omit
287
         * the cost of the MV from the rounded MVP to avoid unfairly biasing against use of
288
         * the predicted motion vector.
289
         *
290
         * Disclaimer: this is a post-hoc rationalization for why this hack works. */
291
0
        bcost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE, &p_fref_w[bmy*stride+bmx], stride );
292
293
0
        if( i_mvc > 0 )
294
0
        {
295
            /* Like in subme>=3, except we also round the candidates to fullpel. */
296
0
            int valid_mvcs = x264_predictor_roundclip( mvc_temp+2, mvc, i_mvc, h->mb.mv_limit_fpel, pmv );
297
0
            if( valid_mvcs > 0 )
298
0
            {
299
0
                int i = 1, cost;
300
0
                M32( mvc_temp[1] ) = pmv;
301
0
                bcost <<= 4;
302
0
                do
303
0
                {
304
0
                    int mx = mvc_temp[i+1][0];
305
0
                    int my = mvc_temp[i+1][1];
306
0
                    cost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE, &p_fref_w[my*stride+mx], stride ) + BITS_MVD( mx, my );
307
0
                    COPY1_IF_LT( bcost, (cost << 4) + i );
308
0
                } while( ++i <= valid_mvcs );
309
0
                bmx = mvc_temp[(bcost&15)+1][0];
310
0
                bmy = mvc_temp[(bcost&15)+1][1];
311
0
                bcost >>= 4;
312
0
            }
313
0
        }
314
315
        /* Same as above, except the condition is simpler. */
316
0
        if( pmv )
317
0
            COST_MV( 0, 0 );
318
0
    }
319
320
0
    switch( h->mb.i_me_method )
321
0
    {
322
0
        case X264_ME_DIA:
323
0
        {
324
            /* diamond search, radius 1 */
325
0
            bcost <<= 4;
326
0
            int i = i_me_range;
327
0
            do
328
0
            {
329
0
                COST_MV_X4_DIR( 0,-1, 0,1, -1,0, 1,0, costs );
330
0
                COPY1_IF_LT( bcost, (costs[0]<<4)+1 );
331
0
                COPY1_IF_LT( bcost, (costs[1]<<4)+3 );
332
0
                COPY1_IF_LT( bcost, (costs[2]<<4)+4 );
333
0
                COPY1_IF_LT( bcost, (costs[3]<<4)+12 );
334
0
                if( !(bcost&15) )
335
0
                    break;
336
0
                bmx -= (int32_t)((uint32_t)bcost<<28)>>30;
337
0
                bmy -= (int32_t)((uint32_t)bcost<<30)>>30;
338
0
                bcost &= ~15;
339
0
            } while( --i && CHECK_MVRANGE(bmx, bmy) );
340
0
            bcost >>= 4;
341
0
            break;
342
0
        }
343
344
0
        case X264_ME_HEX:
345
0
        {
346
0
    me_hex2:
347
            /* hexagon search, radius 2 */
348
    #if 0
349
            for( int i = 0; i < i_me_range/2; i++ )
350
            {
351
                omx = bmx; omy = bmy;
352
                COST_MV( omx-2, omy   );
353
                COST_MV( omx-1, omy+2 );
354
                COST_MV( omx+1, omy+2 );
355
                COST_MV( omx+2, omy   );
356
                COST_MV( omx+1, omy-2 );
357
                COST_MV( omx-1, omy-2 );
358
                if( bmx == omx && bmy == omy )
359
                    break;
360
                if( !CHECK_MVRANGE(bmx, bmy) )
361
                    break;
362
            }
363
    #else
364
            /* equivalent to the above, but eliminates duplicate candidates */
365
366
            /* hexagon */
367
0
            COST_MV_X3_DIR( -2,0, -1, 2,  1, 2, costs   );
368
0
            COST_MV_X3_DIR(  2,0,  1,-2, -1,-2, costs+4 ); /* +4 for 16-byte alignment */
369
0
            bcost <<= 3;
370
0
            COPY1_IF_LT( bcost, (costs[0]<<3)+2 );
371
0
            COPY1_IF_LT( bcost, (costs[1]<<3)+3 );
372
0
            COPY1_IF_LT( bcost, (costs[2]<<3)+4 );
373
0
            COPY1_IF_LT( bcost, (costs[4]<<3)+5 );
374
0
            COPY1_IF_LT( bcost, (costs[5]<<3)+6 );
375
0
            COPY1_IF_LT( bcost, (costs[6]<<3)+7 );
376
377
0
            if( bcost&7 )
378
0
            {
379
0
                int dir = (bcost&7)-2;
380
0
                bmx += hex2[dir+1][0];
381
0
                bmy += hex2[dir+1][1];
382
383
                /* half hexagon, not overlapping the previous iteration */
384
0
                for( int i = (i_me_range>>1) - 1; i > 0 && CHECK_MVRANGE(bmx, bmy); i-- )
385
0
                {
386
0
                    COST_MV_X3_DIR( hex2[dir+0][0], hex2[dir+0][1],
387
0
                                    hex2[dir+1][0], hex2[dir+1][1],
388
0
                                    hex2[dir+2][0], hex2[dir+2][1],
389
0
                                    costs );
390
0
                    bcost &= ~7;
391
0
                    COPY1_IF_LT( bcost, (costs[0]<<3)+1 );
392
0
                    COPY1_IF_LT( bcost, (costs[1]<<3)+2 );
393
0
                    COPY1_IF_LT( bcost, (costs[2]<<3)+3 );
394
0
                    if( !(bcost&7) )
395
0
                        break;
396
0
                    dir += (bcost&7)-2;
397
0
                    dir = mod6m1[dir+1];
398
0
                    bmx += hex2[dir+1][0];
399
0
                    bmy += hex2[dir+1][1];
400
0
                }
401
0
            }
402
0
            bcost >>= 3;
403
0
    #endif
404
            /* square refine */
405
0
            bcost <<= 4;
406
0
            COST_MV_X4_DIR(  0,-1,  0,1, -1,0, 1,0, costs );
407
0
            COPY1_IF_LT( bcost, (costs[0]<<4)+1 );
408
0
            COPY1_IF_LT( bcost, (costs[1]<<4)+2 );
409
0
            COPY1_IF_LT( bcost, (costs[2]<<4)+3 );
410
0
            COPY1_IF_LT( bcost, (costs[3]<<4)+4 );
411
0
            COST_MV_X4_DIR( -1,-1, -1,1, 1,-1, 1,1, costs );
412
0
            COPY1_IF_LT( bcost, (costs[0]<<4)+5 );
413
0
            COPY1_IF_LT( bcost, (costs[1]<<4)+6 );
414
0
            COPY1_IF_LT( bcost, (costs[2]<<4)+7 );
415
0
            COPY1_IF_LT( bcost, (costs[3]<<4)+8 );
416
0
            bmx += square1[bcost&15][0];
417
0
            bmy += square1[bcost&15][1];
418
0
            bcost >>= 4;
419
0
            break;
420
0
        }
421
422
0
        case X264_ME_UMH:
423
0
        {
424
            /* Uneven-cross Multi-Hexagon-grid Search
425
             * as in JM, except with different early termination */
426
427
0
            static const uint8_t pixel_size_shift[7] = { 0, 1, 1, 2, 3, 3, 4 };
428
429
0
            int ucost1, ucost2;
430
0
            int cross_start = 1;
431
432
            /* refine predictors */
433
0
            ucost1 = bcost;
434
0
            DIA1_ITER( pmx, pmy );
435
0
            if( pmx | pmy )
436
0
                DIA1_ITER( 0, 0 );
437
438
0
            if( i_pixel == PIXEL_4x4 )
439
0
                goto me_hex2;
440
441
0
            ucost2 = bcost;
442
0
            if( (bmx | bmy) && ((bmx-pmx) | (bmy-pmy)) )
443
0
                DIA1_ITER( bmx, bmy );
444
0
            if( bcost == ucost2 )
445
0
                cross_start = 3;
446
0
            omx = bmx; omy = bmy;
447
448
            /* early termination */
449
0
#define SAD_THRESH(v) ( bcost < ( v >> pixel_size_shift[i_pixel] ) )
450
0
            if( bcost == ucost2 && SAD_THRESH(2000) )
451
0
            {
452
0
                COST_MV_X4( 0,-2, -1,-1, 1,-1, -2,0 );
453
0
                COST_MV_X4( 2, 0, -1, 1, 1, 1,  0,2 );
454
0
                if( bcost == ucost1 && SAD_THRESH(500) )
455
0
                    break;
456
0
                if( bcost == ucost2 )
457
0
                {
458
0
                    int range = (i_me_range>>1) | 1;
459
0
                    CROSS( 3, range, range );
460
0
                    COST_MV_X4( -1,-2, 1,-2, -2,-1, 2,-1 );
461
0
                    COST_MV_X4( -2, 1, 2, 1, -1, 2, 1, 2 );
462
0
                    if( bcost == ucost2 )
463
0
                        break;
464
0
                    cross_start = range + 2;
465
0
                }
466
0
            }
467
468
            /* adaptive search range */
469
0
            if( i_mvc )
470
0
            {
471
                /* range multipliers based on casual inspection of some statistics of
472
                 * average distance between current predictor and final mv found by ESA.
473
                 * these have not been tuned much by actual encoding. */
474
0
                static const uint8_t range_mul[4][4] =
475
0
                {
476
0
                    { 3, 3, 4, 4 },
477
0
                    { 3, 4, 4, 4 },
478
0
                    { 4, 4, 4, 5 },
479
0
                    { 4, 4, 5, 6 },
480
0
                };
481
0
                int mvd;
482
0
                int sad_ctx, mvd_ctx;
483
0
                int denom = 1;
484
485
0
                if( i_mvc == 1 )
486
0
                {
487
0
                    if( i_pixel == PIXEL_16x16 )
488
                        /* mvc is probably the same as mvp, so the difference isn't meaningful.
489
                         * but prediction usually isn't too bad, so just use medium range */
490
0
                        mvd = 25;
491
0
                    else
492
0
                        mvd = abs( m->mvp[0] - mvc[0][0] )
493
0
                            + abs( m->mvp[1] - mvc[0][1] );
494
0
                }
495
0
                else
496
0
                {
497
                    /* calculate the degree of agreement between predictors. */
498
                    /* in 16x16, mvc includes all the neighbors used to make mvp,
499
                     * so don't count mvp separately. */
500
0
                    denom = i_mvc - 1;
501
0
                    mvd = 0;
502
0
                    if( i_pixel != PIXEL_16x16 )
503
0
                    {
504
0
                        mvd = abs( m->mvp[0] - mvc[0][0] )
505
0
                            + abs( m->mvp[1] - mvc[0][1] );
506
0
                        denom++;
507
0
                    }
508
0
                    mvd += x264_predictor_difference( mvc, i_mvc );
509
0
                }
510
511
0
                sad_ctx = SAD_THRESH(1000) ? 0
512
0
                        : SAD_THRESH(2000) ? 1
513
0
                        : SAD_THRESH(4000) ? 2 : 3;
514
0
                mvd_ctx = mvd < 10*denom ? 0
515
0
                        : mvd < 20*denom ? 1
516
0
                        : mvd < 40*denom ? 2 : 3;
517
518
0
                i_me_range = i_me_range * range_mul[mvd_ctx][sad_ctx] >> 2;
519
0
            }
520
521
            /* FIXME if the above DIA2/OCT2/CROSS found a new mv, it has not updated omx/omy.
522
             * we are still centered on the same place as the DIA2. is this desirable? */
523
0
            CROSS( cross_start, i_me_range, i_me_range>>1 );
524
525
0
            COST_MV_X4( -2,-2, -2,2, 2,-2, 2,2 );
526
527
            /* hexagon grid */
528
0
            omx = bmx; omy = bmy;
529
0
            const uint16_t *p_cost_omvx = p_cost_mvx + omx*4;
530
0
            const uint16_t *p_cost_omvy = p_cost_mvy + omy*4;
531
0
            int i = 1;
532
0
            do
533
0
            {
534
0
                static const int8_t hex4[16][2] = {
535
0
                    { 0,-4}, { 0, 4}, {-2,-3}, { 2,-3},
536
0
                    {-4,-2}, { 4,-2}, {-4,-1}, { 4,-1},
537
0
                    {-4, 0}, { 4, 0}, {-4, 1}, { 4, 1},
538
0
                    {-4, 2}, { 4, 2}, {-2, 3}, { 2, 3},
539
0
                };
540
541
0
                if( 4*i > X264_MIN4( mv_x_max-omx, omx-mv_x_min,
542
0
                                     mv_y_max-omy, omy-mv_y_min ) )
543
0
                {
544
0
                    for( int j = 0; j < 16; j++ )
545
0
                    {
546
0
                        int mx = omx + hex4[j][0]*i;
547
0
                        int my = omy + hex4[j][1]*i;
548
0
                        if( CHECK_MVRANGE(mx, my) )
549
0
                            COST_MV( mx, my );
550
0
                    }
551
0
                }
552
0
                else
553
0
                {
554
0
                    int dir = 0;
555
0
                    pixel *pix_base = p_fref_w + omx + (omy-4*i)*stride;
556
0
                    int dy = i*stride;
557
0
#define SADS(k,x0,y0,x1,y1,x2,y2,x3,y3)\
558
0
                    h->pixf.fpelcmp_x4[i_pixel]( p_fenc,\
559
0
                            pix_base x0*i+(y0-2*k+4)*dy,\
560
0
                            pix_base x1*i+(y1-2*k+4)*dy,\
561
0
                            pix_base x2*i+(y2-2*k+4)*dy,\
562
0
                            pix_base x3*i+(y3-2*k+4)*dy,\
563
0
                            stride, costs+4*k );\
564
0
                    pix_base += 2*dy;
565
0
#define ADD_MVCOST(k,x,y) costs[k] += p_cost_omvx[x*4*i] + p_cost_omvy[y*4*i]
566
0
#define MIN_MV(k,x,y)     COPY2_IF_LT( bcost, costs[k], dir, x*16+(y&15) )
567
0
                    SADS( 0, +0,-4, +0,+4, -2,-3, +2,-3 );
568
0
                    SADS( 1, -4,-2, +4,-2, -4,-1, +4,-1 );
569
0
                    SADS( 2, -4,+0, +4,+0, -4,+1, +4,+1 );
570
0
                    SADS( 3, -4,+2, +4,+2, -2,+3, +2,+3 );
571
0
                    ADD_MVCOST(  0, 0,-4 );
572
0
                    ADD_MVCOST(  1, 0, 4 );
573
0
                    ADD_MVCOST(  2,-2,-3 );
574
0
                    ADD_MVCOST(  3, 2,-3 );
575
0
                    ADD_MVCOST(  4,-4,-2 );
576
0
                    ADD_MVCOST(  5, 4,-2 );
577
0
                    ADD_MVCOST(  6,-4,-1 );
578
0
                    ADD_MVCOST(  7, 4,-1 );
579
0
                    ADD_MVCOST(  8,-4, 0 );
580
0
                    ADD_MVCOST(  9, 4, 0 );
581
0
                    ADD_MVCOST( 10,-4, 1 );
582
0
                    ADD_MVCOST( 11, 4, 1 );
583
0
                    ADD_MVCOST( 12,-4, 2 );
584
0
                    ADD_MVCOST( 13, 4, 2 );
585
0
                    ADD_MVCOST( 14,-2, 3 );
586
0
                    ADD_MVCOST( 15, 2, 3 );
587
0
                    MIN_MV(  0, 0,-4 );
588
0
                    MIN_MV(  1, 0, 4 );
589
0
                    MIN_MV(  2,-2,-3 );
590
0
                    MIN_MV(  3, 2,-3 );
591
0
                    MIN_MV(  4,-4,-2 );
592
0
                    MIN_MV(  5, 4,-2 );
593
0
                    MIN_MV(  6,-4,-1 );
594
0
                    MIN_MV(  7, 4,-1 );
595
0
                    MIN_MV(  8,-4, 0 );
596
0
                    MIN_MV(  9, 4, 0 );
597
0
                    MIN_MV( 10,-4, 1 );
598
0
                    MIN_MV( 11, 4, 1 );
599
0
                    MIN_MV( 12,-4, 2 );
600
0
                    MIN_MV( 13, 4, 2 );
601
0
                    MIN_MV( 14,-2, 3 );
602
0
                    MIN_MV( 15, 2, 3 );
603
0
#undef SADS
604
0
#undef ADD_MVCOST
605
0
#undef MIN_MV
606
0
                    if( dir )
607
0
                    {
608
0
                        bmx = omx + i*(dir>>4);
609
0
                        bmy = omy + i*((int32_t)((uint32_t)dir<<28)>>28);
610
0
                    }
611
0
                }
612
0
            } while( ++i <= i_me_range>>2 );
613
0
            if( bmy <= mv_y_max && bmy >= mv_y_min && bmx <= mv_x_max && bmx >= mv_x_min )
614
0
                goto me_hex2;
615
0
            break;
616
0
        }
617
618
0
        case X264_ME_ESA:
619
0
        case X264_ME_TESA:
620
0
        {
621
0
            const int min_x = X264_MAX( bmx - i_me_range, mv_x_min );
622
0
            const int min_y = X264_MAX( bmy - i_me_range, mv_y_min );
623
0
            const int max_x = X264_MIN( bmx + i_me_range, mv_x_max );
624
0
            const int max_y = X264_MIN( bmy + i_me_range, mv_y_max );
625
            /* SEA is fastest in multiples of 4 */
626
0
            const int width = (max_x - min_x + 3) & ~3;
627
#if 0
628
            /* plain old exhaustive search */
629
            for( int my = min_y; my <= max_y; my++ )
630
                for( int mx = min_x; mx < min_x + width; mx++ )
631
                    COST_MV( mx, my );
632
#else
633
            /* successive elimination by comparing DC before a full SAD,
634
             * because sum(abs(diff)) >= abs(diff(sum)). */
635
0
            uint16_t *sums_base = m->integral;
636
0
            ALIGNED_ARRAY_16( int, enc_dc,[4] );
637
0
            int sad_size = i_pixel <= PIXEL_8x8 ? PIXEL_8x8 : PIXEL_4x4;
638
0
            int delta = x264_pixel_size[sad_size].w;
639
0
            int16_t *xs = h->scratch_buffer;
640
0
            int xn;
641
0
            uint16_t *cost_fpel_mvx = h->cost_mv_fpel[h->mb.i_qp][-m->mvp[0]&3] + (-m->mvp[0]>>2);
642
643
0
            h->pixf.sad_x4[sad_size]( (pixel*)x264_zero, p_fenc, p_fenc+delta,
644
0
                p_fenc+delta*FENC_STRIDE, p_fenc+delta+delta*FENC_STRIDE,
645
0
                FENC_STRIDE, enc_dc );
646
0
            if( delta == 4 )
647
0
                sums_base += stride * (h->fenc->i_lines[0] + PADV*2);
648
0
            if( i_pixel == PIXEL_16x16 || i_pixel == PIXEL_8x16 || i_pixel == PIXEL_4x8 )
649
0
                delta *= stride;
650
0
            if( i_pixel == PIXEL_8x16 || i_pixel == PIXEL_4x8 )
651
0
                enc_dc[1] = enc_dc[2];
652
653
0
            if( h->mb.i_me_method == X264_ME_TESA )
654
0
            {
655
                // ADS threshold, then SAD threshold, then keep the best few SADs, then SATD
656
0
                mvsad_t *mvsads = (mvsad_t *)(xs + ((width+31)&~31) + 4);
657
0
                int nmvsad = 0, limit;
658
0
                int sad_thresh = i_me_range <= 16 ? 10 : i_me_range <= 24 ? 11 : 12;
659
0
                int bsad = h->pixf.sad[i_pixel]( p_fenc, FENC_STRIDE, p_fref_w+bmy*stride+bmx, stride )
660
0
                         + BITS_MVD( bmx, bmy );
661
0
                for( int my = min_y; my <= max_y; my++ )
662
0
                {
663
0
                    int i;
664
0
                    int ycost = p_cost_mvy[my*4];
665
0
                    if( bsad <= ycost )
666
0
                        continue;
667
0
                    bsad -= ycost;
668
0
                    xn = h->pixf.ads[i_pixel]( enc_dc, sums_base + min_x + my * stride, delta,
669
0
                                               cost_fpel_mvx+min_x, xs, width, bsad * 17 >> 4 );
670
0
                    for( i = 0; i < xn-2; i += 3 )
671
0
                    {
672
0
                        pixel *ref = p_fref_w+min_x+my*stride;
673
0
                        ALIGNED_ARRAY_16( int, sads,[4] ); /* padded to [4] for asm */
674
0
                        h->pixf.sad_x3[i_pixel]( p_fenc, ref+xs[i], ref+xs[i+1], ref+xs[i+2], stride, sads );
675
0
                        for( int j = 0; j < 3; j++ )
676
0
                        {
677
0
                            int sad = sads[j] + cost_fpel_mvx[xs[i+j]];
678
0
                            if( sad < bsad*sad_thresh>>3 )
679
0
                            {
680
0
                                COPY1_IF_LT( bsad, sad );
681
0
                                mvsads[nmvsad].sad = sad + ycost;
682
0
                                mvsads[nmvsad].mv[0] = min_x+xs[i+j];
683
0
                                mvsads[nmvsad].mv[1] = my;
684
0
                                nmvsad++;
685
0
                            }
686
0
                        }
687
0
                    }
688
0
                    for( ; i < xn; i++ )
689
0
                    {
690
0
                        int mx = min_x+xs[i];
691
0
                        int sad = h->pixf.sad[i_pixel]( p_fenc, FENC_STRIDE, p_fref_w+mx+my*stride, stride )
692
0
                                + cost_fpel_mvx[xs[i]];
693
0
                        if( sad < bsad*sad_thresh>>3 )
694
0
                        {
695
0
                            COPY1_IF_LT( bsad, sad );
696
0
                            mvsads[nmvsad].sad = sad + ycost;
697
0
                            mvsads[nmvsad].mv[0] = mx;
698
0
                            mvsads[nmvsad].mv[1] = my;
699
0
                            nmvsad++;
700
0
                        }
701
0
                    }
702
0
                    bsad += ycost;
703
0
                }
704
705
0
                limit = i_me_range >> 1;
706
0
                sad_thresh = bsad*sad_thresh>>3;
707
0
                while( nmvsad > limit*2 && sad_thresh > bsad )
708
0
                {
709
0
                    int i = 0;
710
                    // halve the range if the domain is too large... eh, close enough
711
0
                    sad_thresh = (sad_thresh + bsad) >> 1;
712
0
                    while( i < nmvsad && mvsads[i].sad <= sad_thresh )
713
0
                        i++;
714
0
                    for( int j = i; j < nmvsad; j++ )
715
0
                    {
716
0
                        uint32_t sad;
717
0
                        if( WORD_SIZE == 8 && sizeof(mvsad_t) == 8 )
718
0
                        {
719
0
                            uint64_t mvsad = M64( &mvsads[i] ) = M64( &mvsads[j] );
720
#if WORDS_BIGENDIAN
721
                            mvsad >>= 32;
722
#endif
723
0
                            sad = mvsad;
724
0
                        }
725
0
                        else
726
0
                        {
727
0
                            sad = mvsads[j].sad;
728
0
                            CP32( mvsads[i].mv, mvsads[j].mv );
729
0
                            mvsads[i].sad = sad;
730
0
                        }
731
0
                        i += (sad - (sad_thresh+1)) >> 31;
732
0
                    }
733
0
                    nmvsad = i;
734
0
                }
735
0
                while( nmvsad > limit )
736
0
                {
737
0
                    int bi = 0;
738
0
                    for( int i = 1; i < nmvsad; i++ )
739
0
                        if( mvsads[i].sad > mvsads[bi].sad )
740
0
                            bi = i;
741
0
                    nmvsad--;
742
0
                    if( sizeof( mvsad_t ) == sizeof( uint64_t ) )
743
0
                        CP64( &mvsads[bi], &mvsads[nmvsad] );
744
0
                    else
745
0
                        mvsads[bi] = mvsads[nmvsad];
746
0
                }
747
0
                for( int i = 0; i < nmvsad; i++ )
748
0
                    COST_MV( mvsads[i].mv[0], mvsads[i].mv[1] );
749
0
            }
750
0
            else
751
0
            {
752
                // just ADS and SAD
753
0
                for( int my = min_y; my <= max_y; my++ )
754
0
                {
755
0
                    int i;
756
0
                    int ycost = p_cost_mvy[my*4];
757
0
                    if( bcost <= ycost )
758
0
                        continue;
759
0
                    bcost -= ycost;
760
0
                    xn = h->pixf.ads[i_pixel]( enc_dc, sums_base + min_x + my * stride, delta,
761
0
                                               cost_fpel_mvx+min_x, xs, width, bcost );
762
0
                    for( i = 0; i < xn-2; i += 3 )
763
0
                        COST_MV_X3_ABS( min_x+xs[i],my, min_x+xs[i+1],my, min_x+xs[i+2],my );
764
0
                    bcost += ycost;
765
0
                    for( ; i < xn; i++ )
766
0
                        COST_MV( min_x+xs[i], my );
767
0
                }
768
0
            }
769
0
#endif
770
0
        }
771
0
        break;
772
0
    }
773
774
    /* -> qpel mv */
775
0
    uint32_t bmv = pack16to32_mask(bmx,bmy);
776
0
    uint32_t bmv_spel = SPELx2(bmv);
777
0
    if( h->mb.i_subpel_refine < 3 )
778
0
    {
779
0
        m->cost_mv = p_cost_mvx[bmx*4] + p_cost_mvy[bmy*4];
780
0
        m->cost = bcost;
781
        /* compute the real cost */
782
0
        if( bmv == pmv ) m->cost += m->cost_mv;
783
0
        M32( m->mv ) = bmv_spel;
784
0
    }
785
0
    else
786
0
    {
787
0
        M32(m->mv) = bpred_cost < bcost ? bpred_mv : bmv_spel;
788
0
        m->cost = X264_MIN( bpred_cost, bcost );
789
0
    }
790
791
    /* subpel refine */
792
0
    if( h->mb.i_subpel_refine >= 2 )
793
0
    {
794
0
        int hpel = subpel_iterations[h->mb.i_subpel_refine][2];
795
0
        int qpel = subpel_iterations[h->mb.i_subpel_refine][3];
796
0
        refine_subpel( h, m, hpel, qpel, p_halfpel_thresh, 0 );
797
0
    }
798
0
}
Unexecuted instantiation: x264_8_me_search_ref
Unexecuted instantiation: x264_10_me_search_ref
799
#undef COST_MV
800
801
void x264_me_refine_qpel( x264_t *h, x264_me_t *m )
802
0
{
803
0
    int hpel = subpel_iterations[h->mb.i_subpel_refine][0];
804
0
    int qpel = subpel_iterations[h->mb.i_subpel_refine][1];
805
806
0
    if( m->i_pixel <= PIXEL_8x8 )
807
0
        m->cost -= m->i_ref_cost;
808
809
0
    refine_subpel( h, m, hpel, qpel, NULL, 1 );
810
0
}
Unexecuted instantiation: x264_8_me_refine_qpel
Unexecuted instantiation: x264_10_me_refine_qpel
811
812
void x264_me_refine_qpel_refdupe( x264_t *h, x264_me_t *m, int *p_halfpel_thresh )
813
0
{
814
0
    refine_subpel( h, m, 0, X264_MIN( 2, subpel_iterations[h->mb.i_subpel_refine][3] ), p_halfpel_thresh, 0 );
815
0
}
Unexecuted instantiation: x264_8_me_refine_qpel_refdupe
Unexecuted instantiation: x264_10_me_refine_qpel_refdupe
816
817
0
#define COST_MV_SAD( mx, my ) \
818
0
{ \
819
0
    intptr_t stride = 16; \
820
0
    pixel *src = h->mc.get_ref( pix, &stride, m->p_fref, m->i_stride[0], mx, my, bw, bh, &m->weight[0] ); \
821
0
    int cost = h->pixf.fpelcmp[i_pixel]( m->p_fenc[0], FENC_STRIDE, src, stride ) \
822
0
             + p_cost_mvx[ mx ] + p_cost_mvy[ my ]; \
823
0
    COPY3_IF_LT( bcost, cost, bmx, mx, bmy, my ); \
824
0
}
825
826
0
#define COST_MV_SATD( mx, my, dir ) \
827
0
if( b_refine_qpel || (dir^1) != odir ) \
828
0
{ \
829
0
    intptr_t stride = 16; \
830
0
    pixel *src = h->mc.get_ref( pix, &stride, &m->p_fref[0], m->i_stride[0], mx, my, bw, bh, &m->weight[0] ); \
831
0
    int cost = h->pixf.mbcmp_unaligned[i_pixel]( m->p_fenc[0], FENC_STRIDE, src, stride ) \
832
0
             + p_cost_mvx[ mx ] + p_cost_mvy[ my ]; \
833
0
    if( b_chroma_me && cost < bcost ) \
834
0
    { \
835
0
        if( CHROMA444 ) \
836
0
        { \
837
0
            stride = 16; \
838
0
            src = h->mc.get_ref( pix, &stride, &m->p_fref[4], m->i_stride[1], mx, my, bw, bh, &m->weight[1] ); \
839
0
            cost += h->pixf.mbcmp_unaligned[i_pixel]( m->p_fenc[1], FENC_STRIDE, src, stride ); \
840
0
            if( cost < bcost ) \
841
0
            { \
842
0
                stride = 16; \
843
0
                src = h->mc.get_ref( pix, &stride, &m->p_fref[8], m->i_stride[2], mx, my, bw, bh, &m->weight[2] ); \
844
0
                cost += h->pixf.mbcmp_unaligned[i_pixel]( m->p_fenc[2], FENC_STRIDE, src, stride ); \
845
0
            } \
846
0
        } \
847
0
        else \
848
0
        { \
849
0
            h->mc.mc_chroma( pix, pix+8, 16, m->p_fref[4], m->i_stride[1], \
850
0
                             mx, 2*(my+mvy_offset)>>chroma_v_shift, bw>>1, bh>>chroma_v_shift ); \
851
0
            if( m->weight[1].weightfn ) \
852
0
                m->weight[1].weightfn[bw>>3]( pix, 16, pix, 16, &m->weight[1], bh>>chroma_v_shift ); \
853
0
            cost += h->pixf.mbcmp[chromapix]( m->p_fenc[1], FENC_STRIDE, pix, 16 ); \
854
0
            if( cost < bcost ) \
855
0
            { \
856
0
                if( m->weight[2].weightfn ) \
857
0
                    m->weight[2].weightfn[bw>>3]( pix+8, 16, pix+8, 16, &m->weight[2], bh>>chroma_v_shift ); \
858
0
                cost += h->pixf.mbcmp[chromapix]( m->p_fenc[2], FENC_STRIDE, pix+8, 16 ); \
859
0
            } \
860
0
        } \
861
0
    } \
862
0
    COPY4_IF_LT( bcost, cost, bmx, mx, bmy, my, bdir, dir ); \
863
0
}
864
865
static void refine_subpel( x264_t *h, x264_me_t *m, int hpel_iters, int qpel_iters, int *p_halfpel_thresh, int b_refine_qpel )
866
0
{
867
0
    const int bw = x264_pixel_size[m->i_pixel].w;
868
0
    const int bh = x264_pixel_size[m->i_pixel].h;
869
0
    const uint16_t *p_cost_mvx = m->p_cost_mv - m->mvp[0];
870
0
    const uint16_t *p_cost_mvy = m->p_cost_mv - m->mvp[1];
871
0
    const int i_pixel = m->i_pixel;
872
0
    const int b_chroma_me = h->mb.b_chroma_me && (i_pixel <= PIXEL_8x8 || CHROMA444);
873
0
    int chromapix = h->luma2chroma_pixel[i_pixel];
874
0
    int chroma_v_shift = CHROMA_V_SHIFT;
875
0
    int mvy_offset = chroma_v_shift & MB_INTERLACED & m->i_ref ? (h->mb.i_mb_y & 1)*4 - 2 : 0;
876
877
0
    ALIGNED_ARRAY_32( pixel, pix,[64*18] ); // really 17x17x2, but round up for alignment
878
0
    ALIGNED_ARRAY_16( int, costs,[4] );
879
880
0
    int bmx = m->mv[0];
881
0
    int bmy = m->mv[1];
882
0
    int bcost = m->cost;
883
0
    int odir = -1, bdir;
884
885
    /* halfpel diamond search */
886
0
    if( hpel_iters )
887
0
    {
888
        /* try the subpel component of the predicted mv */
889
0
        if( h->mb.i_subpel_refine < 3 )
890
0
        {
891
0
            int mx = x264_clip3( m->mvp[0], h->mb.mv_min_spel[0]+2, h->mb.mv_max_spel[0]-2 );
892
0
            int my = x264_clip3( m->mvp[1], h->mb.mv_min_spel[1]+2, h->mb.mv_max_spel[1]-2 );
893
0
            if( (mx-bmx)|(my-bmy) )
894
0
                COST_MV_SAD( mx, my );
895
0
        }
896
897
0
        bcost <<= 6;
898
0
        for( int i = hpel_iters; i > 0; i-- )
899
0
        {
900
0
            int omx = bmx, omy = bmy;
901
0
            intptr_t stride = 64; // candidates are either all hpel or all qpel, so one stride is enough
902
0
            pixel *src0, *src1, *src2, *src3;
903
0
            src0 = h->mc.get_ref( pix,    &stride, m->p_fref, m->i_stride[0], omx, omy-2, bw, bh+1, &m->weight[0] );
904
0
            src2 = h->mc.get_ref( pix+32, &stride, m->p_fref, m->i_stride[0], omx-2, omy, bw+4, bh, &m->weight[0] );
905
0
            src1 = src0 + stride;
906
0
            src3 = src2 + 1;
907
0
            h->pixf.fpelcmp_x4[i_pixel]( m->p_fenc[0], src0, src1, src2, src3, stride, costs );
908
0
            costs[0] += p_cost_mvx[omx  ] + p_cost_mvy[omy-2];
909
0
            costs[1] += p_cost_mvx[omx  ] + p_cost_mvy[omy+2];
910
0
            costs[2] += p_cost_mvx[omx-2] + p_cost_mvy[omy  ];
911
0
            costs[3] += p_cost_mvx[omx+2] + p_cost_mvy[omy  ];
912
0
            COPY1_IF_LT( bcost, (costs[0]<<6)+2 );
913
0
            COPY1_IF_LT( bcost, (costs[1]<<6)+6 );
914
0
            COPY1_IF_LT( bcost, (costs[2]<<6)+16 );
915
0
            COPY1_IF_LT( bcost, (costs[3]<<6)+48 );
916
0
            if( !(bcost&63) )
917
0
                break;
918
0
            bmx -= (int32_t)((uint32_t)bcost<<26)>>29;
919
0
            bmy -= (int32_t)((uint32_t)bcost<<29)>>29;
920
0
            bcost &= ~63;
921
0
        }
922
0
        bcost >>= 6;
923
0
    }
924
925
0
    if( !b_refine_qpel && (h->pixf.mbcmp_unaligned[0] != h->pixf.fpelcmp[0] || b_chroma_me) )
926
0
    {
927
0
        bcost = COST_MAX;
928
0
        COST_MV_SATD( bmx, bmy, -1 );
929
0
    }
930
931
    /* early termination when examining multiple reference frames */
932
0
    if( p_halfpel_thresh )
933
0
    {
934
0
        if( (bcost*7)>>3 > *p_halfpel_thresh )
935
0
        {
936
0
            m->cost = bcost;
937
0
            m->mv[0] = bmx;
938
0
            m->mv[1] = bmy;
939
            // don't need cost_mv
940
0
            return;
941
0
        }
942
0
        else if( bcost < *p_halfpel_thresh )
943
0
            *p_halfpel_thresh = bcost;
944
0
    }
945
946
    /* quarterpel diamond search */
947
0
    if( h->mb.i_subpel_refine != 1 )
948
0
    {
949
0
        bdir = -1;
950
0
        for( int i = qpel_iters; i > 0; i-- )
951
0
        {
952
0
            if( bmy <= h->mb.mv_min_spel[1] || bmy >= h->mb.mv_max_spel[1] || bmx <= h->mb.mv_min_spel[0] || bmx >= h->mb.mv_max_spel[0] )
953
0
                break;
954
0
            odir = bdir;
955
0
            int omx = bmx, omy = bmy;
956
0
            COST_MV_SATD( omx, omy - 1, 0 );
957
0
            COST_MV_SATD( omx, omy + 1, 1 );
958
0
            COST_MV_SATD( omx - 1, omy, 2 );
959
0
            COST_MV_SATD( omx + 1, omy, 3 );
960
0
            if( (bmx == omx) & (bmy == omy) )
961
0
                break;
962
0
        }
963
0
    }
964
    /* Special simplified case for subme=1 */
965
0
    else if( bmy > h->mb.mv_min_spel[1] && bmy < h->mb.mv_max_spel[1] && bmx > h->mb.mv_min_spel[0] && bmx < h->mb.mv_max_spel[0] )
966
0
    {
967
0
        int omx = bmx, omy = bmy;
968
        /* We have to use mc_luma because all strides must be the same to use fpelcmp_x4 */
969
0
        h->mc.mc_luma( pix   , 64, m->p_fref, m->i_stride[0], omx, omy-1, bw, bh, &m->weight[0] );
970
0
        h->mc.mc_luma( pix+16, 64, m->p_fref, m->i_stride[0], omx, omy+1, bw, bh, &m->weight[0] );
971
0
        h->mc.mc_luma( pix+32, 64, m->p_fref, m->i_stride[0], omx-1, omy, bw, bh, &m->weight[0] );
972
0
        h->mc.mc_luma( pix+48, 64, m->p_fref, m->i_stride[0], omx+1, omy, bw, bh, &m->weight[0] );
973
0
        h->pixf.fpelcmp_x4[i_pixel]( m->p_fenc[0], pix, pix+16, pix+32, pix+48, 64, costs );
974
0
        costs[0] += p_cost_mvx[omx  ] + p_cost_mvy[omy-1];
975
0
        costs[1] += p_cost_mvx[omx  ] + p_cost_mvy[omy+1];
976
0
        costs[2] += p_cost_mvx[omx-1] + p_cost_mvy[omy  ];
977
0
        costs[3] += p_cost_mvx[omx+1] + p_cost_mvy[omy  ];
978
0
        bcost <<= 4;
979
0
        COPY1_IF_LT( bcost, (costs[0]<<4)+1 );
980
0
        COPY1_IF_LT( bcost, (costs[1]<<4)+3 );
981
0
        COPY1_IF_LT( bcost, (costs[2]<<4)+4 );
982
0
        COPY1_IF_LT( bcost, (costs[3]<<4)+12 );
983
0
        bmx -= (int32_t)((uint32_t)bcost<<28)>>30;
984
0
        bmy -= (int32_t)((uint32_t)bcost<<30)>>30;
985
0
        bcost >>= 4;
986
0
    }
987
988
0
    m->cost = bcost;
989
0
    m->mv[0] = bmx;
990
0
    m->mv[1] = bmy;
991
0
    m->cost_mv = p_cost_mvx[bmx] + p_cost_mvy[bmy];
992
0
}
993
994
0
#define BIME_CACHE( dx, dy, list )\
995
0
{\
996
0
    x264_me_t *m = m##list;\
997
0
    int i = 4 + 3*dx + dy;\
998
0
    int mvx = bm##list##x+dx;\
999
0
    int mvy = bm##list##y+dy;\
1000
0
    stride[0][list][i] = bw;\
1001
0
    src[0][list][i] = h->mc.get_ref( pixy_buf[list][i], &stride[0][list][i], &m->p_fref[0],\
1002
0
                                     m->i_stride[0], mvx, mvy, bw, bh, x264_weight_none );\
1003
0
    if( rd )\
1004
0
    {\
1005
0
        if( CHROMA444 )\
1006
0
        {\
1007
0
            stride[1][list][i] = bw;\
1008
0
            src[1][list][i] = h->mc.get_ref( pixu_buf[list][i], &stride[1][list][i], &m->p_fref[4],\
1009
0
                                             m->i_stride[1], mvx, mvy, bw, bh, x264_weight_none );\
1010
0
            stride[2][list][i] = bw;\
1011
0
            src[2][list][i] = h->mc.get_ref( pixv_buf[list][i], &stride[2][list][i], &m->p_fref[8],\
1012
0
                                             m->i_stride[2], mvx, mvy, bw, bh, x264_weight_none );\
1013
0
        }\
1014
0
        else if( CHROMA_FORMAT )\
1015
0
            h->mc.mc_chroma( pixu_buf[list][i], pixv_buf[list][i], 8, m->p_fref[4], m->i_stride[1],\
1016
0
                             mvx, 2*(mvy+mv##list##y_offset)>>chroma_v_shift, bw>>1, bh>>chroma_v_shift );\
1017
0
    }\
1018
0
}
1019
1020
0
#define SATD_THRESH(cost) (cost+(cost>>4))
1021
1022
/* Don't unroll the BIME_CACHE loop. I couldn't find any way to force this
1023
 * other than making its iteration count not a compile-time constant. */
1024
0
#define x264_iter_kludge x264_template(iter_kludge)
1025
int x264_iter_kludge = 0;
1026
1027
static ALWAYS_INLINE void me_refine_bidir( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight, int i8, int i_lambda2, int rd )
1028
0
{
1029
0
    int x = i8&1;
1030
0
    int y = i8>>1;
1031
0
    int s8 = X264_SCAN8_0 + 2*x + 16*y;
1032
0
    int16_t *cache0_mv = h->mb.cache.mv[0][s8];
1033
0
    int16_t *cache1_mv = h->mb.cache.mv[1][s8];
1034
0
    const int i_pixel = m0->i_pixel;
1035
0
    const int bw = x264_pixel_size[i_pixel].w;
1036
0
    const int bh = x264_pixel_size[i_pixel].h;
1037
0
    ALIGNED_ARRAY_32( pixel, pixy_buf,[2],[9][16*16] );
1038
0
    ALIGNED_ARRAY_32( pixel, pixu_buf,[2],[9][16*16] );
1039
0
    ALIGNED_ARRAY_32( pixel, pixv_buf,[2],[9][16*16] );
1040
0
    pixel *src[3][2][9];
1041
0
    int chromapix = h->luma2chroma_pixel[i_pixel];
1042
0
    int chroma_v_shift = CHROMA_V_SHIFT;
1043
0
    int chroma_x = (8 >> CHROMA_H_SHIFT) * x;
1044
0
    int chroma_y = (8 >> chroma_v_shift) * y;
1045
0
    pixel *pix  = &h->mb.pic.p_fdec[0][8*x + 8*y*FDEC_STRIDE];
1046
0
    pixel *pixu = CHROMA_FORMAT ? &h->mb.pic.p_fdec[1][chroma_x + chroma_y*FDEC_STRIDE] : NULL;
1047
0
    pixel *pixv = CHROMA_FORMAT ? &h->mb.pic.p_fdec[2][chroma_x + chroma_y*FDEC_STRIDE] : NULL;
1048
0
    int ref0 = h->mb.cache.ref[0][s8];
1049
0
    int ref1 = h->mb.cache.ref[1][s8];
1050
0
    const int mv0y_offset = chroma_v_shift & MB_INTERLACED & ref0 ? (h->mb.i_mb_y & 1)*4 - 2 : 0;
1051
0
    const int mv1y_offset = chroma_v_shift & MB_INTERLACED & ref1 ? (h->mb.i_mb_y & 1)*4 - 2 : 0;
1052
0
    intptr_t stride[3][2][9];
1053
0
    int bm0x = m0->mv[0];
1054
0
    int bm0y = m0->mv[1];
1055
0
    int bm1x = m1->mv[0];
1056
0
    int bm1y = m1->mv[1];
1057
0
    int bcost = COST_MAX;
1058
0
    int mc_list0 = 1, mc_list1 = 1;
1059
0
    uint64_t bcostrd = COST_MAX64;
1060
0
    uint16_t amvd;
1061
    /* each byte of visited represents 8 possible m1y positions, so a 4D array isn't needed */
1062
0
    ALIGNED_ARRAY_64( uint8_t, visited,[8],[8][8] );
1063
    /* all permutations of an offset in up to 2 of the dimensions */
1064
0
    ALIGNED_4( static const int8_t dia4d[33][4] ) =
1065
0
    {
1066
0
        {0,0,0,0},
1067
0
        {0,0,0,1}, {0,0,0,-1}, {0,0,1,0}, {0,0,-1,0},
1068
0
        {0,1,0,0}, {0,-1,0,0}, {1,0,0,0}, {-1,0,0,0},
1069
0
        {0,0,1,1}, {0,0,-1,-1},{0,1,1,0}, {0,-1,-1,0},
1070
0
        {1,1,0,0}, {-1,-1,0,0},{1,0,0,1}, {-1,0,0,-1},
1071
0
        {0,1,0,1}, {0,-1,0,-1},{1,0,1,0}, {-1,0,-1,0},
1072
0
        {0,0,-1,1},{0,0,1,-1}, {0,-1,1,0},{0,1,-1,0},
1073
0
        {-1,1,0,0},{1,-1,0,0}, {1,0,0,-1},{-1,0,0,1},
1074
0
        {0,-1,0,1},{0,1,0,-1}, {-1,0,1,0},{1,0,-1,0},
1075
0
    };
1076
1077
0
    if( bm0y < h->mb.mv_min_spel[1] + 8 || bm1y < h->mb.mv_min_spel[1] + 8 ||
1078
0
        bm0y > h->mb.mv_max_spel[1] - 8 || bm1y > h->mb.mv_max_spel[1] - 8 ||
1079
0
        bm0x < h->mb.mv_min_spel[0] + 8 || bm1x < h->mb.mv_min_spel[0] + 8 ||
1080
0
        bm0x > h->mb.mv_max_spel[0] - 8 || bm1x > h->mb.mv_max_spel[0] - 8 )
1081
0
        return;
1082
1083
0
    if( rd && m0->i_pixel != PIXEL_16x16 && i8 != 0 )
1084
0
    {
1085
0
        x264_mb_predict_mv( h, 0, i8<<2, bw>>2, m0->mvp );
1086
0
        x264_mb_predict_mv( h, 1, i8<<2, bw>>2, m1->mvp );
1087
0
    }
1088
1089
0
    const uint16_t *p_cost_m0x = m0->p_cost_mv - m0->mvp[0];
1090
0
    const uint16_t *p_cost_m0y = m0->p_cost_mv - m0->mvp[1];
1091
0
    const uint16_t *p_cost_m1x = m1->p_cost_mv - m1->mvp[0];
1092
0
    const uint16_t *p_cost_m1y = m1->p_cost_mv - m1->mvp[1];
1093
1094
0
    h->mc.memzero_aligned( visited, sizeof(uint8_t[8][8][8]) );
1095
1096
0
    for( int pass = 0; pass < 8; pass++ )
1097
0
    {
1098
0
        int bestj = 0;
1099
        /* check all mv pairs that differ in at most 2 components from the current mvs. */
1100
        /* doesn't do chroma ME. this probably doesn't matter, as the gains
1101
         * from bidir ME are the same with and without chroma ME. */
1102
1103
0
        if( mc_list0 )
1104
0
            for( int j = x264_iter_kludge; j < 9; j++ )
1105
0
                BIME_CACHE( square1[j][0], square1[j][1], 0 );
1106
1107
0
        if( mc_list1 )
1108
0
            for( int j = x264_iter_kludge; j < 9; j++ )
1109
0
                BIME_CACHE( square1[j][0], square1[j][1], 1 );
1110
1111
0
        for( int j = !!pass; j < 33; j++ )
1112
0
        {
1113
0
            int m0x = dia4d[j][0] + bm0x;
1114
0
            int m0y = dia4d[j][1] + bm0y;
1115
0
            int m1x = dia4d[j][2] + bm1x;
1116
0
            int m1y = dia4d[j][3] + bm1y;
1117
0
            if( !pass || !((visited[(m0x)&7][(m0y)&7][(m1x)&7] & (1<<((m1y)&7)))) )
1118
0
            {
1119
0
                int i0 = 4 + 3*dia4d[j][0] + dia4d[j][1];
1120
0
                int i1 = 4 + 3*dia4d[j][2] + dia4d[j][3];
1121
0
                visited[(m0x)&7][(m0y)&7][(m1x)&7] |= (1<<((m1y)&7));
1122
0
                h->mc.avg[i_pixel]( pix, FDEC_STRIDE, src[0][0][i0], stride[0][0][i0], src[0][1][i1], stride[0][1][i1], i_weight );
1123
0
                int cost = h->pixf.mbcmp[i_pixel]( m0->p_fenc[0], FENC_STRIDE, pix, FDEC_STRIDE )
1124
0
                         + p_cost_m0x[m0x] + p_cost_m0y[m0y] + p_cost_m1x[m1x] + p_cost_m1y[m1y];
1125
0
                if( rd )
1126
0
                {
1127
0
                    if( cost < SATD_THRESH(bcost) )
1128
0
                    {
1129
0
                        bcost = X264_MIN( cost, bcost );
1130
0
                        M32( cache0_mv ) = pack16to32_mask(m0x,m0y);
1131
0
                        M32( cache1_mv ) = pack16to32_mask(m1x,m1y);
1132
0
                        if( CHROMA444 )
1133
0
                        {
1134
0
                            h->mc.avg[i_pixel]( pixu, FDEC_STRIDE, src[1][0][i0], stride[1][0][i0], src[1][1][i1], stride[1][1][i1], i_weight );
1135
0
                            h->mc.avg[i_pixel]( pixv, FDEC_STRIDE, src[2][0][i0], stride[2][0][i0], src[2][1][i1], stride[2][1][i1], i_weight );
1136
0
                        }
1137
0
                        else if( CHROMA_FORMAT )
1138
0
                        {
1139
0
                            h->mc.avg[chromapix]( pixu, FDEC_STRIDE, pixu_buf[0][i0], 8, pixu_buf[1][i1], 8, i_weight );
1140
0
                            h->mc.avg[chromapix]( pixv, FDEC_STRIDE, pixv_buf[0][i0], 8, pixv_buf[1][i1], 8, i_weight );
1141
0
                        }
1142
0
                        uint64_t costrd = x264_rd_cost_part( h, i_lambda2, i8*4, m0->i_pixel );
1143
0
                        COPY2_IF_LT( bcostrd, costrd, bestj, j );
1144
0
                    }
1145
0
                }
1146
0
                else
1147
0
                    COPY2_IF_LT( bcost, cost, bestj, j );
1148
0
            }
1149
0
        }
1150
1151
0
        if( !bestj )
1152
0
            break;
1153
1154
0
        bm0x += dia4d[bestj][0];
1155
0
        bm0y += dia4d[bestj][1];
1156
0
        bm1x += dia4d[bestj][2];
1157
0
        bm1y += dia4d[bestj][3];
1158
1159
0
        mc_list0 = M16( &dia4d[bestj][0] );
1160
0
        mc_list1 = M16( &dia4d[bestj][2] );
1161
0
    }
1162
1163
0
    if( rd )
1164
0
    {
1165
0
        x264_macroblock_cache_mv ( h, 2*x, 2*y, bw>>2, bh>>2, 0, pack16to32_mask(bm0x, bm0y) );
1166
0
        amvd = pack8to16( X264_MIN(abs(bm0x - m0->mvp[0]),33), X264_MIN(abs(bm0y - m0->mvp[1]),33) );
1167
0
        x264_macroblock_cache_mvd( h, 2*x, 2*y, bw>>2, bh>>2, 0, amvd );
1168
1169
0
        x264_macroblock_cache_mv ( h, 2*x, 2*y, bw>>2, bh>>2, 1, pack16to32_mask(bm1x, bm1y) );
1170
0
        amvd = pack8to16( X264_MIN(abs(bm1x - m1->mvp[0]),33), X264_MIN(abs(bm1y - m1->mvp[1]),33) );
1171
0
        x264_macroblock_cache_mvd( h, 2*x, 2*y, bw>>2, bh>>2, 1, amvd );
1172
0
    }
1173
1174
0
    m0->mv[0] = bm0x;
1175
0
    m0->mv[1] = bm0y;
1176
0
    m1->mv[0] = bm1x;
1177
0
    m1->mv[1] = bm1y;
1178
0
}
1179
1180
void x264_me_refine_bidir_satd( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight )
1181
0
{
1182
0
    me_refine_bidir( h, m0, m1, i_weight, 0, 0, 0 );
1183
0
}
Unexecuted instantiation: x264_8_me_refine_bidir_satd
Unexecuted instantiation: x264_10_me_refine_bidir_satd
1184
1185
void x264_me_refine_bidir_rd( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight, int i8, int i_lambda2 )
1186
0
{
1187
    /* Motion compensation is done as part of bidir_rd; don't repeat
1188
     * it in encoding. */
1189
0
    h->mb.b_skip_mc = 1;
1190
0
    me_refine_bidir( h, m0, m1, i_weight, i8, i_lambda2, 1 );
1191
0
    h->mb.b_skip_mc = 0;
1192
0
}
Unexecuted instantiation: x264_8_me_refine_bidir_rd
Unexecuted instantiation: x264_10_me_refine_bidir_rd
1193
1194
#undef COST_MV_SATD
1195
0
#define COST_MV_SATD( mx, my, dst, avoid_mvp ) \
1196
0
{ \
1197
0
    if( !avoid_mvp || !(mx == pmx && my == pmy) ) \
1198
0
    { \
1199
0
        h->mc.mc_luma( pix, FDEC_STRIDE, m->p_fref, m->i_stride[0], mx, my, bw, bh, &m->weight[0] ); \
1200
0
        dst = h->pixf.mbcmp[i_pixel]( m->p_fenc[0], FENC_STRIDE, pix, FDEC_STRIDE ) \
1201
0
            + p_cost_mvx[mx] + p_cost_mvy[my]; \
1202
0
        COPY1_IF_LT( bsatd, dst ); \
1203
0
    } \
1204
0
    else \
1205
0
        dst = COST_MAX; \
1206
0
}
1207
1208
0
#define COST_MV_RD( mx, my, satd, do_dir, mdir ) \
1209
0
{ \
1210
0
    if( satd <= SATD_THRESH(bsatd) ) \
1211
0
    { \
1212
0
        uint64_t cost; \
1213
0
        M32( cache_mv ) = pack16to32_mask(mx,my); \
1214
0
        if( CHROMA444 ) \
1215
0
        { \
1216
0
            h->mc.mc_luma( pixu, FDEC_STRIDE, &m->p_fref[4], m->i_stride[1], mx, my, bw, bh, &m->weight[1] ); \
1217
0
            h->mc.mc_luma( pixv, FDEC_STRIDE, &m->p_fref[8], m->i_stride[2], mx, my, bw, bh, &m->weight[2] ); \
1218
0
        } \
1219
0
        else if( CHROMA_FORMAT && m->i_pixel <= PIXEL_8x8 ) \
1220
0
        { \
1221
0
            h->mc.mc_chroma( pixu, pixv, FDEC_STRIDE, m->p_fref[4], m->i_stride[1], \
1222
0
                             mx, 2*(my+mvy_offset)>>chroma_v_shift, bw>>1, bh>>chroma_v_shift ); \
1223
0
            if( m->weight[1].weightfn ) \
1224
0
                m->weight[1].weightfn[bw>>3]( pixu, FDEC_STRIDE, pixu, FDEC_STRIDE, &m->weight[1], bh>>chroma_v_shift ); \
1225
0
            if( m->weight[2].weightfn ) \
1226
0
                m->weight[2].weightfn[bw>>3]( pixv, FDEC_STRIDE, pixv, FDEC_STRIDE, &m->weight[2], bh>>chroma_v_shift ); \
1227
0
        } \
1228
0
        cost = x264_rd_cost_part( h, i_lambda2, i4, m->i_pixel ); \
1229
0
        COPY4_IF_LT( bcost, cost, bmx, mx, bmy, my, dir, do_dir?mdir:dir ); \
1230
0
    } \
1231
0
}
1232
1233
void x264_me_refine_qpel_rd( x264_t *h, x264_me_t *m, int i_lambda2, int i4, int i_list )
1234
0
{
1235
0
    int16_t *cache_mv = h->mb.cache.mv[i_list][x264_scan8[i4]];
1236
0
    const uint16_t *p_cost_mvx, *p_cost_mvy;
1237
0
    const int bw = x264_pixel_size[m->i_pixel].w;
1238
0
    const int bh = x264_pixel_size[m->i_pixel].h;
1239
0
    const int i_pixel = m->i_pixel;
1240
0
    int chroma_v_shift = CHROMA_V_SHIFT;
1241
0
    int mvy_offset = chroma_v_shift & MB_INTERLACED & m->i_ref ? (h->mb.i_mb_y & 1)*4 - 2 : 0;
1242
1243
0
    uint64_t bcost = COST_MAX64;
1244
0
    int bmx = m->mv[0];
1245
0
    int bmy = m->mv[1];
1246
0
    int omx, omy, pmx, pmy;
1247
0
    int satd, bsatd;
1248
0
    int dir = -2;
1249
0
    int i8 = i4>>2;
1250
0
    uint16_t amvd;
1251
1252
0
    pixel *pix  = &h->mb.pic.p_fdec[0][block_idx_xy_fdec[i4]];
1253
0
    pixel *pixu, *pixv;
1254
0
    if( CHROMA444 )
1255
0
    {
1256
0
        pixu = &h->mb.pic.p_fdec[1][block_idx_xy_fdec[i4]];
1257
0
        pixv = &h->mb.pic.p_fdec[2][block_idx_xy_fdec[i4]];
1258
0
    }
1259
0
    else if( CHROMA_FORMAT )
1260
0
    {
1261
0
        pixu = &h->mb.pic.p_fdec[1][(i8>>1)*(8*FDEC_STRIDE>>chroma_v_shift)+(i8&1)*4];
1262
0
        pixv = &h->mb.pic.p_fdec[2][(i8>>1)*(8*FDEC_STRIDE>>chroma_v_shift)+(i8&1)*4];
1263
0
    }
1264
0
    else
1265
0
    {
1266
0
        pixu = NULL;
1267
0
        pixv = NULL;
1268
0
    }
1269
1270
0
    h->mb.b_skip_mc = 1;
1271
1272
0
    if( m->i_pixel != PIXEL_16x16 && i4 != 0 )
1273
0
        x264_mb_predict_mv( h, i_list, i4, bw>>2, m->mvp );
1274
0
    pmx = m->mvp[0];
1275
0
    pmy = m->mvp[1];
1276
0
    p_cost_mvx = m->p_cost_mv - pmx;
1277
0
    p_cost_mvy = m->p_cost_mv - pmy;
1278
0
    COST_MV_SATD( bmx, bmy, bsatd, 0 );
1279
0
    if( m->i_pixel != PIXEL_16x16 )
1280
0
        COST_MV_RD( bmx, bmy, 0, 0, 0 )
1281
0
    else
1282
0
        bcost = m->cost;
1283
1284
    /* check the predicted mv */
1285
0
    if( (bmx != pmx || bmy != pmy)
1286
0
        && pmx >= h->mb.mv_min_spel[0] && pmx <= h->mb.mv_max_spel[0]
1287
0
        && pmy >= h->mb.mv_min_spel[1] && pmy <= h->mb.mv_max_spel[1] )
1288
0
    {
1289
0
        COST_MV_SATD( pmx, pmy, satd, 0 );
1290
0
        COST_MV_RD  ( pmx, pmy, satd, 0, 0 );
1291
        /* The hex motion search is guaranteed to not repeat the center candidate,
1292
         * so if pmv is chosen, set the "MV to avoid checking" to bmv instead. */
1293
0
        if( bmx == pmx && bmy == pmy )
1294
0
        {
1295
0
            pmx = m->mv[0];
1296
0
            pmy = m->mv[1];
1297
0
        }
1298
0
    }
1299
1300
0
    if( bmy < h->mb.mv_min_spel[1] + 3 || bmy > h->mb.mv_max_spel[1] - 3 ||
1301
0
        bmx < h->mb.mv_min_spel[0] + 3 || bmx > h->mb.mv_max_spel[0] - 3 )
1302
0
    {
1303
0
        h->mb.b_skip_mc = 0;
1304
0
        return;
1305
0
    }
1306
1307
    /* subpel hex search, same pattern as ME HEX. */
1308
0
    dir = -2;
1309
0
    omx = bmx;
1310
0
    omy = bmy;
1311
0
    for( int j = 0; j < 6; j++ )
1312
0
    {
1313
0
        COST_MV_SATD( omx + hex2[j+1][0], omy + hex2[j+1][1], satd, 1 );
1314
0
        COST_MV_RD  ( omx + hex2[j+1][0], omy + hex2[j+1][1], satd, 1, j );
1315
0
    }
1316
1317
0
    if( dir != -2 )
1318
0
    {
1319
        /* half hexagon, not overlapping the previous iteration */
1320
0
        for( int i = 1; i < 10; i++ )
1321
0
        {
1322
0
            const int odir = mod6m1[dir+1];
1323
0
            if( bmy < h->mb.mv_min_spel[1] + 3 ||
1324
0
                bmy > h->mb.mv_max_spel[1] - 3 )
1325
0
                break;
1326
0
            dir = -2;
1327
0
            omx = bmx;
1328
0
            omy = bmy;
1329
0
            for( int j = 0; j < 3; j++ )
1330
0
            {
1331
0
                COST_MV_SATD( omx + hex2[odir+j][0], omy + hex2[odir+j][1], satd, 1 );
1332
0
                COST_MV_RD  ( omx + hex2[odir+j][0], omy + hex2[odir+j][1], satd, 1, odir-1+j );
1333
0
            }
1334
0
            if( dir == -2 )
1335
0
                break;
1336
0
        }
1337
0
    }
1338
1339
    /* square refine, same pattern as ME HEX. */
1340
0
    omx = bmx;
1341
0
    omy = bmy;
1342
0
    for( int i = 0; i < 8; i++ )
1343
0
    {
1344
0
        COST_MV_SATD( omx + square1[i+1][0], omy + square1[i+1][1], satd, 1 );
1345
0
        COST_MV_RD  ( omx + square1[i+1][0], omy + square1[i+1][1], satd, 0, 0 );
1346
0
    }
1347
1348
0
    m->cost = bcost;
1349
0
    m->mv[0] = bmx;
1350
0
    m->mv[1] = bmy;
1351
0
    x264_macroblock_cache_mv ( h, block_idx_x[i4], block_idx_y[i4], bw>>2, bh>>2, i_list, pack16to32_mask(bmx, bmy) );
1352
0
    amvd = pack8to16( X264_MIN(abs(bmx - m->mvp[0]),66), X264_MIN(abs(bmy - m->mvp[1]),66) );
1353
0
    x264_macroblock_cache_mvd( h, block_idx_x[i4], block_idx_y[i4], bw>>2, bh>>2, i_list, amvd );
1354
0
    h->mb.b_skip_mc = 0;
1355
0
}
Unexecuted instantiation: x264_8_me_refine_qpel_rd
Unexecuted instantiation: x264_10_me_refine_qpel_rd