bitstream.c
3.68 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "libmpegaud.h"
#define LSBMASK 1
#define MASK8 0xff0
#define WORDSIZE 16
static int
getbyte(Bitstream *bs)
{
BitBuffer *bf = &bs->buffer[bs->currBuffer];
int c = bf->buffer[bs->pointer];
bs->currentbyte++;
if (++bs->pointer >= bf->length){
/*
* Swap buffers and initiate a DMA
*/
bs->currBuffer ^= 1;
bs->pointer = 0;
bs->dma(bs->ifile, bf->length, bf->buffer, 0);
bs->ifile += bf->length;
}
return c;
}
static long
getshort(Bitstream *bs)
{
int c1, c2;
if ((c1 = getbyte(bs)) < 0) return(-1);
if ((c2 = getbyte(bs)) < 0) return(-1);
return (c1<<8) | c2;
}
static s32
bytealign(Bitstream *bs)
{
s32 c;
if (bs->bitpointer == WORDSIZE-1)
return(bs->eobitstream);
if (bs->bitpointer > 7)
bs->bitpointer = 7;
else{
if ((c = getshort(bs)) < 0) bs->eobitstream = 1;
bs->word = (u32) c;
bs->bitpointer = WORDSIZE-1;
}
return(bs->eobitstream);
}
u32
readbit(Bitstream *bs)
{
u32 b;
s32 c;
b = (bs->word>>bs->bitpointer--) & LSBMASK;
if (bs->bitpointer < 0){
if ((c = getshort(bs)) < 0) bs->eobitstream = 1;
bs->word = (u32) c;
bs->bitpointer = WORDSIZE-1;
}
return(b);
}
u32
readbits(Bitstream *bs, u32 nbits)
{
s32 c;
u32 b, left;
/* Maximum of WORDSIZE bits can be read */
if (nbits <= bs->bitpointer+1){
u32 mask = (u32) (1L<<nbits) - 1;
b = mask & (bs->word>>(bs->bitpointer-nbits+1));
bs->bitpointer -= nbits;
if (bs->bitpointer < 0){
if ((c = getshort(bs)) < 0) bs->eobitstream = 1;
bs->word = (u32) c;
bs->bitpointer = WORDSIZE-1;
}
return(b);
}
else{
b = bs->word & ((1<<(bs->bitpointer+1)) - 1);
left = nbits - bs->bitpointer - 1;
if ((c = getshort(bs)) < 0) bs->eobitstream = 1;
bs->word = (u32) c;
bs->bitpointer = WORDSIZE-1;
b = (b<<left) | readbits(bs,left);
return(b);
}
}
s32
skiptonextframe(Bitstream *bs, s32 framebytes)
{
int i, c;
/* PRINTF("Skipping framebytes %d currentbytes %d\n", framebytes, bs->currentbyte); */
for (i=0; i<(framebytes-bs->currentbyte); i++)
if((c = getbyte(bs)) < 0)
return(-1);
bs->currentbyte = 0;
return(1);
}
s32
nextsyncword(Bitstream *bs, u32 sync_code, s32 nbits)
{
int x, y;
static skipcount = 0;
int first = 1;
/* This searches for a byte-aligned sync-word. This is definitely
OK, once you are in the bit-stream, but in general the "byte-alignment"
probably should be defined by the first detected sync-word, which
may not be byte-aligned.
The other problem is that the Ancillary data can contain sync-words,
so before starting the sync-word search you should skip to the end
of the Ancillary data. */
if (bytealign(bs))
return(0);
else{
while (1){
if (first){
if ((x = readbits(bs,nbits)) < 0){
bs->eobitstream = 1;
return(0);
}
first = 0;
} else {
if ((y = readbits(bs,8)) < 0){
bs->eobitstream = 1;
return(0);
}
x = (x<<8 & MASK8) | y;
}
if (x==sync_code)
return(1);
else{
skipcount++;
}
}
}
}
void
openbitstream(u8 *ptr, Bitstream *bs)
{
s32 c;
BitBuffer *bf = &bs->buffer[0];
bs->ifile = ptr;
bs->eobitstream = 0;
bs->currentbyte = 0;
bs->pointer = 0;
bs->currBuffer = 0;
/*
* Load two buffers - wait for completion on first
*/
bs->dma(bs->ifile, bf->length, bf->buffer, 1);
bs->ifile += bf->length;
bf = &bs->buffer[1];
bs->dma(bs->ifile, bf->length, bf->buffer, 0);
bs->ifile += bf->length;
if ((c = getshort(bs)) < 0) bs->eobitstream = 1;
bs->word = (u32) c;
bs->bitpointer = WORDSIZE-1;
}