]> git.sesse.net Git - ffmpeg/blob - libavcodec/golomb.h
Merge commit '76eef04f30a768fa80366d679f3de7e9447b67d5'
[ffmpeg] / libavcodec / golomb.h
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
35 #include "get_bits.h"
36 #include "put_bits.h"
37
38 #define INVALID_VLC           0x80000000
39
40 extern const uint8_t ff_golomb_vlc_len[512];
41 extern const uint8_t ff_ue_golomb_vlc_code[512];
42 extern const  int8_t ff_se_golomb_vlc_code[512];
43 extern const uint8_t ff_ue_golomb_len[256];
44
45 extern const uint8_t ff_interleaved_golomb_vlc_len[256];
46 extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
47 extern const  int8_t ff_interleaved_se_golomb_vlc_code[256];
48 extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
49
50 /**
51  * Read an unsigned Exp-Golomb code in the range 0 to 8190.
52  */
53 static inline int get_ue_golomb(GetBitContext *gb)
54 {
55     unsigned int buf;
56
57 #if CACHED_BITSTREAM_READER
58     buf = show_bits_long(gb, 32);
59
60     if (buf >= (1 << 27)) {
61         buf >>= 32 - 9;
62         skip_bits_long(gb, ff_golomb_vlc_len[buf]);
63
64         return ff_ue_golomb_vlc_code[buf];
65     } else {
66         int log = 2 * av_log2(buf) - 31;
67         buf >>= log;
68         buf--;
69         skip_bits_long(gb, 32 - log);
70
71         return buf;
72     }
73 #else
74     OPEN_READER(re, gb);
75     UPDATE_CACHE(re, gb);
76     buf = GET_CACHE(re, gb);
77
78     if (buf >= (1 << 27)) {
79         buf >>= 32 - 9;
80         LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
81         CLOSE_READER(re, gb);
82
83         return ff_ue_golomb_vlc_code[buf];
84     } else {
85         int log = 2 * av_log2(buf) - 31;
86         LAST_SKIP_BITS(re, gb, 32 - log);
87         CLOSE_READER(re, gb);
88         if (log < 7) {
89             av_log(NULL, AV_LOG_ERROR, "Invalid UE golomb code\n");
90             return AVERROR_INVALIDDATA;
91         }
92         buf >>= log;
93         buf--;
94
95         return buf;
96     }
97 #endif
98 }
99
100 /**
101  * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
102  */
103 static inline unsigned get_ue_golomb_long(GetBitContext *gb)
104 {
105     unsigned buf, log;
106
107     buf = show_bits_long(gb, 32);
108     log = 31 - av_log2(buf);
109     skip_bits_long(gb, log);
110
111     return get_bits_long(gb, log + 1) - 1;
112 }
113
114 /**
115  * read unsigned exp golomb code, constraint to a max of 31.
116  * the return value is undefined if the stored value exceeds 31.
117  */
118 static inline int get_ue_golomb_31(GetBitContext *gb)
119 {
120     unsigned int buf;
121
122 #if CACHED_BITSTREAM_READER
123     buf = show_bits_long(gb, 32);
124
125     buf >>= 32 - 9;
126     skip_bits_long(gb, ff_golomb_vlc_len[buf]);
127 #else
128
129     OPEN_READER(re, gb);
130     UPDATE_CACHE(re, gb);
131     buf = GET_CACHE(re, gb);
132
133     buf >>= 32 - 9;
134     LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
135     CLOSE_READER(re, gb);
136 #endif
137
138     return ff_ue_golomb_vlc_code[buf];
139 }
140
141 static inline unsigned get_interleaved_ue_golomb(GetBitContext *gb)
142 {
143     uint32_t buf;
144
145 #if CACHED_BITSTREAM_READER
146     buf = show_bits_long(gb, 32);
147
148     if (buf & 0xAA800000) {
149         buf >>= 32 - 8;
150         skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]);
151
152         return ff_interleaved_ue_golomb_vlc_code[buf];
153     } else {
154         unsigned ret = 1;
155
156         do {
157             buf >>= 32 - 8;
158             skip_bits_long(gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
159
160             if (ff_interleaved_golomb_vlc_len[buf] != 9) {
161                 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
162                 ret  |= ff_interleaved_dirac_golomb_vlc_code[buf];
163                 break;
164             }
165             ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
166             buf = show_bits_long(gb, 32);
167         } while (get_bits_left(gb) > 0);
168
169         return ret - 1;
170     }
171 #else
172     OPEN_READER(re, gb);
173     UPDATE_CACHE(re, gb);
174     buf = GET_CACHE(re, gb);
175
176     if (buf & 0xAA800000) {
177         buf >>= 32 - 8;
178         LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
179         CLOSE_READER(re, gb);
180
181         return ff_interleaved_ue_golomb_vlc_code[buf];
182     } else {
183         unsigned ret = 1;
184
185         do {
186             buf >>= 32 - 8;
187             LAST_SKIP_BITS(re, gb,
188                            FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
189
190             if (ff_interleaved_golomb_vlc_len[buf] != 9) {
191                 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
192                 ret  |= ff_interleaved_dirac_golomb_vlc_code[buf];
193                 break;
194             }
195             ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
196             UPDATE_CACHE(re, gb);
197             buf = GET_CACHE(re, gb);
198         } while (ret<0x8000000U && BITS_AVAILABLE(re, gb));
199
200         CLOSE_READER(re, gb);
201         return ret - 1;
202     }
203 #endif
204 }
205
206 /**
207  * read unsigned truncated exp golomb code.
208  */
209 static inline int get_te0_golomb(GetBitContext *gb, int range)
210 {
211     av_assert2(range >= 1);
212
213     if (range == 1)
214         return 0;
215     else if (range == 2)
216         return get_bits1(gb) ^ 1;
217     else
218         return get_ue_golomb(gb);
219 }
220
221 /**
222  * read unsigned truncated exp golomb code.
223  */
224 static inline int get_te_golomb(GetBitContext *gb, int range)
225 {
226     av_assert2(range >= 1);
227
228     if (range == 2)
229         return get_bits1(gb) ^ 1;
230     else
231         return get_ue_golomb(gb);
232 }
233
234 /**
235  * read signed exp golomb code.
236  */
237 static inline int get_se_golomb(GetBitContext *gb)
238 {
239     unsigned int buf;
240
241 #if CACHED_BITSTREAM_READER
242     buf = show_bits_long(gb, 32);
243
244     if (buf >= (1 << 27)) {
245         buf >>= 32 - 9;
246         skip_bits_long(gb, ff_golomb_vlc_len[buf]);
247
248         return ff_se_golomb_vlc_code[buf];
249     } else {
250         int log = 2 * av_log2(buf) - 31;
251         buf >>= log;
252
253         skip_bits_long(gb, 32 - log);
254
255         if (buf & 1)
256             buf = -(buf >> 1);
257         else
258             buf = (buf >> 1);
259
260         return buf;
261     }
262 #else
263     OPEN_READER(re, gb);
264     UPDATE_CACHE(re, gb);
265     buf = GET_CACHE(re, gb);
266
267     if (buf >= (1 << 27)) {
268         buf >>= 32 - 9;
269         LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
270         CLOSE_READER(re, gb);
271
272         return ff_se_golomb_vlc_code[buf];
273     } else {
274         int log = av_log2(buf), sign;
275         LAST_SKIP_BITS(re, gb, 31 - log);
276         UPDATE_CACHE(re, gb);
277         buf = GET_CACHE(re, gb);
278
279         buf >>= log;
280
281         LAST_SKIP_BITS(re, gb, 32 - log);
282         CLOSE_READER(re, gb);
283
284         sign = -(buf & 1);
285         buf  = ((buf >> 1) ^ sign) - sign;
286
287         return buf;
288     }
289 #endif
290 }
291
292 static inline int get_se_golomb_long(GetBitContext *gb)
293 {
294     unsigned int buf = get_ue_golomb_long(gb);
295     int sign = (buf & 1) - 1;
296     return ((buf >> 1) ^ sign) + 1;
297 }
298
299 static inline int get_interleaved_se_golomb(GetBitContext *gb)
300 {
301     unsigned int buf;
302
303 #if CACHED_BITSTREAM_READER
304     buf = show_bits_long(gb, 32);
305
306     if (buf & 0xAA800000) {
307         buf >>= 32 - 8;
308         skip_bits_long(gb, ff_interleaved_golomb_vlc_len[buf]);
309
310         return ff_interleaved_se_golomb_vlc_code[buf];
311     } else {
312         int log;
313         skip_bits(gb, 8);
314         buf |= 1 | show_bits_long(gb, 24);
315
316         if ((buf & 0xAAAAAAAA) == 0)
317             return INVALID_VLC;
318
319         for (log = 31; (buf & 0x80000000) == 0; log--)
320             buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
321
322         skip_bits_long(gb, 63 - 2 * log - 8);
323
324         return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
325     }
326 #else
327     OPEN_READER(re, gb);
328     UPDATE_CACHE(re, gb);
329     buf = GET_CACHE(re, gb);
330
331     if (buf & 0xAA800000) {
332         buf >>= 32 - 8;
333         LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
334         CLOSE_READER(re, gb);
335
336         return ff_interleaved_se_golomb_vlc_code[buf];
337     } else {
338         int log;
339         LAST_SKIP_BITS(re, gb, 8);
340         UPDATE_CACHE(re, gb);
341         buf |= 1 | (GET_CACHE(re, gb) >> 8);
342
343         if ((buf & 0xAAAAAAAA) == 0)
344             return INVALID_VLC;
345
346         for (log = 31; (buf & 0x80000000) == 0; log--)
347             buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
348
349         LAST_SKIP_BITS(re, gb, 63 - 2 * log - 8);
350         CLOSE_READER(re, gb);
351
352         return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
353     }
354 #endif
355 }
356
357 static inline int dirac_get_se_golomb(GetBitContext *gb)
358 {
359     uint32_t ret = get_interleaved_ue_golomb(gb);
360
361     if (ret) {
362         int sign = -get_bits1(gb);
363         ret = (ret ^ sign) - sign;
364     }
365
366     return ret;
367 }
368
369 /**
370  * read unsigned golomb rice code (ffv1).
371  */
372 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,
373                                 int esc_len)
374 {
375     unsigned int buf;
376     int log;
377
378 #if CACHED_BITSTREAM_READER
379     buf = show_bits_long(gb, 32);
380
381     log = av_log2(buf);
382
383     if (log > 31 - limit) {
384         buf >>= log - k;
385         buf  += (30 - log) << k;
386         skip_bits_long(gb, 32 + k - log);
387
388         return buf;
389     } else {
390         skip_bits_long(gb, limit);
391         buf = get_bits_long(gb, esc_len);
392
393         return buf + limit - 1;
394     }
395 #else
396     OPEN_READER(re, gb);
397     UPDATE_CACHE(re, gb);
398     buf = GET_CACHE(re, gb);
399
400     log = av_log2(buf);
401
402     if (log > 31 - limit) {
403         buf >>= log - k;
404         buf  += (30U - log) << k;
405         LAST_SKIP_BITS(re, gb, 32 + k - log);
406         CLOSE_READER(re, gb);
407
408         return buf;
409     } else {
410         LAST_SKIP_BITS(re, gb, limit);
411         UPDATE_CACHE(re, gb);
412
413         buf = SHOW_UBITS(re, gb, esc_len);
414
415         LAST_SKIP_BITS(re, gb, esc_len);
416         CLOSE_READER(re, gb);
417
418         return buf + limit - 1;
419     }
420 #endif
421 }
422
423 /**
424  * read unsigned golomb rice code (jpegls).
425  */
426 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
427                                        int esc_len)
428 {
429     unsigned int buf;
430     int log;
431
432 #if CACHED_BITSTREAM_READER
433     buf = show_bits_long(gb, 32);
434
435     log = av_log2(buf);
436
437     if (log - k >= 1 && 32 - log < limit) {
438         buf >>= log - k;
439         buf  += (30 - log) << k;
440         skip_bits_long(gb, 32 + k - log);
441
442         return buf;
443     } else {
444         int i;
445         for (i = 0;
446              i < limit && get_bits1(gb) == 0 && get_bits_left(gb) > 0;
447              i++);
448
449         if (i < limit - 1) {
450             buf = get_bits_long(gb, k);
451
452             return buf + (i << k);
453         } else if (i == limit - 1) {
454             buf = get_bits_long(gb, esc_len);
455
456             return buf + 1;
457         } else
458             return -1;
459     }
460 #else
461     OPEN_READER(re, gb);
462     UPDATE_CACHE(re, gb);
463     buf = GET_CACHE(re, gb);
464
465     log = av_log2(buf);
466
467     av_assert2(k <= 31);
468
469     if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
470         32 - log < limit) {
471         buf >>= log - k;
472         buf  += (30U - log) << k;
473         LAST_SKIP_BITS(re, gb, 32 + k - log);
474         CLOSE_READER(re, gb);
475
476         return buf;
477     } else {
478         int i;
479         for (i = 0; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
480             if (gb->size_in_bits <= re_index) {
481                 CLOSE_READER(re, gb);
482                 return -1;
483             }
484             LAST_SKIP_BITS(re, gb, 1);
485             UPDATE_CACHE(re, gb);
486         }
487         SKIP_BITS(re, gb, 1);
488
489         if (i < limit - 1) {
490             if (k) {
491                 if (k > MIN_CACHE_BITS - 1) {
492                     buf = SHOW_UBITS(re, gb, 16) << (k-16);
493                     LAST_SKIP_BITS(re, gb, 16);
494                     UPDATE_CACHE(re, gb);
495                     buf |= SHOW_UBITS(re, gb, k-16);
496                     LAST_SKIP_BITS(re, gb, k-16);
497                 } else {
498                     buf = SHOW_UBITS(re, gb, k);
499                     LAST_SKIP_BITS(re, gb, k);
500                 }
501             } else {
502                 buf = 0;
503             }
504
505             buf += ((SUINT)i << k);
506         } else if (i == limit - 1) {
507             buf = SHOW_UBITS(re, gb, esc_len);
508             LAST_SKIP_BITS(re, gb, esc_len);
509
510             buf ++;
511         } else {
512             buf = -1;
513         }
514         CLOSE_READER(re, gb);
515         return buf;
516     }
517 #endif
518 }
519
520 /**
521  * read signed golomb rice code (ffv1).
522  */
523 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit,
524                                 int esc_len)
525 {
526     unsigned v = get_ur_golomb(gb, k, limit, esc_len);
527     return (v >> 1) ^ -(v & 1);
528 }
529
530 /**
531  * read signed golomb rice code (flac).
532  */
533 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit,
534                                      int esc_len)
535 {
536     unsigned v = get_ur_golomb_jpegls(gb, k, limit, esc_len);
537     return (v >> 1) ^ -(v & 1);
538 }
539
540 /**
541  * read unsigned golomb rice code (shorten).
542  */
543 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
544 {
545     return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
546 }
547
548 /**
549  * read signed golomb rice code (shorten).
550  */
551 static inline int get_sr_golomb_shorten(GetBitContext *gb, int k)
552 {
553     int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
554     return (uvar >> 1) ^ -(uvar & 1);
555 }
556
557 #ifdef TRACE
558
559 static inline int get_ue(GetBitContext *s, const char *file, const char *func,
560                          int line)
561 {
562     int show = show_bits(s, 24);
563     int pos  = get_bits_count(s);
564     int i    = get_ue_golomb(s);
565     int len  = get_bits_count(s) - pos;
566     int bits = show >> (24 - len);
567
568     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue  @%5d in %s %s:%d\n",
569            bits, len, i, pos, file, func, line);
570
571     return i;
572 }
573
574 static inline int get_se(GetBitContext *s, const char *file, const char *func,
575                          int line)
576 {
577     int show = show_bits(s, 24);
578     int pos  = get_bits_count(s);
579     int i    = get_se_golomb(s);
580     int len  = get_bits_count(s) - pos;
581     int bits = show >> (24 - len);
582
583     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se  @%5d in %s %s:%d\n",
584            bits, len, i, pos, file, func, line);
585
586     return i;
587 }
588
589 static inline int get_te(GetBitContext *s, int r, char *file, const char *func,
590                          int line)
591 {
592     int show = show_bits(s, 24);
593     int pos  = get_bits_count(s);
594     int i    = get_te0_golomb(s, r);
595     int len  = get_bits_count(s) - pos;
596     int bits = show >> (24 - len);
597
598     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te  @%5d in %s %s:%d\n",
599            bits, len, i, pos, file, func, line);
600
601     return i;
602 }
603
604 #define get_ue_golomb(a) get_ue(a, __FILE__, __func__, __LINE__)
605 #define get_se_golomb(a) get_se(a, __FILE__, __func__, __LINE__)
606 #define get_te_golomb(a, r)  get_te(a, r, __FILE__, __func__, __LINE__)
607 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
608
609 #endif /* TRACE */
610
611 /**
612  * write unsigned exp golomb code. 2^16 - 2 at most
613  */
614 static inline void set_ue_golomb(PutBitContext *pb, int i)
615 {
616     av_assert2(i >= 0);
617     av_assert2(i <= 0xFFFE);
618
619     if (i < 256)
620         put_bits(pb, ff_ue_golomb_len[i], i + 1);
621     else {
622         int e = av_log2(i + 1);
623         put_bits(pb, 2 * e + 1, i + 1);
624     }
625 }
626
627 /**
628  * write unsigned exp golomb code. 2^32-2 at most.
629  */
630 static inline void set_ue_golomb_long(PutBitContext *pb, uint32_t i)
631 {
632     av_assert2(i <= (UINT32_MAX - 1));
633
634     if (i < 256)
635         put_bits(pb, ff_ue_golomb_len[i], i + 1);
636     else {
637         int e = av_log2(i + 1);
638         put_bits64(pb, 2 * e + 1, i + 1);
639     }
640 }
641
642 /**
643  * write truncated unsigned exp golomb code.
644  */
645 static inline void set_te_golomb(PutBitContext *pb, int i, int range)
646 {
647     av_assert2(range >= 1);
648     av_assert2(i <= range);
649
650     if (range == 2)
651         put_bits(pb, 1, i ^ 1);
652     else
653         set_ue_golomb(pb, i);
654 }
655
656 /**
657  * write signed exp golomb code. 16 bits at most.
658  */
659 static inline void set_se_golomb(PutBitContext *pb, int i)
660 {
661     i = 2 * i - 1;
662     if (i < 0)
663         i ^= -1;    //FIXME check if gcc does the right thing
664     set_ue_golomb(pb, i);
665 }
666
667 /**
668  * write unsigned golomb rice code (ffv1).
669  */
670 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
671                                  int esc_len)
672 {
673     int e;
674
675     av_assert2(i >= 0);
676
677     e = i >> k;
678     if (e < limit)
679         put_bits(pb, e + k + 1, (1 << k) + av_mod_uintp2(i, k));
680     else
681         put_bits(pb, limit + esc_len, i - limit + 1);
682 }
683
684 /**
685  * write unsigned golomb rice code (jpegls).
686  */
687 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k,
688                                         int limit, int esc_len)
689 {
690     int e;
691
692     av_assert2(i >= 0);
693
694     e = (i >> k) + 1;
695     if (e < limit) {
696         while (e > 31) {
697             put_bits(pb, 31, 0);
698             e -= 31;
699         }
700         put_bits(pb, e, 1);
701         if (k)
702             put_sbits(pb, k, i);
703     } else {
704         while (limit > 31) {
705             put_bits(pb, 31, 0);
706             limit -= 31;
707         }
708         put_bits(pb, limit, 1);
709         put_bits(pb, esc_len, i - 1);
710     }
711 }
712
713 /**
714  * write signed golomb rice code (ffv1).
715  */
716 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit,
717                                  int esc_len)
718 {
719     int v;
720
721     v  = -2 * i - 1;
722     v ^= (v >> 31);
723
724     set_ur_golomb(pb, v, k, limit, esc_len);
725 }
726
727 /**
728  * write signed golomb rice code (flac).
729  */
730 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k,
731                                       int limit, int esc_len)
732 {
733     int v;
734
735     v  = -2 * i - 1;
736     v ^= (v >> 31);
737
738     set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
739 }
740
741 #endif /* AVCODEC_GOLOMB_H */