forked from FFmpeg/FFmpeg
This commit implements a standard, compliant, version 3 and version 4 FFv1 encoder, entirely in Vulkan. The encoder is written in standard GLSL and requires a Vulkan 1.3 supporting GPU with the BDA extension. The encoder can use any amount of slices, but nominally, should use 32x32 slices (1024 in total) to maximize parallelism. All features are supported, as well as all pixel formats. This includes: - Rice - Range coding with a custom quantization table - PCM encoding CRC calculation is also massively parallelized on the GPU. Encoding of unaligned dimensions on subsampled data requires version 4, or requires oversizing the image to 64-pixel alignment and cropping out the padding via container flags. Performance-wise, this makes 1080p real-time screen capture possible at 60fps on even modest GPUs.
83 lines
3 KiB
Text
83 lines
3 KiB
Text
/*
|
|
* 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
|
|
*/
|
|
|
|
void encode_slice_rgb(inout SliceContext sc, const uint slice_idx)
|
|
{
|
|
int bits = 9;
|
|
if (bits != 8 || sc.slice_coding_mode != 0)
|
|
bits = bits_per_raw_sample + int(sc.slice_coding_mode != 1);
|
|
|
|
int run_index = 0;
|
|
|
|
#ifndef GOLOMB
|
|
if (sc.slice_coding_mode == 1) {
|
|
if (transparency == 1) {
|
|
for (int y = 0; y < sc.slice_dim.y; y++) {
|
|
encode_line_pcm(sc, y, 0, 1, bits);
|
|
encode_line_pcm(sc, y, 0, 2, bits);
|
|
encode_line_pcm(sc, y, 0, 0, bits);
|
|
encode_line_pcm(sc, y, 0, 3, bits);
|
|
}
|
|
} else {
|
|
for (int y = 0; y < sc.slice_dim.y; y++) {
|
|
encode_line_pcm(sc, y, 0, 1, bits);
|
|
encode_line_pcm(sc, y, 0, 2, bits);
|
|
encode_line_pcm(sc, y, 0, 0, bits);
|
|
}
|
|
}
|
|
} else
|
|
#endif
|
|
{
|
|
uint64_t slice_state_off = uint64_t(slice_state) +
|
|
slice_idx*plane_state_size*codec_planes;
|
|
|
|
if (transparency == 1) {
|
|
for (int y = 0; y < sc.slice_dim.y; y++) {
|
|
encode_line(sc, slice_state_off + plane_state_size*0,
|
|
y, 0, 1, bits, run_index);
|
|
encode_line(sc, slice_state_off + plane_state_size*1,
|
|
y, 0, 2, bits, run_index);
|
|
encode_line(sc, slice_state_off + plane_state_size*1,
|
|
y, 0, 0, bits, run_index);
|
|
encode_line(sc, slice_state_off + plane_state_size*2,
|
|
y, 0, 3, bits, run_index);
|
|
}
|
|
} else {
|
|
for (int y = 0; y < sc.slice_dim.y; y++) {
|
|
encode_line(sc, slice_state_off + plane_state_size*0,
|
|
y, 0, 1, bits, run_index);
|
|
encode_line(sc, slice_state_off + plane_state_size*1,
|
|
y, 0, 2, bits, run_index);
|
|
encode_line(sc, slice_state_off + plane_state_size*1,
|
|
y, 0, 0, bits, run_index);
|
|
}
|
|
}
|
|
}
|
|
|
|
finalize_slice(sc, slice_idx);
|
|
}
|
|
|
|
void main(void)
|
|
{
|
|
const uint slice_idx = gl_WorkGroupID.y*gl_NumWorkGroups.x + gl_WorkGroupID.x;
|
|
encode_slice_rgb(slice_ctx[slice_idx], slice_idx);
|
|
}
|