aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/vulkan/common.comp
blob: a28701584e673cf74122cc1001229411657944ca (plain) (blame)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (c) 2024 Lynne <dev@lynne.ee>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

layout(buffer_reference, buffer_reference_align = 1) buffer u8buf {
    uint8_t v;
};

layout(buffer_reference, buffer_reference_align = 2) buffer u16buf {
    uint16_t v;
};

layout(buffer_reference, buffer_reference_align = 4) buffer u32buf {
    uint32_t v;
};

layout(buffer_reference, buffer_reference_align = 8) buffer u64buf {
    uint64_t v;
};

#define OFFBUF(type, b, l) \
    type(uint64_t(b) + uint64_t(l))

#define zero_extend(a, p) \
    ((a) & ((1 << (p)) - 1))

#define sign_extend(val, bits) \
    bitfieldExtract(val, 0, bits)

#define fold(diff, bits) \
    sign_extend(diff, bits)

#define mid_pred(a, b, c) \
    max(min((a), (b)), min(max((a), (b)), (c)))

/* TODO: optimize */
uint align(uint src, uint a)
{
    uint res = src % a;
    if (res == 0)
        return src;
    return src + a - res;
}

/* TODO: optimize */
uint64_t align64(uint64_t src, uint64_t a)
{
    uint64_t res = src % a;
    if (res == 0)
        return src;
    return src + a - res;
}

#define reverse4(src) \
    (pack32(unpack8(uint32_t(src)).wzyx))

uint64_t reverse8(uint64_t src)
{
    u32vec2 tmp = unpack32(src);
    tmp.x = reverse4(tmp.x);
    tmp.y = reverse4(tmp.y);
    return pack64(tmp.yx);
}

#ifdef PB_32
#define BIT_BUF_TYPE uint32_t
#define BUF_TYPE u32buf
#define BUF_REVERSE(src) reverse4(src)
#define BUF_BITS uint8_t(32)
#define BUF_BYTES uint8_t(4)
#define BYTE_EXTRACT(src, byte_off) \
    (uint8_t(bitfieldExtract((src), ((byte_off) << 3), 8)))
#else
#define BIT_BUF_TYPE uint64_t
#define BUF_TYPE u64buf
#define BUF_REVERSE(src) reverse8(src)
#define BUF_BITS uint8_t(64)
#define BUF_BYTES uint8_t(8)
#define BYTE_EXTRACT(src, byte_off) \
    (uint8_t(((src) >> ((byte_off) << 3)) & 0xFF))
#endif

struct PutBitContext {
    uint64_t buf_start;
    uint64_t buf;

    BIT_BUF_TYPE bit_buf;
    uint8_t bit_left;
};

void put_bits(inout PutBitContext pb, const uint32_t n, uint32_t value)
{
    if (n < pb.bit_left) {
        pb.bit_buf = (pb.bit_buf << n) | value;
        pb.bit_left -= uint8_t(n);
    } else {
        pb.bit_buf <<= pb.bit_left;
        pb.bit_buf |= (value >> (n - pb.bit_left));

#ifdef PB_UNALIGNED
        u8buf bs = u8buf(pb.buf);
        [[unroll]]
        for (uint8_t i = uint8_t(0); i < BUF_BYTES; i++)
            bs[i].v = BYTE_EXTRACT(pb.bit_buf, BUF_BYTES - uint8_t(1) - i);
#else
#ifdef DEBUG
        if ((pb.buf % BUF_BYTES) != 0)
            debugPrintfEXT("put_bits buffer is not aligned!");
#endif

        BUF_TYPE bs = BUF_TYPE(pb.buf);
        bs.v = BUF_REVERSE(pb.bit_buf);
#endif
        pb.buf = uint64_t(bs) + BUF_BYTES;

        pb.bit_left += BUF_BITS - uint8_t(n);
        pb.bit_buf = value;
    }
}

uint32_t flush_put_bits(inout PutBitContext pb)
{
    /* Align bits to MSBs */
    if (pb.bit_left < BUF_BITS)
        pb.bit_buf <<= pb.bit_left;

    if (pb.bit_left < BUF_BITS) {
        uint to_write = ((BUF_BITS - pb.bit_left) >> 3) + 1;

        u8buf bs = u8buf(pb.buf);
        for (int i = 0; i < to_write; i++)
            bs[i].v = BYTE_EXTRACT(pb.bit_buf, BUF_BYTES - uint8_t(1) - i);
        pb.buf = uint64_t(bs) + BUF_BYTES;
    }

    pb.bit_left = BUF_BITS;
    pb.bit_buf = 0x0;

    return uint32_t(pb.buf - pb.buf_start);
}

void init_put_bits(out PutBitContext pb, u8buf data, uint64_t len)
{
    pb.buf_start = uint64_t(data);
    pb.buf = uint64_t(data);

    pb.bit_buf = 0;
    pb.bit_left = BUF_BITS;
}

uint64_t put_bits_count(in PutBitContext pb)
{
    return (pb.buf - pb.buf_start)*8 + BUF_BITS - pb.bit_left;
}