summaryrefslogtreecommitdiff
path: root/src/gallium/auxiliary/vl/vl_bitstream_parser.c
blob: 356faa13481ec340e2766ba65ffaba5c51a20acb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "vl_bitstream_parser.h"
#include <assert.h>
#include <limits.h>
#include <util/u_memory.h>

static unsigned
grab_bits(unsigned cursor, unsigned how_many_bits, unsigned bitstream_elt)
{
   unsigned excess_bits = sizeof(unsigned) * CHAR_BIT - how_many_bits - cursor;
	
   assert(cursor < sizeof(unsigned) * CHAR_BIT);
   assert(how_many_bits > 0 && how_many_bits <= sizeof(unsigned) * CHAR_BIT);
   assert(cursor + how_many_bits <= sizeof(unsigned) * CHAR_BIT);

   return (bitstream_elt << excess_bits) >> (excess_bits + cursor);
}

static unsigned
show_bits(unsigned cursor, unsigned how_many_bits, const unsigned *bitstream)
{	
   unsigned cur_int = cursor / (sizeof(unsigned) * CHAR_BIT);
   unsigned cur_bit = cursor % (sizeof(unsigned) * CHAR_BIT);
	
   assert(bitstream);
	
   if (cur_bit + how_many_bits > sizeof(unsigned) * CHAR_BIT)
   {
      return grab_bits(cur_bit, sizeof(unsigned) * CHAR_BIT - cur_bit,
                       bitstream[cur_int]) |
             grab_bits(0, cur_bit + how_many_bits - sizeof(unsigned) * CHAR_BIT,
                       bitstream[cur_int + 1]) << (sizeof(unsigned) * CHAR_BIT - cur_bit);
   }
   else
      return grab_bits(cur_bit, how_many_bits, bitstream[cur_int]);
}

bool vl_bitstream_parser_init(struct vl_bitstream_parser *parser,
                              unsigned num_bitstreams,
                              const void **bitstreams,
                              const unsigned *sizes)
{
   assert(parser);
   assert(num_bitstreams);
   assert(bitstreams);
   assert(sizes);

   parser->num_bitstreams = num_bitstreams;
   parser->bitstreams = (const unsigned**)bitstreams;
   parser->sizes = sizes;
   parser->cur_bitstream = 0;
   parser->cursor = 0;

   return true;
}

void vl_bitstream_parser_cleanup(struct vl_bitstream_parser *parser)
{
   assert(parser);
}

unsigned
vl_bitstream_parser_get_bits(struct vl_bitstream_parser *parser,
                             unsigned how_many_bits)
{
   unsigned bits;

   assert(parser);

   bits = vl_bitstream_parser_show_bits(parser, how_many_bits);

   vl_bitstream_parser_forward(parser, how_many_bits);

   return bits;
}

unsigned
vl_bitstream_parser_show_bits(struct vl_bitstream_parser *parser,
                              unsigned how_many_bits)
{	
   unsigned bits = 0;
   unsigned shift = 0;
   unsigned cursor;
   unsigned cur_bitstream;

   assert(parser);

   cursor = parser->cursor;
   cur_bitstream = parser->cur_bitstream;

   while (1)
   {
      unsigned bits_left = parser->sizes[cur_bitstream] * CHAR_BIT - cursor;
      unsigned bits_to_show = how_many_bits > bits_left ? bits_left : how_many_bits;

      bits |= show_bits(cursor, bits_to_show,
                        parser->bitstreams[cur_bitstream]) << shift;
		
      if (how_many_bits > bits_to_show)
      {
         how_many_bits -= bits_to_show;
         cursor = 0;
         ++cur_bitstream;
         shift += bits_to_show;
      }
      else
         break;
   }

   return bits;
}

void vl_bitstream_parser_forward(struct vl_bitstream_parser *parser,
                                 unsigned how_many_bits)
{
   assert(parser);
   assert(how_many_bits);

   parser->cursor += how_many_bits;

   while (parser->cursor > parser->sizes[parser->cur_bitstream] * CHAR_BIT)
   {
      parser->cursor -= parser->sizes[parser->cur_bitstream++] * CHAR_BIT;
      assert(parser->cur_bitstream < parser->num_bitstreams);
   }
}

void vl_bitstream_parser_rewind(struct vl_bitstream_parser *parser,
                                unsigned how_many_bits)
{
   signed c;
	
   assert(parser);
   assert(how_many_bits);
	
   c = parser->cursor - how_many_bits;

   while (c < 0)
   {
      c += parser->sizes[parser->cur_bitstream--] * CHAR_BIT;
      assert(parser->cur_bitstream < parser->num_bitstreams);
   }

   parser->cursor = (unsigned)c;
}