]> git.kernelconcepts.de Git - rdstmc.git/blob - decoder/bitstream.c
Add license file
[rdstmc.git] / decoder / bitstream.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "bitstream.h"
6
7 static unsigned char *_BITBUF;
8 static unsigned int _BITPOS;
9 static unsigned int _BUFLEN;
10
11 void printbits(unsigned int val)
12 {
13 unsigned char i=0;
14
15         while (i<32)
16                 if (val & (1<<(31-i++)))
17                         printf("1 ");
18                 else
19                         printf("0 ");
20 //      printf("\n");
21 }
22
23 void printbuf_head(unsigned char *buf)
24 {
25         //printbits(buf[3]<<24 | buf[2]<<16 | buf[1]<<8 | buf[0]);
26         printbits(buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3]);
27 }
28
29 unsigned int bitstream_get_bits(unsigned char *data, unsigned int bitOffset, unsigned int numBits)
30 {
31 unsigned int val;
32 unsigned int mask = (1 << numBits) - 1;
33
34         data += ((bitOffset / 8) -3);
35         val = *((int *)data);
36 //      printf("0x%08x ", val);
37
38         val = val << (bitOffset % 8);
39 //      printf("0x%08x ", val);
40
41         val = val >> (32-numBits);
42 //      printf("0x%08x %d ", val, (32-numBits));
43
44         val &= mask;
45 //      printf("0x%08x\n", val);
46
47 return val;
48 }
49
50 int bitstream_bits_remaining(void)
51 {
52         return (_BUFLEN - _BITPOS);
53 }
54
55 unsigned int bitstream_get_next_bits(unsigned int nbits)
56 {
57 unsigned int res;
58
59         res = bitstream_get_bits(_BITBUF, _BITPOS, nbits);
60         _BITPOS += nbits;
61
62         return res;
63 }
64
65 void bitstream_start(unsigned char *buf, unsigned int len)
66 {
67         _BITBUF = buf;
68         _BITPOS = 0;
69         _BUFLEN = len;
70 }
71
72