[FFmpeg-devel] [RFC][PATCH 1/3] lavu: Add av_crc2() and av_crc2_init()
James Almer
jamrial at gmail.com
Mon Jan 28 08:31:53 CET 2013
Functionally the same as av_crc() and av_crc_init(), but
supporting CRCs of up to 64 bits.
Signed-off-by: James Almer <jamrial at gmail.com>
---
libavutil/crc.c | 75 ++++++++++++------------------------------
libavutil/crc.h | 7 ++++
libavutil/crc_template.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 112 insertions(+), 55 deletions(-)
create mode 100644 libavutil/crc_template.c
diff --git a/libavutil/crc.c b/libavutil/crc.c
index aeb9e46..738496e 100644
--- a/libavutil/crc.c
+++ b/libavutil/crc.c
@@ -40,39 +40,6 @@ static struct {
static AVCRC av_crc_table[AV_CRC_MAX][257];
#endif
-int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
-{
- unsigned i, j;
- uint32_t c;
-
- if (bits < 8 || bits > 32 || poly >= (1LL << bits))
- return -1;
- if (ctx_size != sizeof(AVCRC) * 257 && ctx_size != sizeof(AVCRC) * 1024)
- return -1;
-
- for (i = 0; i < 256; i++) {
- if (le) {
- for (c = i, j = 0; j < 8; j++)
- c = (c >> 1) ^ (poly & (-(c & 1)));
- ctx[i] = c;
- } else {
- for (c = i << 24, j = 0; j < 8; j++)
- c = (c << 1) ^ ((poly << (32 - bits)) & (((int32_t) c) >> 31));
- ctx[i] = av_bswap32(c);
- }
- }
- ctx[256] = 1;
-#if !CONFIG_SMALL
- if (ctx_size >= sizeof(AVCRC) * 1024)
- for (i = 0; i < 256; i++)
- for (j = 0; j < 3; j++)
- ctx[256 *(j + 1) + i] =
- (ctx[256 * j + i] >> 8) ^ ctx[ctx[256 * j + i] & 0xFF];
-#endif
-
- return 0;
-}
-
const AVCRC *av_crc_get_table(AVCRCId crc_id)
{
#if !CONFIG_HARDCODED_TABLES
@@ -87,30 +54,28 @@ const AVCRC *av_crc_get_table(AVCRCId crc_id)
return av_crc_table[crc_id];
}
-uint32_t av_crc(const AVCRC *ctx, uint32_t crc,
- const uint8_t *buffer, size_t length)
-{
- const uint8_t *end = buffer + length;
+#define CRC2 1
+#define CRC_LC(a) av_crc2 ## a
+#define CRC_BITS 64
+#define CRC_TYPE int64_t
+#define CRC_UTYPE uint64_t
+#define AVCRCX AVCRC2
+#include "crc_template.c"
-#if !CONFIG_SMALL
- if (!ctx[256]) {
- while (((intptr_t) buffer & 3) && buffer < end)
- crc = ctx[((uint8_t) crc) ^ *buffer++] ^ (crc >> 8);
+#undef CRC2
+#undef CRC_LC
+#undef CRC_BITS
+#undef CRC_TYPE
+#undef CRC_UTYPE
+#undef AVCRCX
- while (buffer < end - 3) {
- crc ^= av_le2ne32(*(const uint32_t *) buffer); buffer += 4;
- crc = ctx[3 * 256 + ( crc & 0xFF)] ^
- ctx[2 * 256 + ((crc >> 8 ) & 0xFF)] ^
- ctx[1 * 256 + ((crc >> 16) & 0xFF)] ^
- ctx[0 * 256 + ((crc >> 24) )];
- }
- }
-#endif
- while (buffer < end)
- crc = ctx[((uint8_t) crc) ^ *buffer++] ^ (crc >> 8);
-
- return crc;
-}
+#define CRC2 0
+#define CRC_LC(a) av_crc ## a
+#define CRC_BITS 32
+#define CRC_TYPE int32_t
+#define CRC_UTYPE uint32_t
+#define AVCRCX AVCRC
+#include "crc_template.c"
#ifdef TEST
int main(void)
diff --git a/libavutil/crc.h b/libavutil/crc.h
index 2bdfca8..362a11c 100644
--- a/libavutil/crc.h
+++ b/libavutil/crc.h
@@ -36,6 +36,8 @@ typedef enum {
AV_CRC_MAX, /*< Not part of public API! Do not use outside libavutil. */
}AVCRCId;
+typedef uint64_t AVCRC2;
+
/**
* Initialize a CRC table.
* @param ctx must be an array of size sizeof(AVCRC)*257 or sizeof(AVCRC)*1024
@@ -54,6 +56,8 @@ typedef enum {
*/
int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size);
+int av_crc2_init(AVCRC2 *ctx, int le, int bits, uint64_t poly, int ctx_size);
+
/**
* Get an initialized standard CRC table.
* @param crc_id ID of a standard CRC
@@ -71,4 +75,7 @@ const AVCRC *av_crc_get_table(AVCRCId crc_id);
uint32_t av_crc(const AVCRC *ctx, uint32_t crc,
const uint8_t *buffer, size_t length) av_pure;
+uint64_t av_crc2(const AVCRC2 *ctx, uint64_t crc,
+ const uint8_t *buffer, size_t length) av_pure;
+
#endif /* AVUTIL_CRC_H */
diff --git a/libavutil/crc_template.c b/libavutil/crc_template.c
new file mode 100644
index 0000000..4fb4a95
--- /dev/null
+++ b/libavutil/crc_template.c
@@ -0,0 +1,85 @@
+/*
+ * copyright (c) 2006 Michael Niedermayer <michaelni at gmx.at>
+ *
+ * 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
+ */
+
+int CRC_LC(_init)(AVCRCX *ctx, int le, int bits, CRC_UTYPE poly, int ctx_size)
+{
+ unsigned j;
+ CRC_UTYPE i, c;
+
+ if (bits < 8 || bits > CRC_BITS || (bits < CRC_BITS && poly >= (1LL << bits)))
+ return -1;
+
+ if (ctx_size != sizeof(AVCRCX) * 257 && ctx_size != sizeof(AVCRCX) * 1024)
+ return -1;
+
+ for (i = 0; i < 256; i++) {
+ if (le) {
+ for (c = i, j = 0; j < 8; j++)
+ c = (c >> 1) ^ (poly & (-(c & 1)));
+ ctx[i] = c;
+ } else {
+ for (c = i << (CRC_BITS - 8), j = 0; j < 8; j++)
+ c = (c << 1) ^ ((poly << (CRC_BITS - bits)) & (((CRC_TYPE) c) >> (CRC_BITS - 1)));
+#if CRC2
+ ctx[i] = av_bswap64(c);
+#else
+ ctx[i] = av_bswap32(c);
+#endif
+ }
+ }
+ ctx[256] = 1;
+#if !CONFIG_SMALL
+ if (ctx_size >= sizeof(AVCRCX) * 1024)
+ for (i = 0; i < 256; i++)
+ for (j = 0; j < 3; j++)
+ ctx[256 *(j + 1) + i] =
+ (ctx[256 * j + i] >> 8) ^ ctx[ctx[256 * j + i] & 0xFF];
+#endif
+
+ return 0;
+}
+
+CRC_UTYPE CRC_LC()(const AVCRCX *ctx, CRC_UTYPE crc,
+ const uint8_t *buffer, size_t length)
+{
+ const uint8_t *end = buffer + length;
+
+#if !CONFIG_SMALL
+ if (!ctx[256]) {
+ while (((intptr_t) buffer & 3) && buffer < end)
+ crc = ctx[((uint8_t) crc) ^ *buffer++] ^ (crc >> 8);
+
+ while (buffer < end - 3) {
+ crc ^= av_le2ne32(*(const uint32_t *) buffer); buffer += 4;
+ crc = ctx[3 * 256 + ( crc & 0xFF)] ^
+ ctx[2 * 256 + ((crc >> 8 ) & 0xFF)] ^
+#if CRC2
+ (crc >> 32) ^
+#endif
+ ctx[1 * 256 + ((crc >> 16) & 0xFF)] ^
+ ctx[0 * 256 + ((crc >> 24) & 0xFF)];
+ }
+ }
+#endif
+ while (buffer < end)
+ crc = ctx[((uint8_t) crc) ^ *buffer++] ^ (crc >> 8);
+
+ return crc;
+}
--
1.7.12.4
More information about the ffmpeg-devel
mailing list