/*
 * Copyright © 2018-2026, VideoLAN and dav2d authors
 * Copyright © 2018-2026, 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"

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "common/attributes.h"
#include "common/intops.h"

#include "src/itx.h"
#include "src/itx_1d.h"
#include "src/scan.h"
#include "src/tables.h"

static void cctx_c(coef *const u, coef *const v, const int16_t angle[3],
                   const size_t sz HIGHBD_DECL_SUFFIX)
{
    assert(!(sz & (sz - 1)) && sz >= 4 * 4 && sz <= 32 * 32);
    const int bd = bitdepth_from_max(bitdepth_max);
    const int min = -(1 << (bd + 7));
    const int max = (1 << (bd + 7)) - 1;
    const int sina = angle[0];
    const int cosa = angle[1];
    assert(angle[2] == -sina);
    for (size_t i = 0; i < sz; i++) {
        const int a = u[i] * cosa - v[i] * sina;
        const int b = u[i] * sina + v[i] * cosa;
        u[i] = iclip((a + 128 - (a < 0)) >> 8, min, max);
        v[i] = iclip((b + 128 - (b < 0)) >> 8, min, max);
    }
}

static void residual_add(pixel *dst, const ptrdiff_t stride,
                         const int32_t *c, const int w, const int h,
                         const int rnd, const int shift,
                         const enum TxfmType txtp HIGHBD_DECL_SUFFIX)
{
    const int dpcm_flag = txtp >> 8;
    assert(!dpcm_flag || (txtp & 0xe7) == IDTX || (txtp & 0xe7) == WHT_WHT);
    switch (dpcm_flag) {
    default: assert(0);
    case 0:
        for (int y = 0; y < h; y++, dst += PXSTRIDE(stride))
            for (int x = 0; x < w; x++)
                dst[x] = iclip_pixel(dst[x] + ((*c++ + rnd) >> shift));
        break;
    case 1:
        for (int y = 0; y < h; y++, dst += PXSTRIDE(stride))
            for (int x = 0, acc = 0; x < w; x++) {
                acc += (*c++ + rnd) >> shift;
                dst[x] = iclip_pixel(dst[x] + acc);
            }
        break;
    case 2:
        for (int x = 0; x < w; x++, c++, dst++)
            for (int y = 0, acc = 0; y < h; y++) {
                acc += (c[y * w] + rnd) >> shift;
                dst[y * PXSTRIDE(stride)] =
                    iclip_pixel(dst[y * PXSTRIDE(stride)] + acc);
            }
        break;
    }
}

static void inv_txfm_add_wht_wht_4x4_c(pixel *dst, const ptrdiff_t stride,
                                       coef *const coeff, const enum TxfmType txtp,
                                       const int eob HIGHBD_DECL_SUFFIX)
{
    int32_t tmp[4 * 4], *c = tmp;
    for (int y = 0; y < 4; y++, c += 4) {
        for (int x = 0; x < 4; x++)
            c[x] = coeff[y + x * 4] >> 3;
        dav2d_inv_wht4_1d_c(c, 1);
    }
    memset(coeff, 0, sizeof(*coeff) * 4 * 4);

    for (int x = 0; x < 4; x++)
        dav2d_inv_wht4_1d_c(&tmp[x], 4);

    residual_add(dst, stride, tmp, 4, 4, 0, 0, txtp HIGHBD_TAIL_SUFFIX);
}

static NOINLINE void
inv_txfm_add_c(pixel *dst, const ptrdiff_t stride, coef *const coeff,
               const enum TxfmType txtp, const int eob,
               const /*enum RectTxfmSize*/ int tx HIGHBD_DECL_SUFFIX)
{
    if ((txtp & 0xff) == WHT_WHT) {
        assert(tx == TX_4X4);
        inv_txfm_add_wht_wht_4x4_c(dst, stride, coeff, txtp, eob HIGHBD_TAIL_SUFFIX);
        return;
    }
    const TxfmInfo *const t_dim = &dav2d_txfm_dimensions[tx];
    const uint8_t *const tx_shift = dav2d_tx_shift[tx];
    const int w = 4 * t_dim->w, h = 4 * t_dim->h;
    assert(!(w & (w - 1)) && w >= 4 && w <= 64);
    assert(!(h & (h - 1)) && h >= 4 && h <= 64);
    assert(eob >= 0 && eob < 32 * 32);

    const int is_rect2 = (t_dim->lw + t_dim->lh) & 1;
    if (eob + txtp == 0) { // DC-only DCT_DCT
        const int shift_p1 = tx_shift[0];
        const int shift = shift_p1 + tx_shift[1] - 12;
        const int rnd = (1 << (shift - 1)) + shift_p1 - 6;
        int dc = coeff[0];
        coeff[0] = 0;

        if (is_rect2)
            dc = (dc * 181 + 128) >> 8;
        dc = (dc + rnd) >> shift;

        for (int y = 0; y < h; y++, dst += PXSTRIDE(stride))
            for (int x = 0; x < w; x++)
                dst[x] = iclip_pixel(dst[x] + dc);
        return;
    }

    const itx_1d_fn first_1d_fn = dav2d_tx1d_fns[t_dim->lw][txtp & 7];
    const itx_1d_fn second_1d_fn = dav2d_tx1d_fns[t_dim->lh][(txtp >> 5) & 7];
    const int sh = imin(h, 32), sw = imin(w, 32);
#if BITDEPTH == 8
    const int row_clip_min = INT16_MIN;
#else
    const int row_clip_min = (int) ((unsigned) ~bitdepth_max << 7);
#endif
    const int row_clip_max = ~row_clip_min;

    int32_t tmp[32 * 32], *c = tmp;
    int col = 0;
    const enum TxClass tx_class = (txtp >> 3) & 0x3;
    if (tx_class == TX_CLASS_2D) {
        const uint16_t *last_eob = dav2d_last_eob_per_col.table +
                                   dav2d_last_eob_per_col.offset[tx];
        do {
            if (is_rect2)
                for (int x = 0; x < sw; x++)
                    c[x] = (coeff[col + x * sh] * 181 + 128) >> 8;
            else
                for (int x = 0; x < sw; x++)
                    c[x] = coeff[col + x * sh];
            first_1d_fn(c, 1);
            c += sw;
        } while ((++col & 3) || eob > *last_eob++);
    } else {
        int last_nz_col;
        if (tx_class == TX_CLASS_H)
            last_nz_col = imin(sh - 1, eob);
        else if (tx_class == TX_CLASS_V)
            last_nz_col = eob >> (t_dim->lw + 2);
        else /* TX_CLASS_2D_INV */
            last_nz_col = sh - 1;
        assert(last_nz_col < sh);
        do {
            if (is_rect2)
                for (int x = 0; x < sw; x++)
                    c[x] = (coeff[col + x * sh] * 181 + 128) >> 8;
            else
                for (int x = 0; x < sw; x++)
                    c[x] = coeff[col + x * sh];
            first_1d_fn(c, 1);
            c += sw;
        } while (++col <= last_nz_col);
    }
    if (col < sh)
        memset(c, 0, sizeof(*c) * (sh - col) * sw);

    memset(coeff, 0, sizeof(*coeff) * sw * sh);
    int shift = tx_shift[0];
    int rnd = (1 << shift) >> 1;
    for (int i = 0; i < sw * sh; i++)
        tmp[i] = iclip((tmp[i] + rnd) >> shift, row_clip_min, row_clip_max);

    for (int x = 0; x < sw; x++)
        second_1d_fn(&tmp[x], sw);

    shift = tx_shift[1];
    rnd = (1 << shift) >> 1;
    c = tmp;

    /* Handle idct64 upsampling */
    if (w > sw) {
        if (h > sh) {
            for (int y = 0; y < h; y += 2, dst += PXSTRIDE(stride) * 2) {
                pixel *const dst2 = dst + PXSTRIDE(stride);
                for (int x = 0; x < w; x += 2) {
                    const int cf = (*c++ + rnd) >> shift;
                    dst[x + 0]  = iclip_pixel(dst[x + 0]  + cf);
                    dst[x + 1]  = iclip_pixel(dst[x + 1]  + cf);
                    dst2[x + 0] = iclip_pixel(dst2[x + 0] + cf);
                    dst2[x + 1] = iclip_pixel(dst2[x + 1] + cf);
                }
            }
        } else {
            for (int y = 0; y < h; y++, dst += PXSTRIDE(stride))
                for (int x = 0; x < w; x += 2) {
                    const int cf = (*c++ + rnd) >> shift;
                    dst[x + 0] = iclip_pixel(dst[x + 0] + cf);
                    dst[x + 1] = iclip_pixel(dst[x + 1] + cf);
                }
        }
    } else if (h > sh) {
        for (int y = 0; y < h; y += 2, dst += PXSTRIDE(stride) * 2) {
            pixel *const dst2 = dst + PXSTRIDE(stride);
            for (int x = 0; x < w; x++) {
                const int cf = (*c++ + rnd) >> shift;
                dst[x]  = iclip_pixel(dst[x]  + cf);
                dst2[x] = iclip_pixel(dst2[x] + cf);
            }
        }
    } else {
        residual_add(dst, stride, c, w, h, rnd, shift, txtp HIGHBD_TAIL_SUFFIX);
    }
}

#define inv_txfm_fn(pfx, w, h) \
static void \
inv_txfm_add_##w##x##h##_c(pixel *dst, const ptrdiff_t stride, \
                           coef *const coeff, const enum TxfmType txtp, \
                           const int eob HIGHBD_DECL_SUFFIX) \
{ \
    inv_txfm_add_c(dst, stride, coeff, txtp, eob, pfx##TX_##w##X##h \
                   HIGHBD_TAIL_SUFFIX); \
}

inv_txfm_fn( ,  4,  4)
inv_txfm_fn(R,  4,  8)
inv_txfm_fn(R,  4, 16)
inv_txfm_fn(R,  4, 32)
inv_txfm_fn(R,  4, 64)
inv_txfm_fn(R,  8,  4)
inv_txfm_fn( ,  8,  8)
inv_txfm_fn(R,  8, 16)
inv_txfm_fn(R,  8, 32)
inv_txfm_fn(R,  8, 64)
inv_txfm_fn(R, 16,  4)
inv_txfm_fn(R, 16,  8)
inv_txfm_fn( , 16, 16)
inv_txfm_fn(R, 16, 32)
inv_txfm_fn(R, 16, 64)
inv_txfm_fn(R, 32,  4)
inv_txfm_fn(R, 32,  8)
inv_txfm_fn(R, 32, 16)
inv_txfm_fn( , 32, 32)
inv_txfm_fn(R, 32, 64)
inv_txfm_fn(R, 64,  4)
inv_txfm_fn(R, 64,  8)
inv_txfm_fn(R, 64, 16)
inv_txfm_fn(R, 64, 32)
inv_txfm_fn( , 64, 64)

#if HAVE_ASM
#if ARCH_X86
#include "src/x86/itx.h"
#endif
#endif

COLD void bitfn(dav2d_itx_dsp_init)(Dav2dInvTxfmDSPContext *const c) {
#define assign_itx(w, h, pfx) \
    c->itxfm_add[pfx##TX_##w##X##h] = inv_txfm_add_##w##x##h##_c

    c->cctx = cctx_c;
    assign_itx( 4,  4, );
    assign_itx( 4,  8, R);
    assign_itx( 4, 16, R);
    assign_itx( 4, 32, R);
    assign_itx( 4, 64, R);
    assign_itx( 8,  4, R);
    assign_itx( 8,  8, );
    assign_itx( 8, 16, R);
    assign_itx( 8, 32, R);
    assign_itx( 8, 64, R);
    assign_itx(16,  4, R);
    assign_itx(16,  8, R);
    assign_itx(16, 16, );
    assign_itx(16, 32, R);
    assign_itx(16, 64, R);
    assign_itx(32,  4, R);
    assign_itx(32,  8, R);
    assign_itx(32, 16, R);
    assign_itx(32, 32, );
    assign_itx(32, 64, R);
    assign_itx(64,  4, R);
    assign_itx(64,  8, R);
    assign_itx(64, 16, R);
    assign_itx(64, 32, R);
    assign_itx(64, 64, );

#if HAVE_ASM
#if ARCH_X86
    itx_dsp_init_x86(c);
#endif
#endif
}
