avcodec/ffv1: Support slice coding mode 1 with golomb rice

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
This commit is contained in:
Michael Niedermayer 2024-10-25 01:45:56 +02:00
parent 6ccd16dda2
commit 967e5e8f6f
No known key found for this signature in database
GPG Key ID: B18E8928B3948D64
3 changed files with 25 additions and 21 deletions

View File

@ -120,9 +120,8 @@ static int is_input_end(RangeCoder *c, GetBitContext *gb, int ac)
static int decode_plane(FFV1Context *f, FFV1SliceContext *sc,
GetBitContext *gb,
uint8_t *src, int w, int h, int stride, int plane_index,
int pixel_stride)
int pixel_stride, int ac)
{
const int ac = f->ac;
int x, y;
int16_t *sample[2];
sample[0] = sc->sample_buffer + 3;
@ -273,6 +272,7 @@ static int decode_slice(AVCodecContext *c, void *arg)
AVFrame * const p = f->picture.f;
const int si = sc - f->slices;
GetBitContext gb;
int ac = f->ac || sc->slice_coding_mode == 1;
if (!(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f)
ff_progress_frame_await(&f->last_picture, si);
@ -305,7 +305,7 @@ static int decode_slice(AVCodecContext *c, void *arg)
x = sc->slice_x;
y = sc->slice_y;
if (f->ac == AC_GOLOMB_RICE) {
if (ac == AC_GOLOMB_RICE) {
if (f->version == 3 && f->micro_version > 1 || f->version > 3)
get_rac(&sc->c, (uint8_t[]) { 129 });
sc->ac_byte_count = f->version > 2 || (!x && !y) ? sc->c.bytestream - sc->c.bytestream_start - 1 : 0;
@ -320,17 +320,17 @@ static int decode_slice(AVCodecContext *c, void *arg)
const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
const int cx = x >> f->chroma_h_shift;
const int cy = y >> f->chroma_v_shift;
decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1);
decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1, ac);
if (f->chroma_planes) {
decode_plane(f, sc, &gb, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1);
decode_plane(f, sc, &gb, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1);
decode_plane(f, sc, &gb, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1, ac);
decode_plane(f, sc, &gb, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1, ac);
}
if (f->transparency)
decode_plane(f, sc, &gb, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], (f->version >= 4 && !f->chroma_planes) ? 1 : 2, 1);
decode_plane(f, sc, &gb, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], (f->version >= 4 && !f->chroma_planes) ? 1 : 2, 1, ac);
} else if (f->colorspace == 0) {
decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 2);
decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] + 1, width, height, p->linesize[0], 1, 2);
decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 2, ac);
decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] + 1, width, height, p->linesize[0], 1, 2, ac);
} else if (f->use32bit) {
uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
p->data[1] + ps * x + y * p->linesize[1],
@ -344,7 +344,7 @@ static int decode_slice(AVCodecContext *c, void *arg)
p->data[3] + ps * x + y * p->linesize[3] };
decode_rgb_frame(f, sc, &gb, planes, width, height, p->linesize);
}
if (f->ac != AC_GOLOMB_RICE && f->version > 2) {
if (ac != AC_GOLOMB_RICE && f->version > 2) {
int v;
get_rac(&sc->c, (uint8_t[]) { 129 });
v = sc->c.bytestream_end - sc->c.bytestream - 2 - 5*!!f->ec;

View File

@ -143,6 +143,9 @@ static int RENAME(decode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
int transparency = f->transparency;
int ac = f->ac;
if (sc->slice_coding_mode == 1)
ac = 1;
for (x = 0; x < 4; x++) {
sample[x][0] = RENAME(sc->sample_buffer) + x * 2 * (w + 6) + 3;
sample[x][1] = RENAME(sc->sample_buffer) + (x * 2 + 1) * (w + 6) + 3;

View File

@ -271,10 +271,9 @@ static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
static int encode_plane(FFV1Context *f, FFV1SliceContext *sc,
const uint8_t *src, int w, int h,
int stride, int plane_index, int pixel_stride)
int stride, int plane_index, int pixel_stride, int ac)
{
int x, y, i, ret;
const int ac = f->ac;
const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
const int ring_size = f->context_model ? 3 : 2;
int16_t *sample[3];
@ -1068,6 +1067,7 @@ static int encode_slice(AVCodecContext *c, void *arg)
p->data[1] ? p->data[1] + ps*x + y*p->linesize[1] : NULL,
p->data[2] ? p->data[2] + ps*x + y*p->linesize[2] : NULL,
p->data[3] ? p->data[3] + ps*x + y*p->linesize[3] : NULL};
int ac = f->ac;
sc->slice_coding_mode = 0;
if (f->version > 3 && f->colorspace == 1) {
@ -1083,7 +1083,7 @@ retry:
if (f->version > 2) {
encode_slice_header(f, sc);
}
if (f->ac == AC_GOLOMB_RICE) {
if (ac == AC_GOLOMB_RICE) {
sc->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate(&sc->c, f->version > 2) : 0;
init_put_bits(&sc->pb,
sc->c.bytestream_start + sc->ac_byte_count,
@ -1096,24 +1096,24 @@ retry:
const int cx = x >> f->chroma_h_shift;
const int cy = y >> f->chroma_v_shift;
ret = encode_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1);
ret = encode_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1, ac);
if (f->chroma_planes) {
ret |= encode_plane(f, sc, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1);
ret |= encode_plane(f, sc, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1);
ret |= encode_plane(f, sc, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1, ac);
ret |= encode_plane(f, sc, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1, ac);
}
if (f->transparency)
ret |= encode_plane(f, sc, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2, 1);
ret |= encode_plane(f, sc, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2, 1, ac);
} else if (c->pix_fmt == AV_PIX_FMT_YA8) {
ret = encode_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 2);
ret |= encode_plane(f, sc, p->data[0] + 1 + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 2);
ret = encode_plane(f, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 2, ac);
ret |= encode_plane(f, sc, p->data[0] + 1 + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 2, ac);
} else if (f->use32bit) {
ret = encode_rgb_frame32(f, sc, planes, width, height, p->linesize);
} else {
ret = encode_rgb_frame(f, sc, planes, width, height, p->linesize);
}
if (f->ac != AC_GOLOMB_RICE) {
if (ac != AC_GOLOMB_RICE) {
sc->ac_byte_count = ff_rac_terminate(&sc->c, 1);
} else {
flush_put_bits(&sc->pb); // FIXME: nicer padding
@ -1122,11 +1122,12 @@ retry:
if (ret < 0) {
av_assert0(sc->slice_coding_mode == 0);
if (f->version < 4 || !f->ac) {
if (f->version < 4) {
av_log(c, AV_LOG_ERROR, "Buffer too small\n");
return ret;
}
av_log(c, AV_LOG_DEBUG, "Coding slice as PCM\n");
ac = 1;
sc->slice_coding_mode = 1;
sc->c = c_bak;
goto retry;