diff mbox series

[FFmpeg-devel] lavc/texturedsp: add DXT4 texturedspenc function

Message ID 20240202003444.30893-1-connorbworley@gmail.com
State New
Headers show
Series [FFmpeg-devel] lavc/texturedsp: add DXT4 texturedspenc function | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Connor Worley Feb. 2, 2024, 12:34 a.m. UTC
For future use in lavc/dxvenc.

Signed-off-by: Connor Worley <connorbworley@gmail.com>
---
 libavcodec/texturedsp.h    |  1 +
 libavcodec/texturedspenc.c | 41 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

Comments

Andreas Rheinhardt Feb. 2, 2024, 9:52 a.m. UTC | #1
Connor Worley:
> For future use in lavc/dxvenc.
> 
> Signed-off-by: Connor Worley <connorbworley@gmail.com>
> ---
>  libavcodec/texturedsp.h    |  1 +
>  libavcodec/texturedspenc.c | 41 ++++++++++++++++++++++++++++++++++++++
>  2 files changed, 42 insertions(+)
> 
> diff --git a/libavcodec/texturedsp.h b/libavcodec/texturedsp.h
> index 86c8eea02d..8881436187 100644
> --- a/libavcodec/texturedsp.h
> +++ b/libavcodec/texturedsp.h
> @@ -62,6 +62,7 @@ typedef struct TextureDSPContext {
>  
>  typedef struct TextureDSPEncContext {
>      int (*dxt1_block)        (uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
> +    int (*dxt4_block)        (uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
>      int (*dxt5_block)        (uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
>      int (*dxt5ys_block)      (uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
>  } TextureDSPEncContext;
> diff --git a/libavcodec/texturedspenc.c b/libavcodec/texturedspenc.c
> index 5657a6ef61..c98a277f56 100644
> --- a/libavcodec/texturedspenc.c
> +++ b/libavcodec/texturedspenc.c
> @@ -589,6 +589,20 @@ static void rgba2ycocg(uint8_t *dst, const uint8_t *pixel)
>      dst[3] = av_clip_uint8(g + t);                      /* Y */
>  }
>  
> +/** Convert a straight alpha pixel to a premultiplied alpha pixel. */
> +static av_always_inline void straight2premult(uint8_t *dst, const uint8_t *src)
> +{
> +    const int r = src[0];
> +    const int g = src[1];
> +    const int b = src[2];
> +    const int a = src[3]; /* unchanged */
> +
> +    dst[0] = (uint8_t) r * a / 255;
> +    dst[1] = (uint8_t) g * a / 255;
> +    dst[2] = (uint8_t) b * a / 255;
> +    dst[3] = a;
> +}
> +
>  /**
>   * Compress one block of RGBA pixels in a DXT1 texture and store the
>   * resulting bytes in 'dst'. Alpha is not preserved.
> @@ -605,6 +619,32 @@ static int dxt1_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
>      return 8;
>  }
>  
> +/**
> + * Compress one block of RGBA pixels in a DXT4 texture and store the
> + * resulting bytes in 'dst'. Alpha is preserved.
> + *
> + * @param dst    output buffer.
> + * @param stride scanline in bytes.
> + * @param block  block to compress.
> + * @return how much texture data has been written.
> + */
> +static int dxt4_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
> +{
> +    int x, y;
> +    uint8_t premult[64];
> +
> +    for (y = 0; y < 4; y++) {
> +        for (x = 0; x < 4; x++) {
> +            straight2premult(premult + x * 4 + y * 16, block + x * 4 + y * stride);
> +        }
> +    }
> +
> +    compress_alpha(dst, 16, premult);
> +    compress_color(dst + 8, 16, premult);
> +
> +    return 16;
> +}
> +
>  /**
>   * Compress one block of RGBA pixels in a DXT5 texture and store the
>   * resulting bytes in 'dst'. Alpha is preserved.
> @@ -650,6 +690,7 @@ static int dxt5ys_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
>  av_cold void ff_texturedspenc_init(TextureDSPEncContext *c)
>  {
>      c->dxt1_block         = dxt1_block;
> +    c->dxt4_block         = dxt4_block;
>      c->dxt5_block         = dxt5_block;
>      c->dxt5ys_block       = dxt5ys_block;
>  }

This should be added in a patchset that actually makes use of it to
avoid the fate of rgtc1_u_alpha.

- Andreas
diff mbox series

Patch

diff --git a/libavcodec/texturedsp.h b/libavcodec/texturedsp.h
index 86c8eea02d..8881436187 100644
--- a/libavcodec/texturedsp.h
+++ b/libavcodec/texturedsp.h
@@ -62,6 +62,7 @@  typedef struct TextureDSPContext {
 
 typedef struct TextureDSPEncContext {
     int (*dxt1_block)        (uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
+    int (*dxt4_block)        (uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
     int (*dxt5_block)        (uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
     int (*dxt5ys_block)      (uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
 } TextureDSPEncContext;
diff --git a/libavcodec/texturedspenc.c b/libavcodec/texturedspenc.c
index 5657a6ef61..c98a277f56 100644
--- a/libavcodec/texturedspenc.c
+++ b/libavcodec/texturedspenc.c
@@ -589,6 +589,20 @@  static void rgba2ycocg(uint8_t *dst, const uint8_t *pixel)
     dst[3] = av_clip_uint8(g + t);                      /* Y */
 }
 
+/** Convert a straight alpha pixel to a premultiplied alpha pixel. */
+static av_always_inline void straight2premult(uint8_t *dst, const uint8_t *src)
+{
+    const int r = src[0];
+    const int g = src[1];
+    const int b = src[2];
+    const int a = src[3]; /* unchanged */
+
+    dst[0] = (uint8_t) r * a / 255;
+    dst[1] = (uint8_t) g * a / 255;
+    dst[2] = (uint8_t) b * a / 255;
+    dst[3] = a;
+}
+
 /**
  * Compress one block of RGBA pixels in a DXT1 texture and store the
  * resulting bytes in 'dst'. Alpha is not preserved.
@@ -605,6 +619,32 @@  static int dxt1_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
     return 8;
 }
 
+/**
+ * Compress one block of RGBA pixels in a DXT4 texture and store the
+ * resulting bytes in 'dst'. Alpha is preserved.
+ *
+ * @param dst    output buffer.
+ * @param stride scanline in bytes.
+ * @param block  block to compress.
+ * @return how much texture data has been written.
+ */
+static int dxt4_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
+{
+    int x, y;
+    uint8_t premult[64];
+
+    for (y = 0; y < 4; y++) {
+        for (x = 0; x < 4; x++) {
+            straight2premult(premult + x * 4 + y * 16, block + x * 4 + y * stride);
+        }
+    }
+
+    compress_alpha(dst, 16, premult);
+    compress_color(dst + 8, 16, premult);
+
+    return 16;
+}
+
 /**
  * Compress one block of RGBA pixels in a DXT5 texture and store the
  * resulting bytes in 'dst'. Alpha is preserved.
@@ -650,6 +690,7 @@  static int dxt5ys_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
 av_cold void ff_texturedspenc_init(TextureDSPEncContext *c)
 {
     c->dxt1_block         = dxt1_block;
+    c->dxt4_block         = dxt4_block;
     c->dxt5_block         = dxt5_block;
     c->dxt5ys_block       = dxt5ys_block;
 }