]> git.sesse.net Git - ffmpeg/blob - libavcodec/golomb.h
libopenh264dec: Export the decoded profile and level in AVCodecContext
[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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 "bitstream.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 unsigned exp golomb code.
52  */
53 static inline int get_ue_golomb(BitstreamContext *bc)
54 {
55     unsigned int buf;
56
57     buf = bitstream_peek(bc, 32);
58
59     if (buf >= (1 << 27)) {
60         buf >>= 32 - 9;
61         bitstream_skip(bc, ff_golomb_vlc_len[buf]);
62
63         return ff_ue_golomb_vlc_code[buf];
64     } else {
65         int log = 2 * av_log2(buf) - 31;
66         buf >>= log;
67         buf--;
68         bitstream_skip(bc, 32 - log);
69
70         return buf;
71     }
72 }
73
74 /**
75  * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
76  */
77 static inline unsigned get_ue_golomb_long(BitstreamContext *bc)
78 {
79     unsigned buf, log;
80
81     buf = bitstream_peek(bc, 32);
82     log = 31 - av_log2(buf);
83     bitstream_skip(bc, log);
84
85     return bitstream_read(bc, log + 1) - 1;
86 }
87
88 /**
89  * read unsigned exp golomb code, constraint to a max of 31.
90  * the return value is undefined if the stored value exceeds 31.
91  */
92 static inline int get_ue_golomb_31(BitstreamContext *bc)
93 {
94     unsigned int buf;
95
96     buf = bitstream_peek(bc, 32);
97
98     buf >>= 32 - 9;
99     bitstream_skip(bc, ff_golomb_vlc_len[buf]);
100
101     return ff_ue_golomb_vlc_code[buf];
102 }
103
104 static inline unsigned get_interleaved_ue_golomb(BitstreamContext *bc)
105 {
106     uint32_t buf;
107
108     buf = bitstream_peek(bc, 32);
109
110     if (buf & 0xAA800000) {
111         buf >>= 32 - 8;
112         bitstream_skip(bc, ff_interleaved_golomb_vlc_len[buf]);
113
114         return ff_interleaved_ue_golomb_vlc_code[buf];
115     } else {
116         unsigned ret = 1;
117
118         do {
119             buf >>= 32 - 8;
120             bitstream_skip(bc, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
121
122             if (ff_interleaved_golomb_vlc_len[buf] != 9) {
123                 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
124                 ret  |= ff_interleaved_dirac_golomb_vlc_code[buf];
125                 break;
126             }
127             ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
128             buf = bitstream_peek(bc, 32);
129         } while (bitstream_bits_left(bc) > 0);
130
131         return ret - 1;
132     }
133 }
134
135 /**
136  * read unsigned truncated exp golomb code.
137  */
138 static inline int get_te0_golomb(BitstreamContext *bc, int range)
139 {
140     assert(range >= 1);
141
142     if (range == 1)
143         return 0;
144     else if (range == 2)
145         return bitstream_read_bit(bc) ^ 1;
146     else
147         return get_ue_golomb(bc);
148 }
149
150 /**
151  * read unsigned truncated exp golomb code.
152  */
153 static inline int get_te_golomb(BitstreamContext *bc, int range)
154 {
155     assert(range >= 1);
156
157     if (range == 2)
158         return bitstream_read_bit(bc) ^ 1;
159     else
160         return get_ue_golomb(bc);
161 }
162
163 /**
164  * read signed exp golomb code.
165  */
166 static inline int get_se_golomb(BitstreamContext *bc)
167 {
168     unsigned int buf;
169
170     buf = bitstream_peek(bc, 32);
171
172     if (buf >= (1 << 27)) {
173         buf >>= 32 - 9;
174         bitstream_skip(bc, ff_golomb_vlc_len[buf]);
175
176         return ff_se_golomb_vlc_code[buf];
177     } else {
178         int log = 2 * av_log2(buf) - 31;
179         buf >>= log;
180
181         bitstream_skip(bc, 32 - log);
182
183         if (buf & 1)
184             buf = -(buf >> 1);
185         else
186             buf = (buf >> 1);
187
188         return buf;
189     }
190 }
191
192 static inline int get_se_golomb_long(BitstreamContext *bc)
193 {
194     unsigned int buf = get_ue_golomb_long(bc);
195
196     if (buf & 1)
197         buf = (buf + 1) >> 1;
198     else
199         buf = -(buf >> 1);
200
201     return buf;
202 }
203
204 static inline int get_interleaved_se_golomb(BitstreamContext *bc)
205 {
206     unsigned int buf;
207
208     buf = bitstream_peek(bc, 32);
209
210     if (buf & 0xAA800000) {
211         buf >>= 32 - 8;
212         bitstream_skip(bc, ff_interleaved_golomb_vlc_len[buf]);
213
214         return ff_interleaved_se_golomb_vlc_code[buf];
215     } else {
216         int log;
217         bitstream_skip(bc, 8);
218         buf |= 1 | bitstream_peek(bc, 24);
219
220         if ((buf & 0xAAAAAAAA) == 0)
221             return INVALID_VLC;
222
223         for (log = 31; (buf & 0x80000000) == 0; log--)
224             buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
225
226         bitstream_skip(bc, 63 - 2 * log - 8);
227
228         return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
229     }
230 }
231
232 static inline int dirac_get_se_golomb(BitstreamContext *bc)
233 {
234     uint32_t ret = get_interleaved_ue_golomb(bc);
235
236     if (ret) {
237         uint32_t buf;
238         buf = bitstream_read_signed(bc, 1);
239         ret = (ret ^ buf) - buf;
240     }
241
242     return ret;
243 }
244
245 /**
246  * read unsigned golomb rice code (ffv1).
247  */
248 static inline int get_ur_golomb(BitstreamContext *bc, int k, int limit,
249                                 int esc_len)
250 {
251     unsigned int buf;
252     int log;
253
254     buf = bitstream_peek(bc, 32);
255
256     log = av_log2(buf);
257
258     if (log > 31 - limit) {
259         buf >>= log - k;
260         buf  += (30 - log) << k;
261         bitstream_skip(bc, 32 + k - log);
262
263         return buf;
264     } else {
265         bitstream_skip(bc, limit);
266         buf = bitstream_read(bc, esc_len);
267
268         return buf + limit - 1;
269     }
270 }
271
272 /**
273  * read unsigned golomb rice code (jpegls).
274  */
275 static inline int get_ur_golomb_jpegls(BitstreamContext *bc, int k, int limit,
276                                        int esc_len)
277 {
278     unsigned int buf;
279     int log;
280
281     buf = bitstream_peek(bc, 32);
282
283     log = av_log2(buf);
284
285     if (log - k >= 1 && 32 - log < limit) {
286         buf >>= log - k;
287         buf  += (30 - log) << k;
288         bitstream_skip(bc, 32 + k - log);
289
290         return buf;
291     } else {
292         int i;
293         for (i = 0;
294              i < limit && bitstream_read_bit(bc) == 0 && bitstream_bits_left(bc) > 0;
295              i++);
296
297         if (i < limit - 1) {
298             buf = bitstream_read(bc, k);
299
300             return buf + (i << k);
301         } else if (i == limit - 1) {
302             buf = bitstream_read(bc, esc_len);
303
304             return buf + 1;
305         } else
306             return -1;
307     }
308 }
309
310 /**
311  * read signed golomb rice code (ffv1).
312  */
313 static inline int get_sr_golomb(BitstreamContext *bc, int k, int limit,
314                                 int esc_len)
315 {
316     int v = get_ur_golomb(bc, k, limit, esc_len);
317
318     v++;
319     if (v & 1)
320         return v >> 1;
321     else
322         return -(v >> 1);
323
324 //    return (v>>1) ^ -(v&1);
325 }
326
327 /**
328  * read signed golomb rice code (flac).
329  */
330 static inline int get_sr_golomb_flac(BitstreamContext *bc, int k, int limit,
331                                      int esc_len)
332 {
333     int v = get_ur_golomb_jpegls(bc, k, limit, esc_len);
334     return (v >> 1) ^ -(v & 1);
335 }
336
337 /**
338  * read unsigned golomb rice code (shorten).
339  */
340 static inline unsigned int get_ur_golomb_shorten(BitstreamContext *bc, int k)
341 {
342     return get_ur_golomb_jpegls(bc, k, INT_MAX, 0);
343 }
344
345 /**
346  * read signed golomb rice code (shorten).
347  */
348 static inline int get_sr_golomb_shorten(BitstreamContext *bc, int k)
349 {
350     int uvar = get_ur_golomb_jpegls(bc, k + 1, INT_MAX, 0);
351     if (uvar & 1)
352         return ~(uvar >> 1);
353     else
354         return uvar >> 1;
355 }
356
357 #ifdef TRACE
358
359 static inline int get_ue(BitstreamContext *s, const char *file, const char *func,
360                          int line)
361 {
362     int show = bitstream_peek(s, 24);
363     int pos  = bitstream_tell(s);
364     int i    = get_ue_golomb(s);
365     int len  = bitstream_tell(s) - pos;
366     int bits = show >> (24 - len);
367
368     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue  @%5d in %s %s:%d\n",
369            bits, len, i, pos, file, func, line);
370
371     return i;
372 }
373
374 static inline int get_se(BitstreamContext *s, const char *file, const char *func,
375                          int line)
376 {
377     int show = bitstream_peek(s, 24);
378     int pos  = bitstream_tell(s);
379     int i    = get_se_golomb(s);
380     int len  = bitstream_tell(s) - pos;
381     int bits = show >> (24 - len);
382
383     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se  @%5d in %s %s:%d\n",
384            bits, len, i, pos, file, func, line);
385
386     return i;
387 }
388
389 static inline int get_te(BitstreamContext *s, int r, char *file, const char *func,
390                          int line)
391 {
392     int show = bitstream_peek(s, 24);
393     int pos  = bitstream_tell(s);
394     int i    = get_te0_golomb(s, r);
395     int len  = bitstream_tell(s) - pos;
396     int bits = show >> (24 - len);
397
398     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te  @%5d in %s %s:%d\n",
399            bits, len, i, pos, file, func, line);
400
401     return i;
402 }
403
404 #define get_ue_golomb(a) get_ue(a, __FILE__, __func__, __LINE__)
405 #define get_se_golomb(a) get_se(a, __FILE__, __func__, __LINE__)
406 #define get_te_golomb(a, r)  get_te(a, r, __FILE__, __func__, __LINE__)
407 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __func__, __LINE__)
408
409 #endif /* TRACE */
410
411 /**
412  * write unsigned exp golomb code.
413  */
414 static inline void set_ue_golomb(PutBitContext *pb, int i)
415 {
416     assert(i >= 0);
417
418     if (i < 256)
419         put_bits(pb, ff_ue_golomb_len[i], i + 1);
420     else {
421         int e = av_log2(i + 1);
422         put_bits(pb, 2 * e + 1, i + 1);
423     }
424 }
425
426 /**
427  * write truncated unsigned exp golomb code.
428  */
429 static inline void set_te_golomb(PutBitContext *pb, int i, int range)
430 {
431     assert(range >= 1);
432     assert(i <= range);
433
434     if (range == 2)
435         put_bits(pb, 1, i ^ 1);
436     else
437         set_ue_golomb(pb, i);
438 }
439
440 /**
441  * write signed exp golomb code. 16 bits at most.
442  */
443 static inline void set_se_golomb(PutBitContext *pb, int i)
444 {
445     i = 2 * i - 1;
446     if (i < 0)
447         i ^= -1;    //FIXME check if gcc does the right thing
448     set_ue_golomb(pb, i);
449 }
450
451 /**
452  * write unsigned golomb rice code (ffv1).
453  */
454 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit,
455                                  int esc_len)
456 {
457     int e;
458
459     assert(i >= 0);
460
461     e = i >> k;
462     if (e < limit)
463         put_bits(pb, e + k + 1, (1 << k) + (i & ((1 << k) - 1)));
464     else
465         put_bits(pb, limit + esc_len, i - limit + 1);
466 }
467
468 /**
469  * write unsigned golomb rice code (jpegls).
470  */
471 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k,
472                                         int limit, int esc_len)
473 {
474     int e;
475
476     assert(i >= 0);
477
478     e = (i >> k) + 1;
479     if (e < limit) {
480         while (e > 31) {
481             put_bits(pb, 31, 0);
482             e -= 31;
483         }
484         put_bits(pb, e, 1);
485         if (k)
486             put_sbits(pb, k, i);
487     } else {
488         while (limit > 31) {
489             put_bits(pb, 31, 0);
490             limit -= 31;
491         }
492         put_bits(pb, limit, 1);
493         put_bits(pb, esc_len, i - 1);
494     }
495 }
496
497 /**
498  * write signed golomb rice code (ffv1).
499  */
500 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit,
501                                  int esc_len)
502 {
503     int v;
504
505     v  = -2 * i - 1;
506     v ^= (v >> 31);
507
508     set_ur_golomb(pb, v, k, limit, esc_len);
509 }
510
511 /**
512  * write signed golomb rice code (flac).
513  */
514 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k,
515                                       int limit, int esc_len)
516 {
517     int v;
518
519     v  = -2 * i - 1;
520     v ^= (v >> 31);
521
522     set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
523 }
524
525 #endif /* AVCODEC_GOLOMB_H */