mc_tmpl.c 33.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright © 2018, VideoLAN and dav1d authors
 * Copyright © 2018, Two Orioles, LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

30
#include <assert.h>
31 32 33
#include <stdlib.h>
#include <string.h>

34
#include "common/attributes.h"
35 36 37 38 39
#include "common/intops.h"

#include "src/mc.h"
#include "src/tables.h"

Ronald S. Bultje's avatar
Ronald S. Bultje committed
40 41
#if BITDEPTH == 8
#define get_intermediate_bits(bitdepth_max) 4
42 43
// Output in interval [-5132, 9212], fits in int16_t as is
#define PREP_BIAS 0
Ronald S. Bultje's avatar
Ronald S. Bultje committed
44 45 46
#else
// 4 for 10 bits/component, 2 for 12 bits/component
#define get_intermediate_bits(bitdepth_max) (14 - bitdepth_from_max(bitdepth_max))
47 48 49
// Output in interval [-20588, 36956] (10-bit), [-20602, 36983] (12-bit)
// Subtract a bias to ensure the output fits in int16_t
#define PREP_BIAS 8192
Ronald S. Bultje's avatar
Ronald S. Bultje committed
50 51
#endif

52
static NOINLINE void
53 54 55 56 57 58 59 60 61 62 63
put_c(pixel *dst, const ptrdiff_t dst_stride,
      const pixel *src, const ptrdiff_t src_stride, const int w, int h)
{
    do {
        pixel_copy(dst, src, w);

        dst += dst_stride;
        src += src_stride;
    } while (--h);
}

64
static NOINLINE void
65
prep_c(int16_t *tmp, const pixel *src, const ptrdiff_t src_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
66
       const int w, int h HIGHBD_DECL_SUFFIX)
67
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
68
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
69 70
    do {
        for (int x = 0; x < w; x++)
71
            tmp[x] = (src[x] << intermediate_bits) - PREP_BIAS;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

        tmp += w;
        src += src_stride;
    } while (--h);
}

#define FILTER_8TAP(src, x, F, stride) \
    (F[0] * src[x + -3 * stride] + \
     F[1] * src[x + -2 * stride] + \
     F[2] * src[x + -1 * stride] + \
     F[3] * src[x + +0 * stride] + \
     F[4] * src[x + +1 * stride] + \
     F[5] * src[x + +2 * stride] + \
     F[6] * src[x + +3 * stride] + \
     F[7] * src[x + +4 * stride])

88
#define DAV1D_FILTER_8TAP_RND(src, x, F, stride, sh) \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
89
    ((FILTER_8TAP(src, x, F, stride) + ((1 << (sh)) >> 1)) >> (sh))
90

91 92
#define DAV1D_FILTER_8TAP_CLIP(src, x, F, stride, sh) \
    iclip_pixel(DAV1D_FILTER_8TAP_RND(src, x, F, stride, sh))
93

94 95 96 97 98 99 100 101 102 103
#define GET_H_FILTER(mx) \
    const int8_t *const fh = !(mx) ? NULL : w > 4 ? \
        dav1d_mc_subpel_filters[filter_type & 3][(mx) - 1] : \
        dav1d_mc_subpel_filters[3 + (filter_type & 1)][(mx) - 1]

#define GET_V_FILTER(my) \
    const int8_t *const fv = !(my) ? NULL : h > 4 ? \
        dav1d_mc_subpel_filters[filter_type >> 2][(my) - 1] : \
        dav1d_mc_subpel_filters[3 + ((filter_type >> 2) & 1)][(my) - 1]

104
#define GET_FILTERS() \
105 106
    GET_H_FILTER(mx); \
    GET_V_FILTER(my)
107

108
static NOINLINE void
109 110 111
put_8tap_c(pixel *dst, ptrdiff_t dst_stride,
           const pixel *src, ptrdiff_t src_stride,
           const int w, int h, const int mx, const int my,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
112
           const int filter_type HIGHBD_DECL_SUFFIX)
113
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
114 115 116
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
    const int intermediate_rnd = (1 << intermediate_bits) >> 1;

117 118 119 120 121 122 123
    GET_FILTERS();
    dst_stride = PXSTRIDE(dst_stride);
    src_stride = PXSTRIDE(src_stride);

    if (fh) {
        if (fv) {
            int tmp_h = h + 7;
124
            int16_t mid[128 * 135], *mid_ptr = mid;
125 126 127 128

            src -= src_stride * 3;
            do {
                for (int x = 0; x < w; x++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
129 130
                    mid_ptr[x] = DAV1D_FILTER_8TAP_RND(src, x, fh, 1,
                                                       6 - intermediate_bits);
131 132 133 134 135 136 137 138

                mid_ptr += 128;
                src += src_stride;
            } while (--tmp_h);

            mid_ptr = mid + 128 * 3;
            do {
                for (int x = 0; x < w; x++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
139 140
                    dst[x] = DAV1D_FILTER_8TAP_CLIP(mid_ptr, x, fv, 128,
                                                    6 + intermediate_bits);
141 142 143 144 145 146 147

                mid_ptr += 128;
                dst += dst_stride;
            } while (--h);
        } else {
            do {
                for (int x = 0; x < w; x++) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
148 149 150
                    const int px = DAV1D_FILTER_8TAP_RND(src, x, fh, 1,
                                                         6 - intermediate_bits);
                    dst[x] = iclip_pixel((px + intermediate_rnd) >> intermediate_bits);
151 152 153 154 155 156 157 158 159
                }

                dst += dst_stride;
                src += src_stride;
            } while (--h);
        }
    } else if (fv) {
        do {
            for (int x = 0; x < w; x++)
160
                dst[x] = DAV1D_FILTER_8TAP_CLIP(src, x, fv, src_stride, 6);
161 162 163 164 165 166 167 168

            dst += dst_stride;
            src += src_stride;
        } while (--h);
    } else
        put_c(dst, dst_stride, src, src_stride, w, h);
}

169 170
static NOINLINE void
put_8tap_scaled_c(pixel *dst, const ptrdiff_t dst_stride,
171
                  const pixel *src, ptrdiff_t src_stride,
172
                  const int w, int h, const int mx, int my,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
173 174
                  const int dx, const int dy, const int filter_type
                  HIGHBD_DECL_SUFFIX)
175
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
176 177
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
    const int intermediate_rnd = (1 << intermediate_bits) >> 1;
178
    int tmp_h = (((h - 1) * dy + my) >> 10) + 8;
179
    int16_t mid[128 * (256 + 7)], *mid_ptr = mid;
180
    src_stride = PXSTRIDE(src_stride);
181 182 183 184 185 186 187 188

    src -= src_stride * 3;
    do {
        int x;
        int imx = mx, ioff = 0;

        for (x = 0; x < w; x++) {
            GET_H_FILTER(imx >> 6);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
189 190 191
            mid_ptr[x] = fh ? DAV1D_FILTER_8TAP_RND(src, ioff, fh, 1,
                                                    6 - intermediate_bits) :
                              src[ioff] << intermediate_bits;
192 193 194 195 196 197
            imx += dx;
            ioff += imx >> 10;
            imx &= 0x3ff;
        }

        mid_ptr += 128;
198
        src += src_stride;
199 200 201 202 203 204 205 206
    } while (--tmp_h);

    mid_ptr = mid + 128 * 3;
    for (int y = 0; y < h; y++) {
        int x;
        GET_V_FILTER(my >> 6);

        for (x = 0; x < w; x++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
207 208 209 210
            dst[x] = fv ? DAV1D_FILTER_8TAP_CLIP(mid_ptr, x, fv, 128,
                                                 6 + intermediate_bits) :
                          iclip_pixel((mid_ptr[x] + intermediate_rnd) >>
                                              intermediate_bits);
211 212 213 214 215 216 217 218

        my += dy;
        mid_ptr += (my >> 10) * 128;
        my &= 0x3ff;
        dst += PXSTRIDE(dst_stride);
    }
}

219
static NOINLINE void
220
prep_8tap_c(int16_t *tmp, const pixel *src, ptrdiff_t src_stride,
221
            const int w, int h, const int mx, const int my,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
222
            const int filter_type HIGHBD_DECL_SUFFIX)
223
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
224
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
225 226 227 228 229 230
    GET_FILTERS();
    src_stride = PXSTRIDE(src_stride);

    if (fh) {
        if (fv) {
            int tmp_h = h + 7;
231
            int16_t mid[128 * 135], *mid_ptr = mid;
232 233 234 235

            src -= src_stride * 3;
            do {
                for (int x = 0; x < w; x++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
236 237
                    mid_ptr[x] = DAV1D_FILTER_8TAP_RND(src, x, fh, 1,
                                                       6 - intermediate_bits);
238 239 240 241 242 243 244

                mid_ptr += 128;
                src += src_stride;
            } while (--tmp_h);

            mid_ptr = mid + 128 * 3;
            do {
245 246 247 248 249 250
                for (int x = 0; x < w; x++) {
                    int t = DAV1D_FILTER_8TAP_RND(mid_ptr, x, fv, 128, 6) -
                                  PREP_BIAS;
                    assert(t >= INT16_MIN && t <= INT16_MAX);
                    tmp[x] = t;
                }
251 252 253 254 255 256 257

                mid_ptr += 128;
                tmp += w;
            } while (--h);
        } else {
            do {
                for (int x = 0; x < w; x++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
258
                    tmp[x] = DAV1D_FILTER_8TAP_RND(src, x, fh, 1,
259 260
                                                   6 - intermediate_bits) -
                             PREP_BIAS;
261 262 263 264 265 266 267 268

                tmp += w;
                src += src_stride;
            } while (--h);
        }
    } else if (fv) {
        do {
            for (int x = 0; x < w; x++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
269
                tmp[x] = DAV1D_FILTER_8TAP_RND(src, x, fv, src_stride,
270 271
                                               6 - intermediate_bits) -
                         PREP_BIAS;
272 273 274 275 276

            tmp += w;
            src += src_stride;
        } while (--h);
    } else
Ronald S. Bultje's avatar
Ronald S. Bultje committed
277
        prep_c(tmp, src, src_stride, w, h HIGHBD_TAIL_SUFFIX);
278 279
}

280
static NOINLINE void
281
prep_8tap_scaled_c(int16_t *tmp, const pixel *src, ptrdiff_t src_stride,
282
                   const int w, int h, const int mx, int my,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
283 284
                   const int dx, const int dy, const int filter_type
                   HIGHBD_DECL_SUFFIX)
285
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
286
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
287
    int tmp_h = (((h - 1) * dy + my) >> 10) + 8;
288
    int16_t mid[128 * (256 + 7)], *mid_ptr = mid;
289
    src_stride = PXSTRIDE(src_stride);
290 291 292 293 294 295 296 297

    src -= src_stride * 3;
    do {
        int x;
        int imx = mx, ioff = 0;

        for (x = 0; x < w; x++) {
            GET_H_FILTER(imx >> 6);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
298 299 300
            mid_ptr[x] = fh ? DAV1D_FILTER_8TAP_RND(src, ioff, fh, 1,
                                                    6 - intermediate_bits) :
                              src[ioff] << intermediate_bits;
301 302 303 304 305 306
            imx += dx;
            ioff += imx >> 10;
            imx &= 0x3ff;
        }

        mid_ptr += 128;
307
        src += src_stride;
308 309 310 311 312 313 314 315
    } while (--tmp_h);

    mid_ptr = mid + 128 * 3;
    for (int y = 0; y < h; y++) {
        int x;
        GET_V_FILTER(my >> 6);

        for (x = 0; x < w; x++)
316 317
            tmp[x] = (fv ? DAV1D_FILTER_8TAP_RND(mid_ptr, x, fv, 128, 6)
                         : mid_ptr[x]) - PREP_BIAS;
318 319 320 321 322 323 324 325

        my += dy;
        mid_ptr += (my >> 10) * 128;
        my &= 0x3ff;
        tmp += w;
    }
}

326 327 328 329 330 331
#define filter_fns(type, type_h, type_v) \
static void put_8tap_##type##_c(pixel *const dst, \
                                const ptrdiff_t dst_stride, \
                                const pixel *const src, \
                                const ptrdiff_t src_stride, \
                                const int w, const int h, \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
332 333
                                const int mx, const int my \
                                HIGHBD_DECL_SUFFIX) \
334 335
{ \
    put_8tap_c(dst, dst_stride, src, src_stride, w, h, mx, my, \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
336
               type_h | (type_v << 2) HIGHBD_TAIL_SUFFIX); \
337
} \
338 339 340 341 342 343
static void put_8tap_##type##_scaled_c(pixel *const dst, \
                                       const ptrdiff_t dst_stride, \
                                       const pixel *const src, \
                                       const ptrdiff_t src_stride, \
                                       const int w, const int h, \
                                       const int mx, const int my, \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
344 345
                                       const int dx, const int dy \
                                       HIGHBD_DECL_SUFFIX) \
346 347
{ \
    put_8tap_scaled_c(dst, dst_stride, src, src_stride, w, h, mx, my, dx, dy, \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
348
                      type_h | (type_v << 2) HIGHBD_TAIL_SUFFIX); \
349
} \
350
static void prep_8tap_##type##_c(int16_t *const tmp, \
351 352 353
                                 const pixel *const src, \
                                 const ptrdiff_t src_stride, \
                                 const int w, const int h, \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
354 355
                                 const int mx, const int my \
                                 HIGHBD_DECL_SUFFIX) \
356 357
{ \
    prep_8tap_c(tmp, src, src_stride, w, h, mx, my, \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
358
                type_h | (type_v << 2) HIGHBD_TAIL_SUFFIX); \
359
} \
360
static void prep_8tap_##type##_scaled_c(int16_t *const tmp, \
361 362 363 364
                                        const pixel *const src, \
                                        const ptrdiff_t src_stride, \
                                        const int w, const int h, \
                                        const int mx, const int my, \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
365 366
                                        const int dx, const int dy \
                                        HIGHBD_DECL_SUFFIX) \
367 368
{ \
    prep_8tap_scaled_c(tmp, src, src_stride, w, h, mx, my, dx, dy, \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
369
                       type_h | (type_v << 2) HIGHBD_TAIL_SUFFIX); \
370 371
}

372 373 374 375 376 377 378 379 380
filter_fns(regular,        DAV1D_FILTER_8TAP_REGULAR, DAV1D_FILTER_8TAP_REGULAR)
filter_fns(regular_sharp,  DAV1D_FILTER_8TAP_REGULAR, DAV1D_FILTER_8TAP_SHARP)
filter_fns(regular_smooth, DAV1D_FILTER_8TAP_REGULAR, DAV1D_FILTER_8TAP_SMOOTH)
filter_fns(smooth,         DAV1D_FILTER_8TAP_SMOOTH,  DAV1D_FILTER_8TAP_SMOOTH)
filter_fns(smooth_regular, DAV1D_FILTER_8TAP_SMOOTH,  DAV1D_FILTER_8TAP_REGULAR)
filter_fns(smooth_sharp,   DAV1D_FILTER_8TAP_SMOOTH,  DAV1D_FILTER_8TAP_SHARP)
filter_fns(sharp,          DAV1D_FILTER_8TAP_SHARP,   DAV1D_FILTER_8TAP_SHARP)
filter_fns(sharp_regular,  DAV1D_FILTER_8TAP_SHARP,   DAV1D_FILTER_8TAP_REGULAR)
filter_fns(sharp_smooth,   DAV1D_FILTER_8TAP_SHARP,   DAV1D_FILTER_8TAP_SMOOTH)
381 382

#define FILTER_BILIN(src, x, mxy, stride) \
383
    (16 * src[x] + ((mxy) * (src[x + stride] - src[x])))
384 385

#define FILTER_BILIN_RND(src, x, mxy, stride, sh) \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
386
    ((FILTER_BILIN(src, x, mxy, stride) + ((1 << (sh)) >> 1)) >> (sh))
387 388 389 390 391 392

#define FILTER_BILIN_CLIP(src, x, mxy, stride, sh) \
    iclip_pixel(FILTER_BILIN_RND(src, x, mxy, stride, sh))

static void put_bilin_c(pixel *dst, ptrdiff_t dst_stride,
                        const pixel *src, ptrdiff_t src_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
393 394
                        const int w, int h, const int mx, const int my
                        HIGHBD_DECL_SUFFIX)
395
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
396 397
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
    const int intermediate_rnd = (1 << intermediate_bits) >> 1;
398 399 400 401 402
    dst_stride = PXSTRIDE(dst_stride);
    src_stride = PXSTRIDE(src_stride);

    if (mx) {
        if (my) {
403
            int16_t mid[128 * 129], *mid_ptr = mid;
404 405 406 407
            int tmp_h = h + 1;

            do {
                for (int x = 0; x < w; x++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
408 409
                    mid_ptr[x] = FILTER_BILIN_RND(src, x, mx, 1,
                                                  4 - intermediate_bits);
410 411 412 413 414 415 416 417

                mid_ptr += 128;
                src += src_stride;
            } while (--tmp_h);

            mid_ptr = mid;
            do {
                for (int x = 0; x < w; x++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
418 419
                    dst[x] = FILTER_BILIN_CLIP(mid_ptr, x, my, 128,
                                               4 + intermediate_bits);
420 421 422 423 424 425

                mid_ptr += 128;
                dst += dst_stride;
            } while (--h);
        } else {
            do {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
426 427 428 429 430
                for (int x = 0; x < w; x++) {
                    const int px = FILTER_BILIN_RND(src, x, mx, 1,
                                                    4 - intermediate_bits);
                    dst[x] = iclip_pixel((px + intermediate_rnd) >> intermediate_bits);
                }
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

                dst += dst_stride;
                src += src_stride;
            } while (--h);
        }
    } else if (my) {
        do {
            for (int x = 0; x < w; x++)
                dst[x] = FILTER_BILIN_CLIP(src, x, my, src_stride, 4);

            dst += dst_stride;
            src += src_stride;
        } while (--h);
    } else
        put_c(dst, dst_stride, src, src_stride, w, h);
}

448 449 450
static void put_bilin_scaled_c(pixel *dst, ptrdiff_t dst_stride,
                               const pixel *src, ptrdiff_t src_stride,
                               const int w, int h, const int mx, int my,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
451 452
                               const int dx, const int dy
                               HIGHBD_DECL_SUFFIX)
453
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
454
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
455
    int tmp_h = (((h - 1) * dy + my) >> 10) + 2;
456
    int16_t mid[128 * (256 + 1)], *mid_ptr = mid;
457 458 459 460 461 462

    do {
        int x;
        int imx = mx, ioff = 0;

        for (x = 0; x < w; x++) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
463 464
            mid_ptr[x] = FILTER_BILIN_RND(src, ioff, imx >> 6, 1,
                                          4 - intermediate_bits);
465 466 467 468 469 470 471 472 473 474 475 476 477 478
            imx += dx;
            ioff += imx >> 10;
            imx &= 0x3ff;
        }

        mid_ptr += 128;
        src += PXSTRIDE(src_stride);
    } while (--tmp_h);

    mid_ptr = mid;
    do {
        int x;

        for (x = 0; x < w; x++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
479 480
            dst[x] = FILTER_BILIN_CLIP(mid_ptr, x, my >> 6, 128,
                                       4 + intermediate_bits);
481 482 483 484 485 486 487 488

        my += dy;
        mid_ptr += (my >> 10) * 128;
        my &= 0x3ff;
        dst += PXSTRIDE(dst_stride);
    } while (--h);
}

489
static void prep_bilin_c(int16_t *tmp,
490
                         const pixel *src, ptrdiff_t src_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
491 492
                         const int w, int h, const int mx, const int my
                         HIGHBD_DECL_SUFFIX)
493
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
494
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
495 496 497 498
    src_stride = PXSTRIDE(src_stride);

    if (mx) {
        if (my) {
499
            int16_t mid[128 * 129], *mid_ptr = mid;
500 501 502 503
            int tmp_h = h + 1;

            do {
                for (int x = 0; x < w; x++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
504 505
                    mid_ptr[x] = FILTER_BILIN_RND(src, x, mx, 1,
                                                  4 - intermediate_bits);
506 507 508 509 510 511 512 513

                mid_ptr += 128;
                src += src_stride;
            } while (--tmp_h);

            mid_ptr = mid;
            do {
                for (int x = 0; x < w; x++)
514 515
                    tmp[x] = FILTER_BILIN_RND(mid_ptr, x, my, 128, 4) -
                             PREP_BIAS;
516 517 518 519 520 521 522

                mid_ptr += 128;
                tmp += w;
            } while (--h);
        } else {
            do {
                for (int x = 0; x < w; x++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
523
                    tmp[x] = FILTER_BILIN_RND(src, x, mx, 1,
524 525
                                              4 - intermediate_bits) -
                             PREP_BIAS;
526 527 528 529 530 531 532 533

                tmp += w;
                src += src_stride;
            } while (--h);
        }
    } else if (my) {
        do {
            for (int x = 0; x < w; x++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
534
                tmp[x] = FILTER_BILIN_RND(src, x, my, src_stride,
535
                                          4 - intermediate_bits) - PREP_BIAS;
536 537 538 539 540

            tmp += w;
            src += src_stride;
        } while (--h);
    } else
Ronald S. Bultje's avatar
Ronald S. Bultje committed
541
        prep_c(tmp, src, src_stride, w, h HIGHBD_TAIL_SUFFIX);
542 543
}

544
static void prep_bilin_scaled_c(int16_t *tmp,
545 546
                                const pixel *src, ptrdiff_t src_stride,
                                const int w, int h, const int mx, int my,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
547
                                const int dx, const int dy HIGHBD_DECL_SUFFIX)
548
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
549
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
550
    int tmp_h = (((h - 1) * dy + my) >> 10) + 2;
551
    int16_t mid[128 * (256 + 1)], *mid_ptr = mid;
552 553 554 555 556 557

    do {
        int x;
        int imx = mx, ioff = 0;

        for (x = 0; x < w; x++) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
558 559
            mid_ptr[x] = FILTER_BILIN_RND(src, ioff, imx >> 6, 1,
                                          4 - intermediate_bits);
560 561 562 563 564 565 566 567 568 569 570 571 572 573
            imx += dx;
            ioff += imx >> 10;
            imx &= 0x3ff;
        }

        mid_ptr += 128;
        src += PXSTRIDE(src_stride);
    } while (--tmp_h);

    mid_ptr = mid;
    do {
        int x;

        for (x = 0; x < w; x++)
574
            tmp[x] = FILTER_BILIN_RND(mid_ptr, x, my >> 6, 128, 4) - PREP_BIAS;
575 576 577 578 579 580 581 582

        my += dy;
        mid_ptr += (my >> 10) * 128;
        my &= 0x3ff;
        tmp += w;
    } while (--h);
}

583
static void avg_c(pixel *dst, const ptrdiff_t dst_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
584 585
                  const int16_t *tmp1, const int16_t *tmp2, const int w, int h
                  HIGHBD_DECL_SUFFIX)
586
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
587
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
588 589
    const int sh = intermediate_bits + 1;
    const int rnd = (1 << intermediate_bits) + PREP_BIAS * 2;
590 591
    do {
        for (int x = 0; x < w; x++)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
592
            dst[x] = iclip_pixel((tmp1[x] + tmp2[x] + rnd) >> sh);
593 594 595 596 597 598 599 600

        tmp1 += w;
        tmp2 += w;
        dst += PXSTRIDE(dst_stride);
    } while (--h);
}

static void w_avg_c(pixel *dst, const ptrdiff_t dst_stride,
601
                    const int16_t *tmp1, const int16_t *tmp2, const int w, int h,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
602
                    const int weight HIGHBD_DECL_SUFFIX)
603
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
604
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
605 606
    const int sh = intermediate_bits + 4;
    const int rnd = (8 << intermediate_bits) + PREP_BIAS * 16;
607 608 609
    do {
        for (int x = 0; x < w; x++)
            dst[x] = iclip_pixel((tmp1[x] * weight +
Ronald S. Bultje's avatar
Ronald S. Bultje committed
610
                                  tmp2[x] * (16 - weight) + rnd) >> sh);
611 612 613 614 615 616 617 618

        tmp1 += w;
        tmp2 += w;
        dst += PXSTRIDE(dst_stride);
    } while (--h);
}

static void mask_c(pixel *dst, const ptrdiff_t dst_stride,
619
                   const int16_t *tmp1, const int16_t *tmp2, const int w, int h,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
620
                   const uint8_t *mask HIGHBD_DECL_SUFFIX)
621
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
622
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
623 624
    const int sh = intermediate_bits + 6;
    const int rnd = (32 << intermediate_bits) + PREP_BIAS * 64;
625 626 627
    do {
        for (int x = 0; x < w; x++)
            dst[x] = iclip_pixel((tmp1[x] * mask[x] +
Ronald S. Bultje's avatar
Ronald S. Bultje committed
628
                                  tmp2[x] * (64 - mask[x]) + rnd) >> sh);
629 630 631 632 633 634 635 636

        tmp1 += w;
        tmp2 += w;
        mask += w;
        dst += PXSTRIDE(dst_stride);
    } while (--h);
}

Henrik Gramner's avatar
Henrik Gramner committed
637 638 639 640 641
#define blend_px(a, b, m) (((a * (64 - m) + b * m) + 32) >> 6)
static NOINLINE void
blend_internal_c(pixel *dst, const ptrdiff_t dst_stride, const pixel *tmp,
                 const int w, int h, const uint8_t *mask,
                 const ptrdiff_t mask_stride)
642
{
Henrik Gramner's avatar
Henrik Gramner committed
643
    do {
644
        for (int x = 0; x < w; x++) {
Henrik Gramner's avatar
Henrik Gramner committed
645
            dst[x] = blend_px(dst[x], tmp[x], mask[x]);
646 647
        }
        dst += PXSTRIDE(dst_stride);
648
        tmp += w;
Henrik Gramner's avatar
Henrik Gramner committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
        mask += mask_stride;
    } while (--h);
}

static void blend_c(pixel *dst, const ptrdiff_t dst_stride, const pixel *tmp,
                    const int w, const int h, const uint8_t *mask)
{
    blend_internal_c(dst, dst_stride, tmp, w, h, mask, w);
}

static void blend_v_c(pixel *dst, const ptrdiff_t dst_stride, const pixel *tmp,
                      const int w, const int h)
{
    blend_internal_c(dst, dst_stride, tmp, w, h, &dav1d_obmc_masks[w], 0);
}

static void blend_h_c(pixel *dst, const ptrdiff_t dst_stride, const pixel *tmp,
                      const int w, int h)
{
    const uint8_t *mask = &dav1d_obmc_masks[h];
    do {
        const int m = *mask++;
        for (int x = 0; x < w; x++) {
            dst[x] = blend_px(dst[x], tmp[x], m);
        }
        dst += PXSTRIDE(dst_stride);
        tmp += w;
    } while (--h);
677 678 679
}

static void w_mask_c(pixel *dst, const ptrdiff_t dst_stride,
680
                     const int16_t *tmp1, const int16_t *tmp2, const int w, int h,
681
                     uint8_t *mask, const int sign,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
682
                     const int ss_hor, const int ss_ver HIGHBD_DECL_SUFFIX)
683 684 685
{
    // store mask at 2x2 resolution, i.e. store 2x1 sum for even rows,
    // and then load this intermediate to calculate final value for odd rows
Ronald S. Bultje's avatar
Ronald S. Bultje committed
686 687
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
    const int bitdepth = bitdepth_from_max(bitdepth_max);
688 689
    const int sh = intermediate_bits + 6;
    const int rnd = (32 << intermediate_bits) + PREP_BIAS * 64;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
690 691
    const int mask_sh = bitdepth + intermediate_bits - 4;
    const int mask_rnd = 1 << (mask_sh - 5);
692 693
    do {
        for (int x = 0; x < w; x++) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
694
            const int m = imin(38 + ((abs(tmp1[x] - tmp2[x]) + mask_rnd) >> mask_sh), 64);
695
            dst[x] = iclip_pixel((tmp1[x] * m +
Ronald S. Bultje's avatar
Ronald S. Bultje committed
696
                                  tmp2[x] * (64 - m) + rnd) >> sh);
697 698 699 700

            if (ss_hor) {
                x++;

Ronald S. Bultje's avatar
Ronald S. Bultje committed
701
                const int n = imin(38 + ((abs(tmp1[x] - tmp2[x]) + mask_rnd) >> mask_sh), 64);
702
                dst[x] = iclip_pixel((tmp1[x] * n +
Ronald S. Bultje's avatar
Ronald S. Bultje committed
703
                                      tmp2[x] * (64 - n) + rnd) >> sh);
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

                if (h & ss_ver) {
                    mask[x >> 1] = (m + n + mask[x >> 1] + 2 - sign) >> 2;
                } else if (ss_ver) {
                    mask[x >> 1] = m + n;
                } else {
                    mask[x >> 1] = (m + n + 1 - sign) >> 1;
                }
            } else {
                mask[x] = m;
            }
        }

        tmp1 += w;
        tmp2 += w;
        dst += PXSTRIDE(dst_stride);
        if (!ss_ver || (h & 1)) mask += w >> ss_hor;
    } while (--h);
}

#define w_mask_fns(ssn, ss_hor, ss_ver) \
static void w_mask_##ssn##_c(pixel *const dst, const ptrdiff_t dst_stride, \
726
                             const int16_t *const tmp1, const int16_t *const tmp2, \
727
                             const int w, const int h, uint8_t *mask, \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
728
                             const int sign HIGHBD_DECL_SUFFIX) \
729
{ \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
730 731
    w_mask_c(dst, dst_stride, tmp1, tmp2, w, h, mask, sign, ss_hor, ss_ver \
             HIGHBD_TAIL_SUFFIX); \
732 733 734 735 736 737 738 739
}

w_mask_fns(444, 0, 0);
w_mask_fns(422, 1, 0);
w_mask_fns(420, 1, 1);

#undef w_mask_fns

740
#if ARCH_X86
741 742 743 744 745 746 747 748 749
#define FILTER_WARP(src, x, F, stride) \
    (F[0] * src[x + -3 * stride] + \
     F[4] * src[x + -2 * stride] + \
     F[1] * src[x + -1 * stride] + \
     F[5] * src[x + +0 * stride] + \
     F[2] * src[x + +1 * stride] + \
     F[6] * src[x + +2 * stride] + \
     F[3] * src[x + +3 * stride] + \
     F[7] * src[x + +4 * stride])
750 751 752 753 754 755 756 757 758 759 760
#else
#define FILTER_WARP(src, x, F, stride) \
    (F[0] * src[x + -3 * stride] + \
     F[1] * src[x + -2 * stride] + \
     F[2] * src[x + -1 * stride] + \
     F[3] * src[x + +0 * stride] + \
     F[4] * src[x + +1 * stride] + \
     F[5] * src[x + +2 * stride] + \
     F[6] * src[x + +3 * stride] + \
     F[7] * src[x + +4 * stride])
#endif
761 762

#define FILTER_WARP_RND(src, x, F, stride, sh) \
Ronald S. Bultje's avatar
Ronald S. Bultje committed
763
    ((FILTER_WARP(src, x, F, stride) + ((1 << (sh)) >> 1)) >> (sh))
764 765 766 767

#define FILTER_WARP_CLIP(src, x, F, stride, sh) \
    iclip_pixel(FILTER_WARP_RND(src, x, F, stride, sh))

768 769
static void warp_affine_8x8_c(pixel *dst, const ptrdiff_t dst_stride,
                              const pixel *src, const ptrdiff_t src_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
770 771
                              const int16_t *const abcd, int mx, int my
                              HIGHBD_DECL_SUFFIX)
772
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
773
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
774
    int16_t mid[15 * 8], *mid_ptr = mid;
775 776 777 778 779 780 781

    src -= 3 * PXSTRIDE(src_stride);
    for (int y = 0; y < 15; y++, mx += abcd[1]) {
        for (int x = 0, tmx = mx; x < 8; x++, tmx += abcd[0]) {
            const int8_t *const filter =
                dav1d_mc_warp_filter[64 + ((tmx + 512) >> 10)];

Ronald S. Bultje's avatar
Ronald S. Bultje committed
782 783
            mid_ptr[x] = FILTER_WARP_RND(src, x, filter, 1,
                                         7 - intermediate_bits);
784 785 786 787 788 789 790 791 792 793 794
        }
        src += PXSTRIDE(src_stride);
        mid_ptr += 8;
    }

    mid_ptr = &mid[3 * 8];
    for (int y = 0; y < 8; y++, my += abcd[3]) {
        for (int x = 0, tmy = my; x < 8; x++, tmy += abcd[2]) {
            const int8_t *const filter =
                dav1d_mc_warp_filter[64 + ((tmy + 512) >> 10)];

Ronald S. Bultje's avatar
Ronald S. Bultje committed
795 796
            dst[x] = FILTER_WARP_CLIP(mid_ptr, x, filter, 8,
                                      7 + intermediate_bits);
797 798 799 800 801 802
        }
        mid_ptr += 8;
        dst += PXSTRIDE(dst_stride);
    }
}

803
static void warp_affine_8x8t_c(int16_t *tmp, const ptrdiff_t tmp_stride,
804
                               const pixel *src, const ptrdiff_t src_stride,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
805 806
                               const int16_t *const abcd, int mx, int my
                               HIGHBD_DECL_SUFFIX)
807
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
808
    const int intermediate_bits = get_intermediate_bits(bitdepth_max);
809
    int16_t mid[15 * 8], *mid_ptr = mid;
810 811 812 813 814 815 816

    src -= 3 * PXSTRIDE(src_stride);
    for (int y = 0; y < 15; y++, mx += abcd[1]) {
        for (int x = 0, tmx = mx; x < 8; x++, tmx += abcd[0]) {
            const int8_t *const filter =
                dav1d_mc_warp_filter[64 + ((tmx + 512) >> 10)];

Ronald S. Bultje's avatar
Ronald S. Bultje committed
817 818
            mid_ptr[x] = FILTER_WARP_RND(src, x, filter, 1,
                                         7 - intermediate_bits);
819 820 821 822 823 824 825 826 827 828 829
        }
        src += PXSTRIDE(src_stride);
        mid_ptr += 8;
    }

    mid_ptr = &mid[3 * 8];
    for (int y = 0; y < 8; y++, my += abcd[3]) {
        for (int x = 0, tmy = my; x < 8; x++, tmy += abcd[2]) {
            const int8_t *const filter =
                dav1d_mc_warp_filter[64 + ((tmy + 512) >> 10)];

830
            tmp[x] = FILTER_WARP_RND(mid_ptr, x, filter, 8, 7) - PREP_BIAS;
831 832 833 834 835 836
        }
        mid_ptr += 8;
        tmp += tmp_stride;
    }
}

837 838 839 840 841
static void emu_edge_c(const intptr_t bw, const intptr_t bh,
                       const intptr_t iw, const intptr_t ih,
                       const intptr_t x, const intptr_t y,
                       pixel *dst, const ptrdiff_t dst_stride,
                       const pixel *ref, const ptrdiff_t ref_stride)
842 843
{
    // find offset in reference of visible block to copy
844 845
    ref += iclip((int) y, 0, (int) ih - 1) * PXSTRIDE(ref_stride) +
           iclip((int) x, 0, (int) iw - 1);
846 847

    // number of pixels to extend (left, right, top, bottom)
848 849
    const int left_ext = iclip((int) -x, 0, (int) bw - 1);
    const int right_ext = iclip((int) (x + bw - iw), 0, (int) bw - 1);
850
    assert(left_ext + right_ext < bw);
851 852
    const int top_ext = iclip((int) -y, 0, (int) bh - 1);
    const int bottom_ext = iclip((int) (y + bh - ih), 0, (int) bh - 1);
853 854 855 856
    assert(top_ext + bottom_ext < bh);

    // copy visible portion first
    pixel *blk = dst + top_ext * PXSTRIDE(dst_stride);
857 858
    const int center_w = (int) (bw - left_ext - right_ext);
    const int center_h = (int) (bh - top_ext - bottom_ext);
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
    for (int y = 0; y < center_h; y++) {
        pixel_copy(blk + left_ext, ref, center_w);
        // extend left edge for this line
        if (left_ext)
            pixel_set(blk, blk[left_ext], left_ext);
        // extend right edge for this line
        if (right_ext)
            pixel_set(blk + left_ext + center_w, blk[left_ext + center_w - 1],
                      right_ext);
        ref += PXSTRIDE(ref_stride);
        blk += PXSTRIDE(dst_stride);
    }

    // copy top
    blk = dst + top_ext * PXSTRIDE(dst_stride);
    for (int y = 0; y < top_ext; y++) {
        pixel_copy(dst, blk, bw);
        dst += PXSTRIDE(dst_stride);
    }

    // copy bottom
    dst += center_h * PXSTRIDE(dst_stride);
    for (int y = 0; y < bottom_ext; y++) {
        pixel_copy(dst, &dst[-PXSTRIDE(dst_stride)], bw);
        dst += PXSTRIDE(dst_stride);
    }
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
887 888 889
static void resize_c(pixel *dst, const ptrdiff_t dst_stride,
                     const pixel *src, const ptrdiff_t src_stride,
                     const int dst_w, const int src_w, int h,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
890
                     const int dx, const int mx0 HIGHBD_DECL_SUFFIX)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
{
    do {
        int mx = mx0, src_x = -1;
        for (int x = 0; x < dst_w; x++) {
            const int16_t *const F = dav1d_resize_filter[mx >> 8];
            dst[x] = iclip_pixel((F[0] * src[iclip(src_x - 3, 0, src_w - 1)] +
                                  F[1] * src[iclip(src_x - 2, 0, src_w - 1)] +
                                  F[2] * src[iclip(src_x - 1, 0, src_w - 1)] +
                                  F[3] * src[iclip(src_x + 0, 0, src_w - 1)] +
                                  F[4] * src[iclip(src_x + 1, 0, src_w - 1)] +
                                  F[5] * src[iclip(src_x + 2, 0, src_w - 1)] +
                                  F[6] * src[iclip(src_x + 3, 0, src_w - 1)] +
                                  F[7] * src[iclip(src_x + 4, 0, src_w - 1)] +
                                  64) >> 7);
            mx += dx;
            src_x += mx >> 14;
            mx &= 0x3fff;
        }

        dst += PXSTRIDE(dst_stride);
        src += PXSTRIDE(src_stride);
    } while (--h);
}

915 916
void bitfn(dav1d_mc_dsp_init)(Dav1dMCDSPContext *const c) {
#define init_mc_fns(type, name) do { \
917 918 919 920
    c->mc        [type] = put_##name##_c; \
    c->mc_scaled [type] = put_##name##_scaled_c; \
    c->mct       [type] = prep_##name##_c; \
    c->mct_scaled[type] = prep_##name##_scaled_c; \
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
} while (0)

    init_mc_fns(FILTER_2D_8TAP_REGULAR,        8tap_regular);
    init_mc_fns(FILTER_2D_8TAP_REGULAR_SMOOTH, 8tap_regular_smooth);
    init_mc_fns(FILTER_2D_8TAP_REGULAR_SHARP,  8tap_regular_sharp);
    init_mc_fns(FILTER_2D_8TAP_SHARP_REGULAR,  8tap_sharp_regular);
    init_mc_fns(FILTER_2D_8TAP_SHARP_SMOOTH,   8tap_sharp_smooth);
    init_mc_fns(FILTER_2D_8TAP_SHARP,          8tap_sharp);
    init_mc_fns(FILTER_2D_8TAP_SMOOTH_REGULAR, 8tap_smooth_regular);
    init_mc_fns(FILTER_2D_8TAP_SMOOTH,         8tap_smooth);
    init_mc_fns(FILTER_2D_8TAP_SMOOTH_SHARP,   8tap_smooth_sharp);
    init_mc_fns(FILTER_2D_BILINEAR,            bilin);

    c->avg      = avg_c;
    c->w_avg    = w_avg_c;
    c->mask     = mask_c;
    c->blend    = blend_c;
Henrik Gramner's avatar
Henrik Gramner committed
938 939
    c->blend_v  = blend_v_c;
    c->blend_h  = blend_h_c;
940 941 942 943 944
    c->w_mask[0] = w_mask_444_c;
    c->w_mask[1] = w_mask_422_c;
    c->w_mask[2] = w_mask_420_c;
    c->warp8x8  = warp_affine_8x8_c;
    c->warp8x8t = warp_affine_8x8t_c;
945
    c->emu_edge = emu_edge_c;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
946
    c->resize   = resize_c;
Henrik Gramner's avatar
Henrik Gramner committed
947

948 949 950 951
#if HAVE_ASM
#if ARCH_AARCH64 || ARCH_ARM
    bitfn(dav1d_mc_dsp_init_arm)(c);
#elif ARCH_X86
Henrik Gramner's avatar
Henrik Gramner committed
952 953
    bitfn(dav1d_mc_dsp_init_x86)(c);
#endif
954
#endif
955
}