Statistics
| Branch: | Revision:

ffmpeg / libavcodec / golomb.h @ 5509bffa

History | View | Annotate | Download (11 KB)

1 0da71265 Michael Niedermayer
/*
2
 * exp golomb vlc stuff
3
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 4579adb0 Alex Beregszaszi
 * Copyright (c) 2004 Alex Beregszaszi
5 0da71265 Michael Niedermayer
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 0da71265 Michael Niedermayer
 *
20
 */
21 115329f1 Diego Biurrun
22 0da71265 Michael Niedermayer
/**
23
 * @file golomb.h
24 115329f1 Diego Biurrun
 * @brief
25 0da71265 Michael Niedermayer
 *     exp golomb vlc stuff
26 4579adb0 Alex Beregszaszi
 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
27 0da71265 Michael Niedermayer
 */
28
29 8b82a956 Michael Niedermayer
#define INVALID_VLC           0x80000000
30
31 0da71265 Michael Niedermayer
extern const uint8_t ff_golomb_vlc_len[512];
32
extern const uint8_t ff_ue_golomb_vlc_code[512];
33
extern const  int8_t ff_se_golomb_vlc_code[512];
34
extern const uint8_t ff_ue_golomb_len[256];
35
36 4704097a Michael Niedermayer
extern const uint8_t ff_interleaved_golomb_vlc_len[256];
37
extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
38
extern const  int8_t ff_interleaved_se_golomb_vlc_code[256];
39
40 115329f1 Diego Biurrun
41 0da71265 Michael Niedermayer
 /**
42
 * read unsigned exp golomb code.
43
 */
44
static inline int get_ue_golomb(GetBitContext *gb){
45
    unsigned int buf;
46
    int log;
47 115329f1 Diego Biurrun
48 0da71265 Michael Niedermayer
    OPEN_READER(re, gb);
49
    UPDATE_CACHE(re, gb);
50
    buf=GET_CACHE(re, gb);
51 115329f1 Diego Biurrun
52 0da71265 Michael Niedermayer
    if(buf >= (1<<27)){
53
        buf >>= 32 - 9;
54
        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
55
        CLOSE_READER(re, gb);
56 115329f1 Diego Biurrun
57 0da71265 Michael Niedermayer
        return ff_ue_golomb_vlc_code[buf];
58
    }else{
59
        log= 2*av_log2(buf) - 31;
60
        buf>>= log;
61
        buf--;
62
        LAST_SKIP_BITS(re, gb, 32 - log);
63
        CLOSE_READER(re, gb);
64 115329f1 Diego Biurrun
65 0da71265 Michael Niedermayer
        return buf;
66
    }
67
}
68
69 8b82a956 Michael Niedermayer
static inline int svq3_get_ue_golomb(GetBitContext *gb){
70 4704097a Michael Niedermayer
    uint32_t buf;
71 8b82a956 Michael Niedermayer
    int log;
72
73
    OPEN_READER(re, gb);
74
    UPDATE_CACHE(re, gb);
75 4704097a Michael Niedermayer
    buf=GET_CACHE(re, gb);
76 115329f1 Diego Biurrun
77 4704097a Michael Niedermayer
    if(buf&0xAA800000){
78
        buf >>= 32 - 8;
79
        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
80
        CLOSE_READER(re, gb);
81 115329f1 Diego Biurrun
82 4704097a Michael Niedermayer
        return ff_interleaved_ue_golomb_vlc_code[buf];
83
    }else{
84 cceb990e Michael Niedermayer
        LAST_SKIP_BITS(re, gb, 8);
85
        UPDATE_CACHE(re, gb);
86
        buf |= 1 | (GET_CACHE(re, gb) >> 8);
87
88 4704097a Michael Niedermayer
        if((buf & 0xAAAAAAAA) == 0)
89
            return INVALID_VLC;
90 8b82a956 Michael Niedermayer
91 4704097a Michael Niedermayer
        for(log=31; (buf & 0x80000000) == 0; log--){
92
            buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
93
        }
94 8b82a956 Michael Niedermayer
95 cceb990e Michael Niedermayer
        LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
96 4704097a Michael Niedermayer
        CLOSE_READER(re, gb);
97 8b82a956 Michael Niedermayer
98 4704097a Michael Niedermayer
        return ((buf << log) >> log) - 1;
99
    }
100 8b82a956 Michael Niedermayer
}
101
102 0da71265 Michael Niedermayer
/**
103
 * read unsigned truncated exp golomb code.
104
 */
105
static inline int get_te0_golomb(GetBitContext *gb, int range){
106
    assert(range >= 1);
107 115329f1 Diego Biurrun
108 0da71265 Michael Niedermayer
    if(range==1)      return 0;
109 1924f3ce Michael Niedermayer
    else if(range==2) return get_bits1(gb)^1;
110 0da71265 Michael Niedermayer
    else              return get_ue_golomb(gb);
111
}
112
113
/**
114
 * read unsigned truncated exp golomb code.
115
 */
116
static inline int get_te_golomb(GetBitContext *gb, int range){
117
    assert(range >= 1);
118 115329f1 Diego Biurrun
119 1924f3ce Michael Niedermayer
    if(range==2) return get_bits1(gb)^1;
120 0da71265 Michael Niedermayer
    else         return get_ue_golomb(gb);
121
}
122
123
124
/**
125
 * read signed exp golomb code.
126
 */
127
static inline int get_se_golomb(GetBitContext *gb){
128
    unsigned int buf;
129
    int log;
130 115329f1 Diego Biurrun
131 0da71265 Michael Niedermayer
    OPEN_READER(re, gb);
132
    UPDATE_CACHE(re, gb);
133
    buf=GET_CACHE(re, gb);
134 115329f1 Diego Biurrun
135 0da71265 Michael Niedermayer
    if(buf >= (1<<27)){
136
        buf >>= 32 - 9;
137
        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
138
        CLOSE_READER(re, gb);
139 115329f1 Diego Biurrun
140 0da71265 Michael Niedermayer
        return ff_se_golomb_vlc_code[buf];
141
    }else{
142
        log= 2*av_log2(buf) - 31;
143
        buf>>= log;
144 115329f1 Diego Biurrun
145 0da71265 Michael Niedermayer
        LAST_SKIP_BITS(re, gb, 32 - log);
146
        CLOSE_READER(re, gb);
147 115329f1 Diego Biurrun
148 0da71265 Michael Niedermayer
        if(buf&1) buf= -(buf>>1);
149
        else      buf=  (buf>>1);
150
151
        return buf;
152
    }
153
}
154
155 8b82a956 Michael Niedermayer
static inline int svq3_get_se_golomb(GetBitContext *gb){
156
    unsigned int buf;
157
    int log;
158
159
    OPEN_READER(re, gb);
160
    UPDATE_CACHE(re, gb);
161 4704097a Michael Niedermayer
    buf=GET_CACHE(re, gb);
162 8b82a956 Michael Niedermayer
163 4704097a Michael Niedermayer
    if(buf&0xAA800000){
164
        buf >>= 32 - 8;
165
        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
166
        CLOSE_READER(re, gb);
167 115329f1 Diego Biurrun
168 4704097a Michael Niedermayer
        return ff_interleaved_se_golomb_vlc_code[buf];
169
    }else{
170 895345da Michael Niedermayer
        LAST_SKIP_BITS(re, gb, 8);
171
        UPDATE_CACHE(re, gb);
172
        buf |= 1 | (GET_CACHE(re, gb) >> 8);
173
174 4704097a Michael Niedermayer
        if((buf & 0xAAAAAAAA) == 0)
175
            return INVALID_VLC;
176 8b82a956 Michael Niedermayer
177 4704097a Michael Niedermayer
        for(log=31; (buf & 0x80000000) == 0; log--){
178
            buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
179
        }
180 8b82a956 Michael Niedermayer
181 895345da Michael Niedermayer
        LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
182 4704097a Michael Niedermayer
        CLOSE_READER(re, gb);
183 8b82a956 Michael Niedermayer
184 4704097a Michael Niedermayer
        return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
185
    }
186 8b82a956 Michael Niedermayer
}
187
188 11e659c2 Michael Niedermayer
/**
189 d9e6a6c6 Michael Niedermayer
 * read unsigned golomb rice code (ffv1).
190 11e659c2 Michael Niedermayer
 */
191
static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
192
    unsigned int buf;
193
    int log;
194 115329f1 Diego Biurrun
195 11e659c2 Michael Niedermayer
    OPEN_READER(re, gb);
196
    UPDATE_CACHE(re, gb);
197
    buf=GET_CACHE(re, gb);
198
199
    log= av_log2(buf);
200 d9e6a6c6 Michael Niedermayer
201 11e659c2 Michael Niedermayer
    if(log > 31-limit){
202
        buf >>= log - k;
203
        buf += (30-log)<<k;
204
        LAST_SKIP_BITS(re, gb, 32 + k - log);
205
        CLOSE_READER(re, gb);
206 115329f1 Diego Biurrun
207 11e659c2 Michael Niedermayer
        return buf;
208 d9e6a6c6 Michael Niedermayer
    }else{
209
        buf >>= 32 - limit - esc_len;
210
        LAST_SKIP_BITS(re, gb, esc_len + limit);
211 11e659c2 Michael Niedermayer
        CLOSE_READER(re, gb);
212 115329f1 Diego Biurrun
213 d9e6a6c6 Michael Niedermayer
        return buf + limit - 1;
214
    }
215
}
216
217
/**
218
 * read unsigned golomb rice code (jpegls).
219
 */
220
static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
221
    unsigned int buf;
222
    int log;
223 115329f1 Diego Biurrun
224 d9e6a6c6 Michael Niedermayer
    OPEN_READER(re, gb);
225
    UPDATE_CACHE(re, gb);
226
    buf=GET_CACHE(re, gb);
227
228
    log= av_log2(buf);
229 115329f1 Diego Biurrun
230 ce329952 Michael Niedermayer
    if(log > 31-11){
231 d9e6a6c6 Michael Niedermayer
        buf >>= log - k;
232
        buf += (30-log)<<k;
233
        LAST_SKIP_BITS(re, gb, 32 + k - log);
234
        CLOSE_READER(re, gb);
235 115329f1 Diego Biurrun
236 d9e6a6c6 Michael Niedermayer
        return buf;
237
    }else{
238
        int i;
239
        for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
240
            LAST_SKIP_BITS(re, gb, 1);
241
            UPDATE_CACHE(re, gb);
242
        }
243
        SKIP_BITS(re, gb, 1);
244
245
        if(i < limit - 1){
246
            if(k){
247
                buf = SHOW_UBITS(re, gb, k);
248
                LAST_SKIP_BITS(re, gb, k);
249
            }else{
250
                buf=0;
251
            }
252
253
            CLOSE_READER(re, gb);
254
            return buf + (i<<k);
255
        }else if(i == limit - 1){
256
            buf = SHOW_UBITS(re, gb, esc_len);
257
            LAST_SKIP_BITS(re, gb, esc_len);
258
            CLOSE_READER(re, gb);
259 115329f1 Diego Biurrun
260 d9e6a6c6 Michael Niedermayer
            return buf + 1;
261
        }else
262
            return -1;
263
    }
264 11e659c2 Michael Niedermayer
}
265
266 ac2570a8 Michael Niedermayer
/**
267 4579adb0 Alex Beregszaszi
 * read signed golomb rice code (ffv1).
268
 */
269 a6c01275 Michael Niedermayer
static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
270 4579adb0 Alex Beregszaszi
    int v= get_ur_golomb(gb, k, limit, esc_len);
271 115329f1 Diego Biurrun
272 4579adb0 Alex Beregszaszi
    v++;
273
    if (v&1) return v>>1;
274
    else return -(v>>1);
275 115329f1 Diego Biurrun
276 4579adb0 Alex Beregszaszi
//    return (v>>1) ^ -(v&1);
277
}
278
279 a6c01275 Michael Niedermayer
/**
280 4579adb0 Alex Beregszaszi
 * read signed golomb rice code (flac).
281 ac2570a8 Michael Niedermayer
 */
282
static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
283
    int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
284
    return (v>>1) ^ -(v&1);
285
}
286
287 85ad5695 Michael Niedermayer
/**
288
 * read unsigned golomb rice code (shorten).
289
 */
290
static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
291 bb270c08 Diego Biurrun
        return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
292 85ad5695 Michael Niedermayer
}
293
294
/**
295
 * read signed golomb rice code (shorten).
296
 */
297
static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
298
{
299
    int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
300
    if (uvar & 1)
301
        return ~(uvar >> 1);
302
    else
303
        return uvar >> 1;
304
}
305
306
307
308 0da71265 Michael Niedermayer
#ifdef TRACE
309
310 61f040dd Loïc Le Loarer
static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
311 0da71265 Michael Niedermayer
    int show= show_bits(s, 24);
312
    int pos= get_bits_count(s);
313
    int i= get_ue_golomb(s);
314
    int len= get_bits_count(s) - pos;
315
    int bits= show>>(24-len);
316 115329f1 Diego Biurrun
317 0da71265 Michael Niedermayer
    print_bin(bits, len);
318 115329f1 Diego Biurrun
319 4579adb0 Alex Beregszaszi
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue  @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
320 115329f1 Diego Biurrun
321 0da71265 Michael Niedermayer
    return i;
322
}
323
324 61f040dd Loïc Le Loarer
static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
325 0da71265 Michael Niedermayer
    int show= show_bits(s, 24);
326
    int pos= get_bits_count(s);
327
    int i= get_se_golomb(s);
328
    int len= get_bits_count(s) - pos;
329
    int bits= show>>(24-len);
330 115329f1 Diego Biurrun
331 0da71265 Michael Niedermayer
    print_bin(bits, len);
332 115329f1 Diego Biurrun
333 4579adb0 Alex Beregszaszi
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se  @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
334 115329f1 Diego Biurrun
335 0da71265 Michael Niedermayer
    return i;
336
}
337
338 61f040dd Loïc Le Loarer
static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
339 0da71265 Michael Niedermayer
    int show= show_bits(s, 24);
340
    int pos= get_bits_count(s);
341
    int i= get_te0_golomb(s, r);
342
    int len= get_bits_count(s) - pos;
343
    int bits= show>>(24-len);
344 115329f1 Diego Biurrun
345 0da71265 Michael Niedermayer
    print_bin(bits, len);
346 115329f1 Diego Biurrun
347 4579adb0 Alex Beregszaszi
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te  @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
348 115329f1 Diego Biurrun
349 0da71265 Michael Niedermayer
    return i;
350
}
351
352
#define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
353
#define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
354
#define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
355
#define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
356
357
#endif
358
359
/**
360
 * write unsigned exp golomb code.
361
 */
362
static inline void set_ue_golomb(PutBitContext *pb, int i){
363
    int e;
364 115329f1 Diego Biurrun
365 0da71265 Michael Niedermayer
    assert(i>=0);
366
367
#if 0
368
    if(i=0){
369
        put_bits(pb, 1, 1);
370
        return;
371
    }
372
#endif
373
    if(i<256)
374
        put_bits(pb, ff_ue_golomb_len[i], i+1);
375
    else{
376
        e= av_log2(i+1);
377 115329f1 Diego Biurrun
378 0da71265 Michael Niedermayer
        put_bits(pb, 2*e+1, i+1);
379
    }
380
}
381
382
/**
383
 * write truncated unsigned exp golomb code.
384
 */
385
static inline void set_te_golomb(PutBitContext *pb, int i, int range){
386
    assert(range >= 1);
387
    assert(i<=range);
388
389 1924f3ce Michael Niedermayer
    if(range==2) put_bits(pb, 1, i^1);
390 0da71265 Michael Niedermayer
    else         set_ue_golomb(pb, i);
391
}
392
393
/**
394 e134e10f Alex Beregszaszi
 * write signed exp golomb code. 16 bits at most.
395 0da71265 Michael Niedermayer
 */
396
static inline void set_se_golomb(PutBitContext *pb, int i){
397 e134e10f Alex Beregszaszi
//    if (i>32767 || i<-32767)
398 bb270c08 Diego Biurrun
//        av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
399 115329f1 Diego Biurrun
#if 0
400 0da71265 Michael Niedermayer
    if(i<=0) i= -2*i;
401
    else     i=  2*i-1;
402
#elif 1
403
    i= 2*i-1;
404
    if(i<0) i^= -1; //FIXME check if gcc does the right thing
405
#else
406
    i= 2*i-1;
407
    i^= (i>>31);
408
#endif
409
    set_ue_golomb(pb, i);
410
}
411 11e659c2 Michael Niedermayer
412
/**
413 d9e6a6c6 Michael Niedermayer
 * write unsigned golomb rice code (ffv1).
414 11e659c2 Michael Niedermayer
 */
415
static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
416
    int e;
417 115329f1 Diego Biurrun
418 11e659c2 Michael Niedermayer
    assert(i>=0);
419 115329f1 Diego Biurrun
420 11e659c2 Michael Niedermayer
    e= i>>k;
421
    if(e<limit){
422
        put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
423
    }else{
424 d9e6a6c6 Michael Niedermayer
        put_bits(pb, limit + esc_len, i - limit + 1);
425
    }
426
}
427
428
/**
429
 * write unsigned golomb rice code (jpegls).
430
 */
431
static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
432
    int e;
433 115329f1 Diego Biurrun
434 d9e6a6c6 Michael Niedermayer
    assert(i>=0);
435 115329f1 Diego Biurrun
436 d9e6a6c6 Michael Niedermayer
    e= (i>>k) + 1;
437
    if(e<limit){
438
        put_bits(pb, e, 1);
439 ce329952 Michael Niedermayer
        if(k)
440
            put_bits(pb, k, i&((1<<k)-1));
441 d9e6a6c6 Michael Niedermayer
    }else{
442
        put_bits(pb, limit  , 1);
443
        put_bits(pb, esc_len, i - 1);
444 11e659c2 Michael Niedermayer
    }
445
}
446 4579adb0 Alex Beregszaszi
447
/**
448
 * write signed golomb rice code (ffv1).
449
 */
450 a6c01275 Michael Niedermayer
static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
451 4579adb0 Alex Beregszaszi
    int v;
452
453
    v = -2*i-1;
454
    v ^= (v>>31);
455
456
    set_ur_golomb(pb, v, k, limit, esc_len);
457
}
458
459
/**
460
 * write signed golomb rice code (flac).
461
 */
462
static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
463
    int v;
464
465
    v = -2*i-1;
466
    v ^= (v>>31);
467
468
    set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
469
}