On Mon, Sep 30, 2024 at 12:01 AM James Almer <jamr...@gmail.com> wrote:
> On 9/29/2024 3:36 PM, Ramiro Polla wrote:
> > This allows setting the compression level used by zlib.
> > ---
> >   libavcodec/flashsvenc.c              | 10 ++++++++--
> >   tests/ref/vsynth/vsynth1-flashsv     |  2 +-
> >   tests/ref/vsynth/vsynth2-flashsv     |  4 ++--
> >   tests/ref/vsynth/vsynth3-flashsv     |  2 +-
> >   tests/ref/vsynth/vsynth_lena-flashsv |  2 +-
> >   5 files changed, 13 insertions(+), 7 deletions(-)
> >
> > diff --git a/libavcodec/flashsvenc.c b/libavcodec/flashsvenc.c
> > index 5cf0602f5d..f650e517d0 100644
> > --- a/libavcodec/flashsvenc.c
> > +++ b/libavcodec/flashsvenc.c
> > @@ -67,6 +67,7 @@ typedef struct FlashSVContext {
> >       unsigned        packet_size;
> >       int64_t         last_key_frame;
> >       uint8_t         tmpblock[3 * 256 * 256];
> > +    int             compression_level;
> >   } FlashSVContext;
> >
> >   static int copy_region_enc(const uint8_t *sptr, uint8_t *dptr, int dx, 
> > int dy,
> > @@ -121,6 +122,10 @@ static av_cold int flashsv_encode_init(AVCodecContext 
> > *avctx)
> >       nb_blocks = h_blocks * v_blocks;
> >       s->packet_size = 4 + nb_blocks * (2 + 3 * BLOCK_WIDTH * BLOCK_HEIGHT);
> >
> > +    s->compression_level = avctx->compression_level == 
> > FF_COMPRESSION_DEFAULT
> > +                         ? Z_DEFAULT_COMPRESSION
> > +                         : av_clip(avctx->compression_level, 0, 9);
> > +
> >       return 0;
> >   }
> >
> > @@ -170,9 +175,10 @@ static int encode_bitstream(FlashSVContext *s, const 
> > AVFrame *p, uint8_t *buf,
> >                                     p->linesize[0], previous_frame);
> >
> >               if (res || *I_frame) {
> > -                unsigned long zsize = 3 * block_width * block_height;
> > +                unsigned long zsize = 3 * block_width * block_height + 12;
>
> Why the 12?

This is to account for the deflate overhead. I found this information here:
https://refspecs.linuxbase.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/zlib-compress2-1.html
"The application should ensure that this value be at least (sourceLen
* 1.001) + 12."

I've updated the commit message with this information.

> Also, while at it, you could change that 3 to 3UL, since block_* are ints.

Updated patch attached.
From b5790ab0ea5f96cdde5adfff4b4bb49b30e343af Mon Sep 17 00:00:00 2001
From: Ramiro Polla <ramiro.po...@gmail.com>
Date: Sun, 29 Sep 2024 19:33:52 +0200
Subject: [PATCH v3 1/2] avcodec/flashsvenc: add compression_level option

This allows setting the compression level used by zlib.

The size of the destLen parameter passed to compress2() has been
incremented by 12 to account for the deflate overhead, as seen in [1]:
  "The application should ensure that this value be at least (sourceLen * 1.001) + 12."

[1] https://refspecs.linuxbase.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/zlib-compress2-1.html
---
 libavcodec/flashsvenc.c              | 10 ++++++++--
 tests/ref/vsynth/vsynth1-flashsv     |  2 +-
 tests/ref/vsynth/vsynth2-flashsv     |  4 ++--
 tests/ref/vsynth/vsynth3-flashsv     |  2 +-
 tests/ref/vsynth/vsynth_lena-flashsv |  2 +-
 5 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/libavcodec/flashsvenc.c b/libavcodec/flashsvenc.c
index 5cf0602f5d..78f2fc0291 100644
--- a/libavcodec/flashsvenc.c
+++ b/libavcodec/flashsvenc.c
@@ -67,6 +67,7 @@ typedef struct FlashSVContext {
     unsigned        packet_size;
     int64_t         last_key_frame;
     uint8_t         tmpblock[3 * 256 * 256];
+    int             compression_level;
 } FlashSVContext;
 
 static int copy_region_enc(const uint8_t *sptr, uint8_t *dptr, int dx, int dy,
@@ -121,6 +122,10 @@ static av_cold int flashsv_encode_init(AVCodecContext *avctx)
     nb_blocks = h_blocks * v_blocks;
     s->packet_size = 4 + nb_blocks * (2 + 3 * BLOCK_WIDTH * BLOCK_HEIGHT);
 
+    s->compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
+                         ? Z_DEFAULT_COMPRESSION
+                         : av_clip(avctx->compression_level, 0, 9);
+
     return 0;
 }
 
@@ -170,9 +175,10 @@ static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf,
                                   p->linesize[0], previous_frame);
 
             if (res || *I_frame) {
-                unsigned long zsize = 3 * block_width * block_height;
+                unsigned long zsize = 3UL * block_width * block_height + 12;
                 ret = compress2(ptr + 2, &zsize, s->tmpblock,
-                                3 * cur_blk_width * cur_blk_height, 9);
+                                3 * cur_blk_width * cur_blk_height,
+                                s->compression_level);
 
                 if (ret != Z_OK)
                     av_log(s->avctx, AV_LOG_ERROR,
diff --git a/tests/ref/vsynth/vsynth1-flashsv b/tests/ref/vsynth/vsynth1-flashsv
index 8b2783032b..05f9e9432d 100644
--- a/tests/ref/vsynth/vsynth1-flashsv
+++ b/tests/ref/vsynth/vsynth1-flashsv
@@ -1,4 +1,4 @@
-97894502b4cb57aca1105b6333f72dae *tests/data/fate/vsynth1-flashsv.flv
+61b0825258ac6fe85691bdefd892960d *tests/data/fate/vsynth1-flashsv.flv
 14681925 tests/data/fate/vsynth1-flashsv.flv
 791e1fb999deb2e4156e2286d48c4ed1 *tests/data/fate/vsynth1-flashsv.out.rawvideo
 stddev:    2.84 PSNR: 39.04 MAXDIFF:   49 bytes:  7603200/  7603200
diff --git a/tests/ref/vsynth/vsynth2-flashsv b/tests/ref/vsynth/vsynth2-flashsv
index 9bda896687..f659edaab5 100644
--- a/tests/ref/vsynth/vsynth2-flashsv
+++ b/tests/ref/vsynth/vsynth2-flashsv
@@ -1,4 +1,4 @@
-f4b45770dd93b43b4077532e8ef90bfc *tests/data/fate/vsynth2-flashsv.flv
-11636546 tests/data/fate/vsynth2-flashsv.flv
+a2f145e6e44b51f8fc64ead06a994273 *tests/data/fate/vsynth2-flashsv.flv
+11637702 tests/data/fate/vsynth2-flashsv.flv
 7f0fc12c02e68faddc153e69ddd6841c *tests/data/fate/vsynth2-flashsv.out.rawvideo
 stddev:    1.20 PSNR: 46.52 MAXDIFF:   20 bytes:  7603200/  7603200
diff --git a/tests/ref/vsynth/vsynth3-flashsv b/tests/ref/vsynth/vsynth3-flashsv
index 38a34bd71d..cdf9e7bf99 100644
--- a/tests/ref/vsynth/vsynth3-flashsv
+++ b/tests/ref/vsynth/vsynth3-flashsv
@@ -1,4 +1,4 @@
-832fe60169f4d91339458c60a5292924 *tests/data/fate/vsynth3-flashsv.flv
+a05a9ab0ae21925dd10a83639de77d77 *tests/data/fate/vsynth3-flashsv.flv
 171419 tests/data/fate/vsynth3-flashsv.flv
 faa660b0ecaaab1bf9b5d7284019aa01 *tests/data/fate/vsynth3-flashsv.out.rawvideo
 stddev:    2.97 PSNR: 38.67 MAXDIFF:   49 bytes:    86700/    86700
diff --git a/tests/ref/vsynth/vsynth_lena-flashsv b/tests/ref/vsynth/vsynth_lena-flashsv
index 52046cdf2e..7dfec3edf5 100644
--- a/tests/ref/vsynth/vsynth_lena-flashsv
+++ b/tests/ref/vsynth/vsynth_lena-flashsv
@@ -1,4 +1,4 @@
-0667077971e0cb63b5f49c580006e90e *tests/data/fate/vsynth_lena-flashsv.flv
+59cdcf5f92c5113e27b2fda632ccd55c *tests/data/fate/vsynth_lena-flashsv.flv
 12368953 tests/data/fate/vsynth_lena-flashsv.flv
 3a984506f1ebfc9fb73b6814cab201cc *tests/data/fate/vsynth_lena-flashsv.out.rawvideo
 stddev:    0.66 PSNR: 51.73 MAXDIFF:   14 bytes:  7603200/  7603200
-- 
2.30.2

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

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

Reply via email to