Statistics
| Branch: | Revision:

ffmpeg / libavcodec / golomb.h @ bb54f6ab

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