FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/tests/checkasm/crc.c
Date: 2026-09-02 11:02:20
Exec Total Coverage
Lines: 38 40 95.0%
Functions: 3 3 100.0%
Branches: 19 40 47.5%

Line Branch Exec Source
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.h"
35 #include "libavutil/mem_internal.h"
36
37 enum {
38 BUF_SIZE = 16384,
39 };
40
41 typedef struct CustomTest {
42 struct CustomTest *prev;
43 AVCRC ctx[1024];
44 } CustomTest;
45
46 static CustomTest *ctx_list = NULL;
47
48 1 void checkasm_uninit_crc(void)
49 {
50
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (CustomTest *cur = ctx_list; cur;) {
51 2 CustomTest *prev = cur->prev;
52 2 av_free(cur);
53 2 cur = prev;
54 }
55 1 ctx_list = NULL;
56 1 }
57
58 126 static void check_crc(const AVCRC *table_new, const char *name,
59 size_t size, size_t offset)
60 {
61 126 declare_func(uint32_t, const AVCRC *ctx, uint32_t crc,
62 const uint8_t *buffer, size_t length);
63 126 const AVCRC *table_ref = (const AVCRC *) check_key((CheckasmKey) table_new, "crc_%s", name);
64
65
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 18 times.
126 if (!table_ref)
66 108 return;
67
68 DECLARE_ALIGNED(4, uint8_t, buf)[BUF_SIZE];
69 18 uint32_t prev_crc = rnd();
70
71
2/2
✓ Branch 0 taken 73728 times.
✓ Branch 1 taken 18 times.
73746 for (size_t j = 0; j < sizeof(buf); j += 4)
72 73728 AV_WN32A(buf + j, rnd());
73
74
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 uint32_t crc_ref = checkasm_call (av_crc, table_ref, prev_crc, buf + offset, size);
75 18 uint32_t crc_new = checkasm_call_checked(av_crc, table_new, prev_crc, buf + offset, size);
76
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (crc_ref != crc_new)
78 fail();
79
80
1/18
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 54 not taken.
✗ Branch 55 not taken.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
18 bench(av_crc, table_new, prev_crc, buf + offset, size);
81 }
82
83 14 void checkasm_check_crc(void)
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
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 14 times.
140 for (size_t i = 0; i < FF_ARRAY_ELEMS(offsets); ++i) {
101 126 offsets[i] = rnd() & 31;
102 126 sizes[i] = rnd() % (BUF_SIZE - 1 - offsets[i]);
103 }
104 14 le = rnd() & 1;
105 14 bits = 8 + rnd() % 25; // av_crc_init() accepts between 8 and 32 bits
106 14 poly = rnd() >> (32 - bits);
107
108
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 14 times.
126 for (unsigned i = 0; i < AV_CRC_MAX; ++i)
109 112 check_crc(av_crc_get_table(i), tests[i], sizes[i], offsets[i]);
110
111 14 struct CustomTest *new = av_mallocz(sizeof(*new));
112
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!new)
114 fail();
115
116
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 av_assert0(av_crc_init(new->ctx, le, bits, poly, sizeof(new->ctx)) >= 0);
117
4/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 1 times.
14 if (ctx_list && !memcmp(ctx_list->ctx, new->ctx, sizeof(new->ctx))) {
118 12 av_free(new);
119 } else {
120 2 new->prev = ctx_list;
121 2 ctx_list = new;
122 }
123
124 14 check_crc(ctx_list->ctx, "custom_polynomial",
125 sizes[AV_CRC_MAX], offsets[AV_CRC_MAX]);
126 14 report("crc");
127 14 }
128