[FFmpeg-devel] [PATCH 1/4] avcodec/ffv1: simplify version checks with combined_version

Michael Niedermayer michael at niedermayer.cc
Sat Jan 18 06:59:57 EET 2025


Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael at niedermayer.cc>
---
 libavcodec/ffv1.c    | 2 +-
 libavcodec/ffv1.h    | 1 +
 libavcodec/ffv1dec.c | 9 ++++++---
 libavcodec/ffv1enc.c | 2 ++
 4 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/libavcodec/ffv1.c b/libavcodec/ffv1.c
index 927763e24d4..f303ed769f0 100644
--- a/libavcodec/ffv1.c
+++ b/libavcodec/ffv1.c
@@ -130,7 +130,7 @@ int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, in
     int mpw = 1<<chroma_shift;
     int awidth = FFALIGN(width, mpw);
 
-    if (f->version < 4 || f->version == 4 && f->micro_version < 3)
+    if (f->combined_version <= 0x40002)
         return width * sx / num_h_slices;
 
     sx = (2LL * awidth * sx + num_h_slices * mpw) / (2 * num_h_slices * mpw) * mpw;
diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h
index ca03fd2b109..576769ae1e0 100644
--- a/libavcodec/ffv1.h
+++ b/libavcodec/ffv1.h
@@ -113,6 +113,7 @@ typedef struct FFV1Context {
     uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
     int version;
     int micro_version;
+    int combined_version;
     int width, height;
     int chroma_planes;
     int chroma_h_shift, chroma_v_shift;
diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c
index 78458158735..5aec6cdce26 100644
--- a/libavcodec/ffv1dec.c
+++ b/libavcodec/ffv1dec.c
@@ -306,7 +306,7 @@ static int decode_slice(AVCodecContext *c, void *arg)
     y      = sc->slice_y;
 
     if (ac == AC_GOLOMB_RICE) {
-        if (f->version == 3 && f->micro_version > 1 || f->version > 3)
+        if (f->combined_version >= 0x30002)
             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;
         init_get_bits(&gb,
@@ -432,11 +432,13 @@ static int read_extra_header(FFV1Context *f)
             f->version);
         return AVERROR_PATCHWELCOME;
     }
+    f->combined_version = f->version << 16;
     if (f->version > 2) {
         c.bytestream_end -= 4;
         f->micro_version = get_symbol(&c, state, 0);
-        if (f->micro_version < 0)
+        if (f->micro_version < 0 || f->micro_version > 65535)
             return AVERROR_INVALIDDATA;
+        f->combined_version += f->micro_version;
     }
     f->ac = get_symbol(&c, state, 0);
 
@@ -504,7 +506,7 @@ static int read_extra_header(FFV1Context *f)
         f->ec = get_symbol(&c, state, 0);
         if (f->ec >= 2)
             f->crcref = 0x7a8c4079;
-        if (f->micro_version > 2)
+        if (f->combined_version >= 0x30003)
             f->intra = get_symbol(&c, state, 0);
     }
 
@@ -1032,6 +1034,7 @@ static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
 
     fdst->version             = fsrc->version;
     fdst->micro_version       = fsrc->micro_version;
+    fdst->combined_version    = fsrc->combined_version;
     fdst->chroma_planes       = fsrc->chroma_planes;
     fdst->chroma_h_shift      = fsrc->chroma_h_shift;
     fdst->chroma_v_shift      = fsrc->chroma_v_shift;
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index e2db1b41640..c6f450ae6c0 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -414,11 +414,13 @@ av_cold int ff_ffv1_write_extradata(AVCodecContext *avctx)
     ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
 
     put_symbol(&c, state, f->version, 0);
+    f->combined_version = f->version << 16;
     if (f->version > 2) {
         if (f->version == 3) {
             f->micro_version = 4;
         } else if (f->version == 4)
             f->micro_version = 3;
+        f->combined_version += f->micro_version;
         put_symbol(&c, state, f->micro_version, 0);
     }
 
-- 
2.48.1



More information about the ffmpeg-devel mailing list