ReactOS 0.4.15-dev-8127-g6338913
getbits.h
Go to the documentation of this file.
1/*
2 getbits
3
4 copyright ?-2009 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Michael Hipp
7
8 All code is in the header to suggest/force inlining of these small often-used functions.
9 This indeed has some impact on performance.
10*/
11
12#ifndef _MPG123_GETBITS_H_
13#define _MPG123_GETBITS_H_
14
15#include "mpg123lib_intern.h"
16#include "debug.h"
17
18#define backbits(fr,nob) ((void)( \
19 fr->bits_avail += nob, \
20 fr->bitindex -= nob, \
21 fr->wordpointer += (fr->bitindex>>3), \
22 fr->bitindex &= 0x7 ))
23
24#define getbitoffset(fr) ((-fr->bitindex)&0x7)
25/* Precomputing the bytes to be read is error-prone, and some over-read
26 is even expected for Huffman. Just play safe and return zeros in case
27 of overflow. This assumes you made bitindex zero already! */
28#define getbyte(fr) ( (fr)->bits_avail-=8, (fr)->bits_avail >= 0 \
29 ? *((fr)->wordpointer++) \
30 : 0 )
31
32static unsigned int getbits(mpg123_handle *fr, int number_of_bits)
33{
34 unsigned long rval;
35
36#ifdef DEBUG_GETBITS
37fprintf(stderr,"g%d",number_of_bits);
38#endif
39 fr->bits_avail -= number_of_bits;
40 /* Safety catch until we got the nasty code fully figured out. */
41 /* No, that catch stays here, even if we think we got it figured out! */
42 if(fr->bits_avail < 0)
43 {
44 if(NOQUIET)
45 error2( "Tried to read %i bits with %li available."
46 , number_of_bits, fr->bits_avail );
47 return 0;
48 }
49/* This is actually slow: if(!number_of_bits)
50 return 0; */
51
52#if 0
53 check_buffer_range(number_of_bits+fr->bitindex);
54#endif
55
56 {
57 rval = fr->wordpointer[0];
58 rval <<= 8;
59 rval |= fr->wordpointer[1];
60 rval <<= 8;
61 rval |= fr->wordpointer[2];
62
63 rval <<= fr->bitindex;
64 rval &= 0xffffff;
65
66 fr->bitindex += number_of_bits;
67
68 rval >>= (24-number_of_bits);
69
70 fr->wordpointer += (fr->bitindex>>3);
71 fr->bitindex &= 7;
72 }
73
74#ifdef DEBUG_GETBITS
75fprintf(stderr,":%lx\n",rval);
76#endif
77
78 return rval;
79}
80
81
82#define skipbits(fr, nob) fr->ultmp = ( \
83 fr->ultmp = fr->wordpointer[0], fr->ultmp <<= 8, fr->ultmp |= fr->wordpointer[1], \
84 fr->ultmp <<= 8, fr->ultmp |= fr->wordpointer[2], fr->ultmp <<= fr->bitindex, \
85 fr->ultmp &= 0xffffff, fr->bitindex += nob, fr->bits_avail -= nob, \
86 fr->ultmp >>= (24-nob), fr->wordpointer += (fr->bitindex>>3), \
87 fr->bitindex &= 7 )
88
89#define getbits_fast(fr, nob) ( \
90 fr->ultmp = (unsigned char) (fr->wordpointer[0] << fr->bitindex), \
91 fr->ultmp |= ((unsigned long) fr->wordpointer[1]<<fr->bitindex)>>8, \
92 fr->ultmp <<= nob, fr->ultmp >>= 8, \
93 fr->bitindex += nob, fr->bits_avail -= nob, \
94 fr->wordpointer += (fr->bitindex>>3), \
95 fr->bitindex &= 7, fr->ultmp )
96
97#define get1bit(fr) ( \
98 fr->uctmp = *fr->wordpointer << fr->bitindex, \
99 ++fr->bitindex, --fr->bits_avail, \
100 fr->wordpointer += (fr->bitindex>>3), fr->bitindex &= 7, fr->uctmp>>7 )
101
102
103#endif
float rval
Definition: cylfrac.c:48
static unsigned int getbits(mpg123_handle *fr, int number_of_bits)
Definition: getbits.h:32
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define NOQUIET
#define error2(s, a, b)
Definition: debug.h:126
unsigned char * wordpointer
Definition: frame.h:229