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
37
38 #define INVALID_VLC 0x80000000
39
44
49
50 /**
51 * read unsigned exp golomb code.
52 */
54 {
56
60
61 if (buf >= (1 << 27)) {
62 buf >>= 32 - 9;
65
67 } else {
68 int log = 2 *
av_log2(buf) - 31;
71 if (CONFIG_FTRAPV && log < 0) {
74 }
75 buf >>= log;
76 buf--;
77
79 }
80 }
81
82 /**
83 * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
84 */
86 {
88
92
94 }
95
96 /**
97 * read unsigned exp golomb code, constraint to a max of 31.
98 * the return value is undefined if the stored value exceeds 31.
99 */
101 {
103
107
108 buf >>= 32 - 9;
111
113 }
114
116 {
118
122
123 if (buf & 0xAA800000) {
124 buf >>= 32 - 8;
127
129 } else {
131
132 do {
133 buf >>= 32 - 8;
136
140 break;
141 }
146
148 return ret - 1;
149 }
150 }
151
152 /**
153 * read unsigned truncated exp golomb code.
154 */
156 {
158
159 if (range == 1)
160 return 0;
161 else if (range == 2)
163 else
165 }
166
167 /**
168 * read unsigned truncated exp golomb code.
169 */
171 {
173
174 if (range == 2)
176 else
178 }
179
180 /**
181 * read signed exp golomb code.
182 */
184 {
186
190
191 if (buf >= (1 << 27)) {
192 buf >>= 32 - 9;
195
197 } else {
202
203 buf >>= log;
204
207
208 sign = -(buf & 1);
209 buf = ((buf >> 1) ^ sign) - sign;
210
212 }
213 }
214
216 {
218 int sign;
219
220 sign = (buf & 1) - 1;
221 return ((buf >> 1) ^ sign) + 1;
222 }
223
225 {
227
231
232 if (buf & 0xAA800000) {
233 buf >>= 32 - 8;
236
238 } else {
239 int log;
243
244 if ((buf & 0xAAAAAAAA) == 0)
246
247 for (log = 31; (buf & 0x80000000) == 0; log--)
248 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
249
252
253 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
254 }
255 }
256
258 {
260
261 if (ret) {
262 int sign;
264 ret = (ret ^ sign) - sign;
265 }
266
268 }
269
270 /**
271 * read unsigned golomb rice code (ffv1).
272 */
274 int esc_len)
275 {
277 int log;
278
282
284
285 if (log > 31 - limit) {
286 buf >>= log - k;
287 buf += (30 - log) << k;
290
292 } else {
295
297
300
301 return buf + limit - 1;
302 }
303 }
304
305 /**
306 * read unsigned golomb rice code (jpegls).
307 */
309 int esc_len)
310 {
312 int log;
313
317
319
321 32 - log < limit) {
322 buf >>= log - k;
323 buf += (30 - log) << k;
326
328 } else {
329 int i;
330 for (i = 0; i < limit &&
SHOW_UBITS(
re, gb, 1) == 0; i++) {
332 return -1;
335 }
337
338 if (i < limit - 1) {
339 if (k) {
342 } else {
343 buf = 0;
344 }
345
347 return buf + (i << k);
348 } else if (i == limit - 1) {
352
353 return buf + 1;
354 } else
355 return -1;
356 }
357 }
358
359 /**
360 * read signed golomb rice code (ffv1).
361 */
363 int esc_len)
364 {
366 int sign;
367
368 v++;
369 sign = (v & 1) - 1;
370 return ((v >> 1) ^ sign) - sign;
371 }
372
373 /**
374 * read signed golomb rice code (flac).
375 */
377 int esc_len)
378 {
380 return (v >> 1) ^ -(v & 1);
381 }
382
383 /**
384 * read unsigned golomb rice code (shorten).
385 */
387 {
389 }
390
391 /**
392 * read signed golomb rice code (shorten).
393 */
395 {
397 return (uvar >> 1) ^ -(uvar & 1);
398 }
399
400 #ifdef TRACE
401
404 {
410
411 print_bin(bits, len);
412
414 bits, len, i, pos, file, func, line);
415
416 return i;
417 }
418
421 {
427
428 print_bin(bits, len);
429
431 bits, len, i, pos, file, func, line);
432
433 return i;
434 }
435
438 {
444
445 print_bin(bits, len);
446
448 bits, len, i, pos, file, func, line);
449
450 return i;
451 }
452
453 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
454 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
455 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
456 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
457
458 #endif /* TRACE */
459
460 /**
461 * write unsigned exp golomb code.
462 */
464 {
466
467 #if 0
468 if (i = 0) {
470 return;
471 }
472 #endif
473 if (i < 256)
475 else {
478 }
479 }
480
481 /**
482 * write truncated unsigned exp golomb code.
483 */
485 {
488
489 if (range == 2)
491 else
493 }
494
495 /**
496 * write signed exp golomb code. 16 bits at most.
497 */
499 {
500 #if 0
501 if (i <= 0)
502 i = -2 * i;
503 else
504 i = 2 * i - 1;
505 #elif 1
506 i = 2 * i - 1;
507 if (i < 0)
508 i ^= -1; //FIXME check if gcc does the right thing
509 #else
510 i = 2 * i - 1;
511 i ^= (i >> 31);
512 #endif
514 }
515
516 /**
517 * write unsigned golomb rice code (ffv1).
518 */
520 int esc_len)
521 {
522 int e;
523
525
526 e = i >> k;
527 if (e < limit)
528 put_bits(pb, e + k + 1, (1 << k) + (i & ((1 << k) - 1)));
529 else
530 put_bits(pb, limit + esc_len, i - limit + 1);
531 }
532
533 /**
534 * write unsigned golomb rice code (jpegls).
535 */
537 int limit, int esc_len)
538 {
539 int e;
540
542
543 e = (i >> k) + 1;
544 if (e < limit) {
545 while (e > 31) {
547 e -= 31;
548 }
550 if (k)
552 } else {
553 while (limit > 31) {
555 limit -= 31;
556 }
559 }
560 }
561
562 /**
563 * write signed golomb rice code (ffv1).
564 */
566 int esc_len)
567 {
569
570 v = -2 * i - 1;
571 v ^= (v >> 31);
572
574 }
575
576 /**
577 * write signed golomb rice code (flac).
578 */
580 int limit, int esc_len)
581 {
583
584 v = -2 * i - 1;
585 v ^= (v >> 31);
586
588 }
589
590 #endif /* AVCODEC_GOLOMB_H */