aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/tests/bitstream_template.c
blob: ef59845154daee4caae7a8141a8fe6631f396edc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * cached bitstream reader test
 * copyright (c) 2022 Anton Khirnov <anton@khirnov.net>
 *
 * 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
 */

#define ASSERT_LEVEL 2

#include "libavutil/avassert.h"
#include "libavutil/lfg.h"
#include "libavutil/random_seed.h"

#include "libavcodec/bitstream.h"
#include "libavcodec/defs.h"

#ifdef BITSTREAM_LE
#define BITSTREAM_WRITER_LE
#endif
#include "libavcodec/put_bits.h"

#define SIZE 157

enum Op {
    OP_READ,
    OP_READ_NZ,
    OP_READ_BIT,
    OP_READ_63,
    OP_READ_64,
    OP_READ_SIGNED,
    OP_READ_SIGNED_NZ,
    OP_APPLY_SIGN,
    OP_ALIGN,
    OP_NB,
};

int main(int argc, char **argv)
{
    BitstreamContext bc;
    PutBitContext    pb;
    AVLFG            lfg;

    uint8_t buf[SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
    uint8_t dst[SIZE + AV_INPUT_BUFFER_PADDING_SIZE];

    uint32_t random_seed;
    uint64_t val, val1;
    int32_t  sval, sval1;
    unsigned count;

    /* generate random input, using a given or random seed */
    if (argc > 1)
        random_seed = strtoul(argv[1], NULL, 0);
    else
        random_seed = av_get_random_seed();

    fprintf(stderr, "Testing with LFG seed: %"PRIu32"\n", random_seed);
    av_lfg_init(&lfg, random_seed);

    for (unsigned i = 0; i < SIZE; i++)
        buf[i] = av_lfg_get(&lfg);

    bits_init8   (&bc, buf, SIZE);
    init_put_bits(&pb, dst, SIZE);

    /* use a random sequence of bitreading operations to transfer data
     * from BitstreamContext to PutBitContext */
    while (bits_left(&bc) > 0) {
        enum Op op = av_lfg_get(&lfg) % OP_NB;

        switch (op) {
        case OP_READ:
            count = av_lfg_get(&lfg) % FFMIN(33, bits_left(&bc) + 1);
            val1  = bits_peek(&bc, count);
            val   = bits_read(&bc, count);

            fprintf(stderr, "%d read %u: %"PRIu64"\n", bits_tell(&bc) - count, count, val);

            av_assert0(val == val1);

            put_bits64(&pb, count, val);
            break;
        case OP_READ_NZ:
            count = av_lfg_get(&lfg) % FFMIN(33, bits_left(&bc) + 1);
            count = FFMAX(count, 1);
            val1  = bits_peek_nz(&bc, count);
            val   = bits_read_nz(&bc, count);

            fprintf(stderr, "%d read_nz %u: %"PRIu64"\n", bits_tell(&bc) - count, count, val);

            av_assert0(val == val1);

            put_bits64(&pb, count, val);
            break;
        case OP_READ_BIT:
            val = bits_read_bit(&bc);

            fprintf(stderr, "%d read_bit: %"PRIu64"\n", bits_tell(&bc) - 1, val);

            put_bits(&pb, 1, val);
            break;
        case OP_READ_63:
            count = av_lfg_get(&lfg) % FFMIN(64, bits_left(&bc) + 1);
            val   = bits_read_63(&bc, count);

            fprintf(stderr, "%d read_63 %u: %"PRIu64"\n", bits_tell(&bc) - count, count, val);

            put_bits64(&pb, count, val);
            break;
        case OP_READ_64:
            count = av_lfg_get(&lfg) % FFMIN(65, bits_left(&bc) + 1);
            val   = bits_read_64(&bc, count);

            fprintf(stderr, "%d read_64 %u: %"PRIu64"\n", bits_tell(&bc) - count, count, val);

            put_bits64(&pb, count, val);
            break;
        case OP_READ_SIGNED:
            count = av_lfg_get(&lfg) % FFMIN(33, bits_left(&bc) + 1);
            sval1 = bits_peek_signed(&bc, count);
            sval  = bits_read_signed(&bc, count);

            fprintf(stderr, "%d read_signed %u: %"PRId32"\n", bits_tell(&bc) - count, count, sval);

            av_assert0(sval == sval1);

            if (count == 32) put_bits32(&pb, sval);
            else             put_sbits(&pb, count, sval);
            break;
        case OP_READ_SIGNED_NZ:
            count = av_lfg_get(&lfg) % FFMIN(33, bits_left(&bc) + 1);
            count = FFMAX(count, 1);
            sval1 = bits_peek_signed_nz(&bc, count);
            sval  = bits_read_signed_nz(&bc, count);

            fprintf(stderr, "%d read_signed_nz %u: %"PRId32"\n", bits_tell(&bc) - count, count, sval);

            av_assert0(sval == sval1);

            if (count == 32) put_bits32(&pb, sval);
            else             put_sbits(&pb, count, sval);
            break;
        case OP_ALIGN:
            count = (bits_tell(&bc) + 7) / 8 * 8 - bits_tell(&bc);

            fprintf(stderr, "%d align %u\n", bits_tell(&bc), count);

            put_bits(&pb, count, bits_peek(&bc, count));
            bits_align(&bc);
            break;
        case OP_APPLY_SIGN:
            if (bits_left(&bc) < 2)
                continue;

            count = av_lfg_get(&lfg) % FFMIN(32, bits_left(&bc));
            count = FFMAX(count, 1);

            if (!bits_peek(&bc, count))
                continue;

            val   = bits_read(&bc, count);
            sval  = bits_apply_sign(&bc, val);

            fprintf(stderr, "%d apply_sign %u %"PRId32"\n",
                    bits_tell(&bc) - count - 1, count, sval);

            put_bits64(&pb, count, FFABS(sval));
            put_bits(&pb, 1, sval < 0);

            break;
        default:
            av_assert0(0);
        }
    }

    flush_put_bits(&pb);

    for (unsigned i = 0; i < SIZE; i++)
        if (buf[i] != dst[i]) {
            fprintf(stderr, "Mismatch at byte %u: %hhu %hhu; seed %"PRIu32"\n",
                    i, buf[i], dst[i], random_seed);
            return 1;
        }

    return 0;
}