lavc/ffv1dec: move the bitreader to stack

There is no reason to place it in persistent state.
This commit is contained in:
Anton Khirnov
2024-07-08 10:09:22 +02:00
parent 19e9f3d5f2
commit 889faedd26
3 changed files with 27 additions and 25 deletions

View File

@@ -85,7 +85,6 @@ typedef struct FFV1Context {
AVClass *class; AVClass *class;
AVCodecContext *avctx; AVCodecContext *avctx;
RangeCoder c; RangeCoder c;
GetBitContext gb;
PutBitContext pb; PutBitContext pb;
uint64_t rc_stat[256][2]; uint64_t rc_stat[256][2];
uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2]; uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];

View File

@@ -94,14 +94,14 @@ static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
return ret; return ret;
} }
static int is_input_end(FFV1Context *s) static int is_input_end(FFV1Context *s, GetBitContext *gb)
{ {
if (s->ac != AC_GOLOMB_RICE) { if (s->ac != AC_GOLOMB_RICE) {
RangeCoder *const c = &s->c; RangeCoder *const c = &s->c;
if (c->overread > MAX_OVERREAD) if (c->overread > MAX_OVERREAD)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} else { } else {
if (get_bits_left(&s->gb) < 1) if (get_bits_left(gb) < 1)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
return 0; return 0;
@@ -118,6 +118,7 @@ static int is_input_end(FFV1Context *s)
#include "ffv1dec_template.c" #include "ffv1dec_template.c"
static int decode_plane(FFV1Context *s, FFV1SliceContext *sc, static int decode_plane(FFV1Context *s, FFV1SliceContext *sc,
GetBitContext *gb,
uint8_t *src, int w, int h, int stride, int plane_index, uint8_t *src, int w, int h, int stride, int plane_index,
int pixel_stride) int pixel_stride)
{ {
@@ -140,13 +141,13 @@ static int decode_plane(FFV1Context *s, FFV1SliceContext *sc,
sample[0][w] = sample[0][w - 1]; sample[0][w] = sample[0][w - 1];
if (s->avctx->bits_per_raw_sample <= 8) { if (s->avctx->bits_per_raw_sample <= 8) {
int ret = decode_line(s, sc, w, sample, plane_index, 8); int ret = decode_line(s, sc, gb, w, sample, plane_index, 8);
if (ret < 0) if (ret < 0)
return ret; return ret;
for (x = 0; x < w; x++) for (x = 0; x < w; x++)
src[x*pixel_stride + stride * y] = sample[1][x]; src[x*pixel_stride + stride * y] = sample[1][x];
} else { } else {
int ret = decode_line(s, sc, w, sample, plane_index, s->avctx->bits_per_raw_sample); int ret = decode_line(s, sc, gb, w, sample, plane_index, s->avctx->bits_per_raw_sample);
if (ret < 0) if (ret < 0)
return ret; return ret;
if (s->packed_at_lsb) { if (s->packed_at_lsb) {
@@ -262,6 +263,7 @@ static int decode_slice(AVCodecContext *c, void *arg)
AVFrame * const p = f->picture.f; AVFrame * const p = f->picture.f;
const int si = (FFV1Context**)arg - f->slice_context; const int si = (FFV1Context**)arg - f->slice_context;
FFV1SliceContext *sc = &f->slices[si]; FFV1SliceContext *sc = &f->slices[si];
GetBitContext gb;
if (f->fsrc && !(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f) if (f->fsrc && !(p->flags & AV_FRAME_FLAG_KEY) && f->last_picture.f)
ff_progress_frame_await(&f->last_picture, si); ff_progress_frame_await(&f->last_picture, si);
@@ -322,7 +324,7 @@ static int decode_slice(AVCodecContext *c, void *arg)
if (f->version == 3 && f->micro_version > 1 || f->version > 3) if (f->version == 3 && f->micro_version > 1 || f->version > 3)
get_rac(&fs->c, (uint8_t[]) { 129 }); get_rac(&fs->c, (uint8_t[]) { 129 });
fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0; fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
init_get_bits(&fs->gb, init_get_bits(&gb,
fs->c.bytestream_start + fs->ac_byte_count, fs->c.bytestream_start + fs->ac_byte_count,
(fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8); (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
} }
@@ -333,29 +335,29 @@ static int decode_slice(AVCodecContext *c, void *arg)
const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift); const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
const int cx = x >> f->chroma_h_shift; const int cx = x >> f->chroma_h_shift;
const int cy = y >> f->chroma_v_shift; const int cy = y >> f->chroma_v_shift;
decode_plane(fs, sc, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1); decode_plane(fs, sc, &gb, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1);
if (f->chroma_planes) { if (f->chroma_planes) {
decode_plane(fs, sc, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1); decode_plane(fs, sc, &gb, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1);
decode_plane(fs, sc, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1); decode_plane(fs, sc, &gb, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1);
} }
if (fs->transparency) if (fs->transparency)
decode_plane(fs, sc, 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(fs, 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);
} else if (f->colorspace == 0) { } else if (f->colorspace == 0) {
decode_plane(fs, sc, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 2); decode_plane(fs, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 2);
decode_plane(fs, sc, p->data[0] + ps*x + y*p->linesize[0] + 1, width, height, p->linesize[0], 1, 2); decode_plane(fs, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] + 1, width, height, p->linesize[0], 1, 2);
} else if (f->use32bit) { } else if (f->use32bit) {
uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0], uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
p->data[1] + ps * x + y * p->linesize[1], p->data[1] + ps * x + y * p->linesize[1],
p->data[2] + ps * x + y * p->linesize[2], p->data[2] + ps * x + y * p->linesize[2],
p->data[3] + ps * x + y * p->linesize[3] }; p->data[3] + ps * x + y * p->linesize[3] };
decode_rgb_frame32(fs, sc, planes, width, height, p->linesize); decode_rgb_frame32(fs, sc, &gb, planes, width, height, p->linesize);
} else { } else {
uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0], uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
p->data[1] + ps * x + y * p->linesize[1], p->data[1] + ps * x + y * p->linesize[1],
p->data[2] + ps * x + y * p->linesize[2], p->data[2] + ps * x + y * p->linesize[2],
p->data[3] + ps * x + y * p->linesize[3] }; p->data[3] + ps * x + y * p->linesize[3] };
decode_rgb_frame(fs, sc, planes, width, height, p->linesize); decode_rgb_frame(fs, sc, &gb, planes, width, height, p->linesize);
} }
if (fs->ac != AC_GOLOMB_RICE && f->version > 2) { if (fs->ac != AC_GOLOMB_RICE && f->version > 2) {
int v; int v;

View File

@@ -23,8 +23,8 @@
#include "ffv1_template.c" #include "ffv1_template.c"
static av_always_inline int static av_always_inline int
RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, int w, RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, GetBitContext *gb,
TYPE *sample[2], int plane_index, int bits) int w, TYPE *sample[2], int plane_index, int bits)
{ {
PlaneContext *const p = &s->plane[plane_index]; PlaneContext *const p = &s->plane[plane_index];
RangeCoder *const c = &s->c; RangeCoder *const c = &s->c;
@@ -33,7 +33,7 @@ RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, int w,
int run_mode = 0; int run_mode = 0;
int run_index = sc->run_index; int run_index = sc->run_index;
if (is_input_end(s)) if (is_input_end(s, gb))
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
if (s->slice_coding_mode == 1) { if (s->slice_coding_mode == 1) {
@@ -53,7 +53,7 @@ RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, int w,
int diff, context, sign; int diff, context, sign;
if (!(x & 1023)) { if (!(x & 1023)) {
if (is_input_end(s)) if (is_input_end(s, gb))
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
@@ -74,13 +74,13 @@ RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, int w,
if (run_mode) { if (run_mode) {
if (run_count == 0 && run_mode == 1) { if (run_count == 0 && run_mode == 1) {
if (get_bits1(&s->gb)) { if (get_bits1(gb)) {
run_count = 1 << ff_log2_run[run_index]; run_count = 1 << ff_log2_run[run_index];
if (x + run_count <= w) if (x + run_count <= w)
run_index++; run_index++;
} else { } else {
if (ff_log2_run[run_index]) if (ff_log2_run[run_index])
run_count = get_bits(&s->gb, ff_log2_run[run_index]); run_count = get_bits(gb, ff_log2_run[run_index]);
else else
run_count = 0; run_count = 0;
if (run_index) if (run_index)
@@ -105,17 +105,17 @@ RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, int w,
if (run_count < 0) { if (run_count < 0) {
run_mode = 0; run_mode = 0;
run_count = 0; run_count = 0;
diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], diff = get_vlc_symbol(gb, &p->vlc_state[context],
bits); bits);
if (diff >= 0) if (diff >= 0)
diff++; diff++;
} else } else
diff = 0; diff = 0;
} else } else
diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits); diff = get_vlc_symbol(gb, &p->vlc_state[context], bits);
ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n", ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
run_count, run_index, run_mode, x, get_bits_count(&s->gb)); run_count, run_index, run_mode, x, get_bits_count(gb));
} }
if (sign) if (sign)
@@ -128,6 +128,7 @@ RENAME(decode_line)(FFV1Context *s, FFV1SliceContext *sc, int w,
} }
static int RENAME(decode_rgb_frame)(FFV1Context *s, FFV1SliceContext *sc, static int RENAME(decode_rgb_frame)(FFV1Context *s, FFV1SliceContext *sc,
GetBitContext *gb,
uint8_t *src[4], int w, int h, int stride[4]) uint8_t *src[4], int w, int h, int stride[4])
{ {
int x, y, p; int x, y, p;
@@ -157,9 +158,9 @@ static int RENAME(decode_rgb_frame)(FFV1Context *s, FFV1SliceContext *sc,
sample[p][1][-1]= sample[p][0][0 ]; sample[p][1][-1]= sample[p][0][0 ];
sample[p][0][ w]= sample[p][0][w-1]; sample[p][0][ w]= sample[p][0][w-1];
if (lbd && s->slice_coding_mode == 0) if (lbd && s->slice_coding_mode == 0)
ret = RENAME(decode_line)(s, sc, w, sample[p], (p + 1)/2, 9); ret = RENAME(decode_line)(s, sc, gb, w, sample[p], (p + 1)/2, 9);
else else
ret = RENAME(decode_line)(s, sc, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1)); ret = RENAME(decode_line)(s, sc, gb, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1));
if (ret < 0) if (ret < 0)
return ret; return ret;
} }