From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 92D5A4D19E for ; Fri, 30 May 2025 23:20:34 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id AFF5A68DCEF; Sat, 31 May 2025 02:20:30 +0300 (EEST) Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id 66B9268DB19 for ; Sat, 31 May 2025 02:20:24 +0300 (EEST) Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2350b1b9129so14799155ad.0 for ; Fri, 30 May 2025 16:20:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emma-gg.20230601.gappssmtp.com; s=20230601; t=1748647222; x=1749252022; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=m5z10hEbc0yg+DI9RUTIzlaWTAdMC9aAbCiSR9Cj3Vc=; b=gH9saGp/d41IIC/99i8DS4+ZkIrN32Xj4UQAd2xjGi5XGmVLI1jcNF1oq+PkNTTXDX GHIiNV770Sc0baIC34rI9u7Lj/FTAFpoXTEAIwANoyIZYGrlxnE08AIJWuhxpeJXuCvA Q4vm4PXsf9XS2SO4rXq2AVrJWp25i/glpq/dIvgXILR0XyVBcwWLq1dj6rkRerqybwYL rFOFZWV9yvGzBbyTfs33T6OOvtfrjIrDD+uWmOQ2So4uuMcSljxq8GReruYjdq32XjDh QmwGZSHT4R/FMwkSRKhIh11Mmm5idQXKgiBl6RkM7zJPdiPFIGFecdXoQ4nGMFNLjdOr 2aAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748647222; x=1749252022; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=m5z10hEbc0yg+DI9RUTIzlaWTAdMC9aAbCiSR9Cj3Vc=; b=MOVYoppmq17Y1FblgNYCl5m1eL+ZM8DuO2dkoRsA46ebrJiltMHSGOzpbRGePPAMi6 9IFV2Cp5XqWeeCy9QG0WnVaovQsMrVwDCDYW3ZwXmkc0BI+RuWFh3r2qjrkGBFuvw1PE iOMeCEnnjVozr6Urdu+/PjXKS3Ykn+J1Btt4JWidKOIVg9Rp10xYkmOIAi1pJ9c6gFYR OIAvi7GorhmaaL8xHiyReao3ujTCfvIEHcUW0i+FCe7wfTYOtBv3HtaCQJrX7zw6K2y7 s38HOULu2EWrYhzQlD1I5pi8Iglmo2Pr698/a2ZzaB7WOk0C2ap+LVz/5YCcJif/7ljY ME5Q== X-Gm-Message-State: AOJu0YzUxsrpxn459CCntO7UjBa48MI+71Ud3c6W+G8rzBlqTXLoiZBf 1y2s/VJ0SK1pbkTxz9j1GLPIy0x8DO4n1BngWpU+QbQbIfw9zdSRn+DOo2STJnZDVLjwf2plhGw Z7Pey X-Gm-Gg: ASbGncsHVMAyHxAn4wNq+Bpf+QIbTtcZufIt8MoDm+tsPnkJjr+uF5dLI8quLUnbi6c 9uEhujRzAjx1EX7l+6kMQDbiEYZlnW5ITfaIIBDLOYXxKwsW4XuUu5+6LU6yWGjCjjc6QAECJO9 wGrDkDMyplTAtvqMC8JBDLz490SN6twmIVLQrZYvgdy81YBq1KEMO1WN347NOOqNUrDPLDk51ch CtvQ8HuYjyyrPgr+RMaxCX/LFd0fjKAQv0Q4IeHNVSTWcQEYMhq2YgG16ui0mUWcbDhnWIUTCpJ sMGRwI3d09DEy9/KGzYbUAN4U7u8b4YHn1jj5Dt86+uc3oVmw9NzFsXhRCJP X-Google-Smtp-Source: AGHT+IE7LawGDiC9MqnEH3YUXlnjDhgkcpSLogdFl0Pq7LkBc67MVbFxrNuO2PMsdmLtFzMCnxZXyg== X-Received: by 2002:a17:903:b88:b0:235:60e:36ee with SMTP id d9443c01a7336-235396b5e00mr53138325ad.39.1748647221826; Fri, 30 May 2025 16:20:21 -0700 (PDT) Received: from localhost ([2600:1700:65a7:3600:9535:9a1a:bf9e:1c3b]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-23506cf5233sm33248515ad.201.2025.05.30.16.20.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 30 May 2025 16:20:21 -0700 (PDT) From: Emma Worley To: ffmpeg-devel@ffmpeg.org Date: Fri, 30 May 2025 16:20:06 -0700 Message-ID: <20250530232008.29554-1-emma@emma.gg> X-Mailer: git-send-email 2.48.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v6 1/3] lavc/hashtable: create generic robin hood hash table 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: Emma 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: Adds a generic hash table with the DXV encoder as an initial use case. Signed-off-by: Emma Worley --- libavcodec/Makefile | 2 + libavcodec/hashtable.c | 214 +++++++++++++++++++++++++++++++++++ libavcodec/hashtable.h | 94 +++++++++++++++ libavcodec/tests/hashtable.c | 110 ++++++++++++++++++ 4 files changed, 420 insertions(+) create mode 100644 libavcodec/hashtable.c create mode 100644 libavcodec/hashtable.h create mode 100644 libavcodec/tests/hashtable.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 77734dff24..1bb5d4f4a7 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -42,6 +42,7 @@ OBJS = ac3_parser.o \ dv_profile.o \ encode.o \ get_buffer.o \ + hashtable.o \ imgconvert.o \ jni.o \ lcevcdec.o \ @@ -1325,6 +1326,7 @@ TESTPROGS = avcodec \ bitstream_le \ celp_math \ codec_desc \ + hashtable \ htmlsubtitles \ jpeg2000dwt \ mathops \ diff --git a/libavcodec/hashtable.c b/libavcodec/hashtable.c new file mode 100644 index 0000000000..151476176b --- /dev/null +++ b/libavcodec/hashtable.c @@ -0,0 +1,214 @@ +/* + * Generic hashtable + * Copyright (C) 2025 Emma Worley + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "libavutil/crc.h" +#include "libavutil/error.h" +#include "libavutil/mem.h" +#include "hashtable.h" + +#define ALIGN _Alignof(size_t) + +struct FFHashtableContext { + size_t key_size; + size_t key_size_aligned; + size_t val_size; + size_t val_size_aligned; + size_t entry_size; + size_t max_entries; + size_t nb_entries; + const AVCRC *crc; + uint8_t *table; + uint8_t *swapbuf; +}; + +/* + * Hash table entries are comprised of a probe sequence length (PSL), key, and + * value. When the PSL of an entry is zero, it means it is not occupied by a + * key/value pair. When the PSL is non-zero, it represents the "distance" of + * the entry from its "home" location plus one, where the "home" location is + * hash(key) % max_entries. + */ + +#define ENTRY_PSL_VAL(entry) (*(size_t*)(entry)) +#define ENTRY_KEY_PTR(entry) ((entry) + FFALIGN(sizeof(size_t), ALIGN)) +#define ENTRY_VAL_PTR(entry) (ENTRY_KEY_PTR(entry) + ctx->key_size_aligned) + +#define KEYS_EQUAL(k1, k2) (!memcmp((k1), (k2), ctx->key_size)) + +int ff_hashtable_alloc(struct FFHashtableContext **ctx, size_t key_size, size_t val_size, size_t max_entries) +{ + struct FFHashtableContext *res = av_malloc(sizeof(struct FFHashtableContext)); + if (!res) + return AVERROR(ENOMEM); + res->key_size = key_size; + res->key_size_aligned = FFALIGN(key_size, ALIGN); + res->val_size = val_size; + res->val_size_aligned = FFALIGN(val_size, ALIGN); + res->entry_size = FFALIGN(sizeof(size_t), ALIGN) + + res->key_size_aligned + + res->val_size_aligned; + res->max_entries = max_entries; + res->nb_entries = 0; + res->crc = av_crc_get_table(AV_CRC_32_IEEE); + if (!res->crc) { + ff_hashtable_freep(&res); + return AVERROR_BUG; + } + res->table = av_calloc(res->max_entries, res->entry_size); + if (!res->table) { + ff_hashtable_freep(&res); + return AVERROR(ENOMEM); + } + + res->swapbuf = av_calloc(2, res->key_size_aligned + res->val_size_aligned); + if (!res->swapbuf) { + ff_hashtable_freep(&res); + return AVERROR(ENOMEM); + } + *ctx = res; + return 0; +} + +static size_t hash_key(const struct FFHashtableContext *ctx, const void *key) +{ + return av_crc(ctx->crc, 0, key, ctx->key_size) % ctx->max_entries; +} + +int ff_hashtable_get(const struct FFHashtableContext *ctx, const void *key, void *val) +{ + if (!ctx->nb_entries) + return 0; + + size_t hash = hash_key(ctx, key); + + for (size_t psl = 1; psl <= ctx->max_entries; psl++) { + size_t wrapped_index = (hash + psl) % ctx->max_entries; + uint8_t *entry = ctx->table + wrapped_index * ctx->entry_size; + if (ENTRY_PSL_VAL(entry) < psl) + // When PSL stops increasing it means there are no further entries + // with the same key hash. + return 0; + if (KEYS_EQUAL(ENTRY_KEY_PTR(entry), key)) { + memcpy(val, ENTRY_VAL_PTR(entry), ctx->val_size); + return 1; + } + } + return 0; +} + +int ff_hashtable_set(struct FFHashtableContext *ctx, const void *key, const void *val) +{ + int swapping = 0; + size_t psl = 1; + size_t hash = hash_key(ctx, key); + size_t wrapped_index = hash % ctx->max_entries; + uint8_t *set = ctx->swapbuf; + uint8_t *tmp = ctx->swapbuf + ctx->key_size_aligned + ctx->val_size_aligned; + + memcpy(set, key, ctx->key_size); + memcpy(set + ctx->key_size_aligned, val, ctx->val_size); + + for (size_t i = 0; i < ctx->max_entries; i++) { + if (++wrapped_index == ctx->max_entries) + wrapped_index = 0; + uint8_t *entry = ctx->table + wrapped_index * ctx->entry_size; + if (!ENTRY_PSL_VAL(entry) || (!swapping && KEYS_EQUAL(ENTRY_KEY_PTR(entry), set))) { + if (!ENTRY_PSL_VAL(entry)) + ctx->nb_entries++; + ENTRY_PSL_VAL(entry) = psl; + memcpy(ENTRY_KEY_PTR(entry), set, ctx->key_size_aligned + ctx->val_size); + return 1; + } + if (ENTRY_PSL_VAL(entry) < psl) { + // When PSL stops increasing it means there are no further entries + // with the same key hash. We can only hope to find an unoccupied + // entry. + if (ctx->nb_entries == ctx->max_entries) + // The table is full so inserts are impossible. + return 0; + // Robin Hood hash tables "steal from the rich" by minimizing the + // PSL of the inserted entry. + swapping = 1; + // set needs to swap with entry + memcpy(tmp, ENTRY_KEY_PTR(entry), ctx->key_size_aligned + ctx->val_size_aligned); + memcpy(ENTRY_KEY_PTR(entry), set, ctx->key_size_aligned + ctx->val_size_aligned); + FFSWAP(uint8_t*, set, tmp); + FFSWAP(size_t, psl, ENTRY_PSL_VAL(entry)); + } + psl++; + } + return 0; +} + +int ff_hashtable_delete(struct FFHashtableContext *ctx, const void *key) +{ + if (!ctx->nb_entries) + return 0; + + uint8_t *next_entry; + size_t hash = hash_key(ctx, key); + size_t wrapped_index = hash % ctx->max_entries; + + for (size_t psl = 1; psl <= ctx->max_entries; psl++) { + if (++wrapped_index == ctx->max_entries) + wrapped_index = 0; + uint8_t *entry = ctx->table + wrapped_index * ctx->entry_size; + if (ENTRY_PSL_VAL(entry) < psl) + // When PSL stops increasing it means there are no further entries + // with the same key hash. + return 0; + if (KEYS_EQUAL(ENTRY_KEY_PTR(entry), key)) { + ENTRY_PSL_VAL(entry) = 0; + // Shift each following entry that will benefit from a reduced PSL. + for (psl++; psl <= ctx->max_entries; psl++) { + if (++wrapped_index == ctx->max_entries) + wrapped_index = 0; + next_entry = ctx->table + wrapped_index * ctx->entry_size; + if (ENTRY_PSL_VAL(next_entry) <= 1) { + ctx->nb_entries--; + return 1; + } + memcpy(entry, next_entry, ctx->entry_size); + ENTRY_PSL_VAL(entry)--; + ENTRY_PSL_VAL(next_entry) = 0; + entry = next_entry; + } + } + }; + return 0; +} + +void ff_hashtable_clear(struct FFHashtableContext *ctx) +{ + memset(ctx->table, 0, ctx->entry_size * ctx->max_entries); +} + +void ff_hashtable_freep(struct FFHashtableContext **ctx) +{ + if (*ctx) { + av_freep(&(*ctx)->table); + av_freep(&(*ctx)->swapbuf); + } + av_freep(ctx); +} diff --git a/libavcodec/hashtable.h b/libavcodec/hashtable.h new file mode 100644 index 0000000000..f81b4bb93f --- /dev/null +++ b/libavcodec/hashtable.h @@ -0,0 +1,94 @@ +/* + * Generic hashtable + * Copyright (C) 2024 Emma Worley + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_HASHTABLE_H +#define AVCODEC_HASHTABLE_H + +#include + +/* Implements a hash table using Robin Hood open addressing. + * See: https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf + * + * Keys are placed in the table based on their CRC value and are considered + * equal when they are bytewise-identical. + */ + +typedef struct FFHashtableContext FFHashtableContext; + +/** + * Create a fixed-sized Robin Hood hash table. + * + * @param ctx context to allocate and initialize + * @param key_size size of key type in bytes + * @param val_size size of value type in bytes + * @param max_entries maximum number of key-value pairs to store + * + * @return zero on success, nonzero on error + */ +int ff_hashtable_alloc(struct FFHashtableContext **ctx, size_t key_size, size_t val_size, size_t max_entries); + +/** + * Look up a value from a hash table given a key. + * + * @param ctx hash table context + * @param key pointer to key data + * @param val destination pointer for value data + * + * @return 1 if the key is found, zero if the key is not found + */ +int ff_hashtable_get(const struct FFHashtableContext *ctx, const void *key, void *val); + +/** + * Store a value in a hash table given a key. + * + * @param ctx hash table context + * @param key pointer to key data + * @param val pointer for value data + * + * @return 1 if the key is written, zero if the key is not written due to the hash table reaching max capacity + */ +int ff_hashtable_set(struct FFHashtableContext *ctx, const void *key, const void *val); + +/** + * Delete a value from a hash table given a key. + * + * @param ctx hash table context + * @param key pointer to key data + * + * @return 1 if the key is deleted, zero if the key is not deleted due to not being found + */ +int ff_hashtable_delete(struct FFHashtableContext *ctx, const void *key); + +/** + * Delete all values from a hash table. + * + * @param ctx hash table context + */ +void ff_hashtable_clear(struct FFHashtableContext *ctx); + +/** + * Free a hash table. + * + * @param ctx hash table context + */ +void ff_hashtable_freep(struct FFHashtableContext **ctx); + +#endif diff --git a/libavcodec/tests/hashtable.c b/libavcodec/tests/hashtable.c new file mode 100644 index 0000000000..02c0ac8afa --- /dev/null +++ b/libavcodec/tests/hashtable.c @@ -0,0 +1,110 @@ +/* + * Generic hashtable tests + * Copyright (C) 2024 Emma Worley + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include "libavutil/avassert.h" +#include "libavcodec/hashtable.h" + +int main(void) +{ + struct FFHashtableContext *ctx; + uint8_t k; + uint64_t v; + + // impossibly large allocation should fail gracefully + av_assert0(ff_hashtable_alloc(&ctx, -1, -1, -1) < 0); + + // hashtable can store up to 3 uint8_t->uint64_t entries + av_assert0(!ff_hashtable_alloc(&ctx, sizeof(k), sizeof(v), 3)); + + // unsuccessful deletes return 0 + k = 1; + av_assert0(!ff_hashtable_delete(ctx, &k)); + + // unsuccessful gets return 0 + k = 1; + av_assert0(!ff_hashtable_get(ctx, &k, &v)); + + // successful sets returns 1 + k = 1; + v = 1; + av_assert0(ff_hashtable_set(ctx, &k, &v)); + + // get should now contain 1 + k = 1; + v = 0; + av_assert0(ff_hashtable_get(ctx, &k, &v)); + av_assert0(v == 1); + + // updating sets should return 1 + k = 1; + v = 2; + av_assert0(ff_hashtable_set(ctx, &k, &v)); + + // get should now contain 2 + k = 1; + v = 0; + av_assert0(ff_hashtable_get(ctx, &k, &v)); + av_assert0(v == 2); + + // fill the table + k = 2; + v = 2; + av_assert0(ff_hashtable_set(ctx, &k, &v)); + k = 3; + v = 3; + av_assert0(ff_hashtable_set(ctx, &k, &v)); + + // inserting sets on a full table should return 0 + k = 4; + v = 4; + av_assert0(!ff_hashtable_set(ctx, &k, &v)); + + // updating sets on a full table should return 1 + k = 1; + v = 4; + av_assert0(ff_hashtable_set(ctx, &k, &v)); + v = 0; + av_assert0(ff_hashtable_get(ctx, &k, &v)); + av_assert0(v == 4); + + // successful deletes should return 1 + k = 1; + av_assert0(ff_hashtable_delete(ctx, &k)); + + // get should now return 0 + av_assert0(!ff_hashtable_get(ctx, &k, &v)); + + // sanity check remaining keys + k = 2; + v = 0; + av_assert0(ff_hashtable_get(ctx, &k, &v)); + av_assert0(v == 2); + k = 3; + v = 0; + av_assert0(ff_hashtable_get(ctx, &k, &v)); + av_assert0(v == 3); + + ff_hashtable_freep(&ctx); + + 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".