UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
bs.h File Reference
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for bs.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  bs_t
 

Macros

#define _OPTIMIZE_BS_   1
 
#define bs_print_state(b)
 
#define FAST_U8
 

Functions

static uint32_t bs_byte_aligned (bs_t *b)
 
static int bs_bytes_left (bs_t *b)
 
static bs_t * bs_clone (bs_t *dest, const bs_t *src)
 
static int bs_eof (bs_t *b)
 
static void bs_free (bs_t *b)
 
static bs_t * bs_init (bs_t *b, uint8_t *buf, size_t size)
 
static bs_t * bs_new (uint8_t *buf, size_t size)
 
static uint32_t bs_next_bits (bs_t *b, int nbits)
 
static uint64_t bs_next_bytes (bs_t *bs, int nbytes)
 
static int bs_overrun (bs_t *b)
 
static uint32_t bs_peek_u1 (bs_t *b)
 
static int bs_pos (bs_t *b)
 
static int bs_read_bytes (bs_t *b, uint8_t *buf, int len)
 
static uint32_t bs_read_f (bs_t *b, int n)
 
static int32_t bs_read_se (bs_t *b)
 
static uint32_t bs_read_u (bs_t *b, int n)
 
static uint32_t bs_read_u1 (bs_t *b)
 
static uint32_t bs_read_u8 (bs_t *b)
 
static uint32_t bs_read_ue (bs_t *b)
 
static int bs_skip_bytes (bs_t *b, int len)
 
static void bs_skip_u (bs_t *b, int n)
 
static void bs_skip_u1 (bs_t *b)
 
static int bs_write_bytes (bs_t *b, uint8_t *buf, int len)
 
static void bs_write_f (bs_t *b, int n, uint32_t v)
 
static void bs_write_se (bs_t *b, int32_t v)
 
static void bs_write_u (bs_t *b, int n, uint32_t v)
 
static void bs_write_u1 (bs_t *b, uint32_t v)
 
static void bs_write_u8 (bs_t *b, uint32_t v)
 
static void bs_write_ue (bs_t *b, uint32_t v)
 

Macro Definition Documentation

◆ _OPTIMIZE_BS_

#define _OPTIMIZE_BS_   1

Definition at line 42 of file bs.h.

◆ bs_print_state

#define bs_print_state ( b)
Value:
fprintf( stderr, "%s:%d@%s: b->p=0x%02hhX, b->left = %d\n", __FILE__, __LINE__, __FUNCTION__, *b->p, b->bits_left )

Definition at line 382 of file bs.h.

◆ FAST_U8

#define FAST_U8

Definition at line 46 of file bs.h.

Function Documentation

◆ bs_byte_aligned()

static uint32_t bs_byte_aligned ( bs_t * b)
inlinestatic

Definition at line 111 of file bs.h.

112{
113 return (b->bits_left == 8);
114}
int bits_left
Definition bs.h:39

References bs_t::bits_left.

Referenced by read_rbsp_trailing_bits().

◆ bs_bytes_left()

static int bs_bytes_left ( bs_t * b)
inlinestatic

Definition at line 122 of file bs.h.

122{ return (b->end - b->p); }
uint8_t * p
Definition bs.h:37
uint8_t * end
Definition bs.h:38

References bs_t::end, and bs_t::p.

◆ bs_clone()

static bs_t * bs_clone ( bs_t * dest,
const bs_t * src )
inlinestatic

Definition at line 102 of file bs.h.

103{
104 dest->start = src->p;
105 dest->p = src->p;
106 dest->end = src->end;
107 dest->bits_left = src->bits_left;
108 return dest;
109}
uint8_t * start
Definition bs.h:36

References bs_t::bits_left, bs_t::end, bs_t::p, and bs_t::start.

Referenced by bs_next_bits().

◆ bs_eof()

static int bs_eof ( bs_t * b)
inlinestatic

Definition at line 116 of file bs.h.

116{ if (b->p >= b->end) { return 1; } else { return 0; } }

References bs_t::end, and bs_t::p.

Referenced by bs_peek_u1(), bs_read_u1(), bs_read_u8(), bs_read_ue(), bs_write_u1(), and bs_write_u8().

◆ bs_free()

static void bs_free ( bs_t * b)
inlinestatic

Definition at line 97 of file bs.h.

98{
99 free(b);
100}

Referenced by fill_coded_frame_from_sps(), and width_height_from_SDP().

◆ bs_init()

static bs_t * bs_init ( bs_t * b,
uint8_t * buf,
size_t size )
inlinestatic

Definition at line 81 of file bs.h.

82{
83 b->start = buf;
84 b->p = buf;
85 b->end = buf + size;
86 b->bits_left = 8;
87 return b;
88}

References bs_t::bits_left, bs_t::end, bs_t::p, and bs_t::start.

Referenced by bs_new().

◆ bs_new()

static bs_t * bs_new ( uint8_t * buf,
size_t size )
inlinestatic

Definition at line 90 of file bs.h.

91{
92 bs_t* b = (bs_t*)malloc(sizeof(bs_t));
93 bs_init(b, buf, size);
94 return b;
95}
static bs_t * bs_init(bs_t *b, uint8_t *buf, size_t size)
Definition bs.h:81
Definition bs.h:35

References bs_init().

Referenced by fill_coded_frame_from_sps(), and width_height_from_SDP().

Here is the call graph for this function:

◆ bs_next_bits()

static uint32_t bs_next_bits ( bs_t * b,
int nbits )
inlinestatic

Definition at line 363 of file bs.h.

364{
365 bs_t b;
366 bs_clone(&b,bs);
367 return bs_read_u(&b, nbits);
368}
static uint32_t bs_read_u(bs_t *b, int n)
Definition bs.h:158
static bs_t * bs_clone(bs_t *dest, const bs_t *src)
Definition bs.h:102

References bs_clone(), and bs_read_u().

Here is the call graph for this function:

◆ bs_next_bytes()

static uint64_t bs_next_bytes ( bs_t * bs,
int nbytes )
inlinestatic

Definition at line 370 of file bs.h.

371{
372 int i = 0;
373 uint64_t val = 0;
374
375 if ( (nbytes > 8) || (nbytes < 1) ) { return 0; }
376 if (bs->p + nbytes > bs->end) { return 0; }
377
378 for ( i = 0; i < nbytes; i++ ) { val = ( val << 8 ) | bs->p[i]; }
379 return val;
380}
int void * val

References val.

◆ bs_overrun()

static int bs_overrun ( bs_t * b)
inlinestatic

Definition at line 118 of file bs.h.

118{ if (b->p > b->end) { return 1; } else { return 0; } }

References bs_t::end, and bs_t::p.

◆ bs_peek_u1()

static uint32_t bs_peek_u1 ( bs_t * b)
inlinestatic

Definition at line 146 of file bs.h.

147{
148 uint32_t r = 0;
149
150 if (! bs_eof(b))
151 {
152 r = ((*(b->p)) >> ( b->bits_left - 1 )) & 0x01;
153 }
154 return r;
155}
static int bs_eof(bs_t *b)
Definition bs.h:116

References bs_t::bits_left, bs_eof(), and bs_t::p.

Here is the call graph for this function:

◆ bs_pos()

static int bs_pos ( bs_t * b)
inlinestatic

Definition at line 120 of file bs.h.

120{ if (b->p > b->end) { return (b->end - b->start); } else { return (b->p - b->start); } }

References bs_t::end, bs_t::p, and bs_t::start.

◆ bs_read_bytes()

static int bs_read_bytes ( bs_t * b,
uint8_t * buf,
int len )
inlinestatic

Definition at line 331 of file bs.h.

332{
333 int actual_len = len;
334 if (b->end - b->p < actual_len) { actual_len = b->end - b->p; }
335 if (actual_len < 0) { actual_len = 0; }
336 memcpy(buf, b->p, actual_len);
337 if (len < 0) { len = 0; }
338 b->p += len;
339 return actual_len;
340}
int void size_t * len

References bs_t::end, len, and bs_t::p.

◆ bs_read_f()

static uint32_t bs_read_f ( bs_t * b,
int n )
inlinestatic

Definition at line 178 of file bs.h.

178{ return bs_read_u(b, n); }

References bs_read_u().

Here is the call graph for this function:

◆ bs_read_se()

static int32_t bs_read_se ( bs_t * b)
inlinestatic

Definition at line 207 of file bs.h.

208{
209 int32_t r = bs_read_ue(b);
210 if (r & 0x01)
211 {
212 r = (r+1)/2;
213 }
214 else
215 {
216 r = -(r/2);
217 }
218 return r;
219}
static uint32_t bs_read_ue(bs_t *b)
Definition bs.h:193

References bs_read_ue().

Referenced by read_scaling_list(), and read_seq_parameter_set_rbsp().

Here is the call graph for this function:

◆ bs_read_u()

static uint32_t bs_read_u ( bs_t * b,
int n )
inlinestatic

Definition at line 158 of file bs.h.

159{
160 uint32_t r = 0;
161 int i;
162 for (i = 0; i < n; i++)
163 {
164 r |= ( bs_read_u1(b) << ( n - i - 1 ) );
165 }
166 return r;
167}
static uint32_t bs_read_u1(bs_t *b)
Definition bs.h:124

References bs_read_u1().

Referenced by bs_next_bits(), bs_read_f(), bs_read_u8(), bs_read_ue(), read_hrd_parameters(), read_seq_parameter_set_rbsp(), and read_vui_parameters().

Here is the call graph for this function:

◆ bs_read_u1()

static uint32_t bs_read_u1 ( bs_t * b)
inlinestatic

Definition at line 124 of file bs.h.

125{
126 uint32_t r = 0;
127
128 b->bits_left--;
129
130 if (! bs_eof(b))
131 {
132 r = ((*(b->p)) >> b->bits_left) & 0x01;
133 }
134
135 if (b->bits_left == 0) { b->p ++; b->bits_left = 8; }
136
137 return r;
138}

References bs_t::bits_left, bs_eof(), and bs_t::p.

Referenced by bs_read_u(), bs_read_ue(), read_hrd_parameters(), read_rbsp_trailing_bits(), read_seq_parameter_set_rbsp(), and read_vui_parameters().

Here is the call graph for this function:

◆ bs_read_u8()

static uint32_t bs_read_u8 ( bs_t * b)
inlinestatic

Definition at line 180 of file bs.h.

181{
182#ifdef FAST_U8
183 if (b->bits_left == 8 && ! bs_eof(b)) // can do fast read
184 {
185 uint32_t r = b->p[0];
186 b->p++;
187 return r;
188 }
189#endif
190 return bs_read_u(b, 8);
191}

References bs_t::bits_left, bs_eof(), bs_read_u(), and bs_t::p.

Referenced by read_seq_parameter_set_rbsp(), and read_vui_parameters().

Here is the call graph for this function:

◆ bs_read_ue()

static uint32_t bs_read_ue ( bs_t * b)
inlinestatic

Definition at line 193 of file bs.h.

194{
195 int32_t r = 0;
196 int i = 0;
197
198 while( (bs_read_u1(b) == 0) && (i < 32) && (!bs_eof(b)) )
199 {
200 i++;
201 }
202 r = bs_read_u(b, i);
203 r += (1 << i) - 1;
204 return r;
205}

References bs_eof(), bs_read_u(), and bs_read_u1().

Referenced by bs_read_se(), read_hrd_parameters(), read_seq_parameter_set_rbsp(), and read_vui_parameters().

Here is the call graph for this function:

◆ bs_skip_bytes()

static int bs_skip_bytes ( bs_t * b,
int len )
inlinestatic

Definition at line 353 of file bs.h.

354{
355 int actual_len = len;
356 if (b->end - b->p < actual_len) { actual_len = b->end - b->p; }
357 if (actual_len < 0) { actual_len = 0; }
358 if (len < 0) { len = 0; }
359 b->p += len;
360 return actual_len;
361}

References bs_t::end, len, and bs_t::p.

◆ bs_skip_u()

static void bs_skip_u ( bs_t * b,
int n )
inlinestatic

Definition at line 169 of file bs.h.

170{
171 int i;
172 for ( i = 0; i < n; i++ )
173 {
174 bs_skip_u1( b );
175 }
176}
static void bs_skip_u1(bs_t *b)
Definition bs.h:140

References bs_skip_u1().

Here is the call graph for this function:

◆ bs_skip_u1()

static void bs_skip_u1 ( bs_t * b)
inlinestatic

Definition at line 140 of file bs.h.

141{
142 b->bits_left--;
143 if (b->bits_left == 0) { b->p ++; b->bits_left = 8; }
144}

References bs_t::bits_left, and bs_t::p.

Referenced by bs_skip_u().

◆ bs_write_bytes()

static int bs_write_bytes ( bs_t * b,
uint8_t * buf,
int len )
inlinestatic

Definition at line 342 of file bs.h.

343{
344 int actual_len = len;
345 if (b->end - b->p < actual_len) { actual_len = b->end - b->p; }
346 if (actual_len < 0) { actual_len = 0; }
347 memcpy(b->p, buf, actual_len);
348 if (len < 0) { len = 0; }
349 b->p += len;
350 return actual_len;
351}

References bs_t::end, len, and bs_t::p.

◆ bs_write_f()

static void bs_write_f ( bs_t * b,
int n,
uint32_t v )
inlinestatic

Definition at line 247 of file bs.h.

247{ bs_write_u(b, n, v); }
static void bs_write_u(bs_t *b, int n, uint32_t v)
Definition bs.h:238

References bs_write_u().

Here is the call graph for this function:

◆ bs_write_se()

static void bs_write_se ( bs_t * b,
int32_t v )
inlinestatic

Definition at line 319 of file bs.h.

320{
321 if (v <= 0)
322 {
323 bs_write_ue(b, -v*2);
324 }
325 else
326 {
327 bs_write_ue(b, v*2 - 1);
328 }
329}
static void bs_write_ue(bs_t *b, uint32_t v)
Definition bs.h:262

References bs_write_ue().

Here is the call graph for this function:

◆ bs_write_u()

static void bs_write_u ( bs_t * b,
int n,
uint32_t v )
inlinestatic

Definition at line 238 of file bs.h.

239{
240 int i;
241 for (i = 0; i < n; i++)
242 {
243 bs_write_u1(b, (v >> ( n - i - 1 ))&0x01 );
244 }
245}
static void bs_write_u1(bs_t *b, uint32_t v)
Definition bs.h:222

References bs_write_u1().

Referenced by bs_write_f(), bs_write_u8(), and bs_write_ue().

Here is the call graph for this function:

◆ bs_write_u1()

static void bs_write_u1 ( bs_t * b,
uint32_t v )
inlinestatic

Definition at line 222 of file bs.h.

223{
224 b->bits_left--;
225
226 if (! bs_eof(b))
227 {
228 // FIXME this is slow, but we must clear bit first
229 // is it better to memset(0) the whole buffer during bs_init() instead?
230 // if we don't do either, we introduce pretty nasty bugs
231 (*(b->p)) &= ~(0x01 << b->bits_left);
232 (*(b->p)) |= ((v & 0x01) << b->bits_left);
233 }
234
235 if (b->bits_left == 0) { b->p ++; b->bits_left = 8; }
236}

References bs_t::bits_left, bs_eof(), and bs_t::p.

Referenced by bs_write_u(), and bs_write_ue().

Here is the call graph for this function:

◆ bs_write_u8()

static void bs_write_u8 ( bs_t * b,
uint32_t v )
inlinestatic

Definition at line 249 of file bs.h.

250{
251#ifdef FAST_U8
252 if (b->bits_left == 8 && ! bs_eof(b)) // can do fast write
253 {
254 b->p[0] = v;
255 b->p++;
256 return;
257 }
258#endif
259 bs_write_u(b, 8, v);
260}

References bs_t::bits_left, bs_eof(), bs_write_u(), and bs_t::p.

Here is the call graph for this function:

◆ bs_write_ue()

static void bs_write_ue ( bs_t * b,
uint32_t v )
inlinestatic

Definition at line 262 of file bs.h.

263{
264 static const int len_table[256] =
265 {
266 1,
267 1,
268 2,2,
269 3,3,3,3,
270 4,4,4,4,4,4,4,4,
271 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
272 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
273 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
274 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
275 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
276 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
277 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
278 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
279 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
280 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
281 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
282 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
283 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
284 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
285 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
286 };
287
288 int len;
289
290 if (v == 0)
291 {
292 bs_write_u1(b, 1);
293 }
294 else
295 {
296 v++;
297
298 if (v >= 0x01000000)
299 {
300 len = 24 + len_table[ v >> 24 ];
301 }
302 else if(v >= 0x00010000)
303 {
304 len = 16 + len_table[ v >> 16 ];
305 }
306 else if(v >= 0x00000100)
307 {
308 len = 8 + len_table[ v >> 8 ];
309 }
310 else
311 {
312 len = len_table[ v ];
313 }
314
315 bs_write_u(b, 2*len-1, v);
316 }
317}

References bs_write_u(), bs_write_u1(), and len.

Referenced by bs_write_se().

Here is the call graph for this function: