3
* Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4
* Copyright (c) 2004 Alex Beregszaszi
6
* This file is part of FFmpeg.
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.
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.
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
26
* exp golomb vlc stuff
27
* @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
30
#ifndef AVCODEC_GOLOMB_H
31
#define AVCODEC_GOLOMB_H
34
#include "bitstream.h"
36
#define INVALID_VLC 0x80000000
38
extern const uint8_t ff_golomb_vlc_len[512];
39
extern const uint8_t ff_ue_golomb_vlc_code[512];
40
extern const int8_t ff_se_golomb_vlc_code[512];
41
extern const uint8_t ff_ue_golomb_len[256];
43
extern const uint8_t ff_interleaved_golomb_vlc_len[256];
44
extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
45
extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
46
extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
50
* read unsigned exp golomb code.
52
static inline int get_ue_golomb(GetBitContext *gb){
58
buf=GET_CACHE(re, gb);
62
LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
65
return ff_ue_golomb_vlc_code[buf];
67
log= 2*av_log2(buf) - 31;
70
LAST_SKIP_BITS(re, gb, 32 - log);
77
static inline int svq3_get_ue_golomb(GetBitContext *gb){
82
buf=GET_CACHE(re, gb);
86
LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
89
return ff_interleaved_ue_golomb_vlc_code[buf];
95
LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
97
if (ff_interleaved_golomb_vlc_len[buf] != 9){
98
ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
99
ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
102
ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
103
UPDATE_CACHE(re, gb);
104
buf = GET_CACHE(re, gb);
107
CLOSE_READER(re, gb);
113
* read unsigned truncated exp golomb code.
115
static inline int get_te0_golomb(GetBitContext *gb, int range){
118
if(range==1) return 0;
119
else if(range==2) return get_bits1(gb)^1;
120
else return get_ue_golomb(gb);
124
* read unsigned truncated exp golomb code.
126
static inline int get_te_golomb(GetBitContext *gb, int range){
129
if(range==2) return get_bits1(gb)^1;
130
else return get_ue_golomb(gb);
135
* read signed exp golomb code.
137
static inline int get_se_golomb(GetBitContext *gb){
142
UPDATE_CACHE(re, gb);
143
buf=GET_CACHE(re, gb);
147
LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
148
CLOSE_READER(re, gb);
150
return ff_se_golomb_vlc_code[buf];
152
log= 2*av_log2(buf) - 31;
155
LAST_SKIP_BITS(re, gb, 32 - log);
156
CLOSE_READER(re, gb);
158
if(buf&1) buf= -(buf>>1);
165
static inline int svq3_get_se_golomb(GetBitContext *gb){
170
UPDATE_CACHE(re, gb);
171
buf=GET_CACHE(re, gb);
175
LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
176
CLOSE_READER(re, gb);
178
return ff_interleaved_se_golomb_vlc_code[buf];
180
LAST_SKIP_BITS(re, gb, 8);
181
UPDATE_CACHE(re, gb);
182
buf |= 1 | (GET_CACHE(re, gb) >> 8);
184
if((buf & 0xAAAAAAAA) == 0)
187
for(log=31; (buf & 0x80000000) == 0; log--){
188
buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
191
LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
192
CLOSE_READER(re, gb);
194
return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
198
static inline int dirac_get_se_golomb(GetBitContext *gb){
202
ret = svq3_get_ue_golomb(gb);
206
UPDATE_CACHE(re, gb);
207
buf = SHOW_SBITS(re, gb, 1);
208
LAST_SKIP_BITS(re, gb, 1);
209
ret = (ret ^ buf) - buf;
210
CLOSE_READER(re, gb);
217
* read unsigned golomb rice code (ffv1).
219
static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
224
UPDATE_CACHE(re, gb);
225
buf=GET_CACHE(re, gb);
232
LAST_SKIP_BITS(re, gb, 32 + k - log);
233
CLOSE_READER(re, gb);
237
buf >>= 32 - limit - esc_len;
238
LAST_SKIP_BITS(re, gb, esc_len + limit);
239
CLOSE_READER(re, gb);
241
return buf + limit - 1;
246
* read unsigned golomb rice code (jpegls).
248
static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
253
UPDATE_CACHE(re, gb);
254
buf=GET_CACHE(re, gb);
261
LAST_SKIP_BITS(re, gb, 32 + k - log);
262
CLOSE_READER(re, gb);
267
for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
268
LAST_SKIP_BITS(re, gb, 1);
269
UPDATE_CACHE(re, gb);
271
SKIP_BITS(re, gb, 1);
275
buf = SHOW_UBITS(re, gb, k);
276
LAST_SKIP_BITS(re, gb, k);
281
CLOSE_READER(re, gb);
283
}else if(i == limit - 1){
284
buf = SHOW_UBITS(re, gb, esc_len);
285
LAST_SKIP_BITS(re, gb, esc_len);
286
CLOSE_READER(re, gb);
295
* read signed golomb rice code (ffv1).
297
static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
298
int v= get_ur_golomb(gb, k, limit, esc_len);
301
if (v&1) return v>>1;
304
// return (v>>1) ^ -(v&1);
308
* read signed golomb rice code (flac).
310
static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
311
int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
312
return (v>>1) ^ -(v&1);
316
* read unsigned golomb rice code (shorten).
318
static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
319
return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
323
* read signed golomb rice code (shorten).
325
static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
327
int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
338
static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
339
int show= show_bits(s, 24);
340
int pos= get_bits_count(s);
341
int i= get_ue_golomb(s);
342
int len= get_bits_count(s) - pos;
343
int bits= show>>(24-len);
345
print_bin(bits, len);
347
av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
352
static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
353
int show= show_bits(s, 24);
354
int pos= get_bits_count(s);
355
int i= get_se_golomb(s);
356
int len= get_bits_count(s) - pos;
357
int bits= show>>(24-len);
359
print_bin(bits, len);
361
av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
366
static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
367
int show= show_bits(s, 24);
368
int pos= get_bits_count(s);
369
int i= get_te0_golomb(s, r);
370
int len= get_bits_count(s) - pos;
371
int bits= show>>(24-len);
373
print_bin(bits, len);
375
av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
380
#define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
381
#define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
382
#define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
383
#define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
388
* write unsigned exp golomb code.
390
static inline void set_ue_golomb(PutBitContext *pb, int i){
402
put_bits(pb, ff_ue_golomb_len[i], i+1);
406
put_bits(pb, 2*e+1, i+1);
411
* write truncated unsigned exp golomb code.
413
static inline void set_te_golomb(PutBitContext *pb, int i, int range){
417
if(range==2) put_bits(pb, 1, i^1);
418
else set_ue_golomb(pb, i);
422
* write signed exp golomb code. 16 bits at most.
424
static inline void set_se_golomb(PutBitContext *pb, int i){
425
// if (i>32767 || i<-32767)
426
// av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
432
if(i<0) i^= -1; //FIXME check if gcc does the right thing
437
set_ue_golomb(pb, i);
441
* write unsigned golomb rice code (ffv1).
443
static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
450
put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
452
put_bits(pb, limit + esc_len, i - limit + 1);
457
* write unsigned golomb rice code (jpegls).
459
static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
472
put_bits(pb, k, i&((1<<k)-1));
478
put_bits(pb, limit , 1);
479
put_bits(pb, esc_len, i - 1);
484
* write signed golomb rice code (ffv1).
486
static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
492
set_ur_golomb(pb, v, k, limit, esc_len);
496
* write signed golomb rice code (flac).
498
static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
504
set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
507
#endif // AVCODEC_GOLOMB_H