From patchwork Thu May 23 22:10:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Connor Worley X-Patchwork-Id: 49194 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:542:0:b0:460:55fa:d5ed with SMTP id 63csp1350503vqf; Thu, 23 May 2024 15:11:16 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCU1nMzZJFvua05StgLeDsC8qCtqvVCiSy45lDn2F9qEwRXkTM6n9Ble8owdSOTaruSQHZth/HfXXgL1cPofn3MM7nnMwuX3QKHrwg== X-Google-Smtp-Source: AGHT+IGLjMLci55F+lVwQ2A6iMc1lrUCCoVCtNKNeUhUUSrI7hwGyJv4QLMak6fnm1pW14LANYgt X-Received: by 2002:a2e:b385:0:b0:2e6:be3c:9d4d with SMTP id 38308e7fff4ca-2e95b096b68mr3142851fa.12.1716502276027; Thu, 23 May 2024 15:11:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1716502275; cv=none; d=google.com; s=arc-20160816; b=OZJe/gZf+wiy9MU61rRYtYmo5xY4vbTq+wNqvHnqGnzbiWjgMxNrl79C32kIt/a6cM Yae6+1ATGS3LNiFtJRY7y2J7JS2X5bEn34X4jvhPJpHqtVmHBn9LTb7JU+fxTom31dG5 fE5AEz9S0qaE8BuesfpT2ran8Jroqg2vm96PtWinqyFkr5vgoRxhXqj/hDME8RwcS4Ad KNMl0XfkWAMgG0dUAy6HaxSvmwbQo6smaSfk/11JFbhs3tbb1cCJ7kGtDeCfp9gjB5mH 8ewDeVAoTru2RxL1MS96su9Gzx2stHNgjkgWUCBQn31EY4yU0xNrSjkb+3RQqW72y2Qc rV2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=679Q+YNT/0WfM5NKVIkwZYatBtUktxK43s/tNIR7zsw=; fh=4o9WedBEbOrq7GvNs0s+TOKeyzmDZRM3O7oe6ZP6vYM=; b=Y91Cle7EwsMPvt6KjadH1u8E8ZaWtA6v9WEruK8tV3ZMMqVll8owbw+YWKnqQWaLG9 Bog6rwfcUfCpOfPQb1nY2C86wa1Do/ZvFFZ48+jLhQde/tVIgVXgJpaI8Bm3HcWi6jPB Eao7SpbmKoj07+jPYDdC8r5griIaj2FtGn9ZuJPI3RgCJSwZtaHHd1SMC+i4mKBglZ46 r/kPPBc+XfZUj8T9UNgR5Xqwurqe6vRCdMDGHnlAQ5zBl4XUf0boc1YE3qltL2gYL4X7 c7Q8N7puuCJNURJ1dF6Y9IuhFbkECpuXJic4vv4t8zXrUCj7U5wIBH/DUzCEyUmbMNJG yLwA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=RkCUJGC8; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 4fb4d7f45d1cf-578524b03ecsi133946a12.480.2024.05.23.15.11.15; Thu, 23 May 2024 15:11:15 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=RkCUJGC8; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9B10668D51F; Fri, 24 May 2024 01:11:13 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2B6CA68D29F for ; Fri, 24 May 2024 01:11:07 +0300 (EEST) Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-6f8e9870e72so288217b3a.1 for ; Thu, 23 May 2024 15:11:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716502264; x=1717107064; 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=H7xfGCdQLUa6tGdp8AhlUzDShD/84uH5wRKBb+er4v8=; b=RkCUJGC89Y1gJYr3qNNzThWnP4zvAJODkQUuTuGG85sgFhWWThVnU7NtHTiQtyK2St KZygkGApootGwy0+enS21lsjgBzs8oFgA5pwaIly3gixjacQDHCjP8ANJBxGdyY/vpW+ eLCljPomKtO6MmJGQ6ct9MyJKqVvTD8xiDHyOnZKo041zvVXgfr3tprpU3fFa/LZ2Kt1 +umGD43MZO0A/iqWDHXLmOSHK6MqSQvgw9tel/mEAJ7AVnXRX6cy5OV6yPSIcyFCmp5A NLznQhQsqXAHkLkwCDHYdmooUEpXjFgeCJr/jD+tWiMqjkXgwL8JgiXfBDdgc48bcIsp 1tZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716502264; x=1717107064; 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=H7xfGCdQLUa6tGdp8AhlUzDShD/84uH5wRKBb+er4v8=; b=uyk35U9DUFDZosv4mtdFsftsOCZ2Wu4uqhRLZnspPeny+VweiEgjwbTtbjFPi/a4LB nVn7QVYGebT7M2h8NoW4KrAoabEBlgphNMly5sigJGm9G2alDFlwqIpNPmkpNq4ivVrI R4S2CsPxUUVWXHcNBE5BiQ7UYoGKRSmxmVbazGIfACMxqRTlLtiRMg3dDWP12zrjjZpe P+9T4WxehmOB1X5rQ4JHuT434YoiUrrMA4fCSQclKPgNuUr8S6CFbIgyMoTENJlGL8ad /xO7h/jpQ+BRmpnYtYVCinNYpoIxRsKTNmz0dHPiWI8KfWL6XKCT7z7SwDaO3aLJdJp3 CSFA== X-Gm-Message-State: AOJu0Yy4ZwmCEyf2dnf7w7TqzXeJGPzNLkS9QgJuBYL56qIX48MKJvzP slL6GmFjJtceF/eX1rbhcoLRlz1yrar1nT6MbCmsBr+lkuJ7wFAPL7uHPQ== X-Received: by 2002:a05:6a21:186:b0:1a7:3b4a:3e8 with SMTP id adf61e73a8af0-1b212ccf7eamr737494637.7.1716502264235; Thu, 23 May 2024 15:11:04 -0700 (PDT) Received: from localhost ([2607:fb10:7011:1::7d4]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-6f8fcbea96fsm92689b3a.108.2024.05.23.15.11.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 23 May 2024 15:11:03 -0700 (PDT) From: Connor Worley To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 May 2024 15:10:05 -0700 Message-ID: <20240523221005.20659-2-connorbworley@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240523221005.20659-1-connorbworley@gmail.com> References: <20240523221005.20659-1-connorbworley@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: QU4jJPLwxZo3 Offers a modest performance gain due to the switch from naive linear probling to robin hood. Signed-off-by: Connor Worley --- libavcodec/dxvenc.c | 119 ++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 87 deletions(-) diff --git a/libavcodec/dxvenc.c b/libavcodec/dxvenc.c index 8229438373..8962be25a1 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/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. @@ -161,25 +96,30 @@ static int dxv_compress_dxt1(AVCodecContext *avctx) void *value; uint32_t color, lut, idx, color_idx, lut_idx, prev_pos, state = 16, pos = 2, 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; }