Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 1/4] avcodec/ffv1: Fix remap ordering
@ 2025-03-20  1:19 Michael Niedermayer
  2025-03-20  1:19 ` [FFmpeg-devel] [PATCH 2/4] avcodec/ffv1: 32-bit float sample support Michael Niedermayer
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Michael Niedermayer @ 2025-03-20  1:19 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/ffv1dec_template.c |  6 +++---
 libavcodec/ffv1enc.c          |  2 +-
 libavcodec/ffv1enc_template.c | 12 ++++++------
 3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/libavcodec/ffv1dec_template.c b/libavcodec/ffv1dec_template.c
index f9499931b1d..cc0f6f97691 100644
--- a/libavcodec/ffv1dec_template.c
+++ b/libavcodec/ffv1dec_template.c
@@ -198,9 +198,9 @@ static int RENAME(decode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
                 r += g;
             }
             if (sc->remap) {
-                r = sc->fltmap[0][r & 0xFFFF];
-                g = sc->fltmap[1][g & 0xFFFF];
-                b = sc->fltmap[2][b & 0xFFFF];
+                g = sc->fltmap[0][g & 0xFFFF];
+                b = sc->fltmap[1][b & 0xFFFF];
+                r = sc->fltmap[2][r & 0xFFFF];
                 if (transparency)
                     a = sc->fltmap[3][a & 0xFFFF];
             }
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index 64add25b407..72ce22adfb0 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -432,7 +432,7 @@ static void set_micro_version(FFV1Context *f)
         if (f->version == 3) {
             f->micro_version = 4;
         } else if (f->version == 4) {
-            f->micro_version = 5;
+            f->micro_version = 6;
         } else
             av_assert0(0);
 
diff --git a/libavcodec/ffv1enc_template.c b/libavcodec/ffv1enc_template.c
index 8b2d938770f..779e83dad66 100644
--- a/libavcodec/ffv1enc_template.c
+++ b/libavcodec/ffv1enc_template.c
@@ -168,9 +168,9 @@ static void RENAME(load_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
                 r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
             }
 
-            sc->fltmap[0][r] = 1;
-            sc->fltmap[1][g] = 1;
-            sc->fltmap[2][b] = 1;
+            sc->fltmap[0][g] = 1;
+            sc->fltmap[1][b] = 1;
+            sc->fltmap[2][r] = 1;
             if (transparency)
                 sc->fltmap[3][a] = 1;
         }
@@ -230,9 +230,9 @@ static int RENAME(encode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
             }
 
             if (sc->remap) {
-                r = sc->fltmap[0][r];
-                g = sc->fltmap[1][g];
-                b = sc->fltmap[2][b];
+                g = sc->fltmap[0][g];
+                b = sc->fltmap[1][b];
+                r = sc->fltmap[2][r];
                 if (transparency)
                     a = sc->fltmap[3][a];
             }
-- 
2.48.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [FFmpeg-devel] [PATCH 2/4] avcodec/ffv1: 32-bit float sample support
  2025-03-20  1:19 [FFmpeg-devel] [PATCH 1/4] avcodec/ffv1: Fix remap ordering Michael Niedermayer
@ 2025-03-20  1:19 ` Michael Niedermayer
  2025-03-20  1:19 ` [FFmpeg-devel] [PATCH 3/4] avcodec/ffv1enc: remap allows using rice golomb with more bits Michael Niedermayer
  2025-03-20  1:19 ` [FFmpeg-devel] [PATCH 4/4] avcodec/ffv1: RC/RLE/LRU coder for remap table Michael Niedermayer
  2 siblings, 0 replies; 4+ messages in thread
From: Michael Niedermayer @ 2025-03-20  1:19 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/ffv1.h             |   6 +-
 libavcodec/ffv1_parse.c       |  10 ++
 libavcodec/ffv1dec.c          |  36 ++++--
 libavcodec/ffv1dec_template.c |  30 +++--
 libavcodec/ffv1enc.c          | 201 +++++++++++++++++++++++++++++++++-
 5 files changed, 263 insertions(+), 20 deletions(-)

diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h
index dd8a236efad..d19c8e3ed42 100644
--- a/libavcodec/ffv1.h
+++ b/libavcodec/ffv1.h
@@ -109,7 +109,11 @@ typedef struct FFV1SliceContext {
             uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
         };
     };
-    uint16_t   fltmap[4][65536];
+    union {
+        uint16_t   bitmap  [4][65536]; //float encode
+        uint16_t   fltmap  [4][65536]; //halffloat encode & decode
+        uint32_t   fltmap32[4][65536]; //float decode
+    };
 } FFV1SliceContext;
 
 typedef struct FFV1Context {
diff --git a/libavcodec/ffv1_parse.c b/libavcodec/ffv1_parse.c
index 9745f9de694..10f3652ff51 100644
--- a/libavcodec/ffv1_parse.c
+++ b/libavcodec/ffv1_parse.c
@@ -419,6 +419,16 @@ int ff_ffv1_parse_header(FFV1Context *f, RangeCoder *c, uint8_t *state)
             } else
                 f->pix_fmt = AV_PIX_FMT_GBRAP16;
             f->use32bit = 1;
+        } else if (f->avctx->bits_per_raw_sample == 32 && !f->transparency) {
+            if (f->flt) {
+                f->pix_fmt = AV_PIX_FMT_GBRPF32;
+            }
+            f->use32bit = 1;
+        } else if (f->avctx->bits_per_raw_sample == 32 && f->transparency) {
+            if (f->flt) {
+                f->pix_fmt = AV_PIX_FMT_GBRAPF32;
+            }
+            f->use32bit = 1;
         }
     } else {
         av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c
index 75fb5ae2f69..d45aabbbde8 100644
--- a/libavcodec/ffv1dec.c
+++ b/libavcodec/ffv1dec.c
@@ -250,6 +250,16 @@ static int decode_slice_header(const FFV1Context *f,
             sc->rawlsb = ff_ffv1_get_symbol(c, state, 0);
         }
     }
+    if (f->avctx->bits_per_raw_sample == 32) {
+        if (!sc->remap) {
+            av_log(f->avctx, AV_LOG_ERROR, "unsupported remap\n");
+            return AVERROR_INVALIDDATA;
+        }
+        if (sc->slice_width * sc->slice_height > 65536) {
+            av_log(f->avctx, AV_LOG_ERROR, "32bit needs remap\n");
+            return AVERROR_INVALIDDATA;
+        }
+    }
 
     return 0;
 }
@@ -266,28 +276,38 @@ static void slice_set_damaged(FFV1Context *f, FFV1SliceContext *sc)
 
 static int decode_remap(FFV1Context *f, FFV1SliceContext *sc)
 {
-    int flip = sc->remap == 2 ? 0x7FFF : 0;
+    unsigned int end = f->avctx->bits_per_raw_sample == 32 ? 0xFFFFFFFF : 0xFFFF;
+    int flip = sc->remap == 2 ? (end>>1) : 0;
+    int sign = (end>>1)+1;
 
     for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
         int j = 0;
         int lu = 0;
         uint8_t state[2][32];
+        int64_t i;
         memset(state, 128, sizeof(state));
-
-        for (int i= 0; i<65536; i++) {
-            int run = get_symbol_inline(&sc->c, state[lu], 0);
-            if (run > 65536U - i)
+        for (i=0; i <= end ; i++) {
+            unsigned run = get_symbol_inline(&sc->c, state[lu], 0);
+            if (run > end - i + 1)
                 return AVERROR_INVALIDDATA;
             if (lu) {
                 lu ^= !run;
                 while (run--) {
-                    sc->fltmap[p][j++] = i ^ ((i&0x8000) ? 0 : flip);
+                    if (end == 0xFFFF) {
+                        sc->fltmap  [p][j++] = i ^ ((i&    0x8000) ? 0 : flip);
+                    } else
+                        sc->fltmap32[p][j++] = i ^ ((i&0x80000000) ? 0 : flip);
                     i++;
                 }
             } else {
                 i += run;
-                if (i != 65536)
-                    sc->fltmap[p][j++] = i ^ ((i&0x8000) ? 0 : flip);
+                if (i <= end) {
+                    if (end == 0xFFFF) {
+                        sc->fltmap  [p][j++] = i ^ ((i&    0x8000) ? 0 : flip);
+                    } else {
+                        sc->fltmap32[p][j++] = i ^ ((i&0x80000000) ? 0 : flip);
+                    }
+                }
                 lu ^= !run;
             }
         }
diff --git a/libavcodec/ffv1dec_template.c b/libavcodec/ffv1dec_template.c
index cc0f6f97691..727febe8916 100644
--- a/libavcodec/ffv1dec_template.c
+++ b/libavcodec/ffv1dec_template.c
@@ -150,7 +150,7 @@ static int RENAME(decode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
     int x, y, p;
     TYPE *sample[4][2];
     int lbd    = f->avctx->bits_per_raw_sample <= 8;
-    int bits   = f->avctx->bits_per_raw_sample > 0 ? f->avctx->bits_per_raw_sample : 8;
+    int bits   = f->avctx->bits_per_raw_sample > 0 ? FFMIN(f->avctx->bits_per_raw_sample, 16) : 8;
     int offset = 1 << bits;
     int transparency = f->transparency;
     int ac = f->ac;
@@ -198,16 +198,30 @@ static int RENAME(decode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
                 r += g;
             }
             if (sc->remap) {
-                g = sc->fltmap[0][g & 0xFFFF];
-                b = sc->fltmap[1][b & 0xFFFF];
-                r = sc->fltmap[2][r & 0xFFFF];
-                if (transparency)
-                    a = sc->fltmap[3][a & 0xFFFF];
+                if (f->avctx->bits_per_raw_sample == 32) {
+                    g = sc->fltmap32[0][g & 0xFFFF];
+                    b = sc->fltmap32[1][b & 0xFFFF];
+                    r = sc->fltmap32[2][r & 0xFFFF];
+                    if (transparency)
+                        a = sc->fltmap32[3][a & 0xFFFF];
+                } else {
+                    g = sc->fltmap[0][g & 0xFFFF];
+                    b = sc->fltmap[1][b & 0xFFFF];
+                    r = sc->fltmap[2][r & 0xFFFF];
+                    if (transparency)
+                        a = sc->fltmap[3][a & 0xFFFF];
+                }
             }
 
-            if (lbd)
+            if (lbd) {
                 *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + ((unsigned)g<<8) + ((unsigned)r<<16) + ((unsigned)a<<24);
-            else if (sizeof(TYPE) == 4 || transparency) {
+            } else if (f->avctx->bits_per_raw_sample == 32) {
+                *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = g;
+                *((uint32_t*)(src[1] + x*4 + stride[1]*y)) = b;
+                *((uint32_t*)(src[2] + x*4 + stride[2]*y)) = r;
+                if (transparency)
+                    *((uint32_t*)(src[3] + x*4 + stride[3]*y)) = a;
+            } else if (sizeof(TYPE) == 4 || transparency) {
                 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = g;
                 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = b;
                 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index 72ce22adfb0..4fea6cd31c8 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -31,6 +31,7 @@
 #include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/qsort.h"
 
 #include "avcodec.h"
 #include "encode.h"
@@ -576,6 +577,9 @@ int ff_ffv1_encode_determine_slices(AVCodecContext *avctx)
                 continue;
             if (maxw * maxh * (int64_t)(s->bits_per_raw_sample+1) * plane_count > 8<<24)
                 continue;
+            if (s->bits_per_raw_sample == 32)
+                if (maxw * maxh > 65536)
+                    continue;
             if (s->version < 4)
                 if (  ff_need_new_slices(avctx->width , s->num_h_slices, s->chroma_h_shift)
                     ||ff_need_new_slices(avctx->height, s->num_v_slices, s->chroma_v_shift))
@@ -920,6 +924,10 @@ av_cold int ff_ffv1_encode_setup_plane_info(AVCodecContext *avctx,
     case AV_PIX_FMT_GBRAPF16:
         if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
             s->bits_per_raw_sample = 16;
+    case AV_PIX_FMT_GBRPF32:
+    case AV_PIX_FMT_GBRAPF32:
+        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
+            s->bits_per_raw_sample = 32;
         else if (!s->bits_per_raw_sample)
             s->bits_per_raw_sample = avctx->bits_per_raw_sample;
         s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
@@ -942,6 +950,10 @@ av_cold int ff_ffv1_encode_setup_plane_info(AVCodecContext *avctx,
 
     if (s->remap_mode < 0)
         s->remap_mode = s->flt ? 2 : 0;
+    if (s->remap_mode == 0 && s->bits_per_raw_sample == 32) {
+        av_log(avctx, AV_LOG_ERROR, "32bit requires remap\n");
+        return AVERROR(EINVAL);
+    }
 
     return av_pix_fmt_get_chroma_sub_sample(pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
 }
@@ -1158,7 +1170,7 @@ static void choose_rct_params(const FFV1Context *f, FFV1SliceContext *sc,
     sc->slice_rct_ry_coef = rct_y_coeff[best][0];
 }
 
-static void encode_remap(FFV1Context *f, FFV1SliceContext *sc)
+static void encode_histogram_remap(FFV1Context *f, FFV1SliceContext *sc)
 {
     int flip = sc->remap == 2 ? 0x7FFF : 0;
 
@@ -1188,6 +1200,178 @@ static void encode_remap(FFV1Context *f, FFV1SliceContext *sc)
     }
 }
 
+typedef struct Unit {
+    uint32_t val; //this is unneeded if you accept a dereference on each access
+    uint16_t ndx;
+} Unit;
+
+static void load_rgb_float32_frame(FFV1Context *f, FFV1SliceContext *sc,
+                                   const uint8_t *src[4],
+                                   int w, int h, const int stride[4],
+                                   Unit unit[4][65536])
+{
+    int x, y;
+    int transparency = f->transparency;
+    int i = 0;
+
+    for (y = 0; y < h; y++) {
+        for (x = 0; x < w; x++) {
+            int b, g, r, av_uninit(a);
+
+            g = *((const uint32_t *)(src[0] + x*4 + stride[0]*y));
+            b = *((const uint32_t *)(src[1] + x*4 + stride[1]*y));
+            r = *((const uint32_t *)(src[2] + x*4 + stride[2]*y));
+            if (transparency)
+                a = *((const uint32_t *)(src[3] + x*4 + stride[3]*y));
+
+            if (sc->remap == 2) {
+#define FLIP(f) (((f)&0x80000000) ? (f) : (f)^0x7FFFFFFF);
+                g = FLIP(g);
+                b = FLIP(b);
+                r = FLIP(r);
+            }
+            // We cannot build a histogram as we do for 16bit, we need a bit of magic here
+            // Its possible to reduce the memory needed at the cost of more dereferencing
+            unit[0][i].val = g;
+            unit[0][i].ndx = x + y*w;
+
+            unit[1][i].val = b;
+            unit[1][i].ndx = x + y*w;
+
+            unit[2][i].val = r;
+            unit[2][i].ndx = x + y*w;
+
+            if (transparency) {
+                unit[3][i].val = a;
+                unit[3][i].ndx = x + y*w;
+            }
+            i++;
+        }
+    }
+
+    //TODO switch to radix sort
+#define CMP(A,B) ((A)->val - (int64_t)(B)->val)
+    AV_QSORT(unit[0], i, Unit, CMP);
+    AV_QSORT(unit[1], i, Unit, CMP);
+    AV_QSORT(unit[2], i, Unit, CMP);
+    if (transparency)
+        AV_QSORT(unit[3], i, Unit, CMP);
+}
+
+static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc,
+                                 uint8_t *src[4], Unit unit[4][65536])
+{
+    int pixel_num = sc->slice_width * sc->slice_height;
+
+    av_assert0 (pixel_num <= 65536);
+
+    for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
+        int lu = 0;
+        uint8_t state[2][32];
+        int run = 0;
+        int64_t last_val = -1;
+        int compact_index = -1;
+
+        memset(state, 128, sizeof(state));
+        for (int i= 0; i<pixel_num+1; i++) {
+            int64_t val;
+            if (i == pixel_num) {
+                if (last_val == 0xFFFFFFFF) {
+                    break; //i think
+                } else {
+                    val = 1LL<<32;
+                }
+            } else
+                val = unit[p][i].val;
+
+            if (last_val != val) {
+                av_assert2(last_val < val);
+                if (lu) {
+                    if (val - last_val == 1) {
+                        run ++;
+                        last_val = val;
+                    } else {
+                        put_symbol_inline(&sc->c, state[lu], run, 0, NULL, NULL);
+                        if (run == 0)
+                            lu ^= 1;
+                        run = 0;
+                        i--; // we did not encode val so we need to backstep
+                        last_val ++;
+                        continue;
+                    }
+                } else {
+                    av_assert2(run == 0);
+                    put_symbol_inline(&sc->c, state[lu], val - last_val - 1, 0, NULL, NULL);
+                    if (val - last_val == 1)
+                        lu ^= 1;
+                    last_val = val;
+                }
+                compact_index ++;
+            }
+            if (i < pixel_num)
+                sc->bitmap[p][unit[p][i].ndx] = compact_index;
+        }
+    }
+}
+
+//TODO once this is working consider factorizing with the 16bit integer version and see how it looks if its too messy or better
+static int encode_float32_rgb_frame(FFV1Context *f, FFV1SliceContext *sc,
+                                    const uint8_t *src[4],
+                                    int w, int h, const int stride[4], int ac)
+{
+    int x, y, p, i;
+    const int ring_size = f->context_model ? 3 : 2;
+    int32_t *sample[4][3];
+    const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
+    int bits   = 16;  //TODO explain this in the specifciation, we have 32bits in but really encode max 16
+    int offset = 1 << bits;
+    int transparency = f->transparency;
+
+    sc->run_index = 0;
+
+    memset(RENAME(sc->sample_buffer), 0, ring_size * MAX_PLANES *
+           (w + 6) * sizeof(*RENAME(sc->sample_buffer)));
+
+    for (y = 0; y < h; y++) {
+        for (i = 0; i < ring_size; i++)
+            for (p = 0; p < MAX_PLANES; p++)
+                sample[p][i]= RENAME(sc->sample_buffer) + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
+
+        for (x = 0; x < w; x++) {
+            int b, g, r, av_uninit(a);
+            g = sc->bitmap[0][x + w*y];
+            b = sc->bitmap[1][x + w*y];
+            r = sc->bitmap[2][x + w*y];
+            if (transparency)
+                a = sc->bitmap[3][x + w*y];
+
+            if (sc->slice_coding_mode != 1) {
+                b -= g;
+                r -= g;
+                g += (b * sc->slice_rct_by_coef + r * sc->slice_rct_ry_coef) >> 2;
+                b += offset;
+                r += offset;
+            }
+
+            sample[0][0][x] = g;
+            sample[1][0][x] = b;
+            sample[2][0][x] = r;
+            sample[3][0][x] = a;
+        }
+        for (p = 0; p < 3 + transparency; p++) {
+            int ret;
+            sample[p][0][-1] = sample[p][1][0  ];
+            sample[p][1][ w] = sample[p][1][w-1];
+            ret = encode_line32(f, sc, f->avctx, w, sample[p], (p + 1) / 2,
+                                bits + (sc->slice_coding_mode != 1), ac, pass1);
+            if (ret < 0)
+                return ret;
+        }
+    }
+    return 0;
+}
+
+
 static int encode_slice(AVCodecContext *c, void *arg)
 {
     FFV1SliceContext *sc = arg;
@@ -1226,6 +1410,10 @@ retry:
     }
 
     if (sc->remap) {
+      //Both the 16bit and 32bit remap do exactly the same thing but with 16bits we can
+      //Implement this using a "histogram" while for 32bit that would be gb sized, thus a more
+      //complex implementation sorting pairs is used.
+      if (f->bits_per_raw_sample != 32) {
         if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8 && c->pix_fmt != AV_PIX_FMT_YAF16) {
             const int cx            = x >> f->chroma_h_shift;
             const int cy            = y >> f->chroma_v_shift;
@@ -1249,7 +1437,12 @@ retry:
         } else
             load_rgb_frame  (f, sc, planes, width, height, p->linesize);
 
-        encode_remap(f, sc);
+        encode_histogram_remap(f, sc);
+      } else {
+            Unit pairs[4][65536];
+            load_rgb_float32_frame(f, sc, planes, width, height, p->linesize, pairs);
+            encode_float32_remap(f, sc, planes, pairs);
+      }
     }
 
     if (ac == AC_GOLOMB_RICE) {
@@ -1281,6 +1474,8 @@ retry:
     } else if (c->pix_fmt == AV_PIX_FMT_YA8 || c->pix_fmt == AV_PIX_FMT_YAF16) {
         ret  = encode_plane(f, sc, p->data[0] +           ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 0, 2, ac);
         ret |= encode_plane(f, sc, p->data[0] + (ps>>1) + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 1, 2, ac);
+    } else if (f->bits_per_raw_sample == 32) {
+        ret = encode_float32_rgb_frame(f, sc, planes, width, height, p->linesize, ac);
     } else if (f->use32bit) {
         ret = encode_rgb_frame32(f, sc, planes, width, height, p->linesize, ac);
     } else {
@@ -1567,7 +1762,7 @@ const FFCodec ff_ffv1_encoder = {
         AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV440P12,
         AV_PIX_FMT_YAF16,
         AV_PIX_FMT_GRAYF16,
-        AV_PIX_FMT_GBRPF16),
+        AV_PIX_FMT_GBRPF16, AV_PIX_FMT_GBRPF32),
     .color_ranges   = AVCOL_RANGE_MPEG,
     .p.priv_class   = &ffv1_class,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_EOF_FLUSH,
-- 
2.48.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [FFmpeg-devel] [PATCH 3/4] avcodec/ffv1enc: remap allows using rice golomb with more bits
  2025-03-20  1:19 [FFmpeg-devel] [PATCH 1/4] avcodec/ffv1: Fix remap ordering Michael Niedermayer
  2025-03-20  1:19 ` [FFmpeg-devel] [PATCH 2/4] avcodec/ffv1: 32-bit float sample support Michael Niedermayer
@ 2025-03-20  1:19 ` Michael Niedermayer
  2025-03-20  1:19 ` [FFmpeg-devel] [PATCH 4/4] avcodec/ffv1: RC/RLE/LRU coder for remap table Michael Niedermayer
  2 siblings, 0 replies; 4+ messages in thread
From: Michael Niedermayer @ 2025-03-20  1:19 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/ffv1enc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index 4fea6cd31c8..12f3952453b 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -975,7 +975,7 @@ static av_cold int encode_init_internal(AVCodecContext *avctx)
     if (ret < 0)
         return ret;
 
-    if (s->bits_per_raw_sample > (s->version > 3 ? 16 : 8)) {
+    if (s->bits_per_raw_sample > (s->version > 3 ? 16 : 8) && !s->remap_mode) {
         if (s->ac == AC_GOLOMB_RICE) {
             av_log(avctx, AV_LOG_INFO,
                     "high bits_per_raw_sample, forcing range coder\n");
-- 
2.48.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [FFmpeg-devel] [PATCH 4/4] avcodec/ffv1: RC/RLE/LRU coder for remap table
  2025-03-20  1:19 [FFmpeg-devel] [PATCH 1/4] avcodec/ffv1: Fix remap ordering Michael Niedermayer
  2025-03-20  1:19 ` [FFmpeg-devel] [PATCH 2/4] avcodec/ffv1: 32-bit float sample support Michael Niedermayer
  2025-03-20  1:19 ` [FFmpeg-devel] [PATCH 3/4] avcodec/ffv1enc: remap allows using rice golomb with more bits Michael Niedermayer
@ 2025-03-20  1:19 ` Michael Niedermayer
  2 siblings, 0 replies; 4+ messages in thread
From: Michael Niedermayer @ 2025-03-20  1:19 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

8% overall compression gain for 32bit float data which originates from 16bit floats

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/ffv1.h    |  2 ++
 libavcodec/ffv1dec.c | 22 +++++++++++++-
 libavcodec/ffv1enc.c | 70 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 84 insertions(+), 10 deletions(-)

diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h
index d19c8e3ed42..64be996322c 100644
--- a/libavcodec/ffv1.h
+++ b/libavcodec/ffv1.h
@@ -54,6 +54,8 @@
 #define AC_RANGE_CUSTOM_TAB     2
 #define AC_RANGE_DEFAULT_TAB_FORCE -2
 
+#define NLRU 33
+
 typedef struct VlcState {
     uint32_t error_sum;
     int16_t drift;
diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c
index d45aabbbde8..c758040e1a8 100644
--- a/libavcodec/ffv1dec.c
+++ b/libavcodec/ffv1dec.c
@@ -242,7 +242,7 @@ static int decode_slice_header(const FFV1Context *f,
         }
         if (f->combined_version >= 0x40004) {
             sc->remap = ff_ffv1_get_symbol(c, state, 0);
-            if (sc->remap > 2U ||
+            if (sc->remap > 3U ||
                 sc->remap && !f->flt) {
                 av_log(f->avctx, AV_LOG_ERROR, "unsupported remap %d\n", sc->remap);
                 return AVERROR_INVALIDDATA;
@@ -285,12 +285,30 @@ static int decode_remap(FFV1Context *f, FFV1SliceContext *sc)
         int lu = 0;
         uint8_t state[2][32];
         int64_t i;
+        int lru[NLRU];
         memset(state, 128, sizeof(state));
+
+        for(int i = 0; i<NLRU; i++)
+            lru[i] = i;
+
         for (i=0; i <= end ; i++) {
             unsigned run = get_symbol_inline(&sc->c, state[lu], 0);
+            if (sc->remap == 3 && !lu) {
+                if (run < NLRU) {
+                    unsigned v = lru[run];
+                    memmove(lru+1, lru, sizeof(int)*run);
+                    run = v;
+                } else {
+                    memmove(lru+1, lru, sizeof(int)*(NLRU-1));
+                    run -= NLRU;
+                }
+                lru[0] = run;
+            }
             if (run > end - i + 1)
                 return AVERROR_INVALIDDATA;
             if (lu) {
+                if (run > 65536 - j)
+                    return AVERROR_INVALIDDATA;
                 lu ^= !run;
                 while (run--) {
                     if (end == 0xFFFF) {
@@ -305,6 +323,8 @@ static int decode_remap(FFV1Context *f, FFV1SliceContext *sc)
                     if (end == 0xFFFF) {
                         sc->fltmap  [p][j++] = i ^ ((i&    0x8000) ? 0 : flip);
                     } else {
+                        if (j > 65535)
+                            return AVERROR_INVALIDDATA;
                         sc->fltmap32[p][j++] = i ^ ((i&0x80000000) ? 0 : flip);
                     }
                 }
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index 12f3952453b..5b4556e63dd 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -949,7 +949,7 @@ av_cold int ff_ffv1_encode_setup_plane_info(AVCodecContext *avctx,
     av_assert0(s->bits_per_raw_sample >= 8);
 
     if (s->remap_mode < 0)
-        s->remap_mode = s->flt ? 2 : 0;
+        s->remap_mode = s->flt ? (s->bits_per_raw_sample == 32 ? 3 : 2) : 0;
     if (s->remap_mode == 0 && s->bits_per_raw_sample == 32) {
         av_log(avctx, AV_LOG_ERROR, "32bit requires remap\n");
         return AVERROR(EINVAL);
@@ -1179,24 +1179,53 @@ static void encode_histogram_remap(FFV1Context *f, FFV1SliceContext *sc)
         int lu = 0;
         uint8_t state[2][32];
         int run = 0;
+
+        int lru[NLRU];
+
+        for(int i = 0; i<NLRU; i++)
+            lru[i] = i;
+
         memset(state, 128, sizeof(state));
-        for (int i= 0; i<65536; i++) {
+        for (int i= 0; i<=65536; i++) {
             int ri = i ^ ((i&0x8000) ? 0 : flip);
-            int u = sc->fltmap[p][ri];
-            sc->fltmap[p][ri] = j;
+            int u;
+            if (i < 65536) {
+                u = sc->fltmap[p][ri];
+                sc->fltmap[p][ri] = j;
+            } else {
+                if (!run)
+                    break;
+                u = !lu;
+            }
             j+= u;
 
             if (lu == u) {
                 run ++;
             } else {
-                put_symbol_inline(&sc->c, state[lu], run, 0, NULL, NULL);
+                unsigned v = run;
+                if (sc->remap == 3 && !lu) {
+                    int r;
+                    for(r = 0; r < NLRU; r++) {
+                        if (v == lru[r]) {
+                            memmove(lru+1, lru, sizeof(int)*r);
+                            lru[0] = v;
+                            v = r;
+                            break;
+                        }
+                    }
+                    if (r == NLRU) {
+                        memmove(lru+1, lru, sizeof(int)*(NLRU-1));
+                        lru[0] = v;
+                        v += NLRU;
+                    }
+                    //TODO escape handling
+                }
+                put_symbol_inline(&sc->c, state[lu], v, 0, NULL, NULL);
                 if (run == 0)
                     lu = u;
                 run = 0;
             }
         }
-        if (run)
-            put_symbol(&sc->c, state[lu], run, 0);
     }
 }
 
@@ -1271,6 +1300,10 @@ static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc,
         int run = 0;
         int64_t last_val = -1;
         int compact_index = -1;
+        int lru[NLRU];
+
+        for(int i = 0; i<NLRU; i++)
+            lru[i] = i;
 
         memset(state, 128, sizeof(state));
         for (int i= 0; i<pixel_num+1; i++) {
@@ -1300,8 +1333,25 @@ static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc,
                         continue;
                     }
                 } else {
+                    int v = val - last_val - 1;
                     av_assert2(run == 0);
-                    put_symbol_inline(&sc->c, state[lu], val - last_val - 1, 0, NULL, NULL);
+                    if (sc->remap == 3) {
+                        int r;
+                        for(r = 0; r < NLRU; r++) {
+                            if (v == lru[r]) {
+                                memmove(lru+1, lru, sizeof(int)*r);
+                                lru[0] = v;
+                                v = r;
+                                break;
+                            }
+                        }
+                        if (r == NLRU) {
+                            memmove(lru+1, lru, sizeof(int)*(NLRU-1));
+                            lru[0] = v;
+                            v += NLRU;
+                        }
+                    }
+                    put_symbol_inline(&sc->c, state[lu], v, 0, NULL, NULL);
                     if (val - last_val == 1)
                         lu ^= 1;
                     last_val = val;
@@ -1706,7 +1756,7 @@ static const AVOption options[] = {
             { .i64 = QTABLE_GT8BIT }, INT_MIN, INT_MAX, VE, .unit = "qtable" },
     { "rawlsb", "number of LSBs stored RAW", OFFSET(rawlsb), AV_OPT_TYPE_INT,
             { .i64 = 0 }, 0, 8, VE },
-    { "remap_mode", "Remap Mode", OFFSET(remap_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2, VE, .unit = "remap_mode" },
+    { "remap_mode", "Remap Mode", OFFSET(remap_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 3, VE, .unit = "remap_mode" },
         { "auto", "Automatic", 0, AV_OPT_TYPE_CONST,
             { .i64 = -1 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
         { "off", "Disabled", 0, AV_OPT_TYPE_CONST,
@@ -1715,6 +1765,8 @@ static const AVOption options[] = {
             { .i64 =  1 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
         { "flipdualrle", "Dual RLE", 0, AV_OPT_TYPE_CONST,
             { .i64 =  2 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
+        { "rlelru", "RLE/LRU", 0, AV_OPT_TYPE_CONST,
+            { .i64 =  3 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
 
 
     { NULL }
-- 
2.48.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-03-20  1:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-20  1:19 [FFmpeg-devel] [PATCH 1/4] avcodec/ffv1: Fix remap ordering Michael Niedermayer
2025-03-20  1:19 ` [FFmpeg-devel] [PATCH 2/4] avcodec/ffv1: 32-bit float sample support Michael Niedermayer
2025-03-20  1:19 ` [FFmpeg-devel] [PATCH 3/4] avcodec/ffv1enc: remap allows using rice golomb with more bits Michael Niedermayer
2025-03-20  1:19 ` [FFmpeg-devel] [PATCH 4/4] avcodec/ffv1: RC/RLE/LRU coder for remap table Michael Niedermayer

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git