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 4A8234D00C for ; Fri, 5 Dec 2025 09:22:55 +0000 (UTC) Authentication-Results: ffbox; dkim=fail (body hash mismatch (got b'x/6bDyuY+WP3iT4+S/0W5pAZuaJWpUD5+V1M8K+SywQ=', expected b'wDGZG8UjM/75D6TPTspUw80dOsqEmnVhb3DTewnRxrQ=')) header.d=ffmpeg.org header.i=@ffmpeg.org header.a=rsa-sha256 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ffmpeg.org; i=@ffmpeg.org; q=dns/txt; s=mail; t=1764926545; h=mime-version : to : date : message-id : reply-to : subject : list-id : list-archive : list-archive : list-help : list-owner : list-post : list-subscribe : list-unsubscribe : from : cc : content-type : content-transfer-encoding : from; bh=x/6bDyuY+WP3iT4+S/0W5pAZuaJWpUD5+V1M8K+SywQ=; b=ZcoHEd1Mlv18eWn8rPYTpqZMR4Db00JrWihZLv86teNuskEHaJ6CaKP1aBsG+pIbqpAdO PUZocaJIgT7FI9dVDtA0iYg6CTmV32AaOaen8oOH+VtCDfIfzcMsOkVS3Jlg+uFZMYzVbPs 3fTSuNXVf3mHl7xfI52aeUxReeUyBbHDeGJPb5n92SKbvcekTOc0AAkIhdoVexAuZjx3Wgg ONVNVsqoJ5DbXSgdfEk9bHsm1mlGPPF31ezxGWrFTk0Oj9LYpQ3B8P1gU7hvrzfNIXIdfSX uCiwu0FoQkDusTq1XKpZ087+L3/EUCdL4rXaXYClw77bV2BnMPmZILyTP8uQ== Received: from [172.19.0.3] (unknown [172.19.0.3]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id BA0056905AF; Fri, 5 Dec 2025 11:22:25 +0200 (EET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=ffmpeg.org; s=arc; t=1764926523; b=tPrNUqDpvCHNroAr0+dXLAgI3u5Y2lPJBZXqGe68iE5OrJ4EVoddkrAUau8NT+Xg5RkGi SqOJDOLkRL+VPK/J/yF7CQwapNa3cQxwxpcVdHPY+hpOgAMfuaVHMQw7GSUsPktg7Bg6bNe sNdWFrURTLL4Mlo0XvnWDwKQR8qr2GG9dUeiEHHn6vneO7ratOgwYOZeoWmszEQQOzrYZ1H xXqKp5pSO4+iGosDlivNvuY9lt53A6Adg22qWIvE/cf8AdrMBnfywF2pezbnArqfFYZ7wcj Ma1cNXd+PYWyPpL1eu3e9ghHy8lfHt6h9YJTxJY9nGmLXol42RqYlvEA8ezw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=ffmpeg.org; s=arc; t=1764926523; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=ilIvdz+XvDiNQkByFQSd9ZTKE2F3AIw70Yq+tNm9WzM=; b=oiLxssVpYc0gj4JVU8HPCMcXsAUlPLze6VaMFTnLtj57aAjWK35YhR4wpjpq8IOESQWJU 4LM0FHWCJs3/rSk/jq4Lw1Od/cGG1JJGP2zHgjm2M3Rj+o4kDmq1lrd/wKLuBEF6e96/jZk +HM+mUY4JQETjNMKAf9dPcWlEkmUAyBn31YEw22gYtakLDR+RsUmOrkhcjyl2LaPpKopUTU N8bgGgdxlv4SKjK7QlEn6JyqzRD5hnV1doYvGMtMyhCnThsMvX3R1HrCI5hx51tnA2TnbsR cJzfnS6rGbedarIh3kUL9Yo8l6SuubjfaH8mgAcx/hkT2cUXyMzphnG0hL/w== ARC-Authentication-Results: i=1; ffmpeg.org; dkim=pass header.d=ffmpeg.org header.i=@ffmpeg.org; arc=none; dmarc=pass header.from=ffmpeg.org policy.dmarc=quarantine Authentication-Results: ffmpeg.org; dkim=pass header.d=ffmpeg.org header.i=@ffmpeg.org; arc=none (Message is not ARC signed); dmarc=pass (Used From Domain Record) header.from=ffmpeg.org policy.dmarc=quarantine DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ffmpeg.org; i=@ffmpeg.org; q=dns/txt; s=mail; t=1764926513; h=content-type : mime-version : content-transfer-encoding : from : to : reply-to : subject : date : from; bh=wDGZG8UjM/75D6TPTspUw80dOsqEmnVhb3DTewnRxrQ=; b=Pq5eMZHHjsyhXIruhVnXJeqdAeCYvdcQTivnnRyeS7fyeBsFiIr3XXBa8SPeewPbKY2be Vu+b/+qIHNIULlzoXbe4kiZ5TjrPCLmwyOp+tW9vMDJhmmv1bkBfL8KGpzSb+6h/S6zz09a yYTyQw2XXAMlcW+merGjqa6Z1Rm+08vioqe0oRGCqfg2CLJ9uxzLjFZX/EBw72b/aQNwRcX Xd5iuUzBporxgiJsU9gel6i1yKABk9k+4IxRxCUjgUV3ZRyR1MDZXnnz71YDXibL5hh0vWQ ll0YXTXjZqrO/mcy9NPhWvTwSutqjrumk8DBSvxPT4dJGPwXtX0zxGceWAxg== Received: from 55ca25703178 (code.ffmpeg.org [188.245.149.3]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id 3D5EC690531 for ; Fri, 5 Dec 2025 11:21:53 +0200 (EET) MIME-Version: 1.0 To: ffmpeg-devel@ffmpeg.org Date: Fri, 05 Dec 2025 09:21:52 -0000 Message-ID: <176492651342.39.4399437023408407188@2cb04c0e5124> Message-ID-Hash: NIGIURATT4TDQXY6GMNIGUW764SNUK46 X-Message-ID-Hash: NIGIURATT4TDQXY6GMNIGUW764SNUK46 X-MailFrom: code@ffmpeg.org X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-ffmpeg-devel.ffmpeg.org-0; header-match-ffmpeg-devel.ffmpeg.org-1; header-match-ffmpeg-devel.ffmpeg.org-2; header-match-ffmpeg-devel.ffmpeg.org-3; emergency; member-moderation X-Mailman-Version: 3.3.10 Precedence: list Reply-To: FFmpeg development discussions and patches Subject: [FFmpeg-devel] [PATCH] lavu/crc: add a function pointer based API (PR #21108) List-Id: FFmpeg development discussions and patches Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Lynne via ffmpeg-devel Cc: Lynne Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Archived-At: List-Archive: List-Post: PR #21108 opened by Lynne URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21108 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21108.patch This commit adds a new function pointer based API for CRC. The CRC code is some of the oldest in the entire codebase. Despite CRC instructions existing on most CPUs back when it was written, it did not implement support for them, nor even 64-bit CRCs. Our TTA decoder even NIHs its own 64-bit CRC just because the API is simply not capable of it! Attempts to use architecture-specific CRCs remain blocked on this. The new API is much simpler, implementation-wise. It was copied verbatim from the lavu/tx API. There will be no support for hardcoded tables: users are already expected to keep state (the function pointer), and they'll have to init a context. We'll make the old code a wrapper for the new if this is accepted, and we'll get rid of hardcoded tables to save space. Ref: #20751 >>From 5dfa403995e97f1aba251b19d1d39cca6bf0a6f4 Mon Sep 17 00:00:00 2001 From: Lynne Date: Fri, 5 Dec 2025 10:07:59 +0100 Subject: [PATCH] lavu/crc: add a function pointer based API This commit adds a new function pointer based API for CRC. The CRC code is some of the oldest in the entire codebase. Despite CRC instructions existing on most CPUs back when it was written, it did not implement support for them, nor even 64-bit CRCs. Our TTA decoder even NIHs its own 64-bit CRC just because the API is simply not capable of it! Attempts to use architecture-specific CRCs remain blocked on this. The new API is much simpler, implementation-wise. It was copied verbatim from the lavu/tx API. There will be no support for hardcoded tables: users are already expected to keep state (the function pointer), and they'll have to init a context. We'll make the old code a wrapper for the new if this is accepted, and we'll get rid of hardcoded tables to save space. --- libavutil/crc.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ libavutil/crc.h | 60 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 131 insertions(+), 1 deletion(-) diff --git a/libavutil/crc.c b/libavutil/crc.c index 703b56f4e0..ac1186795b 100644 --- a/libavutil/crc.c +++ b/libavutil/crc.c @@ -25,6 +25,7 @@ #include "bswap.h" #include "crc.h" #include "error.h" +#include "mem.h" #if CONFIG_HARDCODED_TABLES static const AVCRC av_crc_table[AV_CRC_MAX][257] = { @@ -413,3 +414,74 @@ uint32_t av_crc(const AVCRC *ctx, uint32_t crc, return crc; } + +struct CRCPreset { + uint64_t poly; + int bits; + int le; +} crc_presets[AV_CRC_MAX] = { + [AV_CRC_8_ATM] = { 0x07, 8, 0 }, + [AV_CRC_8_EBU] = { 0x1D, 8, 0 }, + [AV_CRC_16_ANSI] = { 0x8005, 16, 0 }, + [AV_CRC_16_ANSI_LE] = { 0xA001, 16, 1 }, + [AV_CRC_16_CCITT] = { 0x1021, 16, 0 }, + [AV_CRC_24_IEEE] = { 0x864CFB, 24, 0 }, + [AV_CRC_32_IEEE] = { 0x04C11DB7, 32, 0 }, + [AV_CRC_32_IEEE_LE] = { 0xEDB88320, 32, 1 }, +}; + +void av_crc_preset(AVCRCId crc, uint64_t *poly, int *bits, int *le) +{ + if (crc >= AV_CRC_MAX) { + *poly = *bits = *le = 0; + return; + } + + *poly = crc_presets[crc].poly; + *bits = crc_presets[crc].bits; + *le = crc_presets[crc].le; +} + +static uint64_t av_crc_wrapper(const uint8_t *ctx, uint64_t crc, + const uint8_t *buffer, size_t length) +{ + return av_crc((const AVCRC *)ctx, crc, buffer, length); +} + +int av_crc2_init(uint8_t **ctx, av_crc_fn *fn, + uint64_t poly, int bits, enum AVCRCFlags flags) +{ + *ctx = av_malloc(4096); + if (!(*ctx)) + return AVERROR(ENOMEM); + + int err = av_crc_init((uint32_t *)(*ctx), flags & AV_CRC_FLAG_LE, + bits, poly, 4096); + if (err < 0) { + av_freep(ctx); + return err; + } + + *fn = av_crc_wrapper; + + return 0; +} + +uint64_t av_crc_calc(AVCRCId crc_id, uint64_t crc, + uint8_t *buffer, size_t length) +{ + uint64_t poly; + int bits, le; + av_crc_preset(crc_id, &poly, &bits, &le); + if (!poly) + return 0; + + uint8_t *ctx; + av_crc_fn fn; + av_crc2_init(&ctx, &fn, poly, bits, + AV_CRC_FLAG_UNALIGNED | (le ? AV_CRC_FLAG_LE : 0)); + + crc = fn(ctx, crc, buffer, length); + av_free(&ctx); + return crc; +} diff --git a/libavutil/crc.h b/libavutil/crc.h index 7f59812a18..c8a51c0c9b 100644 --- a/libavutil/crc.h +++ b/libavutil/crc.h @@ -90,11 +90,69 @@ const AVCRC *av_crc_get_table(AVCRCId crc_id); * @param length length of the buffer * @return CRC updated with the data from the given block * - * @see av_crc_init() "le" parameter +x * @see av_crc_init() "le" parameter */ uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length) av_pure; +/** + * Function pointer to a function to perform a round of CRC calculations on a buffer. + * + * @note Using a different context than the one allocated during av_crc2_init() + * is not allowed. + * + * @param ctx the transform context + * @param crc the current CRC state + * @param buffer the buffer on which to perform CRC + * @param length the length of the buffer + * + * The buffer array must be aligned to the maximum required by the CPU + * architecture unless the AV_CRC_UNALIGNED flag was set in av_crc2_init(). + */ +typedef uint64_t (*av_crc_fn)(const uint8_t *ctx, uint64_t crc, + const uint8_t *buffer, size_t length); + +/** + * Get the parameters of a common CRC algorithm. + */ +void av_crc_preset(AVCRCId crc, uint64_t *poly, int *bits, int *le); + +enum AVCRCFlags { + /** + * Specifies that the pointer to perform the CRC on is not guaranteed to be aligned. + */ + AV_CRC_FLAG_UNALIGNED = 1 << 0, + + /** + * The lowest bit represents the coefficient for the highest + * exponent of the corresponding polynomial (both for poly and actual CRC). + * If set, you must bitswap the CRC parameter and the result of av_crc_fn + * if you need the standard representation (can be simplified in + * most cases to e.g. bswap16): + * av_bswap32(crc << (32-bits)) + */ + AV_CRC_FLAG_LE = 1 << 1, +}; + +/** + * Initialize a context and a function pointer for a CRC algorithm. + * + * @param ctx a pointer to where a CRC context will be set + * NOTE: must be freed using av_free() + * @param fn a function pointer where the function to perform a CRC will be set + * @param bits the length of the polynomial + * @param poly the polynomial for the CRC + * @param flags the set of flags to use + */ +int av_crc2_init(uint8_t **ctx, av_crc_fn *fn, + uint64_t poly, int bits, enum AVCRCFlags flags); + +/** + * Convenience wrapper function to perform a well-known CRC algorithm on a function. + * Guaranteed to not require new allocations. + */ +uint64_t av_crc_calc(AVCRCId crc_id, uint64_t crc, uint8_t *buffer, size_t length); + /** * @} */ -- 2.49.1 _______________________________________________ ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org