summaryrefslogtreecommitdiff
path: root/libavcodec/golomb_bs.h
blob: bba126bfc4b6821c6910860227765fafb881ae85 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * 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
 */

#ifndef AVCODEC_GOLOMB_BS_H
#define AVCODEC_GOLOMB_BS_H

#include <stdint.h>

#include "bitstream.h"
#include "golomb_common.h"

/**
 * Read an unsigned Exp-Golomb code in the range 0 to 8190.
 *
 * @returns the read value or a negative error code.
 */
static inline int golomb_bs_get_ue(BitstreamContext *bc)
{
    unsigned int buf = bitstream_peek(bc, 32);

    if (buf >= (1 << 27)) {
        buf >>= 32 - 9;
        bitstream_skip_cache(bc, ff_golomb_vlc_len[buf]);

        return ff_ue_golomb_vlc_code[buf];
    } else {
        int log = 2 * av_log2(buf) - 31;

        bitstream_skip_cache(bc, 32 - log);
        if (log < 7)
            return AVERROR_INVALIDDATA;
        buf >>= log;
        buf--;

        return buf;
    }
}

/**
 * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
 */
static inline unsigned golomb_bs_get_ue_long(BitstreamContext *bc)
{
    unsigned int buf = bitstream_peek(bc, 32);
    unsigned int log = 31 - av_log2(buf);
    bitstream_skip_cache(bc, log);
    return bitstream_read(bc, log + 1) - 1;
}

/**
 * read unsigned exp golomb code, constraint to a max of 31.
 * If the value encountered is not in 0..31, the return value
 * is outside the range 0..30.
 */
static inline int golomb_bs_get_ue_31(BitstreamContext *bc)
{
    unsigned int buf = bitstream_peek(bc, 32);

    buf >>= 32 - 9;
    bitstream_skip_cache(bc, ff_golomb_vlc_len[buf]);

    return ff_ue_golomb_vlc_code[buf];
}

static inline unsigned golomb_bs_get_ue_interleaved(BitstreamContext *bc)
{
    uint32_t buf = bitstream_peek(bc, 32);

    if (buf & 0xAA800000) {
        buf >>= 32 - 8;
        bitstream_skip_cache(bc, ff_interleaved_golomb_vlc_len[buf]);

        return ff_interleaved_ue_golomb_vlc_code[buf];
    } else {
        unsigned ret = 1;

        do {
            buf >>= 32 - 8;
            bitstream_skip_cache(bc, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));

            if (ff_interleaved_golomb_vlc_len[buf] != 9) {
                ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
                ret  |= ff_interleaved_dirac_golomb_vlc_code[buf];
                break;
            }
            ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
            buf = bitstream_peek(bc, 32);
        } while (bitstream_bits_left(bc) > 0);

        return ret - 1;
    }
}

/**
 * read unsigned truncated exp golomb code.
 */
static inline int golomb_bs_get_te0(BitstreamContext *bc, int range)
{
    av_assert2(range >= 1);

    if (range == 1)
        return 0;
    else if (range == 2)
        return bitstream_read(bc, 1) ^ 1;
    else
        return golomb_bs_get_ue(bc);
}

/**
 * read unsigned truncated exp golomb code.
 */
static inline int golomb_bs_get_te(BitstreamContext *bc, int range)
{
    av_assert2(range >= 1);

    if (range == 2)
        return bitstream_read(bc, 1) ^ 1;
    else
        return golomb_bs_get_ue(bc);
}

/**
 * read signed exp golomb code.
 */
static inline int golomb_bs_get_se(BitstreamContext *bc)
{
    unsigned int buf;

    buf = bitstream_peek(bc, 32);

    if (buf >= (1 << 27)) {
        buf >>= 32 - 9;
        bitstream_skip_cache(bc, ff_golomb_vlc_len[buf]);

        return ff_se_golomb_vlc_code[buf];
    } else {
        int log = 2 * av_log2(buf) - 31;
        buf >>= log;

        bitstream_skip_cache(bc, 32 - log);

        if (buf & 1)
            buf = -(buf >> 1);
        else
            buf = (buf >> 1);

        return buf;
    }
}

static inline int golomb_bs_get_se_long(BitstreamContext *bc)
{
    unsigned int buf = golomb_bs_get_ue_long(bc);
    int sign = (buf & 1) - 1;
    return ((buf >> 1) ^ sign) + 1;
}

static inline int golomb_bs_get_se_interleaved(BitstreamContext *bc)
{
    unsigned int buf;

    buf = bitstream_peek(bc, 32);

    if (buf & 0xAA800000) {
        buf >>= 32 - 8;
        bitstream_skip_cache(bc, ff_interleaved_golomb_vlc_len[buf]);

        return ff_interleaved_se_golomb_vlc_code[buf];
    } else {
        int log;
        bitstream_skip_cache(bc, 8);
        buf |= 1 | bitstream_peek(bc, 24);

        if ((buf & 0xAAAAAAAA) == 0)
            return INVALID_VLC;

        for (log = 31; (buf & 0x80000000) == 0; log--)
            buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);

        bitstream_skip(bc, 63 - 2 * log - 8);

        return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
    }
}

static inline int golomb_bs_get_se_dirac(BitstreamContext *bc)
{
    uint32_t ret = golomb_bs_get_ue_interleaved(bc);

    if (ret) {
        int sign = -bitstream_read(bc, 1);
        ret = (ret ^ sign) - sign;
    }

    return ret;
}

/**
 * read unsigned golomb rice code (ffv1).
 */
static inline int golomb_bs_get_ur(BitstreamContext *bc, int k, int limit,
                                   int esc_len)
{
    unsigned int buf;
    int log;

    buf = bitstream_peek(bc, 32);

    log = av_log2(buf);

    if (log > 31 - limit) {
        buf >>= log - k;
        buf  += (30 - log) << k;
        bitstream_skip(bc, 32 + k - log);

        return buf;
    } else {
        bitstream_skip(bc, limit);
        buf = bitstream_read(bc, esc_len);

        return buf + limit - 1;
    }
}

/**
 * read unsigned golomb rice code (jpegls).
 */
static inline int golomb_bs_get_ur_jpegls(BitstreamContext *bc, int k, int limit,
                                          int esc_len)
{
    unsigned int buf;
    int log;

    buf = bitstream_peek(bc, 32);

    log = av_log2(buf);

    if (log - k >= 1 && 32 - log < limit) {
        buf >>= log - k;
        buf  += (30 - log) << k;
        bitstream_skip(bc, 32 + k - log);

        return buf;
    } else {
        int i;
        for (i = 0;
             i < limit && bitstream_peek(bc, 1) == 0 && bitstream_bits_left(bc) > 0;
             i++)
            bitstream_skip_cache(bc, 1);
        bitstream_skip(bc, 1);

        if (i < limit - 1) {
            buf = bitstream_read(bc, k);

            return buf + (i << k);
        } else if (i == limit - 1) {
            buf = bitstream_read(bc, esc_len);

            return buf + 1;
        } else
            return -1;
    }
}

/**
 * read signed golomb rice code (ffv1).
 */
static inline int golomb_bs_get_sr(BitstreamContext *bc, int k, int limit,
                                   int esc_len)
{
    unsigned v = golomb_bs_get_ur(bc, k, limit, esc_len);
    return (v >> 1) ^ -(v & 1);
}

/**
 * read signed golomb rice code (flac).
 */
static inline int golomb_bs_get_sr_flac(BitstreamContext *bc, int k, int limit,
                                        int esc_len)
{
    unsigned v = golomb_bs_get_ur_jpegls(bc, k, limit, esc_len);
    return (v >> 1) ^ -(v & 1);
}

/**
 * read unsigned golomb rice code (shorten).
 */
static inline unsigned int golomb_bs_get_ur_shorten(BitstreamContext *bc, int k)
{
    return golomb_bs_get_ur_jpegls(bc, k, INT_MAX, 0);
}

/**
 * read signed golomb rice code (shorten).
 */
static inline int golomb_bs_get_sr_shorten(BitstreamContext *bc, int k)
{
    int uvar = golomb_bs_get_ur_jpegls(bc, k + 1, INT_MAX, 0);
    return (uvar >> 1) ^ -(uvar & 1);
}

#endif /* AVCODEC_GOLOMB_BS_H */