From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100])
	by master.gitmailbox.com (Postfix) with ESMTPS id 23EE94D8FA
	for <ffmpegdev@gitmailbox.com>; Mon, 21 Apr 2025 02:34:35 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8C60468A351;
	Mon, 21 Apr 2025 05:34:02 +0300 (EEST)
Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com
 [209.85.215.171])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D460F687EB0
 for <ffmpeg-devel@ffmpeg.org>; Mon, 21 Apr 2025 05:33:53 +0300 (EEST)
Received: by mail-pg1-f171.google.com with SMTP id
 41be03b00d2f7-af9a6b3da82so2262161a12.0
 for <ffmpeg-devel@ffmpeg.org>; Sun, 20 Apr 2025 19:33:53 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=emma-gg.20230601.gappssmtp.com; s=20230601; t=1745202832; x=1745807632;
 darn=ffmpeg.org; 
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:from:to:cc:subject:date
 :message-id:reply-to;
 bh=2QRAj+KLPxCMdsmOSR675wCTVeh6VDT+Z6QQxdRK5dw=;
 b=051CYjbGfgBn6L1fQzJLY2hdRboIRyXqGNaR5eUxhFkexGa4uDPKxN+KXec9U/gPBJ
 qD6PbQjSm8Hue/hD4ExFhGBN451pi/yrSVOYzZDpXYoNL1K2Dgw6h1iYWzna4sQK4PyD
 zWNVFgjkcOo4eD+Eox6WoXbwuV0G5utGrC48CjvMGdqBVK8BWkVt0UnqUDrgdDNu9KLI
 6yGUP2PzrhU376eTOFC/PR8uGfxOhszPG7NIT0gDVLmdWk3hriEPnosbxr1BbqxCdvRL
 sNI76Zenwbluft72LU5TtRHR6NEcowVg9pXa8c657r5oek+lrJjQPpjT9BXwfLQto1+Y
 yaAg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1745202832; x=1745807632;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=2QRAj+KLPxCMdsmOSR675wCTVeh6VDT+Z6QQxdRK5dw=;
 b=ND437ZTT5WFXTqWL3sZsbwEUEuIybibrB+v2/A/YtyWDB1XIioUst03wp3Sa2/NTBz
 QFbeKCBXN5cjowXTN3SHgoPIy2ThOE8JKnA9ZAoFNE/ErU95aO55ha2ZEG17rSzc/So3
 XGSC6dkvLOJi66DBB6q5nRwWoZ/0gVj4/9XGDp143clD9H9h3nYnEbYH6/R/os6nUhu8
 w0oId+bM/ZxmQlzkp6lZtaOaPJDZ0r0AsknBUQopmqYoW6LTbuuHBX8kI4Rue0DbHy1e
 mbzPjBwkSbms8K90WNi/003USXE5JnQxjNFkYczcPzJ/7SMBXOw8tiTeS/P+SxheGpAc
 uc/Q==
X-Gm-Message-State: AOJu0Yx4GmBgmwQhshmuhEpgRjvIxemgXLoxA8l5TJRuPV9Y7BgiTw/a
 +lkGfffRIktAOIxA9yWkLz5tDDxSdYlyM2MaINR3CVNt+51whjqYhdK0vCjO9t+5eC4JSSWOwKt
 y
X-Gm-Gg: ASbGncu/T2ZJbIU6qujxjOcWGX0RKqyAzApRBB8ZKjwBH7k6CJ0ggro2KfdU7K+/PO1
 xTJmA4WIGz8nGWeWV4v5rDEAEloXZiimAMtEkZPUlVvo1EkuPNrhya3F6SR44HTes233RQuGv75
 Ql+ONVk2QTzeJiUp/GrTx7w++X6aMsehVq39ZAQ1VxnFoo2pT52r986Nl7/nLOqK469Qk0t3Z33
 qKnEjZ7slL5hr+th/p0bhb5mVhqCC79fLxi0LZF77vYBB3lqd8tAxDfJ2kh5ZUg6IygG+K7MJ4f
 JJ3oVdBIw19aP+sGuvi7mW0pE0S+QMcs1Xx8NT/3PTXWPxSqu+ZwH2U=
X-Google-Smtp-Source: AGHT+IF5gktlikfZlKsc5dO7INyuoVPqDHdCkyAYXRJopcmlD5vfEGMBKVHQbj1CfPf4BVBDnOXfxw==
X-Received: by 2002:a17:90b:2744:b0:2fe:b77a:2eab with SMTP id
 98e67ed59e1d1-3087bbc9e33mr12725679a91.32.1745202831766; 
 Sun, 20 Apr 2025 19:33:51 -0700 (PDT)
Received: from localhost ([2600:1700:65a7:3600:b90a:a0ba:c740:60a5])
 by smtp.gmail.com with UTF8SMTPSA id
 98e67ed59e1d1-3087df05153sm5399424a91.11.2025.04.20.19.33.51
 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
 Sun, 20 Apr 2025 19:33:51 -0700 (PDT)
From: Emma Worley <emma@emma.gg>
To: ffmpeg-devel@ffmpeg.org
Date: Sun, 20 Apr 2025 19:33:45 -0700
Message-ID: <20250421023345.7558-3-emma@emma.gg>
X-Mailer: git-send-email 2.48.0
In-Reply-To: <20250421023345.7558-1-emma@emma.gg>
References: <20250421023345.7558-1-emma@emma.gg>
MIME-Version: 1.0
Subject: [FFmpeg-devel] [PATCH v3 3/3] lavc/dxvenc: improve compatibility
 with Resolume products
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Cc: Emma Worley <emma@emma.gg>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
Archived-At: <https://master.gitmailbox.com/ffmpegdev/20250421023345.7558-3-emma@emma.gg/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

Improves compatibility with Resolume products by adding an additional hashtable
for DXT color+LUT combinations, and padding the DXT texture to the next largest
16x16 multiple. Produces identical packets to Resolume Alley in manual tests.

Signed-off-by: Emma Worley <emma@emma.gg>
---
 libavcodec/dxvenc.c         | 105 ++++++++++++++++++++----------------
 tests/ref/fate/dxv3enc-dxt1 |   2 +-
 2 files changed, 60 insertions(+), 47 deletions(-)

diff --git a/libavcodec/dxvenc.c b/libavcodec/dxvenc.c
index c64b4d5f15..737ccfff3e 100644
--- a/libavcodec/dxvenc.c
+++ b/libavcodec/dxvenc.c
@@ -34,6 +34,11 @@
 
 #define DXV_HEADER_LENGTH 12
 
+/*
+ * Resolume will refuse to display frames that are not padded to 16x16 pixels.
+ */
+#define DXV_ALIGN(x) FFALIGN(x, 16)
+
 /*
  * DXV uses LZ-like back-references to avoid copying words that have already
  * appeared in the decompressed stream. Using a simple hash table (HT)
@@ -60,6 +65,7 @@ typedef struct DXVEncContext {
 
     struct AVHashtableContext *color_ht;
     struct AVHashtableContext *lut_ht;
+    struct AVHashtableContext *combo_ht;
 } DXVEncContext;
 
 /* Converts an index offset value to a 2-bit opcode and pushes it to a stream.
@@ -94,10 +100,14 @@ static int dxv_compress_dxt1(AVCodecContext *avctx)
     DXVEncContext *ctx = avctx->priv_data;
     PutByteContext *pbc = &ctx->pbc;
     void *value;
-    uint32_t color, lut, idx, color_idx, lut_idx, prev_pos, state = 16, pos = 0, op = 0;
+    uint64_t combo;
+    uint32_t color, lut, idx, combo_idx, prev_pos, old_pos, state = 16, pos = 0, op = 0;
 
     av_hashtable_clear(ctx->color_ht);
     av_hashtable_clear(ctx->lut_ht);
+    av_hashtable_clear(ctx->combo_ht);
+
+    av_hashtable_set(ctx->combo_ht, ctx->tex_data, &pos);
 
     bytestream2_put_le32(pbc, AV_RL32(ctx->tex_data));
     av_hashtable_set(ctx->color_ht, ctx->tex_data, &pos);
@@ -107,51 +117,46 @@ static int dxv_compress_dxt1(AVCodecContext *avctx)
     pos++;
 
     while (pos + 2 <= ctx->tex_size / 4) {
-        idx = 0;
-        color_idx = 0;
-        lut_idx = 0;
+        combo = AV_RL64(ctx->tex_data + pos * 4);
+        combo_idx = av_hashtable_get(ctx->combo_ht, &combo, &prev_pos) ? pos - prev_pos : 0;
+        idx = combo_idx;
+        PUSH_OP(2);
+        if (pos >= LOOKBACK_WORDS) {
+            old_pos = pos - LOOKBACK_WORDS;
+            if (av_hashtable_get(ctx->combo_ht, ctx->tex_data + old_pos * 4, &prev_pos) && prev_pos <= old_pos)
+                av_hashtable_delete(ctx->combo_ht, ctx->tex_data + old_pos * 4);
+        }
+        av_hashtable_set(ctx->combo_ht, &combo, &pos);
 
         color = AV_RL32(ctx->tex_data + pos * 4);
-        if (av_hashtable_get(ctx->color_ht, &color, &prev_pos))
-            color_idx = pos - prev_pos;
-        av_hashtable_set(ctx->color_ht, &color, &pos);
-
+        if (!combo_idx) {
+            idx = av_hashtable_get(ctx->color_ht, &color, &prev_pos) ? pos - prev_pos : 0;
+            PUSH_OP(2);
+            if (!idx)
+                bytestream2_put_le32(pbc, color);
+        }
         if (pos >= LOOKBACK_WORDS) {
-            uint32_t old_pos = pos - LOOKBACK_WORDS;
+            old_pos = pos - LOOKBACK_WORDS;
             if (av_hashtable_get(ctx->color_ht, ctx->tex_data + old_pos * 4, &prev_pos) && prev_pos <= old_pos)
                 av_hashtable_delete(ctx->color_ht, ctx->tex_data + old_pos * 4);
         }
+        av_hashtable_set(ctx->color_ht, &color, &pos);
         pos++;
 
         lut = AV_RL32(ctx->tex_data + pos * 4);
-        if (color_idx && lut == AV_RL32(ctx->tex_data + (pos - color_idx) * 4)) {
-            idx = color_idx;
-        } else {
-            idx = 0;
-            if (av_hashtable_get(ctx->lut_ht, &lut, &prev_pos))
-                lut_idx = pos - prev_pos;
-            av_hashtable_set(ctx->lut_ht, &lut, &pos);
+        if (!combo_idx) {
+            idx = av_hashtable_get(ctx->lut_ht, &lut, &prev_pos) ? pos - prev_pos : 0;
+            PUSH_OP(2);
+            if (!idx)
+                bytestream2_put_le32(pbc, lut);
         }
         if (pos >= LOOKBACK_WORDS) {
-            uint32_t old_pos = pos - LOOKBACK_WORDS;
+            old_pos = pos - LOOKBACK_WORDS;
             if (av_hashtable_get(ctx->lut_ht, ctx->tex_data + old_pos * 4, &prev_pos) && prev_pos <= old_pos)
                 av_hashtable_delete(ctx->lut_ht, ctx->tex_data + old_pos * 4);
         }
+        av_hashtable_set(ctx->lut_ht, &lut, &pos);
         pos++;
-
-        PUSH_OP(2);
-
-        if (!idx) {
-            idx = color_idx;
-            PUSH_OP(2);
-            if (!idx)
-                bytestream2_put_le32(pbc,  color);
-
-            idx = lut_idx;
-            PUSH_OP(2);
-            if (!idx)
-                bytestream2_put_le32(pbc,  lut);
-        }
     }
 
     return 0;
@@ -172,11 +177,23 @@ static int dxv_encode(AVCodecContext *avctx, AVPacket *pkt,
         return ret;
 
     if (ctx->enc.tex_funct) {
+        uint8_t *dst_data[4] = {av_mallocz(DXV_ALIGN(avctx->width) * DXV_ALIGN(avctx->height) * 4), 0, 0, 0};
+        int dst_linesize[4] = {DXV_ALIGN(avctx->width) * 4, 0, 0, 0};
+
+        av_image_copy2(
+            dst_data,
+            dst_linesize,
+            frame->data,
+            frame->linesize,
+            frame->format,
+            avctx->width,
+            avctx->height);
+
         ctx->enc.tex_data.out = ctx->tex_data;
-        ctx->enc.frame_data.in = frame->data[0];
-        ctx->enc.stride = frame->linesize[0];
-        ctx->enc.width  = avctx->width;
-        ctx->enc.height = avctx->height;
+        ctx->enc.frame_data.in = dst_data[0];
+        ctx->enc.stride = dst_linesize[0];
+        ctx->enc.width  = DXV_ALIGN(avctx->width);
+        ctx->enc.height = DXV_ALIGN(avctx->height);
         ff_texturedsp_exec_compress_threads(avctx, &ctx->enc);
     } else {
         /* unimplemented: YCoCg formats */
@@ -216,14 +233,6 @@ static av_cold int dxv_init(AVCodecContext *avctx)
         return ret;
     }
 
-    if (avctx->width % TEXTURE_BLOCK_W || avctx->height % TEXTURE_BLOCK_H) {
-        av_log(avctx,
-               AV_LOG_ERROR,
-               "Video size %dx%d is not multiple of "AV_STRINGIFY(TEXTURE_BLOCK_W)"x"AV_STRINGIFY(TEXTURE_BLOCK_H)".\n",
-               avctx->width, avctx->height);
-        return AVERROR_INVALIDDATA;
-    }
-
     ff_texturedspenc_init(&texdsp);
 
     switch (ctx->tex_fmt) {
@@ -237,10 +246,10 @@ static av_cold int dxv_init(AVCodecContext *avctx)
         return AVERROR_INVALIDDATA;
     }
     ctx->enc.raw_ratio = 16;
-    ctx->tex_size = avctx->width  / TEXTURE_BLOCK_W *
-                    avctx->height / TEXTURE_BLOCK_H *
+    ctx->tex_size = DXV_ALIGN(avctx->width) / TEXTURE_BLOCK_W *
+                    DXV_ALIGN(avctx->height) / TEXTURE_BLOCK_H *
                     ctx->enc.tex_ratio;
-    ctx->enc.slice_count = av_clip(avctx->thread_count, 1, avctx->height / TEXTURE_BLOCK_H);
+    ctx->enc.slice_count = av_clip(avctx->thread_count, 1, DXV_ALIGN(avctx->height) / TEXTURE_BLOCK_H);
 
     ctx->tex_data = av_malloc(ctx->tex_size);
     if (!ctx->tex_data) {
@@ -251,6 +260,9 @@ static av_cold int dxv_init(AVCodecContext *avctx)
     if (ret < 0)
         return ret;
     ret = av_hashtable_alloc(&ctx->lut_ht, sizeof(uint32_t), sizeof(uint32_t), LOOKBACK_HT_ELEMS);
+    if (ret < 0)
+        return ret;
+    ret = av_hashtable_alloc(&ctx->combo_ht, sizeof(uint64_t), sizeof(uint32_t), LOOKBACK_HT_ELEMS);
     if (ret < 0)
         return ret;
 
@@ -265,6 +277,7 @@ static av_cold int dxv_close(AVCodecContext *avctx)
 
     av_hashtable_freep(&ctx->color_ht);
     av_hashtable_freep(&ctx->lut_ht);
+    av_hashtable_freep(&ctx->combo_ht);
 
     return 0;
 }
diff --git a/tests/ref/fate/dxv3enc-dxt1 b/tests/ref/fate/dxv3enc-dxt1
index 74849a8031..e09000e181 100644
--- a/tests/ref/fate/dxv3enc-dxt1
+++ b/tests/ref/fate/dxv3enc-dxt1
@@ -3,4 +3,4 @@
 #codec_id 0: dxv
 #dimensions 0: 1920x1080
 #sar 0: 1/1
-0,          0,          0,        1,    76521, 0xed387a5e
+0,          0,          0,        1,    76190, 0x0e6f0326
-- 
2.48.0

_______________________________________________
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".