Statistics
| Branch: | Revision:

ffmpeg / libavcodec / golomb.h @ 699b3f99

History | View | Annotate | Download (11.3 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 FFmpeg.
7
 *
8
 * FFmpeg 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
 * FFmpeg 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 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
/**
25
 * @file golomb.h
26
 * @brief
27
 *     exp golomb vlc stuff
28
 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
29
 */
30

    
31
#ifndef AVCODEC_GOLOMB_H
32
#define AVCODEC_GOLOMB_H
33

    
34
#include <stdint.h>
35
#include "bitstream.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

    
48

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

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

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

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

    
73
        return buf;
74
    }
75
}
76

    
77
static inline int svq3_get_ue_golomb(GetBitContext *gb){
78
    uint32_t buf;
79
    int log;
80

    
81
    OPEN_READER(re, gb);
82
    UPDATE_CACHE(re, gb);
83
    buf=GET_CACHE(re, gb);
84

    
85
    if(buf&0xAA800000){
86
        buf >>= 32 - 8;
87
        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
88
        CLOSE_READER(re, gb);
89

    
90
        return ff_interleaved_ue_golomb_vlc_code[buf];
91
    }else{
92
        LAST_SKIP_BITS(re, gb, 8);
93
        UPDATE_CACHE(re, gb);
94
        buf |= 1 | (GET_CACHE(re, gb) >> 8);
95

    
96
        if((buf & 0xAAAAAAAA) == 0)
97
            return INVALID_VLC;
98

    
99
        for(log=31; (buf & 0x80000000) == 0; log--){
100
            buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
101
        }
102

    
103
        LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
104
        CLOSE_READER(re, gb);
105

    
106
        return ((buf << log) >> log) - 1;
107
    }
108
}
109

    
110
/**
111
 * read unsigned truncated exp golomb code.
112
 */
113
static inline int get_te0_golomb(GetBitContext *gb, int range){
114
    assert(range >= 1);
115

    
116
    if(range==1)      return 0;
117
    else if(range==2) return get_bits1(gb)^1;
118
    else              return get_ue_golomb(gb);
119
}
120

    
121
/**
122
 * read unsigned truncated exp golomb code.
123
 */
124
static inline int get_te_golomb(GetBitContext *gb, int range){
125
    assert(range >= 1);
126

    
127
    if(range==2) return get_bits1(gb)^1;
128
    else         return get_ue_golomb(gb);
129
}
130

    
131

    
132
/**
133
 * read signed exp golomb code.
134
 */
135
static inline int get_se_golomb(GetBitContext *gb){
136
    unsigned int buf;
137
    int log;
138

    
139
    OPEN_READER(re, gb);
140
    UPDATE_CACHE(re, gb);
141
    buf=GET_CACHE(re, gb);
142

    
143
    if(buf >= (1<<27)){
144
        buf >>= 32 - 9;
145
        LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
146
        CLOSE_READER(re, gb);
147

    
148
        return ff_se_golomb_vlc_code[buf];
149
    }else{
150
        log= 2*av_log2(buf) - 31;
151
        buf>>= log;
152

    
153
        LAST_SKIP_BITS(re, gb, 32 - log);
154
        CLOSE_READER(re, gb);
155

    
156
        if(buf&1) buf= -(buf>>1);
157
        else      buf=  (buf>>1);
158

    
159
        return buf;
160
    }
161
}
162

    
163
static inline int svq3_get_se_golomb(GetBitContext *gb){
164
    unsigned int buf;
165
    int log;
166

    
167
    OPEN_READER(re, gb);
168
    UPDATE_CACHE(re, gb);
169
    buf=GET_CACHE(re, gb);
170

    
171
    if(buf&0xAA800000){
172
        buf >>= 32 - 8;
173
        LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
174
        CLOSE_READER(re, gb);
175

    
176
        return ff_interleaved_se_golomb_vlc_code[buf];
177
    }else{
178
        LAST_SKIP_BITS(re, gb, 8);
179
        UPDATE_CACHE(re, gb);
180
        buf |= 1 | (GET_CACHE(re, gb) >> 8);
181

    
182
        if((buf & 0xAAAAAAAA) == 0)
183
            return INVALID_VLC;
184

    
185
        for(log=31; (buf & 0x80000000) == 0; log--){
186
            buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
187
        }
188

    
189
        LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
190
        CLOSE_READER(re, gb);
191

    
192
        return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
193
    }
194
}
195

    
196
/**
197
 * read unsigned golomb rice code (ffv1).
198
 */
199
static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
200
    unsigned int buf;
201
    int log;
202

    
203
    OPEN_READER(re, gb);
204
    UPDATE_CACHE(re, gb);
205
    buf=GET_CACHE(re, gb);
206

    
207
    log= av_log2(buf);
208

    
209
    if(log > 31-limit){
210
        buf >>= log - k;
211
        buf += (30-log)<<k;
212
        LAST_SKIP_BITS(re, gb, 32 + k - log);
213
        CLOSE_READER(re, gb);
214

    
215
        return buf;
216
    }else{
217
        buf >>= 32 - limit - esc_len;
218
        LAST_SKIP_BITS(re, gb, esc_len + limit);
219
        CLOSE_READER(re, gb);
220

    
221
        return buf + limit - 1;
222
    }
223
}
224

    
225
/**
226
 * read unsigned golomb rice code (jpegls).
227
 */
228
static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
229
    unsigned int buf;
230
    int log;
231

    
232
    OPEN_READER(re, gb);
233
    UPDATE_CACHE(re, gb);
234
    buf=GET_CACHE(re, gb);
235

    
236
    log= av_log2(buf);
237

    
238
    if(log > 31-11){
239
        buf >>= log - k;
240
        buf += (30-log)<<k;
241
        LAST_SKIP_BITS(re, gb, 32 + k - log);
242
        CLOSE_READER(re, gb);
243

    
244
        return buf;
245
    }else{
246
        int i;
247
        for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
248
            LAST_SKIP_BITS(re, gb, 1);
249
            UPDATE_CACHE(re, gb);
250
        }
251
        SKIP_BITS(re, gb, 1);
252

    
253
        if(i < limit - 1){
254
            if(k){
255
                buf = SHOW_UBITS(re, gb, k);
256
                LAST_SKIP_BITS(re, gb, k);
257
            }else{
258
                buf=0;
259
            }
260

    
261
            CLOSE_READER(re, gb);
262
            return buf + (i<<k);
263
        }else if(i == limit - 1){
264
            buf = SHOW_UBITS(re, gb, esc_len);
265
            LAST_SKIP_BITS(re, gb, esc_len);
266
            CLOSE_READER(re, gb);
267

    
268
            return buf + 1;
269
        }else
270
            return -1;
271
    }
272
}
273

    
274
/**
275
 * read signed golomb rice code (ffv1).
276
 */
277
static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
278
    int v= get_ur_golomb(gb, k, limit, esc_len);
279

    
280
    v++;
281
    if (v&1) return v>>1;
282
    else return -(v>>1);
283

    
284
//    return (v>>1) ^ -(v&1);
285
}
286

    
287
/**
288
 * read signed golomb rice code (flac).
289
 */
290
static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
291
    int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
292
    return (v>>1) ^ -(v&1);
293
}
294

    
295
/**
296
 * read unsigned golomb rice code (shorten).
297
 */
298
static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
299
        return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
300
}
301

    
302
/**
303
 * read signed golomb rice code (shorten).
304
 */
305
static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
306
{
307
    int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
308
    if (uvar & 1)
309
        return ~(uvar >> 1);
310
    else
311
        return uvar >> 1;
312
}
313

    
314

    
315

    
316
#ifdef TRACE
317

    
318
static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
319
    int show= show_bits(s, 24);
320
    int pos= get_bits_count(s);
321
    int i= get_ue_golomb(s);
322
    int len= get_bits_count(s) - pos;
323
    int bits= show>>(24-len);
324

    
325
    print_bin(bits, len);
326

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

    
329
    return i;
330
}
331

    
332
static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
333
    int show= show_bits(s, 24);
334
    int pos= get_bits_count(s);
335
    int i= get_se_golomb(s);
336
    int len= get_bits_count(s) - pos;
337
    int bits= show>>(24-len);
338

    
339
    print_bin(bits, len);
340

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

    
343
    return i;
344
}
345

    
346
static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
347
    int show= show_bits(s, 24);
348
    int pos= get_bits_count(s);
349
    int i= get_te0_golomb(s, r);
350
    int len= get_bits_count(s) - pos;
351
    int bits= show>>(24-len);
352

    
353
    print_bin(bits, len);
354

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

    
357
    return i;
358
}
359

    
360
#define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
361
#define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
362
#define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
363
#define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
364

    
365
#endif
366

    
367
/**
368
 * write unsigned exp golomb code.
369
 */
370
static inline void set_ue_golomb(PutBitContext *pb, int i){
371
    int e;
372

    
373
    assert(i>=0);
374

    
375
#if 0
376
    if(i=0){
377
        put_bits(pb, 1, 1);
378
        return;
379
    }
380
#endif
381
    if(i<256)
382
        put_bits(pb, ff_ue_golomb_len[i], i+1);
383
    else{
384
        e= av_log2(i+1);
385

    
386
        put_bits(pb, 2*e+1, i+1);
387
    }
388
}
389

    
390
/**
391
 * write truncated unsigned exp golomb code.
392
 */
393
static inline void set_te_golomb(PutBitContext *pb, int i, int range){
394
    assert(range >= 1);
395
    assert(i<=range);
396

    
397
    if(range==2) put_bits(pb, 1, i^1);
398
    else         set_ue_golomb(pb, i);
399
}
400

    
401
/**
402
 * write signed exp golomb code. 16 bits at most.
403
 */
404
static inline void set_se_golomb(PutBitContext *pb, int i){
405
//    if (i>32767 || i<-32767)
406
//        av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
407
#if 0
408
    if(i<=0) i= -2*i;
409
    else     i=  2*i-1;
410
#elif 1
411
    i= 2*i-1;
412
    if(i<0) i^= -1; //FIXME check if gcc does the right thing
413
#else
414
    i= 2*i-1;
415
    i^= (i>>31);
416
#endif
417
    set_ue_golomb(pb, i);
418
}
419

    
420
/**
421
 * write unsigned golomb rice code (ffv1).
422
 */
423
static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
424
    int e;
425

    
426
    assert(i>=0);
427

    
428
    e= i>>k;
429
    if(e<limit){
430
        put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
431
    }else{
432
        put_bits(pb, limit + esc_len, i - limit + 1);
433
    }
434
}
435

    
436
/**
437
 * write unsigned golomb rice code (jpegls).
438
 */
439
static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
440
    int e;
441

    
442
    assert(i>=0);
443

    
444
    e= (i>>k) + 1;
445
    if(e<limit){
446
        while(e > 31) {
447
            put_bits(pb, 31, 0);
448
            e -= 31;
449
        }
450
        put_bits(pb, e, 1);
451
        if(k)
452
            put_bits(pb, k, i&((1<<k)-1));
453
    }else{
454
        while(limit > 31) {
455
            put_bits(pb, 31, 0);
456
            limit -= 31;
457
        }
458
        put_bits(pb, limit  , 1);
459
        put_bits(pb, esc_len, i - 1);
460
    }
461
}
462

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

    
469
    v = -2*i-1;
470
    v ^= (v>>31);
471

    
472
    set_ur_golomb(pb, v, k, limit, esc_len);
473
}
474

    
475
/**
476
 * write signed golomb rice code (flac).
477
 */
478
static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
479
    int v;
480

    
481
    v = -2*i-1;
482
    v ^= (v>>31);
483

    
484
    set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
485
}
486

    
487
#endif