GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/golomb.h Lines: 208 219 95.0 %
Date: 2019-11-20 04:07:19 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
30404095
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
        buf >>= log;
70
        buf--;
71
        skip_bits_long(gb, 32 - log);
72
73
        return buf;
74
    }
75
#else
76
30404095
    OPEN_READER(re, gb);
77
30404095
    UPDATE_CACHE(re, gb);
78
30404095
    buf = GET_CACHE(re, gb);
79
80
30404095
    if (buf >= (1 << 27)) {
81
29333328
        buf >>= 32 - 9;
82
29333328
        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
83
29333328
        CLOSE_READER(re, gb);
84
85
29333328
        return ff_ue_golomb_vlc_code[buf];
86
    } else {
87
1070767
        int log = 2 * av_log2(buf) - 31;
88
1070767
        LAST_SKIP_BITS(re, gb, 32 - log);
89
1070767
        CLOSE_READER(re, gb);
90
1070767
        if (log < 7) {
91
3
            av_log(NULL, AV_LOG_ERROR, "Invalid UE golomb code\n");
92
3
            return AVERROR_INVALIDDATA;
93
        }
94
1070764
        buf >>= log;
95
1070764
        buf--;
96
97
1070764
        return buf;
98
    }
99
#endif
100
}
101
102
/**
103
 * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
104
 */
105
2837701
static inline unsigned get_ue_golomb_long(GetBitContext *gb)
106
{
107
    unsigned buf, log;
108
109
2837701
    buf = show_bits_long(gb, 32);
110
2837701
    log = 31 - av_log2(buf);
111
2837701
    skip_bits_long(gb, log);
112
113
2837701
    return get_bits_long(gb, log + 1) - 1;
114
}
115
116
/**
117
 * read unsigned exp golomb code, constraint to a max of 31.
118
 * the return value is undefined if the stored value exceeds 31.
119
 */
120
5378010
static inline int get_ue_golomb_31(GetBitContext *gb)
121
{
122
    unsigned int buf;
123
124
#if CACHED_BITSTREAM_READER
125
    buf = show_bits_long(gb, 32);
126
127
    buf >>= 32 - 9;
128
    skip_bits_long(gb, ff_golomb_vlc_len[buf]);
129
#else
130
131
5378010
    OPEN_READER(re, gb);
132
5378010
    UPDATE_CACHE(re, gb);
133
5378010
    buf = GET_CACHE(re, gb);
134
135
5378010
    buf >>= 32 - 9;
136
5378010
    LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
137
5378010
    CLOSE_READER(re, gb);
138
#endif
139
140
5378010
    return ff_ue_golomb_vlc_code[buf];
141
}
142
143
8496938
static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb)
144
{
145
    uint32_t buf;
146
147
#if CACHED_BITSTREAM_READER
148
    buf = show_bits_long(gb, 32);
149
150
    if (buf & 0xAA800000) {
151
        buf >>= 32 - 8;
152
        skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]);
153
154
        return ff_interleaved_ue_golomb_vlc_code[buf];
155
    } else {
156
        unsigned ret = 1;
157
158
        do {
159
            buf >>= 32 - 8;
160
            skip_bits_long(gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
161
162
            if (ff_interleaved_golomb_vlc_len[buf] != 9) {
163
                ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
164
                ret  |= ff_interleaved_dirac_golomb_vlc_code[buf];
165
                break;
166
            }
167
            ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
168
            buf = show_bits_long(gb, 32);
169
        } while (get_bits_left(gb) > 0);
170
171
        return ret - 1;
172
    }
173
#else
174
8496938
    OPEN_READER(re, gb);
175
8496938
    UPDATE_CACHE(re, gb);
176
8496938
    buf = GET_CACHE(re, gb);
177
178
8496938
    if (buf & 0xAA800000) {
179
8391118
        buf >>= 32 - 8;
180
8391118
        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
181
8391118
        CLOSE_READER(re, gb);
182
183
8391118
        return ff_interleaved_ue_golomb_vlc_code[buf];
184
    } else {
185
105820
        unsigned ret = 1;
186
187
        do {
188
215801
            buf >>= 32 - 8;
189
215801
            LAST_SKIP_BITS(re, gb,
190
                           FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
191
192
215801
            if (ff_interleaved_golomb_vlc_len[buf] != 9) {
193
105820
                ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
194
105820
                ret  |= ff_interleaved_dirac_golomb_vlc_code[buf];
195
105820
                break;
196
            }
197
109981
            ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
198
109981
            UPDATE_CACHE(re, gb);
199
109981
            buf = GET_CACHE(re, gb);
200

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

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

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