FFmpeg
Loading...
Searching...
No Matches
crc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 Shreesh Adiga <16567adigashreesh@gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef AVUTIL_X86_CRC_H
22#define AVUTIL_X86_CRC_H
23
24#include "config.h"
27#include "libavutil/avassert.h"
28#include "libavutil/cpu.h"
29#include "libavutil/crc.h"
31#include "libavutil/x86/cpu.h"
32
33#if HAVE_CLMUL_EXTERNAL
35
37uint32_t ff_crc_clmul(const AVCRC *ctx, uint32_t crc,
38 const uint8_t *buffer, size_t length);
39uint32_t ff_crc_le_clmul(const AVCRC *ctx, uint32_t crc,
40 const uint8_t *buffer, size_t length);
42
43enum {
44 CRC_C = 0,
45 CLMUL_BE,
46 CLMUL_LE,
47};
48
49static const AVCRC crc_table_clmul[AV_CRC_MAX][17] = {
50 [AV_CRC_8_ATM] = {
51 CLMUL_BE,
52 0x32000000, 0x0, 0xbc000000, 0x0,
53 0xc4000000, 0x0, 0x94000000, 0x0,
54 0x62000000, 0x0, 0x79000000, 0x0,
55 0x07156a16, 0x1, 0x07000000, 0x1,
56 },
57 [AV_CRC_8_EBU] = {
58 CLMUL_BE,
59 0xb5000000, 0x0, 0xf3000000, 0x0,
60 0xfc000000, 0x0, 0x0d000000, 0x0,
61 0x6a000000, 0x0, 0x65000000, 0x0,
62 0x1c4b8192, 0x1, 0x1d000000, 0x1,
63 },
64 [AV_CRC_16_ANSI] = {
65 CLMUL_BE,
66 0xf9e30000, 0x0, 0x807d0000, 0x0,
67 0xf9130000, 0x0, 0xff830000, 0x0,
68 0x807b0000, 0x0, 0x86630000, 0x0,
69 0xfffbffe7, 0x1, 0x80050000, 0x1,
70 },
71 [AV_CRC_16_CCITT] = {
72 CLMUL_BE,
73 0x60190000, 0x0, 0x59b00000, 0x0,
74 0xd5f60000, 0x0, 0x45630000, 0x0,
75 0xaa510000, 0x0, 0xeb230000, 0x0,
76 0x11303471, 0x1, 0x10210000, 0x1,
77 },
78 [AV_CRC_24_IEEE] = {
79 CLMUL_BE,
80 0x1f428700, 0x0, 0x467d2400, 0x0,
81 0x2c8c9d00, 0x0, 0x64e4d700, 0x0,
82 0xd9fe8c00, 0x0, 0xfd7e0c00, 0x0,
83 0xf845fe24, 0x1, 0x864cfb00, 0x1,
84 },
85 [AV_CRC_32_IEEE] = {
86 CLMUL_BE,
87 0x8833794c, 0x0, 0xe6228b11, 0x0,
88 0xc5b9cd4c, 0x0, 0xe8a45605, 0x0,
89 0x490d678d, 0x0, 0xf200aa66, 0x0,
90 0x04d101df, 0x1, 0x04c11db7, 0x1,
91 },
93 CLMUL_LE,
94 0xc6e41596, 0x1, 0x54442bd4, 0x1,
95 0xccaa009e, 0x0, 0x751997d0, 0x1,
96 0xccaa009e, 0x0, 0x63cd6124, 0x1,
97 0xf7011640, 0x1, 0xdb710641, 0x1,
98 },
100 CLMUL_LE,
101 0x0000bffa, 0x0, 0x1b0c2, 0x0,
102 0x00018cc2, 0x0, 0x1d0c2, 0x0,
103 0x00018cc2, 0x0, 0x1bc02, 0x0,
104 0xcfffbffe, 0x1, 0x14003, 0x0,
105 },
106};
107
108static inline void crc_init_x86(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
109{
110 uint64_t poly_;
111 if (le) {
112 // convert the reversed representation to regular form
113 poly = reverse(poly, bits) >> 1;
114 }
115 // convert to 32 degree polynomial
116 poly_ = ((uint64_t)poly) << (32 - bits);
117
118 uint64_t div;
119 uint8_t *dst = (uint8_t*)(ctx + 1);
120 if (le) {
121 ctx[0] = CLMUL_LE;
122 AV_WN64(dst, xnmodp(4 * 128 - 32, poly_, 32, &div, le));
123 AV_WN64(dst + 8, xnmodp(4 * 128 + 32, poly_, 32, &div, le));
124 uint64_t tmp = xnmodp(128 - 32, poly_, 32, &div, le);
125 AV_WN64(dst + 16, tmp);
126 AV_WN64(dst + 24, xnmodp(128 + 32, poly_, 32, &div, le));
127 AV_WN64(dst + 32, tmp);
128 AV_WN64(dst + 40, xnmodp(64, poly_, 32, &div, le));
129 AV_WN64(dst + 48, div);
130 AV_WN64(dst + 56, reverse(poly_ | (1ULL << 32), 32));
131 } else {
132 ctx[0] = CLMUL_BE;
133 AV_WN64(dst, xnmodp(4 * 128 + 64, poly_, 32, &div, le));
134 AV_WN64(dst + 8, xnmodp(4 * 128, poly_, 32, &div, le));
135 AV_WN64(dst + 16, xnmodp(128 + 64, poly_, 32, &div, le));
136 AV_WN64(dst + 24, xnmodp(128, poly_, 32, &div, le));
137 AV_WN64(dst + 32, xnmodp(64, poly_, 32, &div, le));
138 AV_WN64(dst + 48, div);
139 AV_WN64(dst + 40, xnmodp(96, poly_, 32, &div, le));
140 AV_WN64(dst + 56, poly_ | (1ULL << 32));
141 }
142}
143#endif
144
145static inline const AVCRC *ff_crc_get_table_x86(AVCRCId crc_id)
146{
147#if HAVE_CLMUL_EXTERNAL
149
151 return crc_table_clmul[crc_id];
152 }
153#endif
154 return NULL;
155}
156
157static inline av_cold int ff_crc_init_x86(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
158{
159#if HAVE_CLMUL_EXTERNAL
161
163 crc_init_x86(ctx, le, bits, poly, ctx_size);
164 return 1;
165 }
166#endif
167 return 0;
168}
169
170static inline uint32_t ff_crc_x86(const AVCRC *ctx, uint32_t crc,
171 const uint8_t *buffer, size_t length)
172{
173 switch (ctx[0]) {
174#if HAVE_CLMUL_EXTERNAL
175 case CLMUL_BE: return ff_crc_clmul(ctx, crc, buffer, length);
176 case CLMUL_LE: return ff_crc_le_clmul(ctx, crc, buffer, length);
177#endif
178 default: av_unreachable("x86 CRC only uses CLMUL_BE and CLMUL_LE");
179 }
180 return 0;
181}
182
183#endif /* AVUTIL_X86_CRC_H */
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static AVFormatContext * ctx
#define FF_VISIBILITY_PUSH_HIDDEN
#define FF_VISIBILITY_POP_HIDDEN
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
#define NULL
Definition coverity.c:32
Public header for CRC hash function implementation.
static av_unused uint64_t xnmodp(unsigned n, uint64_t poly, unsigned deg, uint64_t *div, int bitreverse)
static uint64_t reverse(uint64_t p, unsigned int deg)
static const uint8_t bits[8]
Definition fastaudio.c:100
AVCRCId
Definition crc.h:48
uint32_t AVCRC
Definition crc.h:46
@ AV_CRC_16_CCITT
Definition crc.h:51
@ AV_CRC_32_IEEE
Definition crc.h:52
@ AV_CRC_24_IEEE
Definition crc.h:55
@ AV_CRC_8_EBU
Definition crc.h:56
@ AV_CRC_8_ATM
Definition crc.h:49
@ AV_CRC_32_IEEE_LE
Definition crc.h:53
@ AV_CRC_MAX
Definition crc.h:57
@ AV_CRC_16_ANSI
Definition crc.h:50
@ AV_CRC_16_ANSI_LE
Definition crc.h:54
#define AV_WN64(p, v)
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
static atomic_int cpu_flags
Definition cpu.c:56
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition cpu.c:109
#define EXTERNAL_CLMUL(flags)
Definition cpu.h:76
static uint8_t tmp[40]
Definition aes_ctr.c:52
static char buffer[20]
Definition seek.c:32
static const AVCRC * ff_crc_get_table_x86(AVCRCId crc_id)
Definition crc.h:145
static uint32_t ff_crc_x86(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Definition crc.h:170
static av_cold int ff_crc_init_x86(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
Definition crc.h:157