FFmpeg
crc.h
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef AVUTIL_AARCH64_CRC_H
20 #define AVUTIL_AARCH64_CRC_H
21 
22 #include <stddef.h>
23 #include <stdint.h>
24 
25 #include "config.h"
26 
27 #include "cpu.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/cpu.h"
31 #include "libavutil/crc.h"
32 #include "libavutil/intreadwrite.h"
33 
34 #if HAVE_ARM_CRC
36 uint32_t ff_crc32_aarch64(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer,
37  size_t length);
39 #endif
40 
41 #if HAVE_PMULL && HAVE_EOR3
42 #include "libavutil/crc_internal.h"
43 
45 uint32_t ff_crc_neon_pmull(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer,
46  size_t length);
47 uint32_t ff_crc_le_neon_pmull(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer,
48  size_t length);
50 
51 enum {
52  CRC_C = 0,
53  PMULL_BE,
54  PMULL_LE,
55 };
56 
57 static const AVCRC crc_table_pmull[AV_CRC_MAX][17] = {
58  [AV_CRC_8_ATM] = {
59  PMULL_BE,
60  0xbc000000, 0x0, 0x32000000, 0x0,
61  0x94000000, 0x0, 0xc4000000, 0x0,
62  0x62000000, 0x0, 0x79000000, 0x0,
63  0x07156a16, 0x1, 0x07000000, 0x1,
64  },
65  [AV_CRC_8_EBU] = {
66  PMULL_BE,
67  0xf3000000, 0x0, 0xb5000000, 0x0,
68  0x0d000000, 0x0, 0xfc000000, 0x0,
69  0x6a000000, 0x0, 0x65000000, 0x0,
70  0x1c4b8192, 0x1, 0x1d000000, 0x1,
71  },
72  [AV_CRC_16_ANSI] = {
73  PMULL_BE,
74  0x807d0000, 0x0, 0xf9e30000, 0x0,
75  0xff830000, 0x0, 0xf9130000, 0x0,
76  0x807b0000, 0x0, 0x86630000, 0x0,
77  0xfffbffe7, 0x1, 0x80050000, 0x1,
78  },
79  [AV_CRC_16_CCITT] = {
80  PMULL_BE,
81  0x59b00000, 0x0, 0x60190000, 0x0,
82  0x45630000, 0x0, 0xd5f60000, 0x0,
83  0xaa510000, 0x0, 0xeb230000, 0x0,
84  0x11303471, 0x1, 0x10210000, 0x1,
85  },
86  [AV_CRC_24_IEEE] = {
87  PMULL_BE,
88  0x467d2400, 0x0, 0x1f428700, 0x0,
89  0x64e4d700, 0x0, 0x2c8c9d00, 0x0,
90  0xd9fe8c00, 0x0, 0xfd7e0c00, 0x0,
91  0xf845fe24, 0x1, 0x864cfb00, 0x1,
92  },
93  [AV_CRC_32_IEEE] = {
94  PMULL_BE,
95  0xe6228b11, 0x0, 0x8833794c, 0x0,
96  0xe8a45605, 0x0, 0xc5b9cd4c, 0x0,
97  0x490d678d, 0x0, 0xf200aa66, 0x0,
98  0x04d101df, 0x1, 0x04c11db7, 0x1,
99  },
100  [AV_CRC_32_IEEE_LE] = {
101  PMULL_LE,
102  0x54442bd4, 0x1, 0xc6e41596, 0x1,
103  0x751997d0, 0x1, 0xccaa009e, 0x0,
104  0xccaa009e, 0x0, 0x63cd6124, 0x1,
105  0xf7011640, 0x1, 0xdb710641, 0x1,
106  },
107  [AV_CRC_16_ANSI_LE] = {
108  PMULL_LE,
109  0x1b0c2, 0x0, 0x0000bffa, 0x0,
110  0x1d0c2, 0x0, 0x00018cc2, 0x0,
111  0x00018cc2, 0x0, 0x1bc02, 0x0,
112  0xcfffbffe, 0x1, 0x14003, 0x0,
113  },
114 };
115 
116 
117 static inline void crc_init_aarch64(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
118 {
119  uint64_t poly_;
120  if (le) {
121  // convert the reversed representation to regular form
122  poly = reverse(poly, bits) >> 1;
123  }
124  // convert to 32 degree polynomial
125  poly_ = ((uint64_t)poly) << (32 - bits);
126 
127  uint64_t div;
128  uint8_t *dst = (uint8_t*)(ctx + 1);
129  if (le) {
130  ctx[0] = PMULL_LE;
131  AV_WN64(dst + 0, xnmodp(4 * 128 + 32, poly_, 32, &div, le));
132  AV_WN64(dst + 8, xnmodp(4 * 128 - 32, poly_, 32, &div, le));
133  uint64_t tmp = xnmodp(128 - 32, poly_, 32, &div, le);
134  AV_WN64(dst + 16, xnmodp(128 + 32, poly_, 32, &div, le));
135  AV_WN64(dst + 24, tmp);
136  AV_WN64(dst + 32, tmp);
137  AV_WN64(dst + 40, xnmodp(64, poly_, 32, &div, le));
138  AV_WN64(dst + 48, div);
139  AV_WN64(dst + 56, reverse(poly_ | (1ULL << 32), 32));
140  } else {
141  ctx[0] = PMULL_BE;
142  AV_WN64(dst + 0, xnmodp(4 * 128, poly_, 32, &div, le));
143  AV_WN64(dst + 8, xnmodp(4 * 128 + 64, poly_, 32, &div, le));
144  AV_WN64(dst + 16, xnmodp(128, poly_, 32, &div, le));
145  AV_WN64(dst + 24, xnmodp(128 + 64, poly_, 32, &div, le));
146  AV_WN64(dst + 32, xnmodp(64, poly_, 32, &div, le));
147  AV_WN64(dst + 48, div);
148  AV_WN64(dst + 40, xnmodp(96, poly_, 32, &div, le));
149  AV_WN64(dst + 56, poly_ | (1ULL << 32));
150  }
151 }
152 #endif
153 
154 static inline av_cold int ff_crc_init_aarch64(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
155 {
156 #if HAVE_PMULL && HAVE_EOR3
157  int cpu_flags = av_get_cpu_flags();
158 
160  crc_init_aarch64(ctx, le, bits, poly, ctx_size);
161  return 1;
162  }
163 #endif
164  return 0;
165 }
166 
167 static inline uint32_t ff_crc_aarch64(const AVCRC *ctx, uint32_t crc,
168  const uint8_t *buffer, size_t length)
169 {
170  switch (ctx[0]) {
171 #if HAVE_PMULL && HAVE_EOR3
172  case PMULL_BE: return ff_crc_neon_pmull(ctx, crc, buffer, length);
173  case PMULL_LE: return ff_crc_le_neon_pmull(ctx, crc, buffer, length);
174 #endif
175 #if HAVE_ARM_CRC
176  case (AV_CRC_32_IEEE_LE + 1): return ff_crc32_aarch64(ctx, crc, buffer, length);
177 #endif
178  default: av_unreachable("AARCH64 has PMULL_LE, PMULL_BE and AV_CRC_32_IEEE_LE arch-specific CRC code");
179  }
180  return 0;
181 }
182 
183 static inline const AVCRC *ff_crc_get_table_aarch64(AVCRCId crc_id)
184 {
185  int cpu_flags = av_get_cpu_flags();
186 #if HAVE_PMULL && HAVE_EOR3
188  return crc_table_pmull[crc_id];
189  }
190 #endif
191 #if HAVE_ARM_CRC
192  static const AVCRC crc32_ieee_le_ctx[] = {
194  };
195 
196  if (crc_id != AV_CRC_32_IEEE_LE)
197  return NULL;
198 
199  if (have_arm_crc(cpu_flags)) {
200  return crc32_ieee_le_ctx;
201  }
202 #endif
203  return NULL;
204 }
205 
206 #endif /* AVUTIL_AARCH64_CRC_H */
FF_VISIBILITY_PUSH_HIDDEN
#define FF_VISIBILITY_PUSH_HIDDEN
Definition: attributes_internal.h:30
AV_CRC_8_EBU
@ AV_CRC_8_EBU
Definition: crc.h:56
AVCRC
uint32_t AVCRC
Definition: crc.h:46
xnmodp
static uint64_t xnmodp(unsigned n, uint64_t poly, unsigned deg, uint64_t *div, int bitreverse)
Definition: crc_internal.h:38
AV_CRC_24_IEEE
@ AV_CRC_24_IEEE
Definition: crc.h:55
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
crc.h
ff_crc_aarch64
static uint32_t ff_crc_aarch64(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Definition: crc.h:167
AV_CRC_16_ANSI_LE
@ AV_CRC_16_ANSI_LE
Definition: crc.h:54
avassert.h
av_cold
#define av_cold
Definition: attributes.h:111
attributes_internal.h
intreadwrite.h
bits
uint8_t bits
Definition: vp3data.h:128
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
crc_internal.h
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:116
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
have_pmull
#define have_pmull(flags)
Definition: cpu.h:29
FF_VISIBILITY_POP_HIDDEN
#define FF_VISIBILITY_POP_HIDDEN
Definition: attributes_internal.h:31
have_arm_crc
#define have_arm_crc(flags)
Definition: cpu.h:28
AV_CRC_16_CCITT
@ AV_CRC_16_CCITT
Definition: crc.h:51
AV_CRC_32_IEEE
@ AV_CRC_32_IEEE
Definition: crc.h:52
ff_crc_init_aarch64
static av_cold int ff_crc_init_aarch64(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
Definition: crc.h:154
ff_crc_get_table_aarch64
static const AVCRC * ff_crc_get_table_aarch64(AVCRCId crc_id)
Definition: crc.h:183
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
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
have_eor3
#define have_eor3(flags)
Definition: cpu.h:30
AV_WN64
#define AV_WN64(p, v)
Definition: intreadwrite.h:376
reverse
static uint64_t reverse(uint64_t p, unsigned int deg)
Definition: crc_internal.h:25
cpu.h