From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id D8ECE47F66 for ; Mon, 5 Feb 2024 05:27:50 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A4BF568D164; Mon, 5 Feb 2024 07:27:40 +0200 (EET) Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E6E3E68C8CA for ; Mon, 5 Feb 2024 07:27:34 +0200 (EET) Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-3bd884146e9so3258586b6e.0 for ; Sun, 04 Feb 2024 21:27:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707110852; x=1707715652; 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=83heej+m+Flkls8R3Ql0th3gAUYejDMeWpDy6jG3Rpo=; b=dXIJFEJTvNsu1tNWHwohZ8Djk9SKPIU2t/ifxQMTNZAkcJmzWXcO10m/UbEn9D+mtg SmgHNTOWsEtBI4VbcPY39v/RlcC6Q8SB44YmY2Il4UqqdEShFkhCj2E0Pq2/oPCL9zEO JxmaeeEcylciYmrU/fy/vVHqN/AWjVhrvdG1275jJ6kEarvA0BsHfMBaOTvmzdcudPEs 1v4UK8JFlQLefHMrN7BGG23WIhL1MaO8RguY6bXBuSlacwzvjLRez/0C+W8ZpdOoFKi/ ACrUvTFliSPLVaGOy0dwyF+ZxZ2bDTfjPiNHzrFP+jRneujNw60Cw9NQWa5Q0XPMbfRJ gnTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707110852; x=1707715652; 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=83heej+m+Flkls8R3Ql0th3gAUYejDMeWpDy6jG3Rpo=; b=Gabje/yvynMd1kbtqIo7eUNKKug+0ncskmFQsYJaj5Jcjp0+jC9eH/ryWhATqAdET3 9LUHVRcf1ji1Q8JohHj6bsZN9akAwKoFvT0+dqV+UHzmONw22qIfA5SLj38W0tOc2JO9 R47QlNYTQ/Yl7u1SNuMZj78R0R6V825mR0tZTCHL2/N5UvbjnI89lxz6oLi1ek3ZWRxN mVXYwT47XxEU4P8L3PWgpYyLlmFrAkBxIzMvzleJODFHcw79Xqj/e3bP/5vQWSlnICuj dls5mmD3hWTjDHJ7zLWDjo3RbWfmMjry0TNilQli9Ezcruh39Hw82Eo6sRgbLTVyAu7W MWAg== X-Gm-Message-State: AOJu0YwYoDupm9IsaJJQbI92F1bZd90XleztQA1EvgZpYlZmQ85SxlqI m66qG5/pYh72dcMfp2eXQap8tKzbHnXWN9FJnupBv5kCRR/gH7/kHpNKDABk X-Google-Smtp-Source: AGHT+IFbybtoRJUyY9MjQ6eCIvGjnrpaZLsxEsFSUBW5D0zTH2zvr8gPidlFxjkWFDEnRFaWNaZdeg== X-Received: by 2002:a05:6808:14d5:b0:3bf:dfa8:5190 with SMTP id f21-20020a05680814d500b003bfdfa85190mr561662oiw.10.1707110852026; Sun, 04 Feb 2024 21:27:32 -0800 (PST) Received: from localhost (c-24-4-236-173.hsd1.ca.comcast.net. [24.4.236.173]) by smtp.gmail.com with UTF8SMTPSA id p21-20020a056a000a1500b006e04a3a3df6sm550614pfh.185.2024.02.04.21.27.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 04 Feb 2024 21:27:31 -0800 (PST) From: Connor Worley To: ffmpeg-devel@ffmpeg.org Date: Sun, 4 Feb 2024 21:27:24 -0800 Message-Id: <20240205052724.26862-2-connorbworley@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205052724.26862-1-connorbworley@gmail.com> References: <20240205052724.26862-1-connorbworley@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Connor Worley Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: Offers a modest performance gain due to the switch from naive linear probling to robin hood. Signed-off-by: Connor Worley --- libavcodec/dxvenc.c | 121 +++++++++++++------------------------------- 1 file changed, 35 insertions(+), 86 deletions(-) diff --git a/libavcodec/dxvenc.c b/libavcodec/dxvenc.c index b274175689..e17b3b2c36 100644 --- a/libavcodec/dxvenc.c +++ b/libavcodec/dxvenc.c @@ -21,7 +21,7 @@ #include -#include "libavutil/crc.h" +#include "libavutil/hashtable.h" #include "libavutil/imgutils.h" #include "libavutil/opt.h" @@ -44,69 +44,6 @@ enum DXVTextureFormat { DXV_FMT_DXT1 = MKBETAG('D', 'X', 'T', '1'), }; -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; @@ -123,10 +60,8 @@ typedef struct DXVEncContext { enum 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]; + AVHashtableContext color_ht; + AVHashtableContext lut_ht; } DXVEncContext; /* Converts an index offset value to a 2-bit opcode and pushes it to a stream. @@ -161,27 +96,32 @@ static int dxv_compress_dxt1(AVCodecContext *avctx) DXVEncContext *ctx = avctx->priv_data; PutByteContext *pbc = &ctx->pbc; uint32_t *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++; @@ -190,13 +130,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++; @@ -298,10 +239,15 @@ 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_init(&ctx->color_ht, sizeof(uint32_t), sizeof(uint32_t), LOOKBACK_HT_ELEMS); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Could not initialize color lookback table.\n"); + return ret; + } + ret = av_hashtable_init(&ctx->lut_ht, sizeof(uint32_t), sizeof(uint32_t), LOOKBACK_HT_ELEMS); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Could not initialize LUT lookback table.\n"); + return ret; } return 0; @@ -313,6 +259,9 @@ static av_cold int dxv_close(AVCodecContext *avctx) av_freep(&ctx->tex_data); + av_hashtable_destroy(&ctx->color_ht); + av_hashtable_destroy(&ctx->lut_ht); + return 0; } -- 2.40.1 _______________________________________________ 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".