FFmpeg
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"
25 #include "libavutil/attributes.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/cpu.h"
29 #include "libavutil/crc.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/reverse.h"
32 #include "libavutil/x86/cpu.h"
33 
34 #if HAVE_CLMUL_EXTERNAL
36 uint32_t ff_crc_clmul(const AVCRC *ctx, uint32_t crc,
37  const uint8_t *buffer, size_t length);
38 uint32_t ff_crc_le_clmul(const AVCRC *ctx, uint32_t crc,
39  const uint8_t *buffer, size_t length);
41 
42 enum {
43  CRC_C = 0,
44  CLMUL_BE,
45  CLMUL_LE,
46 };
47 
48 static const AVCRC crc_table_clmul[AV_CRC_MAX][17] = {
49  [AV_CRC_8_ATM] = {
50  CLMUL_BE,
51  0x32000000, 0x0, 0xbc000000, 0x0,
52  0xc4000000, 0x0, 0x94000000, 0x0,
53  0x62000000, 0x0, 0x79000000, 0x0,
54  0x07156a16, 0x1, 0x07000000, 0x1,
55  },
56  [AV_CRC_8_EBU] = {
57  CLMUL_BE,
58  0xb5000000, 0x0, 0xf3000000, 0x0,
59  0xfc000000, 0x0, 0x0d000000, 0x0,
60  0x6a000000, 0x0, 0x65000000, 0x0,
61  0x1c4b8192, 0x1, 0x1d000000, 0x1,
62  },
63  [AV_CRC_16_ANSI] = {
64  CLMUL_BE,
65  0xf9e30000, 0x0, 0x807d0000, 0x0,
66  0xf9130000, 0x0, 0xff830000, 0x0,
67  0x807b0000, 0x0, 0x86630000, 0x0,
68  0xfffbffe7, 0x1, 0x80050000, 0x1,
69  },
70  [AV_CRC_16_CCITT] = {
71  CLMUL_BE,
72  0x60190000, 0x0, 0x59b00000, 0x0,
73  0xd5f60000, 0x0, 0x45630000, 0x0,
74  0xaa510000, 0x0, 0xeb230000, 0x0,
75  0x11303471, 0x1, 0x10210000, 0x1,
76  },
77  [AV_CRC_24_IEEE] = {
78  CLMUL_BE,
79  0x1f428700, 0x0, 0x467d2400, 0x0,
80  0x2c8c9d00, 0x0, 0x64e4d700, 0x0,
81  0xd9fe8c00, 0x0, 0xfd7e0c00, 0x0,
82  0xf845fe24, 0x1, 0x864cfb00, 0x1,
83  },
84  [AV_CRC_32_IEEE] = {
85  CLMUL_BE,
86  0x8833794c, 0x0, 0xe6228b11, 0x0,
87  0xc5b9cd4c, 0x0, 0xe8a45605, 0x0,
88  0x490d678d, 0x0, 0xf200aa66, 0x0,
89  0x04d101df, 0x1, 0x04c11db7, 0x1,
90  },
91  [AV_CRC_32_IEEE_LE] = {
92  CLMUL_LE,
93  0xc6e41596, 0x1, 0x54442bd4, 0x1,
94  0xccaa009e, 0x0, 0x751997d0, 0x1,
95  0xccaa009e, 0x0, 0x63cd6124, 0x1,
96  0xf7011640, 0x1, 0xdb710641, 0x1,
97  },
98  [AV_CRC_16_ANSI_LE] = {
99  CLMUL_LE,
100  0x0000bffa, 0x0, 0x1b0c2, 0x0,
101  0x00018cc2, 0x0, 0x1d0c2, 0x0,
102  0x00018cc2, 0x0, 0x1bc02, 0x0,
103  0xcfffbffe, 0x1, 0x14003, 0x0,
104  },
105 };
106 
107 static uint64_t reverse(uint64_t p, unsigned int deg)
108 {
109  uint64_t ret = 0;
110  int i;
111  for (i = 0; i < (deg / 8); i += 1) {
112  ret = (ret << 8) | (ff_reverse[p & 0xff]);
113  p >>= 8;
114  }
115  int rem = (deg + 1) - 8 * i;
116  ret = (ret << rem) | (ff_reverse[p & 0xff] >> (8 - rem));
117  return ret;
118 }
119 
120 static uint64_t xnmodp(unsigned n, uint64_t poly, unsigned deg, uint64_t *div, int bitreverse)
121 {
122  uint64_t mod, mask, high;
123 
124  if (n < deg) {
125  *div = 0;
126  return poly;
127  }
128  mask = ((uint64_t)1 << deg) - 1;
129  poly &= mask;
130  mod = poly;
131  *div = 1;
132  deg--;
133  while (--n > deg) {
134  high = (mod >> deg) & 1;
135  *div = (*div << 1) | high;
136  mod <<= 1;
137  if (high)
138  mod ^= poly;
139  }
140  uint64_t ret = mod & mask;
141  if (bitreverse) {
142  *div = reverse(*div, deg) << 1;
143  return reverse(ret, deg) << 1;
144  }
145  return ret;
146 }
147 
148 static inline void crc_init_x86(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
149 {
150  uint64_t poly_;
151  if (le) {
152  // convert the reversed representation to regular form
153  poly = reverse(poly, bits) >> 1;
154  }
155  // convert to 32 degree polynomial
156  poly_ = ((uint64_t)poly) << (32 - bits);
157 
158  uint64_t div;
159  uint8_t *dst = (uint8_t*)(ctx + 1);
160  if (le) {
161  ctx[0] = CLMUL_LE;
162  AV_WN64(dst, xnmodp(4 * 128 - 32, poly_, 32, &div, le));
163  AV_WN64(dst + 8, xnmodp(4 * 128 + 32, poly_, 32, &div, le));
164  uint64_t tmp = xnmodp(128 - 32, poly_, 32, &div, le);
165  AV_WN64(dst + 16, tmp);
166  AV_WN64(dst + 24, xnmodp(128 + 32, poly_, 32, &div, le));
167  AV_WN64(dst + 32, tmp);
168  AV_WN64(dst + 40, xnmodp(64, poly_, 32, &div, le));
169  AV_WN64(dst + 48, div);
170  AV_WN64(dst + 56, reverse(poly_ | (1ULL << 32), 32));
171  } else {
172  ctx[0] = CLMUL_BE;
173  AV_WN64(dst, xnmodp(4 * 128 + 64, poly_, 32, &div, le));
174  AV_WN64(dst + 8, xnmodp(4 * 128, poly_, 32, &div, le));
175  AV_WN64(dst + 16, xnmodp(128 + 64, poly_, 32, &div, le));
176  AV_WN64(dst + 24, xnmodp(128, poly_, 32, &div, le));
177  AV_WN64(dst + 32, xnmodp(64, poly_, 32, &div, le));
178  AV_WN64(dst + 48, div);
179  AV_WN64(dst + 40, xnmodp(96, poly_, 32, &div, le));
180  AV_WN64(dst + 56, poly_ | (1ULL << 32));
181  }
182 }
183 #endif
184 
185 static inline const AVCRC *ff_crc_get_table_x86(AVCRCId crc_id)
186 {
187 #if HAVE_CLMUL_EXTERNAL
188  int cpu_flags = av_get_cpu_flags();
189 
190  if (EXTERNAL_CLMUL(cpu_flags)) {
191  return crc_table_clmul[crc_id];
192  }
193 #endif
194  return NULL;
195 }
196 
197 static inline av_cold int ff_crc_init_x86(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
198 {
199 #if HAVE_CLMUL_EXTERNAL
200  int cpu_flags = av_get_cpu_flags();
201 
202  if (EXTERNAL_CLMUL(cpu_flags)) {
203  crc_init_x86(ctx, le, bits, poly, ctx_size);
204  return 1;
205  }
206 #endif
207  return 0;
208 }
209 
210 static inline uint32_t ff_crc_x86(const AVCRC *ctx, uint32_t crc,
211  const uint8_t *buffer, size_t length)
212 {
213  switch (ctx[0]) {
214 #if HAVE_CLMUL_EXTERNAL
215  case CLMUL_BE: return ff_crc_clmul(ctx, crc, buffer, length);
216  case CLMUL_LE: return ff_crc_le_clmul(ctx, crc, buffer, length);
217 #endif
218  default: av_unreachable("x86 CRC only uses CLMUL_BE and CLMUL_LE");
219  }
220  return 0;
221 }
222 
223 #endif /* AVUTIL_X86_CRC_H */
FF_VISIBILITY_PUSH_HIDDEN
#define FF_VISIBILITY_PUSH_HIDDEN
Definition: attributes_internal.h:30
cpu.h
AV_CRC_8_EBU
@ AV_CRC_8_EBU
Definition: crc.h:56
EXTERNAL_CLMUL
#define EXTERNAL_CLMUL(flags)
Definition: cpu.h:76
AVCRC
uint32_t AVCRC
Definition: crc.h:46
mask
int mask
Definition: mediacodecdec_common.c:154
ff_reverse
const uint8_t ff_reverse[256]
Definition: reverse.c:23
high
int high
Definition: dovi_rpuenc.c:39
AV_CRC_24_IEEE
@ AV_CRC_24_IEEE
Definition: crc.h:55
reverse.h
av_get_cpu_flags
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:109
cpu_flags
static atomic_int cpu_flags
Definition: cpu.c:56
ff_crc_get_table_x86
static const AVCRC * ff_crc_get_table_x86(AVCRCId crc_id)
Definition: crc.h:185
crc.h
AV_CRC_16_ANSI_LE
@ AV_CRC_16_ANSI_LE
Definition: crc.h:54
avassert.h
av_cold
#define av_cold
Definition: attributes.h:106
attributes_internal.h
intreadwrite.h
bits
uint8_t bits
Definition: vp3data.h:128
ctx
AVFormatContext * ctx
Definition: movenc.c:49
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
AV_CRC_16_ANSI
@ AV_CRC_16_ANSI
Definition: crc.h:50
NULL
#define NULL
Definition: coverity.c:32
av_unreachable
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition: avassert.h:108
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
cpu.h
attributes.h
FF_VISIBILITY_POP_HIDDEN
#define FF_VISIBILITY_POP_HIDDEN
Definition: attributes_internal.h:31
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AV_CRC_16_CCITT
@ AV_CRC_16_CCITT
Definition: crc.h:51
AV_CRC_32_IEEE
@ AV_CRC_32_IEEE
Definition: crc.h:52
mod
static int mod(int a, int b)
Modulo operation with only positive remainders.
Definition: vf_v360.c:755
ret
ret
Definition: filter_design.txt:187
ff_crc_x86
static uint32_t ff_crc_x86(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Definition: crc.h:210
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
AV_CRC_MAX
@ AV_CRC_MAX
Definition: crc.h:57
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
AV_CRC_32_IEEE_LE
@ AV_CRC_32_IEEE_LE
Definition: crc.h:53
AV_CRC_8_ATM
@ AV_CRC_8_ATM
Definition: crc.h:49
AVCRCId
AVCRCId
Definition: crc.h:48
AV_WN64
#define AV_WN64(p, v)
Definition: intreadwrite.h:376
ff_crc_init_x86
static av_cold int ff_crc_init_x86(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
Definition: crc.h:197