File: c_getbits.h

package info (click to toggle)
ogle 0.9.2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,940 kB
  • ctags: 3,499
  • sloc: ansic: 35,155; sh: 7,555; makefile: 259; asm: 71
file content (308 lines) | stat: -rw-r--r-- 6,601 bytes parent folder | download | duplicates (4)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#ifndef C_GETBITS_H_INCLUDED
#define C_GETBITS_H_INCLUDED

/* Ogle - A video player
 * Copyright (C) 2000, 2001 Bjrn Englund, Hkan Hjort
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <inttypes.h>

#include "ogle_endian.h"
#include "video_types.h"


//#define GETBITS32
#define GETBITS64
#define GETBITSMMAP


#ifdef GETBITSMMAP // Mmap i/o
void setup_mmap(char *);
void get_next_packet(void);
extern uint32_t *buf;
extern uint32_t buf_size;
extern uint32_t packet_offset;
extern uint32_t packet_length;
extern uint8_t *mmap_base;

#else // Normal i/o
#define READ_SIZE 1024*8
#define ALLOC_SIZE 2048
#define BUF_SIZE_MAX 1024*8
extern uint32_t buf[];

#endif // Common

extern FILE *infile;
extern char *infilename;

#ifdef GETBITS32
void back_word(void);
void next_word(void);
extern unsigned int backed;
extern unsigned int buf_pos;
extern unsigned int bits_left;
extern uint32_t cur_word;
#endif

#ifdef GETBITS64
void read_buf(void);
extern int offs;
extern unsigned int bits_left;
extern uint64_t cur_word;
#endif



#ifdef DEBUG
#define GETBITS(a,b) getbits(a,b)
#else
#define GETBITS(a,b) getbits(a)
#endif


/* ---------------------------------------------------------------------- */
#ifdef GETBITS64
#ifdef GETBITSMMAP // (64bit) Discontinuous buffers of variable size


/* 5.2.2 Definition of nextbits() function */
static inline
uint32_t nextbits(unsigned int nr)
{
  uint32_t result = (cur_word << (64-bits_left)) >> 32;
  return result >> (32-nr);
  //  return (cur_word << (64-bits_left)) >> (64-nr);
}

#ifdef DEBUG
static inline
uint32_t getbits(unsigned int nr, char *func)
#else
static inline
uint32_t getbits(unsigned int nr)
#endif
{
  uint32_t result;
  result = (cur_word << (64-bits_left)) >> 32;
  result = result >> (32-nr);
  //  result = cur_word >> (64-nr); //+
  //  cur_word = cur_word << nr; //+
  
  bits_left -= nr;
  if(bits_left <= 32) {
    if(offs >= buf_size) {
      read_buf();
   } else {
      uint32_t new_word = FROM_BE_32(buf[offs++]);
      cur_word = (cur_word << 32) | new_word;
      //cur_word = cur_word | (((uint64_t)new_word) << (32-bits_left)); //+
      bits_left += 32;
    }
  }
  DPRINTF(5, "%s getbits(%u): %x, ", func, nr, result);
  DPRINTBITS(6, nr, result);
  DPRINTF(5, "\n");
  return result;
}


static inline
void dropbits(unsigned int nr)
{
  //cur_word = cur_word << nr; //+
  bits_left -= nr;
  if(bits_left <= 32) {
    if(offs >= buf_size)
      read_buf();
    else {
      uint32_t new_word = FROM_BE_32(buf[offs++]);
      cur_word = (cur_word << 32) | new_word;
      //cur_word = cur_word | (((uint64_t)new_word) << (32-bits_left)); //+
      bits_left += 32;
    }
  }
}

#endif
#endif



/* ---------------------------------------------------------------------- */
#ifdef GETBITS64
#ifndef GETBITSMMAP // (64bit) Discontinuous buffers of *static* size


/* 5.2.2 Definition of nextbits() function */
static inline
uint32_t nextbits(unsigned int nr)
{
  uint32_t result = (cur_word << (64-bits_left)) >> 32;
  return result >> (32-nr);
}

#ifdef DEBUG
static inline
uint32_t getbits(unsigned int nr, char *func)
#else
static inline
uint32_t getbits(unsigned int nr)
#endif
{
  uint32_t result;
  result = (cur_word << (64-bits_left)) >> 32;
  result = result >> (32-nr);
  bits_left -=nr;
  if(bits_left <= 32) {
    uint32_t new_word = FROM_BE_32(buf[offs++]);
    if(offs >= READ_SIZE/4)
      read_buf();
    cur_word = (cur_word << 32) | new_word;
    bits_left += 32;
  }
  DPRINTF(5, "%s getbits(%u): %x, ", func, nr, result);
  DPRINTBITS(6, nr, result);
  DPRINTF(5, "\n");
  return result;
}


static inline
void dropbits(unsigned int nr)
{
  bits_left -= nr;
  if(bits_left <= 32) {
    uint32_t new_word = FROM_BE_32(buf[offs++]);
    if(offs >= READ_SIZE/4)
      read_buf();
    cur_word = (cur_word << 32) | new_word;
    bits_left += 32;
  }
}

#endif
#endif


/* ---------------------------------------------------------------------- */
#ifdef GETBITS32  // (32bit) 'Normal' getbits, word based.


#ifdef DEBUG
static inline
uint32_t getbits(unsigned int nr, char *func)
#else
static inline
uint32_t getbits(unsigned int nr)
#endif
{
  uint32_t result;
  uint32_t rem;
  if(nr <= bits_left) {
    result = (cur_word << (32-bits_left)) >> (32-nr);
    bits_left -=nr;
    if(bits_left == 0) {
      next_word();
      bits_left = 32;
    } 
  } else {
    rem = nr-bits_left;
    result = ((cur_word << (32-bits_left)) >> (32-bits_left)) << rem;

    next_word();
    bits_left = 32;
    result |= ((cur_word << (32-bits_left)) >> (32-rem));
    bits_left -=rem;
    if(bits_left == 0) {
      next_word();
      bits_left = 32;
    }
  }
  DPRINTF(5,"%s getbits(%u): %x ", func, nr, result);
  DPRINTBITS(6, nr, result);
  DPRINTF(5, "\n");
  return result;
}


static inline
void dropbits(unsigned int nr)
{
  bits_left -= nr;
  if(bits_left <= 0) {
    next_word();
    bits_left += 32;
  } 
}


/* 5.2.2 Definition of nextbits() function */
static inline
uint32_t nextbits(unsigned int nr)
{
  uint32_t result;
  uint32_t rem;
  unsigned int t_bits_left = bits_left;

  if(nr <= t_bits_left) {
    result = (cur_word << (32-t_bits_left)) >> (32-nr);
  } else {
    rem = nr-t_bits_left;
    result = ((cur_word << (32-t_bits_left)) >> (32-t_bits_left)) << rem;
    t_bits_left = 32;
    next_word();
    result |= ((cur_word << (32-t_bits_left)) >> (32-rem));
    back_word();
  }
  return result;
}
#endif


/* ---------------------------------------------------------------------- */






/* 5.2.1 Definition of bytealigned() function */
static inline
int bytealigned(void)
{
  return !(bits_left%8);
}

#ifdef DEBUG
static inline
void marker_bit(void)
{
  if(!GETBITS(1, "markerbit")) {
    fprintf(stderr, "*** video_decoder: incorrect marker_bit in stream\n");
    //exit_program(-1);
  }
}
#else // DEBUG
static inline
void marker_bit(void)
{
  dropbits(1);
}
#endif //DEBUG

#endif /* C_GETBITS_H_INCLUDED */