]> git.kernelconcepts.de Git - rdstmc.git/blob - decoder/bitstream.c
Make CC optional (so cross CC will override)
[rdstmc.git] / decoder / bitstream.c
1 /*
2  * Copyright (C) 2009, 2010 by Nils Faerber <nils.faerber@kernelconcepts.de>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  *
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "bitstream.h"
16
17 static unsigned char *_BITBUF;
18 static unsigned int _BITPOS;
19 static unsigned int _BUFLEN;
20
21 void printbits(unsigned int val)
22 {
23 unsigned char i=0;
24
25         while (i<32)
26                 if (val & (1<<(31-i++)))
27                         printf("1 ");
28                 else
29                         printf("0 ");
30 //      printf("\n");
31 }
32
33 void printbuf_head(unsigned char *buf)
34 {
35         //printbits(buf[3]<<24 | buf[2]<<16 | buf[1]<<8 | buf[0]);
36         printbits(buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3]);
37 }
38
39 unsigned int bitstream_get_bits(unsigned char *data, unsigned int bitOffset, unsigned int numBits)
40 {
41 unsigned int val;
42 unsigned int mask = (1 << numBits) - 1;
43
44         data += ((bitOffset / 8) -3);
45         val = *((int *)data);
46 //      printf("0x%08x ", val);
47
48         val = val << (bitOffset % 8);
49 //      printf("0x%08x ", val);
50
51         val = val >> (32-numBits);
52 //      printf("0x%08x %d ", val, (32-numBits));
53
54         val &= mask;
55 //      printf("0x%08x\n", val);
56
57 return val;
58 }
59
60 int bitstream_bits_remaining(void)
61 {
62         return (_BUFLEN - _BITPOS);
63 }
64
65 unsigned int bitstream_get_next_bits(unsigned int nbits)
66 {
67 unsigned int res;
68
69         res = bitstream_get_bits(_BITBUF, _BITPOS, nbits);
70         _BITPOS += nbits;
71
72         return res;
73 }
74
75 void bitstream_start(unsigned char *buf, unsigned int len)
76 {
77         _BITBUF = buf;
78         _BITPOS = 0;
79         _BUFLEN = len;
80 }
81
82