summaryrefslogtreecommitdiffstats
path: root/mpg123_artsplugin/mpg123/getbits.c
blob: 3ed993cb7f55c2d3869b35cc4898493affa9fa20 (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
#include "mpg123.h"
#include "common.h"

void backbits(struct bitstream_info *bitbuf,int number_of_bits)
{
  bitbuf->bitindex    -= number_of_bits;
  bitbuf->wordpointer += (bitbuf->bitindex>>3);
  bitbuf->bitindex    &= 0x7;
}

int getbitoffset(struct bitstream_info *bitbuf) 
{
  return (-bitbuf->bitindex)&0x7;
}

int getbyte(struct bitstream_info *bitbuf)
{
#ifdef DEBUG_GETBITS
  if(bitbuf->bitindex) 
    fprintf(stderr,"getbyte called unsynched!\n");
#endif
  return *bitbuf->wordpointer++;
}

unsigned int getbits(struct bitstream_info *bitbuf,int number_of_bits)
{
  unsigned long rval;

#ifdef DEBUG_GETBITS
fprintf(stderr,"g%d",number_of_bits);
#endif

  if(!number_of_bits)
    return 0;

#if 0
   check_buffer_range(number_of_bits+bitbuf->bitindex);
#endif

  {
    rval = bitbuf->wordpointer[0];
    rval <<= 8;
    rval |= bitbuf->wordpointer[1];
    rval <<= 8;
    rval |= bitbuf->wordpointer[2];

    rval <<= bitbuf->bitindex;
    rval &= 0xffffff;

    bitbuf->bitindex += number_of_bits;

    rval >>= (24-number_of_bits);

    bitbuf->wordpointer += (bitbuf->bitindex>>3);
    bitbuf->bitindex &= 7;
  }

#ifdef DEBUG_GETBITS
fprintf(stderr,":%x ",rval);
#endif

  return rval;
}

unsigned int getbits_fast(struct bitstream_info *bitbuf,int number_of_bits)
{
  unsigned int rval;
#ifdef DEBUG_GETBITS
fprintf(stderr,"g%d",number_of_bits);
#endif

#if 0
   check_buffer_range(number_of_bits+bitbuf->bitindex);
#endif

  rval =  (unsigned char) (bitbuf->wordpointer[0] << bitbuf->bitindex);
  rval |= ((unsigned int) bitbuf->wordpointer[1]<<bitbuf->bitindex)>>8;
  rval <<= number_of_bits;
  rval >>= 8;

  bitbuf->bitindex += number_of_bits;

  bitbuf->wordpointer += (bitbuf->bitindex>>3);
  bitbuf->bitindex &= 7;

#ifdef DEBUG_GETBITS
fprintf(stderr,":%x ",rval);
#endif
  return rval;
}

unsigned int get1bit(struct bitstream_info *bitbuf)
{
  unsigned char rval;

#ifdef DEBUG_GETBITS
fprintf(stderr,"g%d",1);
#endif

#if 0
   check_buffer_range(1+bitbuf->bitindex);
#endif

  rval = *(bitbuf->wordpointer) << bitbuf->bitindex;

  bitbuf->bitindex++;
  bitbuf->wordpointer += (bitbuf->bitindex>>3);
  bitbuf->bitindex &= 7;

#ifdef DEBUG_GETBITS
fprintf(stderr,":%d ",rval>>7);
#endif

  return rval>>7;
}