golomb.h 13.8 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 52
 * read unsigned exp golomb code.
 */
53 54
static inline int get_ue_golomb(GetBitContext *gb)
{
55
    unsigned int buf;
56

57 58
    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
59
    buf = GET_CACHE(re, gb);
60

61
    if (buf >= (1 << 27)) {
62 63 64
        buf >>= 32 - 9;
        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
        CLOSE_READER(re, gb);
65

66
        return ff_ue_golomb_vlc_code[buf];
67
    } else {
68
        int log = 2 * av_log2(buf) - 31;
69 70
        LAST_SKIP_BITS(re, gb, 32 - log);
        CLOSE_READER(re, gb);
71 72 73 74
        if (CONFIG_FTRAPV && log < 0) {
            av_log(0, AV_LOG_ERROR, "Invalid UE golomb code\n");
            return AVERROR_INVALIDDATA;
        }
75
        buf >>= log;
76
        buf--;
77

78 79 80 81
        return buf;
    }
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95
/**
 * 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;
}

96
/**
97 98
 * read unsigned exp golomb code, constraint to a max of 31.
 * the return value is undefined if the stored value exceeds 31.
99
 */
100 101
static inline int get_ue_golomb_31(GetBitContext *gb)
{
102 103 104 105
    unsigned int buf;

    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
106
    buf = GET_CACHE(re, gb);
107 108 109 110 111 112 113 114

    buf >>= 32 - 9;
    LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
    CLOSE_READER(re, gb);

    return ff_ue_golomb_vlc_code[buf];
}

115 116
static inline unsigned svq3_get_ue_golomb(GetBitContext *gb)
{
Michael Niedermayer's avatar
Michael Niedermayer committed
117
    uint32_t buf;
118 119 120

    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
121
    buf = GET_CACHE(re, gb);
122

123
    if (buf & 0xAA800000) {
Michael Niedermayer's avatar
Michael Niedermayer committed
124 125 126
        buf >>= 32 - 8;
        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
        CLOSE_READER(re, gb);
127

Michael Niedermayer's avatar
Michael Niedermayer committed
128
        return ff_interleaved_ue_golomb_vlc_code[buf];
129
    } else {
130
        unsigned ret = 1;
131

132
        do {
133
            buf >>= 32 - 8;
134 135
            LAST_SKIP_BITS(re, gb,
                           FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
136

137
            if (ff_interleaved_golomb_vlc_len[buf] != 9) {
138
                ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
139
                ret  |= ff_interleaved_dirac_golomb_vlc_code[buf];
140 141 142 143 144
                break;
            }
            ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
            UPDATE_CACHE(re, gb);
            buf = GET_CACHE(re, gb);
145
        } while (ret<0x8000000U && HAVE_BITS_REMAINING(re, gb));
146

Michael Niedermayer's avatar
Michael Niedermayer committed
147
        CLOSE_READER(re, gb);
148
        return ret - 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
149
    }
150 151
}

152 153 154
/**
 * read unsigned truncated exp golomb code.
 */
155 156
static inline int get_te0_golomb(GetBitContext *gb, int range)
{
157
    av_assert2(range >= 1);
158

159 160 161 162 163 164
    if (range == 1)
        return 0;
    else if (range == 2)
        return get_bits1(gb) ^ 1;
    else
        return get_ue_golomb(gb);
165 166 167 168 169
}

/**
 * read unsigned truncated exp golomb code.
 */
170 171
static inline int get_te_golomb(GetBitContext *gb, int range)
{
172
    av_assert2(range >= 1);
173

174 175 176 177
    if (range == 2)
        return get_bits1(gb) ^ 1;
    else
        return get_ue_golomb(gb);
178 179 180 181 182
}

/**
 * read signed exp golomb code.
 */
183 184
static inline int get_se_golomb(GetBitContext *gb)
{
185
    unsigned int buf;
186

187 188
    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
189
    buf = GET_CACHE(re, gb);
190

191
    if (buf >= (1 << 27)) {
192 193 194
        buf >>= 32 - 9;
        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
        CLOSE_READER(re, gb);
195

196
        return ff_se_golomb_vlc_code[buf];
197
    } else {
198
        int log = av_log2(buf);
199 200 201 202
        LAST_SKIP_BITS(re, gb, 31 - log);
        UPDATE_CACHE(re, gb);
        buf = GET_CACHE(re, gb);

203
        buf >>= log;
204

205 206
        LAST_SKIP_BITS(re, gb, 32 - log);
        CLOSE_READER(re, gb);
207

208 209 210 211
        if (buf & 1)
            buf = -(buf >> 1);
        else
            buf = (buf >> 1);
212 213 214 215 216

        return buf;
    }
}

217 218
static inline int svq3_get_se_golomb(GetBitContext *gb)
{
219 220 221 222
    unsigned int buf;

    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
223
    buf = GET_CACHE(re, gb);
224

225
    if (buf & 0xAA800000) {
Michael Niedermayer's avatar
Michael Niedermayer committed
226 227 228
        buf >>= 32 - 8;
        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
        CLOSE_READER(re, gb);
229

Michael Niedermayer's avatar
Michael Niedermayer committed
230
        return ff_interleaved_se_golomb_vlc_code[buf];
231
    } else {
232
        int log;
233 234 235 236
        LAST_SKIP_BITS(re, gb, 8);
        UPDATE_CACHE(re, gb);
        buf |= 1 | (GET_CACHE(re, gb) >> 8);

237
        if ((buf & 0xAAAAAAAA) == 0)
Michael Niedermayer's avatar
Michael Niedermayer committed
238
            return INVALID_VLC;
239

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

243
        LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8);
Michael Niedermayer's avatar
Michael Niedermayer committed
244
        CLOSE_READER(re, gb);
245

Michael Niedermayer's avatar
Michael Niedermayer committed
246 247
        return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
    }
248 249
}

250 251
static inline int dirac_get_se_golomb(GetBitContext *gb)
{
252
    uint32_t ret = svq3_get_ue_golomb(gb);
253 254

    if (ret) {
255
        uint32_t buf;
256 257 258 259 260 261 262 263 264 265 266
        OPEN_READER(re, gb);
        UPDATE_CACHE(re, gb);
        buf = SHOW_SBITS(re, gb, 1);
        LAST_SKIP_BITS(re, gb, 1);
        ret = (ret ^ buf) - buf;
        CLOSE_READER(re, gb);
    }

    return ret;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
267
/**
268
 * read unsigned golomb rice code (ffv1).
Michael Niedermayer's avatar
Michael Niedermayer committed
269
 */
270 271 272
static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,
                                int esc_len)
{
Michael Niedermayer's avatar
Michael Niedermayer committed
273 274
    unsigned int buf;
    int log;
275

Michael Niedermayer's avatar
Michael Niedermayer committed
276 277
    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
278
    buf = GET_CACHE(re, gb);
Michael Niedermayer's avatar
Michael Niedermayer committed
279

280
    log = av_log2(buf);
281

282
    if (log > 31 - limit) {
Michael Niedermayer's avatar
Michael Niedermayer committed
283
        buf >>= log - k;
284
        buf  += (30 - log) << k;
Michael Niedermayer's avatar
Michael Niedermayer committed
285 286
        LAST_SKIP_BITS(re, gb, 32 + k - log);
        CLOSE_READER(re, gb);
287

Michael Niedermayer's avatar
Michael Niedermayer committed
288
        return buf;
289
    } else {
290 291 292 293 294 295
        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
296
        CLOSE_READER(re, gb);
297

298 299 300 301 302 303 304
        return buf + limit - 1;
    }
}

/**
 * read unsigned golomb rice code (jpegls).
 */
305 306 307
static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
                                       int esc_len)
{
308 309
    unsigned int buf;
    int log;
310

311 312
    OPEN_READER(re, gb);
    UPDATE_CACHE(re, gb);
313
    buf = GET_CACHE(re, gb);
314

315
    log = av_log2(buf);
316

317 318
    if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
        32 - log < limit) {
319
        buf >>= log - k;
320
        buf  += (30 - log) << k;
321 322
        LAST_SKIP_BITS(re, gb, 32 + k - log);
        CLOSE_READER(re, gb);
323

324
        return buf;
325
    } else {
326
        int i;
327
        for (i = 0; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
328
            if (gb->size_in_bits <= re_index)
329
                return -1;
330 331 332 333 334
            LAST_SKIP_BITS(re, gb, 1);
            UPDATE_CACHE(re, gb);
        }
        SKIP_BITS(re, gb, 1);

335 336
        if (i < limit - 1) {
            if (k) {
337 338
                buf = SHOW_UBITS(re, gb, k);
                LAST_SKIP_BITS(re, gb, k);
339 340
            } else {
                buf = 0;
341 342 343
            }

            CLOSE_READER(re, gb);
344 345
            return buf + (i << k);
        } else if (i == limit - 1) {
346 347 348
            buf = SHOW_UBITS(re, gb, esc_len);
            LAST_SKIP_BITS(re, gb, esc_len);
            CLOSE_READER(re, gb);
349

350
            return buf + 1;
351
        } else
352 353
            return -1;
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
354 355
}

Michael Niedermayer's avatar
Michael Niedermayer committed
356
/**
357 358
 * read signed golomb rice code (ffv1).
 */
359 360 361 362
static inline int get_sr_golomb(GetBitContext *gb, int k, int limit,
                                int esc_len)
{
    int v = get_ur_golomb(gb, k, limit, esc_len);
363

364
    v++;
365 366 367 368
    if (v & 1)
        return v >> 1;
    else
        return -(v >> 1);
369

370 371 372
//    return (v>>1) ^ -(v&1);
}

373
/**
374
 * read signed golomb rice code (flac).
Michael Niedermayer's avatar
Michael Niedermayer committed
375
 */
376 377 378 379 380
static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit,
                                     int esc_len)
{
    int v = get_ur_golomb_jpegls(gb, k, limit, esc_len);
    return (v >> 1) ^ -(v & 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
381 382
}

383 384 385
/**
 * read unsigned golomb rice code (shorten).
 */
386 387 388
static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
{
    return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
389 390 391 392 393
}

/**
 * read signed golomb rice code (shorten).
 */
394
static inline int get_sr_golomb_shorten(GetBitContext *gb, int k)
395 396 397 398 399 400 401 402
{
    int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
    if (uvar & 1)
        return ~(uvar >> 1);
    else
        return uvar >> 1;
}

403 404
#ifdef TRACE

405 406 407
static inline int get_ue(GetBitContext *s, const char *file, const char *func,
                         int line)
{
408 409 410 411 412
    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);
413

414
    print_bin(bits, len);
415

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

419 420 421
    return i;
}

422 423 424
static inline int get_se(GetBitContext *s, const char *file, const char *func,
                         int line)
{
425 426 427 428 429
    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);
430

431
    print_bin(bits, len);
432

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

436 437 438
    return i;
}

439 440 441 442 443 444 445 446
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);
447

448
    print_bin(bits, len);
449

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

453 454 455 456 457
    return i;
}

#define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
#define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
458
#define get_te_golomb(a, r)  get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
459 460
#define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)

461
#endif /* TRACE */
462 463 464 465

/**
 * write unsigned exp golomb code.
 */
466 467
static inline void set_ue_golomb(PutBitContext *pb, int i)
{
468
    av_assert2(i >= 0);
469 470

#if 0
471
    if (i = 0) {
472 473 474 475
        put_bits(pb, 1, 1);
        return;
    }
#endif
476 477 478
    if (i < 256)
        put_bits(pb, ff_ue_golomb_len[i], i + 1);
    else {
479
        int e = av_log2(i + 1);
480
        put_bits(pb, 2 * e + 1, i + 1);
481 482 483 484 485 486
    }
}

/**
 * write truncated unsigned exp golomb code.
 */
487 488
static inline void set_te_golomb(PutBitContext *pb, int i, int range)
{
489
    av_assert2(range >= 1);
490
    av_assert2(i <= range);
491

492 493 494 495
    if (range == 2)
        put_bits(pb, 1, i ^ 1);
    else
        set_ue_golomb(pb, i);
496 497 498
}

/**
499
 * write signed exp golomb code. 16 bits at most.
500
 */
501 502
static inline void set_se_golomb(PutBitContext *pb, int i)
{
503
#if 0
504 505 506 507
    if (i <= 0)
        i = -2 * i;
    else
        i = 2 * i - 1;
508
#elif 1
509 510 511
    i = 2 * i - 1;
    if (i < 0)
        i ^= -1;    //FIXME check if gcc does the right thing
512
#else
513 514
    i  = 2 * i - 1;
    i ^= (i >> 31);
515 516 517
#endif
    set_ue_golomb(pb, i);
}
Michael Niedermayer's avatar
Michael Niedermayer committed
518 519

/**
520
 * write unsigned golomb rice code (ffv1).
Michael Niedermayer's avatar
Michael Niedermayer committed
521
 */
522 523 524
static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
                                 int esc_len)
{
Michael Niedermayer's avatar
Michael Niedermayer committed
525
    int e;
526

527
    av_assert2(i >= 0);
528

529 530 531 532
    e = i >> k;
    if (e < limit)
        put_bits(pb, e + k + 1, (1 << k) + (i & ((1 << k) - 1)));
    else
533 534 535 536 537 538
        put_bits(pb, limit + esc_len, i - limit + 1);
}

/**
 * write unsigned golomb rice code (jpegls).
 */
539 540 541
static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k,
                                        int limit, int esc_len)
{
542
    int e;
543

544
    av_assert2(i >= 0);
545

546 547 548
    e = (i >> k) + 1;
    if (e < limit) {
        while (e > 31) {
549 550 551
            put_bits(pb, 31, 0);
            e -= 31;
        }
552
        put_bits(pb, e, 1);
553
        if (k)
554
            put_sbits(pb, k, i);
555 556
    } else {
        while (limit > 31) {
557 558 559
            put_bits(pb, 31, 0);
            limit -= 31;
        }
560
        put_bits(pb, limit, 1);
561
        put_bits(pb, esc_len, i - 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
562 563
    }
}
564 565 566 567

/**
 * write signed golomb rice code (ffv1).
 */
568 569 570
static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit,
                                 int esc_len)
{
571 572
    int v;

573 574
    v  = -2 * i - 1;
    v ^= (v >> 31);
575 576 577 578 579 580 581

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

/**
 * write signed golomb rice code (flac).
 */
582 583 584
static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k,
                                      int limit, int esc_len)
{
585 586
    int v;

587 588
    v  = -2 * i - 1;
    v ^= (v >> 31);
589 590 591

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

593
#endif /* AVCODEC_GOLOMB_H */