FFmpeg
Loading...
Searching...
No Matches
crc.c
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 modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (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
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#include <assert.h>
20#include <stddef.h>
21#include <stdint.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include "checkasm.h"
27// Undefine av_pure so that calls to av_crc are not optimized away.
28#undef av_pure
29#define av_pure
30#include "libavutil/avassert.h"
31#include "libavutil/crc.h"
33#include "libavutil/macros.h"
34#include "libavutil/mem.h"
36
37enum {
38 BUF_SIZE = 16384,
39};
40
41typedef struct CustomTest {
43 AVCRC ctx[1024];
45
47
49{
50 for (CustomTest *cur = ctx_list; cur;) {
51 CustomTest *prev = cur->prev;
52 av_free(cur);
53 cur = prev;
54 }
55 ctx_list = NULL;
56}
57
58static void check_crc(const AVCRC *table_new, const char *name,
59 size_t size, size_t offset)
60{
61 declare_func(uint32_t, const AVCRC *ctx, uint32_t crc,
62 const uint8_t *buffer, size_t length);
63 const AVCRC *table_ref = (const AVCRC *) check_key((CheckasmKey) table_new, "crc_%s", name);
64
65 if (!table_ref)
66 return;
67
68 DECLARE_ALIGNED(4, uint8_t, buf)[BUF_SIZE];
69 uint32_t prev_crc = rnd();
70
71 for (size_t j = 0; j < sizeof(buf); j += 4)
72 AV_WN32A(buf + j, rnd());
73
74 uint32_t crc_ref = checkasm_call (av_crc, table_ref, prev_crc, buf + offset, size);
75 uint32_t crc_new = checkasm_call_checked(av_crc, table_new, prev_crc, buf + offset, size);
76
77 if (crc_ref != crc_new)
78 fail();
79
80 bench(av_crc, table_new, prev_crc, buf + offset, size);
81}
82
84{
85 static const char *const tests[] = {
86#define TEST(CRC) [AV_CRC_ ## CRC] = #CRC
87 TEST(8_ATM), TEST(8_EBU),
88 TEST(16_ANSI), TEST(16_ANSI_LE), TEST(16_CCITT),
89 TEST(24_IEEE), TEST(32_IEEE_LE), TEST(32_IEEE),
90 };
91 static_assert(FF_ARRAY_ELEMS(tests) == AV_CRC_MAX, "test needs to be added");
92
93 size_t offsets[AV_CRC_MAX + 1];
94 size_t sizes[AV_CRC_MAX + 1];
95 uint32_t poly;
96 int le, bits;
97
98 // Initialize parameters before any test so that different instruction sets
99 // use the same values.
100 for (size_t i = 0; i < FF_ARRAY_ELEMS(offsets); ++i) {
101 offsets[i] = rnd() & 31;
102 sizes[i] = rnd() % (BUF_SIZE - 1 - offsets[i]);
103 }
104 le = rnd() & 1;
105 bits = 8 + rnd() % 25; // av_crc_init() accepts between 8 and 32 bits
106 poly = rnd() >> (32 - bits);
107
108 for (unsigned i = 0; i < AV_CRC_MAX; ++i)
110
111 struct CustomTest *new = av_mallocz(sizeof(*new));
112
113 if (!new)
114 fail();
115
116 av_assert0(av_crc_init(new->ctx, le, bits, poly, sizeof(new->ctx)) >= 0);
117 if (ctx_list && !memcmp(ctx_list->ctx, new->ctx, sizeof(new->ctx))) {
118 av_free(new);
119 } else {
120 new->prev = ctx_list;
121 ctx_list = new;
122 }
123
124 check_crc(ctx_list->ctx, "custom_polynomial",
126 report("crc");
127}
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define bench(...)
Definition checkasm.h:138
#define rnd
Definition checkasm.h:136
#define NULL
Definition coverity.c:32
Public header for CRC hash function implementation.
uintptr_t CheckasmKey
Opaque type used to identify function implementations.
Definition checkasm.h:96
static const uint8_t bits[8]
Definition fastaudio.c:100
const TestCase tests[]
Definition fifo_muxer.c:363
#define declare_func
Definition test.h:489
#define fail
Definition test.h:479
#define check_key
Definition test.h:482
#define report
Definition test.h:480
int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
Initialize a CRC table.
Definition crc.c:346
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t AVCRC
Definition crc.h:46
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition crc.c:421
@ AV_CRC_MAX
Definition crc.h:57
static const int offsets[]
Definition hevc_pel.c:34
static const int sizes[][2]
Definition img2dec.c:62
#define AV_WN32A(p, v)
unsigned offset
Definition libaomenc.c:763
Macro definitions for various function/variable attributes.
Utility Preprocessor macros.
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
#define checkasm_call_checked(func,...)
Definition aarch64.h:93
const char * name
Definition qsvenc.c:142
#define BUF_SIZE
Definition setpts.c:159
#define FF_ARRAY_ELEMS(a)
AVCRC ctx[1024]
Definition crc.c:43
struct CustomTest * prev
Definition crc.c:42
#define av_free(p)
#define av_mallocz(s)
#define checkasm_call(func,...)
Call a function with signal handling.
Definition test.h:252
void checkasm_check_crc(void)
Definition crc.c:83
#define TEST(CRC)
static void check_crc(const AVCRC *table_new, const char *name, size_t size, size_t offset)
Definition crc.c:58
static CustomTest * ctx_list
Definition crc.c:46
void checkasm_uninit_crc(void)
Definition crc.c:48
static AVFormatContext * ctx
Definition movenc.c:49
static char buffer[20]
Definition seek.c:32
int size