golomb.h 17.9 KB
Newer Older
1 2 3
/*
 * exp golomb vlc stuff
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4
 * Copyright (c) 2004 Alex Beregszaszi
5
 *
6 7 8
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * 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 15 16 17 18
 * 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
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 31
#ifndef AVCODEC_GOLOMB_H
#define AVCODEC_GOLOMB_H
32

33
#include <stdint.h>
34

35
#include "get_bits.h"
36
#include "put_bits.h"
37

38 39
#define INVALID_VLC           0x80000000

40 41 42 43 44
extern const uint8_t ff_golomb_vlc_len[512];
extern const uint8_t ff_ue_golomb_vlc_code[512];
extern const  int8_t ff_se_golomb_vlc_code[512];
extern const uint8_t ff_ue_golomb_len[256];

Michael Niedermayer's avatar
Michael Niedermayer committed
45 46 47
extern const uint8_t ff_interleaved_golomb_vlc_len[256];
extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
extern const  int8_t ff_interleaved_se_golomb_vlc_code[256];
48
extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
Michael Niedermayer's avatar
Michael Niedermayer committed
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 56
static inline int get_ue_golomb(GetBitContext *gb)
{
57
    unsigned int buf;
58

59
#if CACHED_BITSTREAM_READER
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    buf = show_bits_long(gb, 32);

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

        return ff_ue_golomb_vlc_code[buf];
    } else {
        int log = 2 * av_log2(buf) - 31;
        buf >>= log;
        buf--;
        skip_bits_long(gb, 32 - log);

        return buf;
    }
#else
76 77
    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
78
    buf = GET_CACHE(re, gb);
79

80
    if (buf >= (1 << 27)) {
81 82 83
        buf >>= 32 - 9;
        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
        CLOSE_READER(re, gb);
84

85
        return ff_ue_golomb_vlc_code[buf];
86
    } else {
87
        int log = 2 * av_log2(buf) - 31;
88 89
        LAST_SKIP_BITS(re, gb, 32 - log);
        CLOSE_READER(re, gb);
90
        if (log < 7) {
91
            av_log(NULL, AV_LOG_ERROR, "Invalid UE golomb code\n");
92 93
            return AVERROR_INVALIDDATA;
        }
94
        buf >>= log;
95
        buf--;
96

97 98
        return buf;
    }
99
#endif
100 101
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115
/**
 * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
 */
static inline unsigned get_ue_golomb_long(GetBitContext *gb)
{
    unsigned buf, log;

    buf = show_bits_long(gb, 32);
    log = 31 - av_log2(buf);
    skip_bits_long(gb, log);

    return get_bits_long(gb, log + 1) - 1;
}

116
/**
117 118
 * read unsigned exp golomb code, constraint to a max of 31.
 * the return value is undefined if the stored value exceeds 31.
119
 */
120 121
static inline int get_ue_golomb_31(GetBitContext *gb)
{
122 123
    unsigned int buf;

124
#if CACHED_BITSTREAM_READER
125 126 127 128 129 130
    buf = show_bits_long(gb, 32);

    buf >>= 32 - 9;
    skip_bits_long(gb, ff_golomb_vlc_len[buf]);
#else

131 132
    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
133
    buf = GET_CACHE(re, gb);
134 135 136 137

    buf >>= 32 - 9;
    LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
    CLOSE_READER(re, gb);
138
#endif
139 140 141 142

    return ff_ue_golomb_vlc_code[buf];
}

143
static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb)
144
{
Michael Niedermayer's avatar
Michael Niedermayer committed
145
    uint32_t buf;
146

147
#if CACHED_BITSTREAM_READER
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
    buf = show_bits_long(gb, 32);

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

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

        do {
            buf >>= 32 - 8;
            skip_bits_long(gb, 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 = show_bits_long(gb, 32);
        } while (get_bits_left(gb) > 0);

        return ret - 1;
    }
#else
174 175
    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
176
    buf = GET_CACHE(re, gb);
177

178
    if (buf & 0xAA800000) {
Michael Niedermayer's avatar
Michael Niedermayer committed
179 180 181
        buf >>= 32 - 8;
        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
        CLOSE_READER(re, gb);
182

Michael Niedermayer's avatar
Michael Niedermayer committed
183
        return ff_interleaved_ue_golomb_vlc_code[buf];
184
    } else {
185
        unsigned ret = 1;
186

187
        do {
188
            buf >>= 32 - 8;
189 190
            LAST_SKIP_BITS(re, gb,
                           FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
191

192
            if (ff_interleaved_golomb_vlc_len[buf] != 9) {
193
                ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
194
                ret  |= ff_interleaved_dirac_golomb_vlc_code[buf];
195 196 197 198 199
                break;
            }
            ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
            UPDATE_CACHE(re, gb);
            buf = GET_CACHE(re, gb);
200
        } while (ret<0x8000000U && BITS_AVAILABLE(re, gb));
201

Michael Niedermayer's avatar
Michael Niedermayer committed
202
        CLOSE_READER(re, gb);
203
        return ret - 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
204
    }
205
#endif
206 207
}

208 209 210
/**
 * read unsigned truncated exp golomb code.
 */
211 212
static inline int get_te0_golomb(GetBitContext *gb, int range)
{
213
    av_assert2(range >= 1);
214

215 216 217 218 219 220
    if (range == 1)
        return 0;
    else if (range == 2)
        return get_bits1(gb) ^ 1;
    else
        return get_ue_golomb(gb);
221 222 223 224 225
}

/**
 * read unsigned truncated exp golomb code.
 */
226 227
static inline int get_te_golomb(GetBitContext *gb, int range)
{
228
    av_assert2(range >= 1);
229

230 231 232 233
    if (range == 2)
        return get_bits1(gb) ^ 1;
    else
        return get_ue_golomb(gb);
234 235 236 237 238
}

/**
 * read signed exp golomb code.
 */
239 240
static inline int get_se_golomb(GetBitContext *gb)
{
241
    unsigned int buf;
242

243
#if CACHED_BITSTREAM_READER
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    buf = show_bits_long(gb, 32);

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

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

        skip_bits_long(gb, 32 - log);

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

        return buf;
    }
#else
265 266
    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
267
    buf = GET_CACHE(re, gb);
268

269
    if (buf >= (1 << 27)) {
270 271 272
        buf >>= 32 - 9;
        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
        CLOSE_READER(re, gb);
273

274
        return ff_se_golomb_vlc_code[buf];
275
    } else {
276
        int log = av_log2(buf), sign;
277 278 279 280
        LAST_SKIP_BITS(re, gb, 31 - log);
        UPDATE_CACHE(re, gb);
        buf = GET_CACHE(re, gb);

281
        buf >>= log;
282

283 284
        LAST_SKIP_BITS(re, gb, 32 - log);
        CLOSE_READER(re, gb);
285

286 287
        sign = -(buf & 1);
        buf  = ((buf >> 1) ^ sign) - sign;
288 289 290

        return buf;
    }
291
#endif
292 293
}

294 295 296
static inline int get_se_golomb_long(GetBitContext *gb)
{
    unsigned int buf = get_ue_golomb_long(gb);
Zeng Zhaoxiu's avatar
Zeng Zhaoxiu committed
297
    int sign = (buf & 1) - 1;
298
    return ((buf >> 1) ^ sign) + 1;
299 300
}

301
static inline int get_interleaved_se_golomb(GetBitContext *gb)
302
{
303 304
    unsigned int buf;

305
#if CACHED_BITSTREAM_READER
306 307 308 309 310 311 312 313 314 315
    buf = show_bits_long(gb, 32);

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

        return ff_interleaved_se_golomb_vlc_code[buf];
    } else {
        int log;
        skip_bits(gb, 8);
316
        buf |= 1 | show_bits(gb, 24);
317 318 319 320 321 322 323 324 325 326 327 328

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

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

        skip_bits_long(gb, 63 - 2 * log - 8);

        return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
    }
#else
329 330
    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
331
    buf = GET_CACHE(re, gb);
332

333
    if (buf & 0xAA800000) {
Michael Niedermayer's avatar
Michael Niedermayer committed
334 335 336
        buf >>= 32 - 8;
        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
        CLOSE_READER(re, gb);
337

Michael Niedermayer's avatar
Michael Niedermayer committed
338
        return ff_interleaved_se_golomb_vlc_code[buf];
339
    } else {
340
        int log;
341 342 343 344
        LAST_SKIP_BITS(re, gb, 8);
        UPDATE_CACHE(re, gb);
        buf |= 1 | (GET_CACHE(re, gb) >> 8);

345
        if ((buf & 0xAAAAAAAA) == 0)
Michael Niedermayer's avatar
Michael Niedermayer committed
346
            return INVALID_VLC;
347

348
        for (log = 31; (buf & 0x80000000) == 0; log--)
Michael Niedermayer's avatar
Michael Niedermayer committed
349
            buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
350

351
        LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8);
Michael Niedermayer's avatar
Michael Niedermayer committed
352
        CLOSE_READER(re, gb);
353

Michael Niedermayer's avatar
Michael Niedermayer committed
354 355
        return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
    }
356
#endif
357 358
}

359 360
static inline int dirac_get_se_golomb(GetBitContext *gb)
{
361
    uint32_t ret = get_interleaved_ue_golomb(gb);
362 363

    if (ret) {
Zeng Zhaoxiu's avatar
Zeng Zhaoxiu committed
364
        int sign = -get_bits1(gb);
365
        ret = (ret ^ sign) - sign;
366 367 368 369 370
    }

    return ret;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
371
/**
372
 * read unsigned golomb rice code (ffv1).
Michael Niedermayer's avatar
Michael Niedermayer committed
373
 */
374 375 376
static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,
                                int esc_len)
{
Michael Niedermayer's avatar
Michael Niedermayer committed
377 378
    unsigned int buf;
    int log;
379

380
#if CACHED_BITSTREAM_READER
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    buf = show_bits_long(gb, 32);

    log = av_log2(buf);

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

        return buf;
    } else {
        skip_bits_long(gb, limit);
        buf = get_bits_long(gb, esc_len);

        return buf + limit - 1;
    }
#else
Michael Niedermayer's avatar
Michael Niedermayer committed
398 399
    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
400
    buf = GET_CACHE(re, gb);
Michael Niedermayer's avatar
Michael Niedermayer committed
401

402
    log = av_log2(buf);
403

404
    if (log > 31 - limit) {
Michael Niedermayer's avatar
Michael Niedermayer committed
405
        buf >>= log - k;
406
        buf  += (30U - log) << k;
Michael Niedermayer's avatar
Michael Niedermayer committed
407 408
        LAST_SKIP_BITS(re, gb, 32 + k - log);
        CLOSE_READER(re, gb);
409

Michael Niedermayer's avatar
Michael Niedermayer committed
410
        return buf;
411
    } else {
412 413 414 415 416 417
        LAST_SKIP_BITS(re, gb, limit);
        UPDATE_CACHE(re, gb);

        buf = SHOW_UBITS(re, gb, esc_len);

        LAST_SKIP_BITS(re, gb, esc_len);
Michael Niedermayer's avatar
Michael Niedermayer committed
418
        CLOSE_READER(re, gb);
419

420 421
        return buf + limit - 1;
    }
422
#endif
423 424 425 426 427
}

/**
 * read unsigned golomb rice code (jpegls).
 */
428 429 430
static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
                                       int esc_len)
{
431 432
    unsigned int buf;
    int log;
433

434
#if CACHED_BITSTREAM_READER
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    buf = show_bits_long(gb, 32);

    log = av_log2(buf);

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

        return buf;
    } else {
        int i;
        for (i = 0;
             i < limit && get_bits1(gb) == 0 && get_bits_left(gb) > 0;
             i++);

        if (i < limit - 1) {
            buf = get_bits_long(gb, k);

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

            return buf + 1;
        } else
            return -1;
    }
#else
463 464
    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
465
    buf = GET_CACHE(re, gb);
466

467
    log = av_log2(buf);
468

469 470
    av_assert2(k <= 31);

471 472
    if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
        32 - log < limit) {
473
        buf >>= log - k;
474
        buf  += (30U - log) << k;
475 476
        LAST_SKIP_BITS(re, gb, 32 + k - log);
        CLOSE_READER(re, gb);
477

478
        return buf;
479
    } else {
480
        int i;
481
        for (i = 0; i + MIN_CACHE_BITS <= limit && SHOW_UBITS(re, gb, MIN_CACHE_BITS) == 0; i += MIN_CACHE_BITS) {
482 483
            if (gb->size_in_bits <= re_index) {
                CLOSE_READER(re, gb);
484
                return -1;
485
            }
486
            LAST_SKIP_BITS(re, gb, MIN_CACHE_BITS);
487 488
            UPDATE_CACHE(re, gb);
        }
489 490 491 492 493
        for (; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
            SKIP_BITS(re, gb, 1);
        }
        LAST_SKIP_BITS(re, gb, 1);
        UPDATE_CACHE(re, gb);
494

495 496
        if (i < limit - 1) {
            if (k) {
497 498 499 500 501 502 503 504 505 506
                if (k > MIN_CACHE_BITS - 1) {
                    buf = SHOW_UBITS(re, gb, 16) << (k-16);
                    LAST_SKIP_BITS(re, gb, 16);
                    UPDATE_CACHE(re, gb);
                    buf |= SHOW_UBITS(re, gb, k-16);
                    LAST_SKIP_BITS(re, gb, k-16);
                } else {
                    buf = SHOW_UBITS(re, gb, k);
                    LAST_SKIP_BITS(re, gb, k);
                }
507 508
            } else {
                buf = 0;
509 510
            }

511
            buf += ((SUINT)i << k);
512
        } else if (i == limit - 1) {
513 514
            buf = SHOW_UBITS(re, gb, esc_len);
            LAST_SKIP_BITS(re, gb, esc_len);
515

516 517 518 519 520 521
            buf ++;
        } else {
            buf = -1;
        }
        CLOSE_READER(re, gb);
        return buf;
522
    }
523
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
524 525
}

Michael Niedermayer's avatar
Michael Niedermayer committed
526
/**
527 528
 * read signed golomb rice code (ffv1).
 */
529 530 531
static inline int get_sr_golomb(GetBitContext *gb, int k, int limit,
                                int esc_len)
{
532
    unsigned v = get_ur_golomb(gb, k, limit, esc_len);
Zeng Zhaoxiu's avatar
Zeng Zhaoxiu committed
533
    return (v >> 1) ^ -(v & 1);
534 535
}

536
/**
537
 * read signed golomb rice code (flac).
Michael Niedermayer's avatar
Michael Niedermayer committed
538
 */
539 540 541
static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit,
                                     int esc_len)
{
542
    unsigned v = get_ur_golomb_jpegls(gb, k, limit, esc_len);
543
    return (v >> 1) ^ -(v & 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
544 545
}

546 547 548
/**
 * read unsigned golomb rice code (shorten).
 */
549 550 551
static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
{
    return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
552 553 554 555 556
}

/**
 * read signed golomb rice code (shorten).
 */
557
static inline int get_sr_golomb_shorten(GetBitContext *gb, int k)
558 559
{
    int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
560
    return (uvar >> 1) ^ -(uvar & 1);
561 562
}

563 564
#ifdef TRACE

565 566 567
static inline int get_ue(GetBitContext *s, const char *file, const char *func,
                         int line)
{
568 569 570 571 572
    int show = show_bits(s, 24);
    int pos  = get_bits_count(s);
    int i    = get_ue_golomb(s);
    int len  = get_bits_count(s) - pos;
    int bits = show >> (24 - len);
573

574 575
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue  @%5d in %s %s:%d\n",
           bits, len, i, pos, file, func, line);
576

577 578 579
    return i;
}

580 581 582
static inline int get_se(GetBitContext *s, const char *file, const char *func,
                         int line)
{
583 584 585 586 587
    int show = show_bits(s, 24);
    int pos  = get_bits_count(s);
    int i    = get_se_golomb(s);
    int len  = get_bits_count(s) - pos;
    int bits = show >> (24 - len);
588

589 590
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se  @%5d in %s %s:%d\n",
           bits, len, i, pos, file, func, line);
591

592 593 594
    return i;
}

595 596 597 598 599 600 601 602
static inline int get_te(GetBitContext *s, int r, char *file, const char *func,
                         int line)
{
    int show = show_bits(s, 24);
    int pos  = get_bits_count(s);
    int i    = get_te0_golomb(s, r);
    int len  = get_bits_count(s) - pos;
    int bits = show >> (24 - len);
603

604 605
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te  @%5d in %s %s:%d\n",
           bits, len, i, pos, file, func, line);
606

607 608 609
    return i;
}

610 611 612 613
#define get_ue_golomb(a) get_ue(a, __FILE__, __func__, __LINE__)
#define get_se_golomb(a) get_se(a, __FILE__, __func__, __LINE__)
#define get_te_golomb(a, r)  get_te(a, r, __FILE__, __func__, __LINE__)
#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 621
static inline void set_ue_golomb(PutBitContext *pb, int i)
{
622
    av_assert2(i >= 0);
623
    av_assert2(i <= 0xFFFE);
624

625 626 627
    if (i < 256)
        put_bits(pb, ff_ue_golomb_len[i], i + 1);
    else {
628
        int e = av_log2(i + 1);
629
        put_bits(pb, 2 * e + 1, i + 1);
630 631 632
    }
}

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
/**
 * write unsigned exp golomb code. 2^32-2 at most.
 */
static inline void set_ue_golomb_long(PutBitContext *pb, uint32_t i)
{
    av_assert2(i <= (UINT32_MAX - 1));

    if (i < 256)
        put_bits(pb, ff_ue_golomb_len[i], i + 1);
    else {
        int e = av_log2(i + 1);
        put_bits64(pb, 2 * e + 1, i + 1);
    }
}

648 649 650
/**
 * write truncated unsigned exp golomb code.
 */
651 652
static inline void set_te_golomb(PutBitContext *pb, int i, int range)
{
653
    av_assert2(range >= 1);
654
    av_assert2(i <= range);
655

656 657 658 659
    if (range == 2)
        put_bits(pb, 1, i ^ 1);
    else
        set_ue_golomb(pb, i);
660 661 662
}

/**
663
 * write signed exp golomb code. 16 bits at most.
664
 */
665 666 667 668 669
static inline void set_se_golomb(PutBitContext *pb, int i)
{
    i = 2 * i - 1;
    if (i < 0)
        i ^= -1;    //FIXME check if gcc does the right thing
670 671
    set_ue_golomb(pb, i);
}
Michael Niedermayer's avatar
Michael Niedermayer committed
672 673

/**
674
 * write unsigned golomb rice code (ffv1).
Michael Niedermayer's avatar
Michael Niedermayer committed
675
 */
676 677 678
static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
                                 int esc_len)
{
Michael Niedermayer's avatar
Michael Niedermayer committed
679
    int e;
680

681
    av_assert2(i >= 0);
682

683 684
    e = i >> k;
    if (e < limit)
685
        put_bits(pb, e + k + 1, (1 << k) + av_mod_uintp2(i, k));
686
    else
687 688 689 690 691 692
        put_bits(pb, limit + esc_len, i - limit + 1);
}

/**
 * write unsigned golomb rice code (jpegls).
 */
693 694 695
static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k,
                                        int limit, int esc_len)
{
696
    int e;
697

698
    av_assert2(i >= 0);
699

700 701 702
    e = (i >> k) + 1;
    if (e < limit) {
        while (e > 31) {
703 704 705
            put_bits(pb, 31, 0);
            e -= 31;
        }
706
        put_bits(pb, e, 1);
707
        if (k)
708
            put_sbits(pb, k, i);
709 710
    } else {
        while (limit > 31) {
711 712 713
            put_bits(pb, 31, 0);
            limit -= 31;
        }
714
        put_bits(pb, limit, 1);
715
        put_bits(pb, esc_len, i - 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
716 717
    }
}
718 719 720 721

/**
 * write signed golomb rice code (ffv1).
 */
722 723 724
static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit,
                                 int esc_len)
{
725 726
    int v;

727 728
    v  = -2 * i - 1;
    v ^= (v >> 31);
729 730 731 732 733 734 735

    set_ur_golomb(pb, v, k, limit, esc_len);
}

/**
 * write signed golomb rice code (flac).
 */
736 737 738
static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k,
                                      int limit, int esc_len)
{
739 740
    int v;

741 742
    v  = -2 * i - 1;
    v ^= (v >> 31);
743 744 745

    set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
}
746

747
#endif /* AVCODEC_GOLOMB_H */