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 80F054D8F2 for <ffmpegdev@gitmailbox.com>; Mon, 21 Apr 2025 02:34:23 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 328CC68A114; Mon, 21 Apr 2025 05:34:00 +0300 (EEST) Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 79DFD687EB0 for <ffmpeg-devel@ffmpeg.org>; Mon, 21 Apr 2025 05:33:52 +0300 (EEST) Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-736b98acaadso3237450b3a.1 for <ffmpeg-devel@ffmpeg.org>; Sun, 20 Apr 2025 19:33:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emma-gg.20230601.gappssmtp.com; s=20230601; t=1745202830; x=1745807630; 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=WHhYKXFvmnYZ6aegIYf09tu5zxEa03KfAkSTpNFDZlI=; b=2h6oRhFe+FrGJxrrpF5Mbm4qmvomLPddGfFwWaH2wKfpoQIPbzcfzbcqJcy1juvxKe a9bX4numr6qdYWxfw5myxg9rpCMKpZIFov0oAh7shb0uGxqTGlZ6CYPo+lvVm+p+1Cpp XHOeO7sp+GSVuKBzeuJzMBC2YJCj4D7JpjeC31pD+dBlFsQ7j4/x0GpAGUg3h6DZSvo+ SG1z73PpV48zC1LPotkMkCzyBkqC4+hHnAG6fEs4NqSUW+zY5PYNF3hhDLIWgPs/zbTb VL4z5JmgKle2AeOiB0tXZ0KcbTUsc0aUSlxi/UL7YPaROZ7xRr35spWok/zWvCceXNkX nceg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745202830; x=1745807630; 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=WHhYKXFvmnYZ6aegIYf09tu5zxEa03KfAkSTpNFDZlI=; b=T2uU4w0sg+QWu8OiJnmsLU0A6O6aALPLlhsQ4dQJraX+HuqM0Mm4bPSou8iiQyglMk DX+21Aj2V2DEowPETCxAecsya2dprC/JIIJy6MRMxsTBzywAo+3VgJYU7n79440aqZE4 7z6KAeAzlXig0Cdvek1kcUbxFOZMT2GNgKRNc24AjPX4B4/fu1P5qZCXdJ2UF52CUNnK WFF1UNUC6yVkQpNUqpbUIM1gO7aoqDtWy7szLhGfJWfrRXrXk33SW9AFuNHFdx6EEALh WCZjxo//ETxAY0thP4d4KUoRmGvwEZGl2WKJLUJy+ARj8cTxAKh6KHGOm+khkIRn7hE5 aZqg== X-Gm-Message-State: AOJu0YySHv34pf/PWBSAImoLzVyvKaaGJYC1OQLrsJAiK3d6SmVerJ0Z Ii8XfaZ/83u+klTD8XGiBXhSdovMTHiZ5Cb26Q/3roLvAIcrQBY8xRC33bYPF7dDoGIrV9XKtUq z X-Gm-Gg: ASbGncvZpvoH3JpfTqoydYVet980zDypuXd829SPIeidvUcjnILFYfjrVWysqGI7LTo Mb3qfy5CWidV+WvMw8TGdkSzUvt3pn/XOqk/O+VlNaTR/u9ALovnpT7vi1OTeG6A2ib4OUDbzV8 dEbSz5sp/i0DdgGOG6UF5sbKTP5iL0/xylCHPnAC6XUj3hABoTFm5Z2icPd1zNRW/17sOehXB78 1i19199t/quf1ZkzwuigHjz1rT6RWhh29cZjjWIqiiYwN8OjIpuV+Decwe6tYjSnbGkAVu3wWs+ ABUyDQ4DXxAKKRqq27dJjMHkgfjfNNTO81bmgpIWgGYFY0g2VJNqT3k= X-Google-Smtp-Source: AGHT+IFiQWY6kNMY1tWvSK7dd62qgVrMgAJeucuOgAu92Dyk3jVbjXSomguV2t4pWqDLd3ZR2sNW0g== X-Received: by 2002:a05:6a00:328b:b0:736:46b4:beef with SMTP id d2e1a72fcca58-73dc147bc0bmr13735463b3a.3.1745202830433; Sun, 20 Apr 2025 19:33:50 -0700 (PDT) Received: from localhost ([2600:1700:65a7:3600:b90a:a0ba:c740:60a5]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-73dbfaacf48sm5602174b3a.148.2025.04.20.19.33.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 20 Apr 2025 19:33:50 -0700 (PDT) From: Emma Worley <emma@emma.gg> To: ffmpeg-devel@ffmpeg.org Date: Sun, 20 Apr 2025 19:33:44 -0700 Message-ID: <20250421023345.7558-2-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 2/3] lavc/dxvenc: migrate DXT1 encoder to lavu hashtable 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-2-emma@emma.gg/> List-Archive: <https://master.gitmailbox.com/ffmpegdev/> List-Post: <mailto:ffmpegdev@gitmailbox.com> Offers a modest performance gain due to the switch from naive linear probling to robin hood. Signed-off-by: Emma Worley <emma@emma.gg> --- libavcodec/dxvenc.c | 121 ++++++++++++-------------------------------- 1 file changed, 33 insertions(+), 88 deletions(-) diff --git a/libavcodec/dxvenc.c b/libavcodec/dxvenc.c index 808d8daedb..c64b4d5f15 100644 --- a/libavcodec/dxvenc.c +++ b/libavcodec/dxvenc.c @@ -21,7 +21,7 @@ #include <stdint.h> -#include "libavutil/crc.h" +#include "libavutil/hashtable.h" #include "libavutil/imgutils.h" #include "libavutil/mem.h" #include "libavutil/opt.h" @@ -39,72 +39,9 @@ * appeared in the decompressed stream. Using a simple hash table (HT) * significantly speeds up the lookback process while encoding. */ -#define LOOKBACK_HT_ELEMS 0x40000 +#define LOOKBACK_HT_ELEMS 0x20202 #define LOOKBACK_WORDS 0x20202 -typedef struct HTEntry { - uint32_t key; - uint32_t pos; -} HTEntry; - -static void ht_init(HTEntry *ht) -{ - for (size_t i = 0; i < LOOKBACK_HT_ELEMS; i++) { - ht[i].pos = -1; - } -} - -static uint32_t ht_lookup_and_upsert(HTEntry *ht, const AVCRC *hash_ctx, - uint32_t key, uint32_t pos) -{ - uint32_t ret = -1; - size_t hash = av_crc(hash_ctx, 0, (uint8_t*)&key, 4) % LOOKBACK_HT_ELEMS; - for (size_t i = hash; i < hash + LOOKBACK_HT_ELEMS; i++) { - size_t wrapped_index = i % LOOKBACK_HT_ELEMS; - HTEntry *entry = &ht[wrapped_index]; - if (entry->key == key || entry->pos == -1) { - ret = entry->pos; - entry->key = key; - entry->pos = pos; - break; - } - } - return ret; -} - -static void ht_delete(HTEntry *ht, const AVCRC *hash_ctx, - uint32_t key, uint32_t pos) -{ - HTEntry *removed_entry = NULL; - size_t removed_hash; - size_t hash = av_crc(hash_ctx, 0, (uint8_t*)&key, 4) % LOOKBACK_HT_ELEMS; - - for (size_t i = hash; i < hash + LOOKBACK_HT_ELEMS; i++) { - size_t wrapped_index = i % LOOKBACK_HT_ELEMS; - HTEntry *entry = &ht[wrapped_index]; - if (entry->pos == -1) - return; - if (removed_entry) { - size_t candidate_hash = av_crc(hash_ctx, 0, (uint8_t*)&entry->key, 4) % LOOKBACK_HT_ELEMS; - if ((wrapped_index > removed_hash && (candidate_hash <= removed_hash || candidate_hash > wrapped_index)) || - (wrapped_index < removed_hash && (candidate_hash <= removed_hash && candidate_hash > wrapped_index))) { - *removed_entry = *entry; - entry->pos = -1; - removed_entry = entry; - removed_hash = wrapped_index; - } - } else if (entry->key == key) { - if (entry->pos <= pos) { - entry->pos = -1; - removed_entry = entry; - removed_hash = wrapped_index; - } else { - return; - } - } - } -} - typedef struct DXVEncContext { AVClass *class; @@ -121,10 +58,8 @@ typedef struct DXVEncContext { DXVTextureFormat tex_fmt; int (*compress_tex)(AVCodecContext *avctx); - const AVCRC *crc_ctx; - - HTEntry color_lookback_ht[LOOKBACK_HT_ELEMS]; - HTEntry lut_lookback_ht[LOOKBACK_HT_ELEMS]; + struct AVHashtableContext *color_ht; + struct AVHashtableContext *lut_ht; } DXVEncContext; /* Converts an index offset value to a 2-bit opcode and pushes it to a stream. @@ -159,27 +94,32 @@ 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 = 2, op = 0; + uint32_t color, lut, idx, color_idx, lut_idx, prev_pos, state = 16, pos = 0, op = 0; - ht_init(ctx->color_lookback_ht); - ht_init(ctx->lut_lookback_ht); + av_hashtable_clear(ctx->color_ht); + av_hashtable_clear(ctx->lut_ht); bytestream2_put_le32(pbc, AV_RL32(ctx->tex_data)); + av_hashtable_set(ctx->color_ht, ctx->tex_data, &pos); + pos++; bytestream2_put_le32(pbc, AV_RL32(ctx->tex_data + 4)); - - ht_lookup_and_upsert(ctx->color_lookback_ht, ctx->crc_ctx, AV_RL32(ctx->tex_data), 0); - ht_lookup_and_upsert(ctx->lut_lookback_ht, ctx->crc_ctx, AV_RL32(ctx->tex_data + 4), 1); + av_hashtable_set(ctx->lut_ht, ctx->tex_data + 4, &pos); + pos++; while (pos + 2 <= ctx->tex_size / 4) { idx = 0; + color_idx = 0; + lut_idx = 0; color = AV_RL32(ctx->tex_data + pos * 4); - prev_pos = ht_lookup_and_upsert(ctx->color_lookback_ht, ctx->crc_ctx, color, pos); - color_idx = prev_pos != -1 ? pos - prev_pos : 0; + if (av_hashtable_get(ctx->color_ht, &color, &prev_pos)) + color_idx = pos - prev_pos; + av_hashtable_set(ctx->color_ht, &color, &pos); + if (pos >= LOOKBACK_WORDS) { uint32_t old_pos = pos - LOOKBACK_WORDS; - uint32_t old_color = AV_RL32(ctx->tex_data + old_pos * 4); - ht_delete(ctx->color_lookback_ht, ctx->crc_ctx, old_color, old_pos); + 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); } pos++; @@ -188,13 +128,14 @@ static int dxv_compress_dxt1(AVCodecContext *avctx) idx = color_idx; } else { idx = 0; - prev_pos = ht_lookup_and_upsert(ctx->lut_lookback_ht, ctx->crc_ctx, lut, pos); - lut_idx = prev_pos != -1 ? pos - prev_pos : 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 (pos >= LOOKBACK_WORDS) { uint32_t old_pos = pos - LOOKBACK_WORDS; - uint32_t old_lut = AV_RL32(ctx->tex_data + old_pos * 4); - ht_delete(ctx->lut_lookback_ht, ctx->crc_ctx, old_lut, old_pos); + 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); } pos++; @@ -306,11 +247,12 @@ static av_cold int dxv_init(AVCodecContext *avctx) return AVERROR(ENOMEM); } - ctx->crc_ctx = av_crc_get_table(AV_CRC_32_IEEE); - if (!ctx->crc_ctx) { - av_log(avctx, AV_LOG_ERROR, "Could not initialize CRC table.\n"); - return AVERROR_BUG; - } + ret = av_hashtable_alloc(&ctx->color_ht, sizeof(uint32_t), sizeof(uint32_t), LOOKBACK_HT_ELEMS); + 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; return 0; } @@ -321,6 +263,9 @@ static av_cold int dxv_close(AVCodecContext *avctx) av_freep(&ctx->tex_data); + av_hashtable_freep(&ctx->color_ht); + av_hashtable_freep(&ctx->lut_ht); + return 0; } -- 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".