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

Connor Worley connorbworley at gmail.com
Fri Feb 2 02:34:44 EET 2024


For future use in lavc/dxvenc.

Signed-off-by: Connor Worley <connorbworley at 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;
 }
-- 
2.40.1



More information about the ffmpeg-devel mailing list