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
37 #define INVALID_VLC 0x80000000
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];
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];
49 * Read an unsigned Exp-Golomb code in the range 0 to 8190.
51 * @returns the read value or a negative error code.
53 static inline int get_ue_golomb(GetBitContext
*gb
)
57 #if CACHED_BITSTREAM_READER
58 buf
= show_bits_long(gb
, 32);
60 if (buf
>= (1 << 27)) {
62 skip_bits_long(gb
, ff_golomb_vlc_len
[buf
]);
64 return ff_ue_golomb_vlc_code
[buf
];
66 int log
= 2 * av_log2(buf
) - 31;
68 skip_bits_long(gb
, 32 - log
);
70 return AVERROR_INVALIDDATA
;
79 buf
= GET_CACHE(re
, gb
);
81 if (buf
>= (1 << 27)) {
83 LAST_SKIP_BITS(re
, gb
, ff_golomb_vlc_len
[buf
]);
86 return ff_ue_golomb_vlc_code
[buf
];
88 int log
= 2 * av_log2(buf
) - 31;
89 LAST_SKIP_BITS(re
, gb
, 32 - log
);
92 return AVERROR_INVALIDDATA
;
102 * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
104 static inline unsigned get_ue_golomb_long(GetBitContext
*gb
)
108 buf
= show_bits_long(gb
, 32);
109 log
= 31 - av_log2(buf
);
110 skip_bits_long(gb
, log
);
112 return get_bits_long(gb
, log
+ 1) - 1;
116 * read unsigned exp golomb code, constraint to a max of 31.
117 * If the value encountered is not in 0..31, the return value
118 * is outside the range 0..30.
120 static inline int get_ue_golomb_31(GetBitContext
*gb
)
124 #if CACHED_BITSTREAM_READER
125 buf
= show_bits_long(gb
, 32);
128 skip_bits_long(gb
, ff_golomb_vlc_len
[buf
]);
132 UPDATE_CACHE(re
, gb
);
133 buf
= GET_CACHE(re
, gb
);
136 LAST_SKIP_BITS(re
, gb
, ff_golomb_vlc_len
[buf
]);
137 CLOSE_READER(re
, gb
);
140 return ff_ue_golomb_vlc_code
[buf
];
143 static inline unsigned get_interleaved_ue_golomb(GetBitContext
*gb
)
147 #if CACHED_BITSTREAM_READER
148 buf
= show_bits_long(gb
, 32);
150 if (buf
& 0xAA800000) {
152 skip_bits_long(gb
, ff_interleaved_golomb_vlc_len
[buf
]);
154 return ff_interleaved_ue_golomb_vlc_code
[buf
];
160 skip_bits_long(gb
, FFMIN(ff_interleaved_golomb_vlc_len
[buf
], 8));
162 if (ff_interleaved_golomb_vlc_len
[buf
] != 9) {
163 ret
<<= (ff_interleaved_golomb_vlc_len
[buf
] - 1) >> 1;
164 ret
|= ff_interleaved_dirac_golomb_vlc_code
[buf
];
167 ret
= (ret
<< 4) | ff_interleaved_dirac_golomb_vlc_code
[buf
];
168 buf
= show_bits_long(gb
, 32);
169 } while (get_bits_left(gb
) > 0);
175 UPDATE_CACHE(re
, gb
);
176 buf
= GET_CACHE(re
, gb
);
178 if (buf
& 0xAA800000) {
180 LAST_SKIP_BITS(re
, gb
, ff_interleaved_golomb_vlc_len
[buf
]);
181 CLOSE_READER(re
, gb
);
183 return ff_interleaved_ue_golomb_vlc_code
[buf
];
189 LAST_SKIP_BITS(re
, gb
,
190 FFMIN(ff_interleaved_golomb_vlc_len
[buf
], 8));
192 if (ff_interleaved_golomb_vlc_len
[buf
] != 9) {
193 ret
<<= (ff_interleaved_golomb_vlc_len
[buf
] - 1) >> 1;
194 ret
|= ff_interleaved_dirac_golomb_vlc_code
[buf
];
197 ret
= (ret
<< 4) | ff_interleaved_dirac_golomb_vlc_code
[buf
];
198 UPDATE_CACHE(re
, gb
);
199 buf
= GET_CACHE(re
, gb
);
200 } while (ret
<0x8000000U
&& BITS_AVAILABLE(re
, gb
));
202 CLOSE_READER(re
, gb
);
209 * read unsigned truncated exp golomb code.
211 static inline int get_te0_golomb(GetBitContext
*gb
, int range
)
213 av_assert2(range
>= 1);
218 return get_bits1(gb
) ^ 1;
220 return get_ue_golomb(gb
);
224 * read unsigned truncated exp golomb code.
226 static inline int get_te_golomb(GetBitContext
*gb
, int range
)
228 av_assert2(range
>= 1);
231 return get_bits1(gb
) ^ 1;
233 return get_ue_golomb(gb
);
237 * read signed exp golomb code.
239 static inline int get_se_golomb(GetBitContext
*gb
)
243 #if CACHED_BITSTREAM_READER
244 buf
= show_bits_long(gb
, 32);
246 if (buf
>= (1 << 27)) {
248 skip_bits_long(gb
, ff_golomb_vlc_len
[buf
]);
250 return ff_se_golomb_vlc_code
[buf
];
252 int log
= 2 * av_log2(buf
) - 31;
255 skip_bits_long(gb
, 32 - log
);
266 UPDATE_CACHE(re
, gb
);
267 buf
= GET_CACHE(re
, gb
);
269 if (buf
>= (1 << 27)) {
271 LAST_SKIP_BITS(re
, gb
, ff_golomb_vlc_len
[buf
]);
272 CLOSE_READER(re
, gb
);
274 return ff_se_golomb_vlc_code
[buf
];
276 int log
= av_log2(buf
), sign
;
277 LAST_SKIP_BITS(re
, gb
, 31 - log
);
278 UPDATE_CACHE(re
, gb
);
279 buf
= GET_CACHE(re
, gb
);
283 LAST_SKIP_BITS(re
, gb
, 32 - log
);
284 CLOSE_READER(re
, gb
);
287 buf
= ((buf
>> 1) ^ sign
) - sign
;
294 static inline int get_se_golomb_long(GetBitContext
*gb
)
296 unsigned int buf
= get_ue_golomb_long(gb
);
297 int sign
= (buf
& 1) - 1;
298 return ((buf
>> 1) ^ sign
) + 1;
301 static inline int get_interleaved_se_golomb(GetBitContext
*gb
)
305 #if CACHED_BITSTREAM_READER
306 buf
= show_bits_long(gb
, 32);
308 if (buf
& 0xAA800000) {
310 skip_bits_long(gb
, ff_interleaved_golomb_vlc_len
[buf
]);
312 return ff_interleaved_se_golomb_vlc_code
[buf
];
316 buf
|= 1 | show_bits(gb
, 24);
318 if ((buf
& 0xAAAAAAAA) == 0)
321 for (log
= 31; (buf
& 0x80000000) == 0; log
--)
322 buf
= (buf
<< 2) - ((buf
<< log
) >> (log
- 1)) + (buf
>> 30);
324 skip_bits_long(gb
, 63 - 2 * log
- 8);
326 return (signed) (((((buf
<< log
) >> log
) - 1) ^ -(buf
& 0x1)) + 1) >> 1;
330 UPDATE_CACHE(re
, gb
);
331 buf
= GET_CACHE(re
, gb
);
333 if (buf
& 0xAA800000) {
335 LAST_SKIP_BITS(re
, gb
, ff_interleaved_golomb_vlc_len
[buf
]);
336 CLOSE_READER(re
, gb
);
338 return ff_interleaved_se_golomb_vlc_code
[buf
];
341 LAST_SKIP_BITS(re
, gb
, 8);
342 UPDATE_CACHE(re
, gb
);
343 buf
|= 1 | (GET_CACHE(re
, gb
) >> 8);
345 if ((buf
& 0xAAAAAAAA) == 0)
348 for (log
= 31; (buf
& 0x80000000) == 0; log
--)
349 buf
= (buf
<< 2) - ((buf
<< log
) >> (log
- 1)) + (buf
>> 30);
351 LAST_SKIP_BITS(re
, gb
, 63 - 2 * log
- 8);
352 CLOSE_READER(re
, gb
);
354 return (signed) (((((buf
<< log
) >> log
) - 1) ^ -(buf
& 0x1)) + 1) >> 1;
359 static inline int dirac_get_se_golomb(GetBitContext
*gb
)
361 uint32_t ret
= get_interleaved_ue_golomb(gb
);
364 int sign
= -get_bits1(gb
);
365 ret
= (ret
^ sign
) - sign
;
372 * read unsigned golomb rice code (ffv1).
374 static inline int get_ur_golomb(GetBitContext
*gb
, int k
, int limit
,
380 #if CACHED_BITSTREAM_READER
381 buf
= show_bits_long(gb
, 32);
385 if (log
> 31 - limit
) {
387 buf
+= (30 - log
) << k
;
388 skip_bits_long(gb
, 32 + k
- log
);
392 skip_bits_long(gb
, limit
);
393 buf
= get_bits_long(gb
, esc_len
);
395 return buf
+ limit
- 1;
399 UPDATE_CACHE(re
, gb
);
400 buf
= GET_CACHE(re
, gb
);
404 if (log
> 31 - limit
) {
405 av_assert2(log
>= k
);
407 buf
+= (30U - log
) << k
;
408 LAST_SKIP_BITS(re
, gb
, 32 + k
- log
);
409 CLOSE_READER(re
, gb
);
413 LAST_SKIP_BITS(re
, gb
, limit
);
414 UPDATE_CACHE(re
, gb
);
416 buf
= SHOW_UBITS(re
, gb
, esc_len
);
418 LAST_SKIP_BITS(re
, gb
, esc_len
);
419 CLOSE_READER(re
, gb
);
421 return buf
+ limit
- 1;
427 * read unsigned golomb rice code (jpegls).
429 * @returns -1 on error
431 static inline int get_ur_golomb_jpegls(GetBitContext
*gb
, int k
, int limit
,
437 #if CACHED_BITSTREAM_READER
438 buf
= show_bits_long(gb
, 32);
442 if (log
- k
>= 1 && 32 - log
< limit
) {
444 buf
+= (30 - log
) << k
;
445 skip_bits_long(gb
, 32 + k
- log
);
451 i
< limit
&& get_bits1(gb
) == 0 && get_bits_left(gb
) > 0;
455 buf
= get_bits_long(gb
, k
);
457 return buf
+ (i
<< k
);
458 } else if (i
== limit
- 1) {
459 buf
= get_bits_long(gb
, esc_len
);
467 UPDATE_CACHE(re
, gb
);
468 buf
= GET_CACHE(re
, gb
);
474 if (log
- k
>= 32 - MIN_CACHE_BITS
+ (MIN_CACHE_BITS
== 32) &&
477 buf
+= (30U - log
) << k
;
478 LAST_SKIP_BITS(re
, gb
, 32 + k
- log
);
479 CLOSE_READER(re
, gb
);
484 for (i
= 0; i
+ MIN_CACHE_BITS
<= limit
&& SHOW_UBITS(re
, gb
, MIN_CACHE_BITS
) == 0; i
+= MIN_CACHE_BITS
) {
485 if (gb
->size_in_bits
<= re_index
) {
486 CLOSE_READER(re
, gb
);
489 LAST_SKIP_BITS(re
, gb
, MIN_CACHE_BITS
);
490 UPDATE_CACHE(re
, gb
);
492 for (; i
< limit
&& SHOW_UBITS(re
, gb
, 1) == 0; i
++) {
493 SKIP_BITS(re
, gb
, 1);
495 LAST_SKIP_BITS(re
, gb
, 1);
496 UPDATE_CACHE(re
, gb
);
500 if (k
> MIN_CACHE_BITS
- 1) {
501 buf
= SHOW_UBITS(re
, gb
, 16) << (k
-16);
502 LAST_SKIP_BITS(re
, gb
, 16);
503 UPDATE_CACHE(re
, gb
);
504 buf
|= SHOW_UBITS(re
, gb
, k
-16);
505 LAST_SKIP_BITS(re
, gb
, k
-16);
507 buf
= SHOW_UBITS(re
, gb
, k
);
508 LAST_SKIP_BITS(re
, gb
, k
);
514 buf
+= ((SUINT
)i
<< k
);
515 } else if (i
== limit
- 1) {
516 buf
= SHOW_UBITS(re
, gb
, esc_len
);
517 LAST_SKIP_BITS(re
, gb
, esc_len
);
523 CLOSE_READER(re
, gb
);
530 * read signed golomb rice code (ffv1).
532 static inline int get_sr_golomb(GetBitContext
*gb
, int k
, int limit
,
535 unsigned v
= get_ur_golomb(gb
, k
, limit
, esc_len
);
536 return (v
>> 1) ^ -(v
& 1);
540 * read signed golomb rice code (flac).
542 * @returns INT_MIN on error
544 static inline int get_sr_golomb_flac(GetBitContext
*gb
, int k
, int limit
,
547 unsigned v
= get_ur_golomb_jpegls(gb
, k
, limit
, esc_len
);
548 return (v
>> 1) ^ -(v
& 1);
552 * read unsigned golomb rice code (shorten).
554 static inline unsigned int get_ur_golomb_shorten(GetBitContext
*gb
, int k
)
556 return get_ur_golomb_jpegls(gb
, k
, INT_MAX
, 0);
560 * read signed golomb rice code (shorten).
562 static inline int get_sr_golomb_shorten(GetBitContext
*gb
, int k
)
564 int uvar
= get_ur_golomb_jpegls(gb
, k
+ 1, INT_MAX
, 0);
565 return (uvar
>> 1) ^ -(uvar
& 1);
570 static inline int get_ue(GetBitContext
*s
, const char *file
, const char *func
,
573 int show
= show_bits(s
, 24);
574 int pos
= get_bits_count(s
);
575 int i
= get_ue_golomb(s
);
576 int len
= get_bits_count(s
) - pos
;
577 int bits
= show
>> (24 - len
);
579 av_log(NULL
, AV_LOG_DEBUG
, "%5d %2d %3d ue @%5d in %s %s:%d\n",
580 bits
, len
, i
, pos
, file
, func
, line
);
585 static inline int get_se(GetBitContext
*s
, const char *file
, const char *func
,
588 int show
= show_bits(s
, 24);
589 int pos
= get_bits_count(s
);
590 int i
= get_se_golomb(s
);
591 int len
= get_bits_count(s
) - pos
;
592 int bits
= show
>> (24 - len
);
594 av_log(NULL
, AV_LOG_DEBUG
, "%5d %2d %3d se @%5d in %s %s:%d\n",
595 bits
, len
, i
, pos
, file
, func
, line
);
600 static inline int get_te(GetBitContext
*s
, int r
, char *file
, const char *func
,
603 int show
= show_bits(s
, 24);
604 int pos
= get_bits_count(s
);
605 int i
= get_te0_golomb(s
, r
);
606 int len
= get_bits_count(s
) - pos
;
607 int bits
= show
>> (24 - len
);
609 av_log(NULL
, AV_LOG_DEBUG
, "%5d %2d %3d te @%5d in %s %s:%d\n",
610 bits
, len
, i
, pos
, file
, func
, line
);
615 #define get_ue_golomb(a) get_ue(a, __FILE__, __func__, __LINE__)
616 #define get_se_golomb(a) get_se(a, __FILE__, __func__, __LINE__)
617 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
618 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
621 #endif /* AVCODEC_GOLOMB_H */