Statistics
| Branch: | Revision:

ffmpeg / libavcodec / golomb.h @ 2912e87a

History | View | Annotate | Download (12.5 KB)

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 Libav.
7
 *
8
 * Libav 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
 * Libav 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 Libav; 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
#include "get_bits.h"
35
#include "put_bits.h"
36

    
37
#define INVALID_VLC           0x80000000
38

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

    
44
extern const uint8_t ff_interleaved_golomb_vlc_len[256];
45
extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
46
extern const  int8_t ff_interleaved_se_golomb_vlc_code[256];
47
extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
48

    
49

    
50
 /**
51
 * read unsigned exp golomb code.
52
 */
53
static inline int get_ue_golomb(GetBitContext *gb){
54
    unsigned int buf;
55
    int log;
56

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

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

    
66
        return ff_ue_golomb_vlc_code[buf];
67
    }else{
68
        log= 2*av_log2(buf) - 31;
69
        buf>>= log;
70
        buf--;
71
        LAST_SKIP_BITS(re, gb, 32 - log);
72
        CLOSE_READER(re, gb);
73

    
74
        return buf;
75
    }
76
}
77

    
78
 /**
79
 * read unsigned exp golomb code, constraint to a max of 31.
80
 * the return value is undefined if the stored value exceeds 31.
81
 */
82
static inline int get_ue_golomb_31(GetBitContext *gb){
83
    unsigned int buf;
84

    
85
    OPEN_READER(re, gb);
86
    UPDATE_CACHE(re, gb);
87
    buf=GET_CACHE(re, gb);
88

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

    
93
    return ff_ue_golomb_vlc_code[buf];
94
}
95

    
96
static inline int svq3_get_ue_golomb(GetBitContext *gb){
97
    uint32_t buf;
98

    
99
    OPEN_READER(re, gb);
100
    UPDATE_CACHE(re, gb);
101
    buf=GET_CACHE(re, gb);
102

    
103
    if(buf&0xAA800000){
104
        buf >>= 32 - 8;
105
        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
106
        CLOSE_READER(re, gb);
107

    
108
        return ff_interleaved_ue_golomb_vlc_code[buf];
109
    }else{
110
        int ret = 1;
111

    
112
        while (1) {
113
            buf >>= 32 - 8;
114
            LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
115

    
116
            if (ff_interleaved_golomb_vlc_len[buf] != 9){
117
                ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
118
                ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
119
                break;
120
            }
121
            ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
122
            UPDATE_CACHE(re, gb);
123
            buf = GET_CACHE(re, gb);
124
        }
125

    
126
        CLOSE_READER(re, gb);
127
        return ret - 1;
128
    }
129
}
130

    
131
/**
132
 * read unsigned truncated exp golomb code.
133
 */
134
static inline int get_te0_golomb(GetBitContext *gb, int range){
135
    assert(range >= 1);
136

    
137
    if(range==1)      return 0;
138
    else if(range==2) return get_bits1(gb)^1;
139
    else              return get_ue_golomb(gb);
140
}
141

    
142
/**
143
 * read unsigned truncated exp golomb code.
144
 */
145
static inline int get_te_golomb(GetBitContext *gb, int range){
146
    assert(range >= 1);
147

    
148
    if(range==2) return get_bits1(gb)^1;
149
    else         return get_ue_golomb(gb);
150
}
151

    
152

    
153
/**
154
 * read signed exp golomb code.
155
 */
156
static inline int get_se_golomb(GetBitContext *gb){
157
    unsigned int buf;
158
    int log;
159

    
160
    OPEN_READER(re, gb);
161
    UPDATE_CACHE(re, gb);
162
    buf=GET_CACHE(re, gb);
163

    
164
    if(buf >= (1<<27)){
165
        buf >>= 32 - 9;
166
        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
167
        CLOSE_READER(re, gb);
168

    
169
        return ff_se_golomb_vlc_code[buf];
170
    }else{
171
        log= 2*av_log2(buf) - 31;
172
        buf>>= log;
173

    
174
        LAST_SKIP_BITS(re, gb, 32 - log);
175
        CLOSE_READER(re, gb);
176

    
177
        if(buf&1) buf= -(buf>>1);
178
        else      buf=  (buf>>1);
179

    
180
        return buf;
181
    }
182
}
183

    
184
static inline int svq3_get_se_golomb(GetBitContext *gb){
185
    unsigned int buf;
186
    int log;
187

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

    
192
    if(buf&0xAA800000){
193
        buf >>= 32 - 8;
194
        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
195
        CLOSE_READER(re, gb);
196

    
197
        return ff_interleaved_se_golomb_vlc_code[buf];
198
    }else{
199
        LAST_SKIP_BITS(re, gb, 8);
200
        UPDATE_CACHE(re, gb);
201
        buf |= 1 | (GET_CACHE(re, gb) >> 8);
202

    
203
        if((buf & 0xAAAAAAAA) == 0)
204
            return INVALID_VLC;
205

    
206
        for(log=31; (buf & 0x80000000) == 0; log--){
207
            buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
208
        }
209

    
210
        LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
211
        CLOSE_READER(re, gb);
212

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

    
217
static inline int dirac_get_se_golomb(GetBitContext *gb){
218
    uint32_t buf;
219
    uint32_t ret;
220

    
221
    ret = svq3_get_ue_golomb(gb);
222

    
223
    if (ret) {
224
        OPEN_READER(re, gb);
225
        UPDATE_CACHE(re, gb);
226
        buf = SHOW_SBITS(re, gb, 1);
227
        LAST_SKIP_BITS(re, gb, 1);
228
        ret = (ret ^ buf) - buf;
229
        CLOSE_READER(re, gb);
230
    }
231

    
232
    return ret;
233
}
234

    
235
/**
236
 * read unsigned golomb rice code (ffv1).
237
 */
238
static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
239
    unsigned int buf;
240
    int log;
241

    
242
    OPEN_READER(re, gb);
243
    UPDATE_CACHE(re, gb);
244
    buf=GET_CACHE(re, gb);
245

    
246
    log= av_log2(buf);
247

    
248
    if(log > 31-limit){
249
        buf >>= log - k;
250
        buf += (30-log)<<k;
251
        LAST_SKIP_BITS(re, gb, 32 + k - log);
252
        CLOSE_READER(re, gb);
253

    
254
        return buf;
255
    }else{
256
        LAST_SKIP_BITS(re, gb, limit);
257
        UPDATE_CACHE(re, gb);
258

    
259
        buf = SHOW_UBITS(re, gb, esc_len);
260

    
261
        LAST_SKIP_BITS(re, gb, esc_len);
262
        CLOSE_READER(re, gb);
263

    
264
        return buf + limit - 1;
265
    }
266
}
267

    
268
/**
269
 * read unsigned golomb rice code (jpegls).
270
 */
271
static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
272
    unsigned int buf;
273
    int log;
274

    
275
    OPEN_READER(re, gb);
276
    UPDATE_CACHE(re, gb);
277
    buf=GET_CACHE(re, gb);
278

    
279
    log= av_log2(buf);
280

    
281
    if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){
282
        buf >>= log - k;
283
        buf += (30-log)<<k;
284
        LAST_SKIP_BITS(re, gb, 32 + k - log);
285
        CLOSE_READER(re, gb);
286

    
287
        return buf;
288
    }else{
289
        int i;
290
        for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
291
            LAST_SKIP_BITS(re, gb, 1);
292
            UPDATE_CACHE(re, gb);
293
        }
294
        SKIP_BITS(re, gb, 1);
295

    
296
        if(i < limit - 1){
297
            if(k){
298
                buf = SHOW_UBITS(re, gb, k);
299
                LAST_SKIP_BITS(re, gb, k);
300
            }else{
301
                buf=0;
302
            }
303

    
304
            CLOSE_READER(re, gb);
305
            return buf + (i<<k);
306
        }else if(i == limit - 1){
307
            buf = SHOW_UBITS(re, gb, esc_len);
308
            LAST_SKIP_BITS(re, gb, esc_len);
309
            CLOSE_READER(re, gb);
310

    
311
            return buf + 1;
312
        }else
313
            return -1;
314
    }
315
}
316

    
317
/**
318
 * read signed golomb rice code (ffv1).
319
 */
320
static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
321
    int v= get_ur_golomb(gb, k, limit, esc_len);
322

    
323
    v++;
324
    if (v&1) return v>>1;
325
    else return -(v>>1);
326

    
327
//    return (v>>1) ^ -(v&1);
328
}
329

    
330
/**
331
 * read signed golomb rice code (flac).
332
 */
333
static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
334
    int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
335
    return (v>>1) ^ -(v&1);
336
}
337

    
338
/**
339
 * read unsigned golomb rice code (shorten).
340
 */
341
static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
342
        return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
343
}
344

    
345
/**
346
 * read signed golomb rice code (shorten).
347
 */
348
static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
349
{
350
    int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
351
    if (uvar & 1)
352
        return ~(uvar >> 1);
353
    else
354
        return uvar >> 1;
355
}
356

    
357

    
358

    
359
#ifdef TRACE
360

    
361
static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
362
    int show= show_bits(s, 24);
363
    int pos= get_bits_count(s);
364
    int i= get_ue_golomb(s);
365
    int len= get_bits_count(s) - pos;
366
    int bits= show>>(24-len);
367

    
368
    print_bin(bits, len);
369

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

    
372
    return i;
373
}
374

    
375
static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
376
    int show= show_bits(s, 24);
377
    int pos= get_bits_count(s);
378
    int i= get_se_golomb(s);
379
    int len= get_bits_count(s) - pos;
380
    int bits= show>>(24-len);
381

    
382
    print_bin(bits, len);
383

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

    
386
    return i;
387
}
388

    
389
static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
390
    int show= show_bits(s, 24);
391
    int pos= get_bits_count(s);
392
    int i= get_te0_golomb(s, r);
393
    int len= get_bits_count(s) - pos;
394
    int bits= show>>(24-len);
395

    
396
    print_bin(bits, len);
397

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

    
400
    return i;
401
}
402

    
403
#define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
404
#define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
405
#define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
406
#define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
407

    
408
#endif
409

    
410
/**
411
 * write unsigned exp golomb code.
412
 */
413
static inline void set_ue_golomb(PutBitContext *pb, int i){
414
    int e;
415

    
416
    assert(i>=0);
417

    
418
#if 0
419
    if(i=0){
420
        put_bits(pb, 1, 1);
421
        return;
422
    }
423
#endif
424
    if(i<256)
425
        put_bits(pb, ff_ue_golomb_len[i], i+1);
426
    else{
427
        e= av_log2(i+1);
428

    
429
        put_bits(pb, 2*e+1, i+1);
430
    }
431
}
432

    
433
/**
434
 * write truncated unsigned exp golomb code.
435
 */
436
static inline void set_te_golomb(PutBitContext *pb, int i, int range){
437
    assert(range >= 1);
438
    assert(i<=range);
439

    
440
    if(range==2) put_bits(pb, 1, i^1);
441
    else         set_ue_golomb(pb, i);
442
}
443

    
444
/**
445
 * write signed exp golomb code. 16 bits at most.
446
 */
447
static inline void set_se_golomb(PutBitContext *pb, int i){
448
//    if (i>32767 || i<-32767)
449
//        av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
450
#if 0
451
    if(i<=0) i= -2*i;
452
    else     i=  2*i-1;
453
#elif 1
454
    i= 2*i-1;
455
    if(i<0) i^= -1; //FIXME check if gcc does the right thing
456
#else
457
    i= 2*i-1;
458
    i^= (i>>31);
459
#endif
460
    set_ue_golomb(pb, i);
461
}
462

    
463
/**
464
 * write unsigned golomb rice code (ffv1).
465
 */
466
static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
467
    int e;
468

    
469
    assert(i>=0);
470

    
471
    e= i>>k;
472
    if(e<limit){
473
        put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
474
    }else{
475
        put_bits(pb, limit + esc_len, i - limit + 1);
476
    }
477
}
478

    
479
/**
480
 * write unsigned golomb rice code (jpegls).
481
 */
482
static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
483
    int e;
484

    
485
    assert(i>=0);
486

    
487
    e= (i>>k) + 1;
488
    if(e<limit){
489
        while(e > 31) {
490
            put_bits(pb, 31, 0);
491
            e -= 31;
492
        }
493
        put_bits(pb, e, 1);
494
        if(k)
495
            put_sbits(pb, k, i);
496
    }else{
497
        while(limit > 31) {
498
            put_bits(pb, 31, 0);
499
            limit -= 31;
500
        }
501
        put_bits(pb, limit  , 1);
502
        put_bits(pb, esc_len, i - 1);
503
    }
504
}
505

    
506
/**
507
 * write signed golomb rice code (ffv1).
508
 */
509
static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
510
    int v;
511

    
512
    v = -2*i-1;
513
    v ^= (v>>31);
514

    
515
    set_ur_golomb(pb, v, k, limit, esc_len);
516
}
517

    
518
/**
519
 * write signed golomb rice code (flac).
520
 */
521
static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
522
    int v;
523

    
524
    v = -2*i-1;
525
    v ^= (v>>31);
526

    
527
    set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
528
}
529

    
530
#endif /* AVCODEC_GOLOMB_H */