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 * @file
25 * @brief
26 * exp golomb vlc stuff
27 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
28 */
29
30 #ifndef AVCODEC_GOLOMB_H
31 #define AVCODEC_GOLOMB_H
32
33 #include <stdint.h>
34
36
37 #define INVALID_VLC 0x80000000
38
42
47
48 /**
49 * Read an unsigned Exp-Golomb code in the range 0 to 8190.
50 *
51 * @returns the read value or a negative error code.
52 */
54 {
55 unsigned int buf;
56
57 #if CACHED_BITSTREAM_READER
59
60 if (buf >= (1 << 27)) {
61 buf >>= 32 - 9;
63
65 } else {
67
72 buf--;
73
74 return buf;
75 }
76 #else
80
81 if (buf >= (1 << 27)) {
82 buf >>= 32 - 9;
85
87 } else {
94 buf--;
95
96 return buf;
97 }
98 #endif
99 }
100
101 /**
102 * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
103 */
105 {
107
111
113 }
114
115 /**
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.
119 */
121 {
122 unsigned int buf;
123
124 #if CACHED_BITSTREAM_READER
126
127 buf >>= 32 - 9;
129 #else
130
134
135 buf >>= 32 - 9;
138 #endif
139
141 }
142
144 {
145 uint32_t buf;
146
147 #if CACHED_BITSTREAM_READER
149
150 if (buf & 0xAA800000) {
151 buf >>= 32 - 8;
153
155 } else {
157
158 do {
159 buf >>= 32 - 8;
161
165 break;
166 }
170
172 }
173 #else
177
178 if (buf & 0xAA800000) {
179 buf >>= 32 - 8;
182
184 } else {
186
187 do {
188 buf >>= 32 - 8;
191
195 break;
196 }
201
204 }
205 #endif
206 }
207
208 /**
209 * read unsigned truncated exp golomb code.
210 */
212 {
214
216 return 0;
219 else
221 }
222
223 /**
224 * read unsigned truncated exp golomb code.
225 */
227 {
229
232 else
234 }
235
236 /**
237 * read signed exp golomb code.
238 */
240 {
241 unsigned int buf;
242
243 #if CACHED_BITSTREAM_READER
245
246 if (buf >= (1 << 27)) {
247 buf >>= 32 - 9;
249
251 } else {
254
256
257 if (buf & 1)
258 buf = -(buf >> 1);
259 else
260 buf = (buf >> 1);
261
262 return buf;
263 }
264 #else
268
269 if (buf >= (1 << 27)) {
270 buf >>= 32 - 9;
273
275 } else {
280
282
285
286 sign = -(buf & 1);
287 buf = ((buf >> 1) ^ sign) - sign;
288
289 return buf;
290 }
291 #endif
292 }
293
295 {
297 int sign = (buf & 1) - 1;
298 return ((buf >> 1) ^ sign) + 1;
299 }
300
302 {
303 unsigned int buf;
304
305 #if CACHED_BITSTREAM_READER
307
308 if (buf & 0xAA800000) {
309 buf >>= 32 - 8;
311
313 } else {
317
318 if ((buf & 0xAAAAAAAA) == 0)
320
321 for (
log = 31; (buf & 0x80000000) == 0;
log--)
322 buf = (buf << 2) - ((buf << log) >> (
log - 1)) + (buf >> 30);
323
325
326 return (
signed) (((((buf << log) >>
log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
327 }
328 #else
332
333 if (buf & 0xAA800000) {
334 buf >>= 32 - 8;
337
339 } else {
344
345 if ((buf & 0xAAAAAAAA) == 0)
347
348 for (
log = 31; (buf & 0x80000000) == 0;
log--)
349 buf = (buf << 2) - ((buf << log) >> (
log - 1)) + (buf >> 30);
350
353
354 return (
signed) (((((buf << log) >>
log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
355 }
356 #endif
357 }
358
360 {
362
365 ret = (
ret ^ sign) - sign;
366 }
367
369 }
370
371 /**
372 * read unsigned golomb rice code (ffv1).
373 */
375 int esc_len)
376 {
377 unsigned int buf;
379
380 #if CACHED_BITSTREAM_READER
382
384
387 buf += (30 -
log) << k;
389
390 return buf;
391 } else {
394
395 return buf +
limit - 1;
396 }
397 #else
401
403
407 buf += (30
U -
log) << k;
410
411 return buf;
412 } else {
415
417
420
421 return buf +
limit - 1;
422 }
423 #endif
424 }
425
426 /**
427 * read unsigned golomb rice code (jpegls).
428 *
429 * @returns -1 on error
430 */
432 int esc_len)
433 {
434 unsigned int buf;
436
437 #if CACHED_BITSTREAM_READER
439
441
444 buf += (30 -
log) << k;
446
447 return buf;
448 } else {
453
456
457 return buf + (
i << k);
458 }
else if (
i ==
limit - 1) {
460
461 return buf + 1;
462 } else
463 return -1;
464 }
465 #else
469
471
473
477 buf += (30
U -
log) << k;
480
481 return buf;
482 } else {
487 return -1;
488 }
491 }
494 }
497
499 if (k) {
506 } else {
509 }
510 } else {
511 buf = 0;
512 }
513
515 }
else if (
i ==
limit - 1) {
518
519 buf ++;
520 } else {
521 buf = -1;
522 }
524 return buf;
525 }
526 #endif
527 }
528
529 /**
530 * read signed golomb rice code (ffv1).
531 */
533 int esc_len)
534 {
536 return (v >> 1) ^ -(v & 1);
537 }
538
539 /**
540 * read signed golomb rice code (flac).
541 *
542 * @returns INT_MIN on error
543 */
545 int esc_len)
546 {
548 return (v >> 1) ^ -(v & 1);
549 }
550
551 /**
552 * read unsigned golomb rice code (shorten).
553 */
555 {
557 }
558
559 /**
560 * read signed golomb rice code (shorten).
561 */
563 {
565 return (uvar >> 1) ^ -(uvar & 1);
566 }
567
568 #ifdef TRACE
569
572 {
578
581
583 }
584
587 {
593
596
598 }
599
602 {
608
611
613 }
614
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__)
619
620 #endif /* TRACE */
621 #endif /* AVCODEC_GOLOMB_H */