2025-03-10 03:04:39 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GOLOMB
|
2025-04-10 08:44:31 +00:00
|
|
|
#ifdef CACHED_SYMBOL_READER
|
|
|
|
shared uint8_t state[CONTEXT_SIZE];
|
|
|
|
#define READ(c, off) get_rac_direct(c, state[off])
|
|
|
|
#else
|
|
|
|
#define READ(c, off) get_rac(c, uint64_t(slice_state) + (state_off + off))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int get_isymbol(inout RangeCoder c, uint state_off)
|
2025-03-10 03:04:39 +00:00
|
|
|
{
|
2025-04-10 08:44:31 +00:00
|
|
|
if (READ(c, 0))
|
2025-03-10 03:04:39 +00:00
|
|
|
return 0;
|
|
|
|
|
2025-04-10 08:44:31 +00:00
|
|
|
uint e = 1;
|
|
|
|
for (; e < 33; e++)
|
|
|
|
if (!READ(c, min(e, 10)))
|
2025-03-10 03:04:39 +00:00
|
|
|
break;
|
2025-04-08 05:45:16 +00:00
|
|
|
|
2025-04-10 08:44:31 +00:00
|
|
|
if (expectEXT(e == 1, false)) {
|
|
|
|
return READ(c, 11) ? -1 : 1;
|
|
|
|
} else if (expectEXT(e == 33, false)) {
|
2025-03-10 03:04:39 +00:00
|
|
|
corrupt = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-04-02 20:46:12 +00:00
|
|
|
int a = 1;
|
2025-04-10 08:44:31 +00:00
|
|
|
for (uint i = e + 20; i >= 22; i--) {
|
2025-04-02 20:46:12 +00:00
|
|
|
a <<= 1;
|
2025-04-10 08:44:31 +00:00
|
|
|
a |= int(READ(c, min(i, 31)));
|
2025-04-02 20:46:12 +00:00
|
|
|
}
|
2025-03-10 03:04:39 +00:00
|
|
|
|
2025-04-10 08:44:31 +00:00
|
|
|
return READ(c, min(e + 10, 21)) ? -a : a;
|
2025-03-10 03:04:39 +00:00
|
|
|
}
|
|
|
|
|
2025-04-01 22:36:46 +00:00
|
|
|
void decode_line_pcm(inout SliceContext sc, ivec2 sp, int w, int y, int p, int bits)
|
2025-03-10 03:04:39 +00:00
|
|
|
{
|
2025-05-03 14:15:04 +02:00
|
|
|
#ifdef CACHED_SYMBOL_READER
|
|
|
|
if (gl_LocalInvocationID.x > 0)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2025-03-10 03:04:39 +00:00
|
|
|
#ifndef RGB
|
|
|
|
if (p > 0 && p < 3) {
|
|
|
|
w >>= chroma_shift.x;
|
|
|
|
sp >>= chroma_shift;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
|
|
uint v = 0;
|
|
|
|
for (int i = (bits - 1); i >= 0; i--)
|
|
|
|
v |= uint(get_rac_equi(sc.c)) << i;
|
|
|
|
|
2025-04-01 22:36:46 +00:00
|
|
|
imageStore(dec[p], sp + LADDR(ivec2(x, y)), uvec4(v));
|
2025-03-10 03:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-01 22:36:46 +00:00
|
|
|
void decode_line(inout SliceContext sc, ivec2 sp, int w,
|
2025-04-10 08:44:31 +00:00
|
|
|
int y, int p, int bits, uint state_off,
|
2025-04-05 05:04:29 +00:00
|
|
|
uint8_t quant_table_idx, const int run_index)
|
2025-03-10 03:04:39 +00:00
|
|
|
{
|
|
|
|
#ifndef RGB
|
|
|
|
if (p > 0 && p < 3) {
|
|
|
|
w >>= chroma_shift.x;
|
|
|
|
sp >>= chroma_shift;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (int x = 0; x < w; x++) {
|
2025-05-01 14:36:23 +02:00
|
|
|
ivec2 pr = get_pred(dec[p], sp, ivec2(x, y), 0, w,
|
2025-05-01 19:45:08 +02:00
|
|
|
quant_table_idx, extend_lookup[quant_table_idx] > 0);
|
2025-03-10 03:04:39 +00:00
|
|
|
|
2025-04-10 08:44:31 +00:00
|
|
|
uint context_off = state_off + CONTEXT_SIZE*abs(pr[0]);
|
|
|
|
#ifdef CACHED_SYMBOL_READER
|
|
|
|
u8buf sb = u8buf(uint64_t(slice_state) + context_off + gl_LocalInvocationID.x);
|
|
|
|
state[gl_LocalInvocationID.x] = sb.v;
|
|
|
|
barrier();
|
|
|
|
if (gl_LocalInvocationID.x == 0) {
|
2025-03-10 03:04:39 +00:00
|
|
|
|
2025-04-10 08:44:31 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
int diff = get_isymbol(sc.c, context_off);
|
|
|
|
if (pr[0] < 0)
|
|
|
|
diff = -diff;
|
|
|
|
|
|
|
|
uint v = zero_extend(pr[1] + diff, bits);
|
|
|
|
imageStore(dec[p], sp + LADDR(ivec2(x, y)), uvec4(v));
|
|
|
|
|
|
|
|
#ifdef CACHED_SYMBOL_READER
|
|
|
|
}
|
2025-05-23 05:11:07 +09:00
|
|
|
|
|
|
|
barrier();
|
2025-04-10 08:44:31 +00:00
|
|
|
sb.v = state[gl_LocalInvocationID.x];
|
|
|
|
#endif
|
2025-03-10 03:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* GOLOMB */
|
|
|
|
|
2025-04-01 22:36:46 +00:00
|
|
|
void decode_line(inout SliceContext sc, ivec2 sp, int w,
|
2025-04-10 08:44:31 +00:00
|
|
|
int y, int p, int bits, uint state_off,
|
2025-04-05 05:04:29 +00:00
|
|
|
uint8_t quant_table_idx, inout int run_index)
|
2025-03-10 03:04:39 +00:00
|
|
|
{
|
|
|
|
#ifndef RGB
|
|
|
|
if (p > 0 && p < 3) {
|
|
|
|
w >>= chroma_shift.x;
|
|
|
|
sp >>= chroma_shift;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int run_count = 0;
|
|
|
|
int run_mode = 0;
|
|
|
|
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
|
|
ivec2 pos = sp + ivec2(x, y);
|
|
|
|
int diff;
|
2025-05-01 14:36:23 +02:00
|
|
|
ivec2 pr = get_pred(dec[p], sp, ivec2(x, y), 0, w,
|
2025-05-01 19:45:08 +02:00
|
|
|
quant_table_idx, extend_lookup[quant_table_idx] > 0);
|
2025-03-10 03:04:39 +00:00
|
|
|
|
2025-05-27 06:32:55 +09:00
|
|
|
uint context_off = state_off + VLC_STATE_SIZE*abs(pr[0]);
|
|
|
|
VlcState sb = VlcState(uint64_t(slice_state) + context_off);
|
2025-03-10 03:04:39 +00:00
|
|
|
|
|
|
|
if (pr[0] == 0 && run_mode == 0)
|
|
|
|
run_mode = 1;
|
|
|
|
|
|
|
|
if (run_mode != 0) {
|
|
|
|
if (run_count == 0 && run_mode == 1) {
|
|
|
|
int tmp_idx = int(log2_run[run_index]);
|
|
|
|
if (get_bit(sc.gb)) {
|
|
|
|
run_count = 1 << tmp_idx;
|
|
|
|
if (x + run_count <= w)
|
|
|
|
run_index++;
|
|
|
|
} else {
|
|
|
|
if (tmp_idx != 0) {
|
|
|
|
run_count = int(get_bits(sc.gb, tmp_idx));
|
|
|
|
} else
|
|
|
|
run_count = 0;
|
|
|
|
|
|
|
|
if (run_index != 0)
|
|
|
|
run_index--;
|
|
|
|
run_mode = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run_count--;
|
|
|
|
if (run_count < 0) {
|
|
|
|
run_mode = 0;
|
|
|
|
run_count = 0;
|
|
|
|
diff = read_vlc_symbol(sc.gb, sb, bits);
|
|
|
|
if (diff >= 0)
|
|
|
|
diff++;
|
|
|
|
} else {
|
|
|
|
diff = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
diff = read_vlc_symbol(sc.gb, sb, bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pr[0] < 0)
|
|
|
|
diff = -diff;
|
|
|
|
|
|
|
|
uint v = zero_extend(pr[1] + diff, bits);
|
2025-04-01 22:36:46 +00:00
|
|
|
imageStore(dec[p], sp + LADDR(ivec2(x, y)), uvec4(v));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RGB
|
|
|
|
ivec4 transform_sample(ivec4 pix, ivec2 rct_coef)
|
|
|
|
{
|
|
|
|
pix.b -= rct_offset;
|
|
|
|
pix.r -= rct_offset;
|
|
|
|
pix.g -= (pix.b*rct_coef.y + pix.r*rct_coef.x) >> 2;
|
|
|
|
pix.b += pix.g;
|
|
|
|
pix.r += pix.g;
|
|
|
|
return ivec4(pix[fmt_lut[0]], pix[fmt_lut[1]],
|
|
|
|
pix[fmt_lut[2]], pix[fmt_lut[3]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeout_rgb(in SliceContext sc, ivec2 sp, int w, int y, bool apply_rct)
|
|
|
|
{
|
2025-04-10 08:44:31 +00:00
|
|
|
for (uint x = gl_LocalInvocationID.x; x < w; x += gl_WorkGroupSize.x) {
|
2025-04-01 22:36:46 +00:00
|
|
|
ivec2 lpos = sp + LADDR(ivec2(x, y));
|
|
|
|
ivec2 pos = sc.slice_pos + ivec2(x, y);
|
|
|
|
|
|
|
|
ivec4 pix;
|
|
|
|
pix.r = int(imageLoad(dec[2], lpos)[0]);
|
|
|
|
pix.g = int(imageLoad(dec[0], lpos)[0]);
|
|
|
|
pix.b = int(imageLoad(dec[1], lpos)[0]);
|
|
|
|
if (transparency != 0)
|
|
|
|
pix.a = int(imageLoad(dec[3], lpos)[0]);
|
|
|
|
|
2025-04-11 23:44:26 +00:00
|
|
|
if (expectEXT(apply_rct, true))
|
2025-04-01 22:36:46 +00:00
|
|
|
pix = transform_sample(pix, sc.slice_rct_coef);
|
|
|
|
|
|
|
|
imageStore(dst[0], pos, pix);
|
|
|
|
if (planar_rgb != 0) {
|
|
|
|
for (int i = 1; i < color_planes; i++)
|
|
|
|
imageStore(dst[i], pos, ivec4(pix[i]));
|
|
|
|
}
|
2025-03-10 03:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void decode_slice(inout SliceContext sc, const uint slice_idx)
|
|
|
|
{
|
2025-04-01 22:36:46 +00:00
|
|
|
int w = sc.slice_dim.x;
|
|
|
|
ivec2 sp = sc.slice_pos;
|
2025-03-10 03:04:39 +00:00
|
|
|
|
|
|
|
#ifndef RGB
|
|
|
|
int bits = bits_per_raw_sample;
|
|
|
|
#else
|
|
|
|
int bits = 9;
|
|
|
|
if (bits != 8 || sc.slice_coding_mode != 0)
|
|
|
|
bits = bits_per_raw_sample + int(sc.slice_coding_mode != 1);
|
2025-04-01 22:36:46 +00:00
|
|
|
|
|
|
|
sp.y = int(gl_WorkGroupID.y)*RGB_LINECACHE;
|
2025-03-10 03:04:39 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* PCM coding */
|
|
|
|
#ifndef GOLOMB
|
|
|
|
if (sc.slice_coding_mode == 1) {
|
|
|
|
#ifndef RGB
|
|
|
|
for (int p = 0; p < planes; p++) {
|
|
|
|
int h = sc.slice_dim.y;
|
|
|
|
if (p > 0 && p < 3)
|
|
|
|
h >>= chroma_shift.y;
|
|
|
|
|
|
|
|
for (int y = 0; y < h; y++)
|
2025-04-01 22:36:46 +00:00
|
|
|
decode_line_pcm(sc, sp, w, y, p, bits);
|
2025-03-10 03:04:39 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
for (int y = 0; y < sc.slice_dim.y; y++) {
|
|
|
|
for (int p = 0; p < color_planes; p++)
|
2025-04-01 22:36:46 +00:00
|
|
|
decode_line_pcm(sc, sp, w, y, p, bits);
|
|
|
|
|
|
|
|
writeout_rgb(sc, sp, w, y, false);
|
2025-03-10 03:04:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
|
|
|
|
/* Arithmetic coding */
|
|
|
|
#endif
|
|
|
|
{
|
2025-04-05 05:04:29 +00:00
|
|
|
u8vec4 quant_table_idx = sc.quant_table_idx.xyyz;
|
2025-04-10 08:44:31 +00:00
|
|
|
u32vec4 slice_state_off = (slice_idx*codec_planes + uvec4(0, 1, 1, 2))*plane_state_size;
|
2025-03-10 03:04:39 +00:00
|
|
|
|
|
|
|
#ifndef RGB
|
|
|
|
for (int p = 0; p < planes; p++) {
|
|
|
|
int h = sc.slice_dim.y;
|
|
|
|
if (p > 0 && p < 3)
|
|
|
|
h >>= chroma_shift.y;
|
|
|
|
|
2025-05-27 06:38:47 +09:00
|
|
|
int run_index = 0;
|
2025-03-10 03:04:39 +00:00
|
|
|
for (int y = 0; y < h; y++)
|
2025-04-01 22:36:46 +00:00
|
|
|
decode_line(sc, sp, w, y, p, bits,
|
2025-04-05 05:04:29 +00:00
|
|
|
slice_state_off[p], quant_table_idx[p], run_index);
|
2025-03-10 03:04:39 +00:00
|
|
|
}
|
|
|
|
#else
|
2025-05-27 06:38:47 +09:00
|
|
|
int run_index = 0;
|
2025-03-10 03:04:39 +00:00
|
|
|
for (int y = 0; y < sc.slice_dim.y; y++) {
|
|
|
|
for (int p = 0; p < color_planes; p++)
|
2025-04-01 22:36:46 +00:00
|
|
|
decode_line(sc, sp, w, y, p, bits,
|
2025-04-05 05:04:29 +00:00
|
|
|
slice_state_off[p], quant_table_idx[p], run_index);
|
2025-04-01 22:36:46 +00:00
|
|
|
|
|
|
|
writeout_rgb(sc, sp, w, y, true);
|
2025-03-10 03:04:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
|
|
|
const uint slice_idx = gl_WorkGroupID.y*gl_NumWorkGroups.x + gl_WorkGroupID.x;
|
|
|
|
decode_slice(slice_ctx[slice_idx], slice_idx);
|
2025-05-06 11:53:12 +02:00
|
|
|
|
|
|
|
uint32_t status = corrupt ? uint32_t(corrupt) : overread;
|
|
|
|
if (status != 0)
|
|
|
|
slice_status[2*slice_idx + 1] = status;
|
2025-03-10 03:04:39 +00:00
|
|
|
}
|