aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/vulkan/ffv1_common.comp
blob: 3d40592739b5ca0ac314fa4ba2048a0dabf2afd7 (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
171
172
173
174
175
176
177
178
179
180
181
/*
 * FFv1 codec
 *
 * 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
 */

struct SliceContext {
    RangeCoder c;

#if !defined(DECODE)
    PutBitContext pb; /* 8*8 bytes */
#else
    GetBitContext gb;
#endif

    ivec2 slice_dim;
    ivec2 slice_pos;
    ivec2 slice_rct_coef;
    u8vec3 quant_table_idx;

    uint hdr_len; // only used for golomb

    uint slice_coding_mode;
    bool slice_reset_contexts;
};

/* -1, { -1, 0 } */
int predict(int L, ivec2 top)
{
    return mid_pred(L, L + top[1] - top[0], top[1]);
}

/* { -2, -1 }, { -1, 0, 1 }, 0 */
int get_context(VTYPE2 cur_l, VTYPE3 top_l, TYPE top2, uint8_t quant_table_idx)
{
    const int LT = top_l[0]; /* -1 */
    const int T  = top_l[1]; /*  0 */
    const int RT = top_l[2]; /*  1 */
    const int L  = cur_l[1]; /* -1 */

    int base = quant_table[quant_table_idx][0][(L - LT) & MAX_QUANT_TABLE_MASK] +
               quant_table[quant_table_idx][1][(LT - T) & MAX_QUANT_TABLE_MASK] +
               quant_table[quant_table_idx][2][(T - RT) & MAX_QUANT_TABLE_MASK];

    if ((quant_table[quant_table_idx][3][127] == 0) &&
        (quant_table[quant_table_idx][4][127] == 0))
        return base;

    const int TT = top2;     /* -2 */
    const int LL = cur_l[0]; /* -2 */
    return base +
           quant_table[quant_table_idx][3][(LL - L) & MAX_QUANT_TABLE_MASK] +
           quant_table[quant_table_idx][4][(TT - T) & MAX_QUANT_TABLE_MASK];
}

const uint32_t log2_run[41] = {
     0,  0,  0,  0,  1,  1,  1,  1,
     2,  2,  2,  2,  3,  3,  3,  3,
     4,  4,  5,  5,  6,  6,  7,  7,
     8,  9, 10, 11, 12, 13, 14, 15,
    16, 17, 18, 19, 20, 21, 22, 23,
    24,
};

uint slice_coord(uint width, uint sx, uint num_h_slices, uint chroma_shift)
{
    uint mpw = 1 << chroma_shift;
    uint awidth = align(width, mpw);

    if ((version < 4) || ((version == 4) && (micro_version < 3)))
        return width * sx / num_h_slices;

    sx = (2 * awidth * sx + num_h_slices * mpw) / (2 * num_h_slices * mpw) * mpw;
    if (sx == awidth)
        sx = width;

    return sx;
}

#ifdef RGB
#define RGB_LBUF (RGB_LINECACHE - 1)
#define LADDR(p) (ivec2((p).x, ((p).y & RGB_LBUF)))

ivec2 get_pred(readonly uimage2D pred, ivec2 sp, ivec2 off,
               int comp, int sw, uint8_t quant_table_idx, bool extend_lookup)
{
    const ivec2 yoff_border1 = expectEXT(off.x == 0, false) ? off + ivec2(1, -1) : off;

    /* Thanks to the same coincidence as below, we can skip checking if off == 0, 1 */
    VTYPE3 top  = VTYPE3(TYPE(imageLoad(pred, sp + LADDR(yoff_border1 + ivec2(-1, -1)))[comp]),
                         TYPE(imageLoad(pred, sp + LADDR(off + ivec2(0, -1)))[comp]),
                         TYPE(imageLoad(pred, sp + LADDR(off + ivec2(min(1, sw - off.x - 1), -1)))[comp]));

    /* Normally, we'd need to check if off != ivec2(0, 0) here, since otherwise, we must
     * return zero. However, ivec2(-1,  0) + ivec2(1, -1) == ivec2(0, -1), e.g. previous
     * row, 0 offset, same slice, which is zero since we zero out the buffer for RGB */
    TYPE cur = TYPE(imageLoad(pred, sp + LADDR(yoff_border1 + ivec2(-1,  0)))[comp]);

    int base = quant_table[quant_table_idx][0][(cur    - top[0]) & MAX_QUANT_TABLE_MASK] +
               quant_table[quant_table_idx][1][(top[0] - top[1]) & MAX_QUANT_TABLE_MASK] +
               quant_table[quant_table_idx][2][(top[1] - top[2]) & MAX_QUANT_TABLE_MASK];

    if (expectEXT(extend_lookup, false)) {
        TYPE cur2 = TYPE(0);
        if (expectEXT(off.x > 0, true)) {
            const ivec2 yoff_border2 = expectEXT(off.x == 1, false) ? ivec2(-1, -1) : ivec2(-2, 0);
            cur2 = TYPE(imageLoad(pred, sp + LADDR(off + yoff_border2))[comp]);
        }
        base += quant_table[quant_table_idx][3][(cur2 - cur) & MAX_QUANT_TABLE_MASK];

        /* top-2 became current upon swap */
        TYPE top2 = TYPE(imageLoad(pred, sp + LADDR(off))[comp]);
        base += quant_table[quant_table_idx][4][(top2 - top[1]) & MAX_QUANT_TABLE_MASK];
    }

    /* context, prediction */
    return ivec2(base, predict(cur, VTYPE2(top)));
}

#else /* RGB */

#define LADDR(p) (p)

ivec2 get_pred(readonly uimage2D pred, ivec2 sp, ivec2 off,
               int comp, int sw, uint8_t quant_table_idx, bool extend_lookup)
{
    const ivec2 yoff_border1 = off.x == 0 ? ivec2(1, -1) : ivec2(0, 0);
    sp += off;

    VTYPE3 top  = VTYPE3(TYPE(0),
                         TYPE(0),
                         TYPE(0));
    if (off.y > 0 && off != ivec2(0, 1))
        top[0] = TYPE(imageLoad(pred, sp + ivec2(-1, -1) + yoff_border1)[comp]);
    if (off.y > 0) {
        top[1] = TYPE(imageLoad(pred, sp + ivec2(0, -1))[comp]);
        top[2] = TYPE(imageLoad(pred, sp + ivec2(min(1, sw - off.x - 1), -1))[comp]);
    }

    TYPE cur = TYPE(0);
    if (off != ivec2(0, 0))
        cur = TYPE(imageLoad(pred, sp + ivec2(-1,  0) + yoff_border1)[comp]);

    int base = quant_table[quant_table_idx][0][(cur - top[0]) & MAX_QUANT_TABLE_MASK] +
               quant_table[quant_table_idx][1][(top[0] - top[1]) & MAX_QUANT_TABLE_MASK] +
               quant_table[quant_table_idx][2][(top[1] - top[2]) & MAX_QUANT_TABLE_MASK];

    if (expectEXT(extend_lookup, false)) {
        TYPE cur2 = TYPE(0);
        if (off.x > 0 && off != ivec2(1, 0)) {
            const ivec2 yoff_border2 = off.x == 1 ? ivec2(1, -1) : ivec2(0, 0);
            cur2 = TYPE(imageLoad(pred, sp + ivec2(-2,  0) + yoff_border2)[comp]);
        }
        base += quant_table[quant_table_idx][3][(cur2 - cur) & MAX_QUANT_TABLE_MASK];

        TYPE top2 = TYPE(0);
        if (off.y > 1)
            top2 = TYPE(imageLoad(pred, sp + ivec2(0, -2))[comp]);
        base += quant_table[quant_table_idx][4][(top2 - top[1]) & MAX_QUANT_TABLE_MASK];
    }

    /* context, prediction */
    return ivec2(base, predict(cur, VTYPE2(top)));
}
#endif