GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/golomb.h Lines: 207 218 95.0 %
Date: 2020-09-21 17:35:45 Branches: 54 62 87.1 %

Line Branch Exec Source
1
/*
2
 * exp golomb vlc stuff
3
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4
 * Copyright (c) 2004 Alex Beregszaszi
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
/**
24
 * @file
25
 * @brief
26
 *     exp golomb vlc stuff
27
 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
28
 */
29
30
#ifndef AVCODEC_GOLOMB_H
31
#define AVCODEC_GOLOMB_H
32
33
#include <stdint.h>
34
35
#include "get_bits.h"
36
#include "put_bits.h"
37
38
#define INVALID_VLC           0x80000000
39
40
extern const uint8_t ff_golomb_vlc_len[512];
41
extern const uint8_t ff_ue_golomb_vlc_code[512];
42
extern const  int8_t ff_se_golomb_vlc_code[512];
43
extern const uint8_t ff_ue_golomb_len[256];
44
45
extern const uint8_t ff_interleaved_golomb_vlc_len[256];
46
extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
47
extern const  int8_t ff_interleaved_se_golomb_vlc_code[256];
48
extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
49
50
/**
51
 * Read an unsigned Exp-Golomb code in the range 0 to 8190.
52
 *
53
 * @returns the read value or a negative error code.
54
 */
55
30357534
static inline int get_ue_golomb(GetBitContext *gb)
56
{
57
    unsigned int buf;
58
59
#if CACHED_BITSTREAM_READER
60
    buf = show_bits_long(gb, 32);
61
62
    if (buf >= (1 << 27)) {
63
        buf >>= 32 - 9;
64
        skip_bits_long(gb, ff_golomb_vlc_len[buf]);
65
66
        return ff_ue_golomb_vlc_code[buf];
67
    } else {
68
        int log = 2 * av_log2(buf) - 31;
69
70
        skip_bits_long(gb, 32 - log);
71
        if (log < 7)
72
            return AVERROR_INVALIDDATA;
73
        buf >>= log;
74
        buf--;
75
76
        return buf;
77
    }
78
#else
79
30357534
    OPEN_READER(re, gb);
80
30357534
    UPDATE_CACHE(re, gb);
81
30357534
    buf = GET_CACHE(re, gb);
82
83
30357534
    if (buf >= (1 << 27)) {
84
29286699
        buf >>= 32 - 9;
85
29286699
        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
86
29286699
        CLOSE_READER(re, gb);
87
88
29286699
        return ff_ue_golomb_vlc_code[buf];
89
    } else {
90
1070835
        int log = 2 * av_log2(buf) - 31;
91
1070835
        LAST_SKIP_BITS(re, gb, 32 - log);
92
1070835
        CLOSE_READER(re, gb);
93
1070835
        if (log < 7)
94
3
            return AVERROR_INVALIDDATA;
95
1070832
        buf >>= log;
96
1070832
        buf--;
97
98
1070832
        return buf;
99
    }
100
#endif
101
}
102
103
/**
104
 * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
105
 */
106
2840334
static inline unsigned get_ue_golomb_long(GetBitContext *gb)
107
{
108
    unsigned buf, log;
109
110
2840334
    buf = show_bits_long(gb, 32);
111
2840334
    log = 31 - av_log2(buf);
112
2840334
    skip_bits_long(gb, log);
113
114
2840334
    return get_bits_long(gb, log + 1) - 1;
115
}
116
117
/**
118
 * read unsigned exp golomb code, constraint to a max of 31.
119
 * If the value encountered is not in 0..31, the return value
120
 * is outside the range 0..30.
121
 */
122
5429891
static inline int get_ue_golomb_31(GetBitContext *gb)
123
{
124
    unsigned int buf;
125
126
#if CACHED_BITSTREAM_READER
127
    buf = show_bits_long(gb, 32);
128
129
    buf >>= 32 - 9;
130
    skip_bits_long(gb, ff_golomb_vlc_len[buf]);
131
#else
132
133
5429891
    OPEN_READER(re, gb);
134
5429891
    UPDATE_CACHE(re, gb);
135
5429891
    buf = GET_CACHE(re, gb);
136
137
5429891
    buf >>= 32 - 9;
138
5429891
    LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
139
5429891
    CLOSE_READER(re, gb);
140
#endif
141
142
5429891
    return ff_ue_golomb_vlc_code[buf];
143
}
144
145
13657861
static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb)
146
{
147
    uint32_t buf;
148
149
#if CACHED_BITSTREAM_READER
150
    buf = show_bits_long(gb, 32);
151
152
    if (buf & 0xAA800000) {
153
        buf >>= 32 - 8;
154
        skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]);
155
156
        return ff_interleaved_ue_golomb_vlc_code[buf];
157
    } else {
158
        unsigned ret = 1;
159
160
        do {
161
            buf >>= 32 - 8;
162
            skip_bits_long(gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
163
164
            if (ff_interleaved_golomb_vlc_len[buf] != 9) {
165
                ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
166
                ret  |= ff_interleaved_dirac_golomb_vlc_code[buf];
167
                break;
168
            }
169
            ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
170
            buf = show_bits_long(gb, 32);
171
        } while (get_bits_left(gb) > 0);
172
173
        return ret - 1;
174
    }
175
#else
176
13657861
    OPEN_READER(re, gb);
177
13657861
    UPDATE_CACHE(re, gb);
178
13657861
    buf = GET_CACHE(re, gb);
179
180
13657861
    if (buf & 0xAA800000) {
181
13489899
        buf >>= 32 - 8;
182
13489899
        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
183
13489899
        CLOSE_READER(re, gb);
184
185
13489899
        return ff_interleaved_ue_golomb_vlc_code[buf];
186
    } else {
187
167962
        unsigned ret = 1;
188
189
        do {
190
340518
            buf >>= 32 - 8;
191
340518
            LAST_SKIP_BITS(re, gb,
192
                           FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
193
194
340518
            if (ff_interleaved_golomb_vlc_len[buf] != 9) {
195
167962
                ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
196
167962
                ret  |= ff_interleaved_dirac_golomb_vlc_code[buf];
197
167962
                break;
198
            }
199
172556
            ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
200
172556
            UPDATE_CACHE(re, gb);
201
172556
            buf = GET_CACHE(re, gb);
202

172556
        } while (ret<0x8000000U && BITS_AVAILABLE(re, gb));
203
204
167962
        CLOSE_READER(re, gb);
205
167962
        return ret - 1;
206
    }
207
#endif
208
}
209
210
/**
211
 * read unsigned truncated exp golomb code.
212
 */
213
static inline int get_te0_golomb(GetBitContext *gb, int range)
214
{
215
    av_assert2(range >= 1);
216
217
    if (range == 1)
218
        return 0;
219
    else if (range == 2)
220
        return get_bits1(gb) ^ 1;
221
    else
222
        return get_ue_golomb(gb);
223
}
224
225
/**
226
 * read unsigned truncated exp golomb code.
227
 */
228
static inline int get_te_golomb(GetBitContext *gb, int range)
229
{
230
    av_assert2(range >= 1);
231
232
    if (range == 2)
233
        return get_bits1(gb) ^ 1;
234
    else
235
        return get_ue_golomb(gb);
236
}
237
238
/**
239
 * read signed exp golomb code.
240
 */
241
16332203
static inline int get_se_golomb(GetBitContext *gb)
242
{
243
    unsigned int buf;
244
245
#if CACHED_BITSTREAM_READER
246
    buf = show_bits_long(gb, 32);
247
248
    if (buf >= (1 << 27)) {
249
        buf >>= 32 - 9;
250
        skip_bits_long(gb, ff_golomb_vlc_len[buf]);
251
252
        return ff_se_golomb_vlc_code[buf];
253
    } else {
254
        int log = 2 * av_log2(buf) - 31;
255
        buf >>= log;
256
257
        skip_bits_long(gb, 32 - log);
258
259
        if (buf & 1)
260
            buf = -(buf >> 1);
261
        else
262
            buf = (buf >> 1);
263
264
        return buf;
265
    }
266
#else
267
16332203
    OPEN_READER(re, gb);
268
16332203
    UPDATE_CACHE(re, gb);
269
16332203
    buf = GET_CACHE(re, gb);
270
271
16332203
    if (buf >= (1 << 27)) {
272
15541553
        buf >>= 32 - 9;
273
15541553
        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
274
15541553
        CLOSE_READER(re, gb);
275
276
15541553
        return ff_se_golomb_vlc_code[buf];
277
    } else {
278
790650
        int log = av_log2(buf), sign;
279
790650
        LAST_SKIP_BITS(re, gb, 31 - log);
280
790650
        UPDATE_CACHE(re, gb);
281
790650
        buf = GET_CACHE(re, gb);
282
283
790650
        buf >>= log;
284
285
790650
        LAST_SKIP_BITS(re, gb, 32 - log);
286
790650
        CLOSE_READER(re, gb);
287
288
790650
        sign = -(buf & 1);
289
790650
        buf  = ((buf >> 1) ^ sign) - sign;
290
291
790650
        return buf;
292
    }
293
#endif
294
}
295
296
275
static inline int get_se_golomb_long(GetBitContext *gb)
297
{
298
275
    unsigned int buf = get_ue_golomb_long(gb);
299
275
    int sign = (buf & 1) - 1;
300
275
    return ((buf >> 1) ^ sign) + 1;
301
}
302
303
1313145
static inline int get_interleaved_se_golomb(GetBitContext *gb)
304
{
305
    unsigned int buf;
306
307
#if CACHED_BITSTREAM_READER
308
    buf = show_bits_long(gb, 32);
309
310
    if (buf & 0xAA800000) {
311
        buf >>= 32 - 8;
312
        skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]);
313
314
        return ff_interleaved_se_golomb_vlc_code[buf];
315
    } else {
316
        int log;
317
        skip_bits(gb, 8);
318
        buf |= 1 | show_bits(gb, 24);
319
320
        if ((buf & 0xAAAAAAAA) == 0)
321
            return INVALID_VLC;
322
323
        for (log = 31; (buf & 0x80000000) == 0; log--)
324
            buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
325
326
        skip_bits_long(gb, 63 - 2 * log - 8);
327
328
        return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
329
    }
330
#else
331
1313145
    OPEN_READER(re, gb);
332
1313145
    UPDATE_CACHE(re, gb);
333
1313145
    buf = GET_CACHE(re, gb);
334
335
1313145
    if (buf & 0xAA800000) {
336
1264154
        buf >>= 32 - 8;
337
1264154
        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
338
1264154
        CLOSE_READER(re, gb);
339
340
1264154
        return ff_interleaved_se_golomb_vlc_code[buf];
341
    } else {
342
        int log;
343
48991
        LAST_SKIP_BITS(re, gb, 8);
344
48991
        UPDATE_CACHE(re, gb);
345
48991
        buf |= 1 | (GET_CACHE(re, gb) >> 8);
346
347
48991
        if ((buf & 0xAAAAAAAA) == 0)
348
            return INVALID_VLC;
349
350
325501
        for (log = 31; (buf & 0x80000000) == 0; log--)
351
276510
            buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
352
353
48991
        LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8);
354
48991
        CLOSE_READER(re, gb);
355
356
48991
        return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
357
    }
358
#endif
359
}
360
361
1949306
static inline int dirac_get_se_golomb(GetBitContext *gb)
362
{
363
1949306
    uint32_t ret = get_interleaved_ue_golomb(gb);
364
365
1949306
    if (ret) {
366
336860
        int sign = -get_bits1(gb);
367
336860
        ret = (ret ^ sign) - sign;
368
    }
369
370
1949306
    return ret;
371
}
372
373
/**
374
 * read unsigned golomb rice code (ffv1).
375
 */
376
113342102
static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,
377
                                int esc_len)
378
{
379
    unsigned int buf;
380
    int log;
381
382
#if CACHED_BITSTREAM_READER
383
    buf = show_bits_long(gb, 32);
384
385
    log = av_log2(buf);
386
387
    if (log > 31 - limit) {
388
        buf >>= log - k;
389
        buf  += (30 - log) << k;
390
        skip_bits_long(gb, 32 + k - log);
391
392
        return buf;
393
    } else {
394
        skip_bits_long(gb, limit);
395
        buf = get_bits_long(gb, esc_len);
396
397
        return buf + limit - 1;
398
    }
399
#else
400
113342102
    OPEN_READER(re, gb);
401
113342102
    UPDATE_CACHE(re, gb);
402
113342102
    buf = GET_CACHE(re, gb);
403
404
113342102
    log = av_log2(buf);
405
406
113342102
    if (log > 31 - limit) {
407
112768247
        buf >>= log - k;
408
112768247
        buf  += (30U - log) << k;
409
112768247
        LAST_SKIP_BITS(re, gb, 32 + k - log);
410
112768247
        CLOSE_READER(re, gb);
411
412
112768247
        return buf;
413
    } else {
414
573855
        LAST_SKIP_BITS(re, gb, limit);
415
573855
        UPDATE_CACHE(re, gb);
416
417
573855
        buf = SHOW_UBITS(re, gb, esc_len);
418
419
573855
        LAST_SKIP_BITS(re, gb, esc_len);
420
573855
        CLOSE_READER(re, gb);
421
422
573855
        return buf + limit - 1;
423
    }
424
#endif
425
}
426
427
/**
428
 * read unsigned golomb rice code (jpegls).
429
 */
430
100840612
static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
431
                                       int esc_len)
432
{
433
    unsigned int buf;
434
    int log;
435
436
#if CACHED_BITSTREAM_READER
437
    buf = show_bits_long(gb, 32);
438
439
    log = av_log2(buf);
440
441
    if (log - k >= 1 && 32 - log < limit) {
442
        buf >>= log - k;
443
        buf  += (30 - log) << k;
444
        skip_bits_long(gb, 32 + k - log);
445
446
        return buf;
447
    } else {
448
        int i;
449
        for (i = 0;
450
             i < limit && get_bits1(gb) == 0 && get_bits_left(gb) > 0;
451
             i++);
452
453
        if (i < limit - 1) {
454
            buf = get_bits_long(gb, k);
455
456
            return buf + (i << k);
457
        } else if (i == limit - 1) {
458
            buf = get_bits_long(gb, esc_len);
459
460
            return buf + 1;
461
        } else
462
            return -1;
463
    }
464
#else
465
100840612
    OPEN_READER(re, gb);
466
100840612
    UPDATE_CACHE(re, gb);
467
100840612
    buf = GET_CACHE(re, gb);
468
469
100840612
    log = av_log2(buf);
470
471
    av_assert2(k <= 31);
472
473
100840612
    if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
474
100792033
        32 - log < limit) {
475
100749975
        buf >>= log - k;
476
100749975
        buf  += (30U - log) << k;
477
100749975
        LAST_SKIP_BITS(re, gb, 32 + k - log);
478
100749975
        CLOSE_READER(re, gb);
479
480
100749975
        return buf;
481
    } else {
482
        int i;
483

91125
        for (i = 0; i + MIN_CACHE_BITS <= limit && SHOW_UBITS(re, gb, MIN_CACHE_BITS) == 0; i += MIN_CACHE_BITS) {
484
488
            if (gb->size_in_bits <= re_index) {
485
                CLOSE_READER(re, gb);
486
                return -1;
487
            }
488
488
            LAST_SKIP_BITS(re, gb, MIN_CACHE_BITS);
489
488
            UPDATE_CACHE(re, gb);
490
        }
491

2155092
        for (; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
492
2064455
            SKIP_BITS(re, gb, 1);
493
        }
494
90637
        LAST_SKIP_BITS(re, gb, 1);
495
90637
        UPDATE_CACHE(re, gb);
496
497
90637
        if (i < limit - 1) {
498
2548
            if (k) {
499
2375
                if (k > MIN_CACHE_BITS - 1) {
500
                    buf = SHOW_UBITS(re, gb, 16) << (k-16);
501
                    LAST_SKIP_BITS(re, gb, 16);
502
                    UPDATE_CACHE(re, gb);
503
                    buf |= SHOW_UBITS(re, gb, k-16);
504
                    LAST_SKIP_BITS(re, gb, k-16);
505
                } else {
506
2375
                    buf = SHOW_UBITS(re, gb, k);
507
2375
                    LAST_SKIP_BITS(re, gb, k);
508
                }
509
            } else {
510
173
                buf = 0;
511
            }
512
513
2548
            buf += ((SUINT)i << k);
514
88089
        } else if (i == limit - 1) {
515
88089
            buf = SHOW_UBITS(re, gb, esc_len);
516
88089
            LAST_SKIP_BITS(re, gb, esc_len);
517
518
88089
            buf ++;
519
        } else {
520
            buf = -1;
521
        }
522
90637
        CLOSE_READER(re, gb);
523
90637
        return buf;
524
    }
525
#endif
526
}
527
528
/**
529
 * read signed golomb rice code (ffv1).
530
 */
531
113342102
static inline int get_sr_golomb(GetBitContext *gb, int k, int limit,
532
                                int esc_len)
533
{
534
113342102
    unsigned v = get_ur_golomb(gb, k, limit, esc_len);
535
113342102
    return (v >> 1) ^ -(v & 1);
536
}
537
538
/**
539
 * read signed golomb rice code (flac).
540
 */
541
54781036
static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit,
542
                                     int esc_len)
543
{
544
54781036
    unsigned v = get_ur_golomb_jpegls(gb, k, limit, esc_len);
545
54781036
    return (v >> 1) ^ -(v & 1);
546
}
547
548
/**
549
 * read unsigned golomb rice code (shorten).
550
 */
551
6320
static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
552
{
553
6320
    return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
554
}
555
556
/**
557
 * read signed golomb rice code (shorten).
558
 */
559
793344
static inline int get_sr_golomb_shorten(GetBitContext *gb, int k)
560
{
561
793344
    int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
562
793344
    return (uvar >> 1) ^ -(uvar & 1);
563
}
564
565
#ifdef TRACE
566
567
static inline int get_ue(GetBitContext *s, const char *file, const char *func,
568
                         int line)
569
{
570
    int show = show_bits(s, 24);
571
    int pos  = get_bits_count(s);
572
    int i    = get_ue_golomb(s);
573
    int len  = get_bits_count(s) - pos;
574
    int bits = show >> (24 - len);
575
576
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue  @%5d in %s %s:%d\n",
577
           bits, len, i, pos, file, func, line);
578
579
    return i;
580
}
581
582
static inline int get_se(GetBitContext *s, const char *file, const char *func,
583
                         int line)
584
{
585
    int show = show_bits(s, 24);
586
    int pos  = get_bits_count(s);
587
    int i    = get_se_golomb(s);
588
    int len  = get_bits_count(s) - pos;
589
    int bits = show >> (24 - len);
590
591
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se  @%5d in %s %s:%d\n",
592
           bits, len, i, pos, file, func, line);
593
594
    return i;
595
}
596
597
static inline int get_te(GetBitContext *s, int r, char *file, const char *func,
598
                         int line)
599
{
600
    int show = show_bits(s, 24);
601
    int pos  = get_bits_count(s);
602
    int i    = get_te0_golomb(s, r);
603
    int len  = get_bits_count(s) - pos;
604
    int bits = show >> (24 - len);
605
606
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te  @%5d in %s %s:%d\n",
607
           bits, len, i, pos, file, func, line);
608
609
    return i;
610
}
611
612
#define get_ue_golomb(a) get_ue(a, __FILE__, __func__, __LINE__)
613
#define get_se_golomb(a) get_se(a, __FILE__, __func__, __LINE__)
614
#define get_te_golomb(a, r)  get_te(a, r, __FILE__, __func__, __LINE__)
615
#define get_te0_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
616
617
#endif /* TRACE */
618
619
/**
620
 * write unsigned exp golomb code. 2^16 - 2 at most
621
 */
622
24573
static inline void set_ue_golomb(PutBitContext *pb, int i)
623
{
624
    av_assert2(i >= 0);
625
    av_assert2(i <= 0xFFFE);
626
627
24573
    if (i < 256)
628
544
        put_bits(pb, ff_ue_golomb_len[i], i + 1);
629
    else {
630
24029
        int e = av_log2(i + 1);
631
24029
        put_bits(pb, 2 * e + 1, i + 1);
632
    }
633
24573
}
634
635
/**
636
 * write unsigned exp golomb code. 2^32-2 at most.
637
 */
638
8191
static inline void set_ue_golomb_long(PutBitContext *pb, uint32_t i)
639
{
640
    av_assert2(i <= (UINT32_MAX - 1));
641
642
8191
    if (i < 256)
643
16
        put_bits(pb, ff_ue_golomb_len[i], i + 1);
644
    else {
645
8175
        int e = av_log2(i + 1);
646
8175
        put_bits64(pb, 2 * e + 1, i + 1);
647
    }
648
8191
}
649
650
/**
651
 * write truncated unsigned exp golomb code.
652
 */
653
static inline void set_te_golomb(PutBitContext *pb, int i, int range)
654
{
655
    av_assert2(range >= 1);
656
    av_assert2(i <= range);
657
658
    if (range == 2)
659
        put_bits(pb, 1, i ^ 1);
660
    else
661
        set_ue_golomb(pb, i);
662
}
663
664
/**
665
 * write signed exp golomb code. 16 bits at most.
666
 */
667
8191
static inline void set_se_golomb(PutBitContext *pb, int i)
668
{
669
8191
    i = 2 * i - 1;
670
8191
    if (i < 0)
671
4096
        i ^= -1;    //FIXME check if gcc does the right thing
672
8191
    set_ue_golomb(pb, i);
673
8191
}
674
675
/**
676
 * write unsigned golomb rice code (ffv1).
677
 */
678
110987686
static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
679
                                 int esc_len)
680
{
681
    int e;
682
683
    av_assert2(i >= 0);
684
685
110987686
    e = i >> k;
686
110987686
    if (e < limit)
687
110433986
        put_bits(pb, e + k + 1, (1 << k) + av_mod_uintp2(i, k));
688
    else
689
553700
        put_bits(pb, limit + esc_len, i - limit + 1);
690
110987686
}
691
692
/**
693
 * write unsigned golomb rice code (jpegls).
694
 */
695
88335513
static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k,
696
                                        int limit, int esc_len)
697
{
698
    int e;
699
700
    av_assert2(i >= 0);
701
702
88335513
    e = (i >> k) + 1;
703
88335513
    if (e < limit) {
704
88249262
        while (e > 31) {
705
33
            put_bits(pb, 31, 0);
706
33
            e -= 31;
707
        }
708
88249229
        put_bits(pb, e, 1);
709
88249229
        if (k)
710
86264470
            put_sbits(pb, k, i);
711
    } else {
712
86284
        while (limit > 31) {
713
            put_bits(pb, 31, 0);
714
            limit -= 31;
715
        }
716
86284
        put_bits(pb, limit, 1);
717
86284
        put_bits(pb, esc_len, i - 1);
718
    }
719
88335513
}
720
721
/**
722
 * write signed golomb rice code (ffv1).
723
 */
724
110987686
static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit,
725
                                 int esc_len)
726
{
727
    int v;
728
729
110987686
    v  = -2 * i - 1;
730
110987686
    v ^= (v >> 31);
731
732
110987686
    set_ur_golomb(pb, v, k, limit, esc_len);
733
110987686
}
734
735
/**
736
 * write signed golomb rice code (flac).
737
 */
738
44742164
static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k,
739
                                      int limit, int esc_len)
740
{
741
    int v;
742
743
44742164
    v  = -2 * i - 1;
744
44742164
    v ^= (v >> 31);
745
746
44742164
    set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
747
44742164
}
748
749
#endif /* AVCODEC_GOLOMB_H */