FFmpeg
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"
26 #include "libavutil/attributes.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"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/macros.h"
34 #include "libavutil/mem_internal.h"
35 
36 
37 static void check_crc(const AVCRC *table_new, const char *name, unsigned idx)
38 {
39  declare_func(uint32_t, const AVCRC *ctx, uint32_t crc,
40  const uint8_t *buffer, size_t length);
41  const AVCRC *table_ref = check_key((AVCRC*)table_new, "crc_%s", name);
42 
43  if (!table_ref)
44  return;
45 
46  DECLARE_ALIGNED(4, uint8_t, buf)[8192];
47  size_t offset = rnd() & 31;
48  static size_t sizes[AV_CRC_MAX + 1];
49  static unsigned sizes_initialized = 0;
50  uint32_t prev_crc = rnd();
51 
52  if (!(sizes_initialized & (1 << idx))) {
53  sizes_initialized |= 1 << idx;
54  sizes[idx] = rnd() % (sizeof(buf) - 1 - offset);
55  }
56 
57  size_t size = sizes[idx];
58 
59  for (size_t j = 0; j < sizeof(buf); j += 4)
60  AV_WN32A(buf + j, rnd());
61 
62  uint32_t crc_ref = checkasm_call (av_crc, table_ref, prev_crc, buf + offset, size);
63  uint32_t crc_new = checkasm_call_checked(av_crc, table_new, prev_crc, buf + offset, size);
64 
65  if (crc_ref != crc_new)
66  fail();
67 
68  bench(av_crc, table_new, prev_crc, buf + offset, size);
69 }
70 
72 {
73  static const char *const tests[] = {
74 #define TEST(CRC) [AV_CRC_ ## CRC] = #CRC
75  TEST(8_ATM), TEST(8_EBU),
76  TEST(16_ANSI), TEST(16_ANSI_LE), TEST(16_CCITT),
77  TEST(24_IEEE), TEST(32_IEEE_LE), TEST(32_IEEE),
78  };
79  static_assert(FF_ARRAY_ELEMS(tests) == AV_CRC_MAX, "test needs to be added");
80 
81  for (unsigned i = 0; i < AV_CRC_MAX; ++i)
83 
84  static struct CustomTest {
85  struct CustomTest *prev;
86  AVCRC ctx[1024];
87  } *ctx = NULL;
88  struct CustomTest *new = malloc(sizeof(*new));
89  static int le, bits;
90  static uint32_t poly;
91 
92  if (!new)
93  fail();
94 
95  memset(new, 0, sizeof(*new));
96 
97  if (!ctx) {
98  le = rnd() & 1;
99  bits = 8 + rnd() % 25; // av_crc_init() accepts between 8 and 32 bits
100  poly = rnd() >> (32 - bits);
101  }
102  av_assert0(av_crc_init(new->ctx, le, bits, poly, sizeof(new->ctx)) >= 0);
103  if (ctx && !memcmp(ctx->ctx, new->ctx, sizeof(new->ctx))) {
104  free(new);
105  } else {
106  new->prev = ctx;
107  ctx = new;
108  }
109 
110  check_crc(ctx->ctx, "custom_polynomial", AV_CRC_MAX);
111  report("crc");
112 }
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
mem_internal.h
AVCRC
uint32_t AVCRC
Definition: crc.h:46
AV_WN32A
#define AV_WN32A(p, v)
Definition: intreadwrite.h:534
crc.h
macros.h
fail
#define fail()
Definition: checkasm.h:216
checkasm.h
av_crc_init
int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
Initialize a CRC table.
Definition: crc.c:346
avassert.h
rnd
#define rnd()
Definition: checkasm.h:199
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
intreadwrite.h
bits
uint8_t bits
Definition: vp3data.h:128
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
ctx
AVFormatContext * ctx
Definition: movenc.c:49
NULL
#define NULL
Definition: coverity.c:32
sizes
static const int sizes[][2]
Definition: img2dec.c:61
checkasm_check_crc
void checkasm_check_crc(void)
Definition: crc.c:71
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:104
size
int size
Definition: twinvq_data.h:10344
TEST
#define TEST(CRC)
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:385
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
attributes.h
tests
const TestCase tests[]
Definition: fifo_muxer.c:363
report
#define report
Definition: checkasm.h:219
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
checkasm_call_checked
#define checkasm_call_checked(func,...)
Definition: checkasm.h:331
check_crc
static void check_crc(const AVCRC *table_new, const char *name, unsigned idx)
Definition: crc.c:37
bench
#define bench(func,...)
Definition: checkasm.h:416
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
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:417
AV_CRC_MAX
@ AV_CRC_MAX
Definition: crc.h:57
check_key
#define check_key(key,...)
Definition: checkasm.h:207
declare_func
#define declare_func(ret,...)
Definition: checkasm.h:211
checkasm_call
#define checkasm_call(func,...)
Definition: checkasm.h:225