CI130X SDK API手册  2.2.0
本手册用于描述CI130X SDK各个组件和驱动API
bitstreamf.h
浏览该文件的文档.
1 
6 #ifndef BITSTREAMF_H
7 #define BITSTREAMF_H
8 
9 #if (defined(__ICCARM__) || defined(__CC_ARM) || (defined(__GNUC__) && (defined(ARM) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)))) /* arm */
10  #include "ci130x_system.h"
11  #include "core_cm4.h"
12  #define letoh32(x) (x)
13  #define betoh32(x) (uint32_t)__REV(x)
14 #else
15 
16  #define __STATIC_INLINE static inline
17 
18  /* Endian conversion routines for standalone compilation */
19  #define letoh32(x) (x)
20  #define betoh32(x) swap32(x)
21 
22  /* Taken from rockbox/firmware/export/system.h */
23 
24  __STATIC_INLINE unsigned short swap16(unsigned short value)
25  /*
26  result[15..8] = value[ 7..0];
27  result[ 7..0] = value[15..8];
28  */
29  {
30  return (value >> 8) | (value << 8);
31  }
32 
33  __STATIC_INLINE unsigned long swap32(unsigned long value)
34  /*
35  result[31..24] = value[ 7.. 0];
36  result[23..16] = value[15.. 8];
37  result[15.. 8] = value[23..16];
38  result[ 7.. 0] = value[31..24];
39  */
40  {
41  unsigned long hi = swap16(value >> 16);
42  unsigned long lo = swap16(value & 0xffff);
43 
44  return (lo << 16) | hi;
45  }
46 #endif
47 
48 #define IBSS_ATTR
49 #define ICONST_ATTR
50 #define ICODE_ATTR
51 
52 #ifndef ICODE_ATTR_FLAC
53 #define ICODE_ATTR_FLAC ICODE_ATTR
54 #endif
55 
56 #ifndef IBSS_ATTR_FLAC_DECODED0
57 #define IBSS_ATTR_FLAC_DECODED0 IBSS_ATTR
58 #endif
59 
60 
61 
62 
63 /* FLAC files are big-endian */
64 #define ALT_BITSTREAM_READER_BE
65 
66 #define NEG_SSR32(a,s) (((int32_t)(a))>>(32-(s)))
67 #define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
68 
69 /* bit input */
70 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
71 typedef struct GetBitContext {
72  const uint8_t *buffer, *buffer_end;
73  int index;
76 
77 #define VLC_TYPE int16_t
78 
79 typedef struct VLC {
80  int bits;
81  VLC_TYPE (*table)[2];
83 } VLC;
84 
85 typedef struct RL_VLC_ELEM {
86  int16_t level;
87  int8_t len;
88  uint8_t run;
89 } RL_VLC_ELEM;
90 
91 
92 __STATIC_INLINE uint32_t unaligned32(const void *v) {
93  #pragma pack(push)
94  #pragma pack(1)
95  struct Unaligned {
96  uint32_t i;
97  };
98  #pragma pack(pop)
99 
100  return ((struct Unaligned *)v)->i;
101 }
102 
103 
104 /* Bitstream reader API docs:
105 name
106  abritary name which is used as prefix for the internal variables
107 
108 gb
109  getbitcontext
110 
111 OPEN_READER(name, gb)
112  loads gb into local variables
113 
114 CLOSE_READER(name, gb)
115  stores local vars in gb
116 
117 UPDATE_CACHE(name, gb)
118  refills the internal cache from the bitstream
119  after this call at least MIN_CACHE_BITS will be available,
120 
121 GET_CACHE(name, gb)
122  will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
123 
124 SHOW_UBITS(name, gb, num)
125  will return the next num bits
126 
127 SHOW_SBITS(name, gb, num)
128  will return the next num bits and do sign extension
129 
130 SKIP_BITS(name, gb, num)
131  will skip over the next num bits
132  note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
133 
134 SKIP_CACHE(name, gb, num)
135  will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
136 
137 SKIP_COUNTER(name, gb, num)
138  will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
139 
140 LAST_SKIP_CACHE(name, gb, num)
141  will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
142 
143 LAST_SKIP_BITS(name, gb, num)
144  is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
145 
146 for examples see get_bits, show_bits, skip_bits, get_vlc
147 */
148 
149 __STATIC_INLINE int unaligned32_be(const void *v){
150  return betoh32(unaligned32(v)); // original
151 }
152 
153 __STATIC_INLINE int unaligned32_le(const void *v){
154  return letoh32(unaligned32(v)); // original
155 }
156 
157 # define MIN_CACHE_BITS 25
158 
159 # define OPEN_READER(name, gb)\
160  int name##_index= (gb)->index;\
161  int name##_cache= 0;\
162 
163 # define CLOSE_READER(name, gb)\
164  (gb)->index= name##_index;\
165 
166 # define UPDATE_CACHE(name, gb)\
167  name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
168 
169 # define SKIP_CACHE(name, gb, num)\
170  name##_cache <<= (num);
171 
172 
173 // FIXME name?
174 # define SKIP_COUNTER(name, gb, num)\
175  name##_index += (num);\
176 
177 # define SKIP_BITS(name, gb, num)\
178  {\
179  SKIP_CACHE(name, gb, num)\
180  SKIP_COUNTER(name, gb, num)\
181  }\
182 
183 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
184 # define LAST_SKIP_CACHE(name, gb, num) ;
185 
186 # define SHOW_UBITS(name, gb, num)\
187  NEG_USR32(name##_cache, num)
188 
189 # define SHOW_SBITS(name, gb, num)\
190  NEG_SSR32(name##_cache, num)
191 
192 # define GET_CACHE(name, gb)\
193  ((uint32_t)name##_cache)
194 
195 
197  return s->index;
198 }
199 
201  register int tmp;
202  OPEN_READER(re, s)
203  UPDATE_CACHE(re, s)
204  tmp = SHOW_SBITS(re, s, n);
205  LAST_SKIP_BITS(re, s, n)
206  CLOSE_READER(re, s)
207  return tmp;
208 }
209 
210 __STATIC_INLINE unsigned int get_bits(GetBitContext *s, int n){
211  register int tmp;
212  OPEN_READER(re, s)
213  UPDATE_CACHE(re, s)
214  tmp = SHOW_UBITS(re, s, n);
215  LAST_SKIP_BITS(re, s, n)
216  CLOSE_READER(re, s)
217  return tmp;
218 }
219 
220 __STATIC_INLINE unsigned int show_bits(GetBitContext *s, int n){
221  register int tmp;
222  OPEN_READER(re, s)
223  UPDATE_CACHE(re, s)
224  tmp = SHOW_UBITS(re, s, n);
225 // CLOSE_READER(re, s)
226  return tmp;
227 }
228 
230 /*
231  OPEN_READER(re, s)
232  UPDATE_CACHE(re, s)
233  LAST_SKIP_BITS(re, s, n)
234  CLOSE_READER(re, s)
235 
236  int re_index = s->index;
237  unaligned32_be(((const uint8_t *)s->buffer) + (re_index >> 3)) << (re_index & 0x07);
238  re_index += n;
239  s->index = re_index;
240 */
241  s->index += n;
242 }
243 
245  int index = s->index;
246  uint8_t result = s->buffer[index >> 3];
247  result <<= (index & 0x07);
248  result >>= 8 - 1;
249  index ++;
250  s->index = index;
251  return result;
252 }
253 
255  return show_bits(s, 1);
256 }
257 
259  skip_bits(s, 1);
260 }
261 
262 __STATIC_INLINE void init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size){
263  int buffer_size= (bit_size+7)>>3;
264  if(buffer_size < 0 || bit_size < 0) {
265  buffer_size = bit_size = 0;
266  buffer = 0;
267  }
268 
269  s->buffer= buffer;
270  s->size_in_bits= bit_size;
271  s->buffer_end= buffer + buffer_size;
272  s->index=0;
273 /*
274  {
275  OPEN_READER(re, s)
276  UPDATE_CACHE(re, s)
277  UPDATE_CACHE(re, s)
278  CLOSE_READER(re, s)
279  }
280 
281  {
282  int re_index = s->index;
283  unaligned32_be(((const uint8_t *)s->buffer) + (re_index >> 3)) << (re_index & 0x07);
284  unaligned32_be(((const uint8_t *)s->buffer) + (re_index >> 3)) << (re_index & 0x07);
285  s->index = re_index;
286  }
287 */
288 }
289 
291 
292 #endif /* BITSTREAM_H */
static unsigned int show_bits(GetBitContext *s, int n)
Definition: bitstreamf.h:220
static int get_sbits(GetBitContext *s, int n)
Definition: bitstreamf.h:200
static int unaligned32_le(const void *v)
Definition: bitstreamf.h:153
void align_get_bits(GetBitContext *s)
Definition: bitstreamf.c:60
struct RL_VLC_ELEM RL_VLC_ELEM
#define LAST_SKIP_BITS(name, gb, num)
Definition: bitstreamf.h:183
static unsigned int get_bits(GetBitContext *s, int n)
Definition: bitstreamf.h:210
static void skip_bits(GetBitContext *s, int n)
Definition: bitstreamf.h:229
static uint32_t unaligned32(const void *v)
Definition: bitstreamf.h:92
#define SHOW_SBITS(name, gb, num)
Definition: bitstreamf.h:189
#define VLC_TYPE
Definition: bitstreamf.h:77
#define SHOW_UBITS(name, gb, num)
Definition: bitstreamf.h:186
int16_t(* table)[2]
code, bits
Definition: bitstreamf.h:81
static unsigned int get_bits1(GetBitContext *s)
Definition: bitstreamf.h:244
int bits
Definition: bitstreamf.h:80
static unsigned int show_bits1(GetBitContext *s)
Definition: bitstreamf.h:254
Definition: bitstreamf.h:71
Definition: bitstreamf.h:85
static void skip_bits1(GetBitContext *s)
Definition: bitstreamf.h:258
#define betoh32(x)
Definition: bitstreamf.h:20
const uint8_t * buffer_end
Definition: bitstreamf.h:72
struct GetBitContext GetBitContext
int16_t level
Definition: bitstreamf.h:86
int8_t len
Definition: bitstreamf.h:87
int table_size
Definition: bitstreamf.h:82
static volatile char tmp[32]
Definition: flash_manage_outside_port.c:144
static unsigned short swap16(unsigned short value)
Definition: bitstreamf.h:24
int table_allocated
Definition: bitstreamf.h:82
chip级定义
#define OPEN_READER(name, gb)
Definition: bitstreamf.h:159
const uint8_t * buffer
Definition: bitstreamf.h:72
#define letoh32(x)
Definition: bitstreamf.h:19
Definition: bitstreamf.h:79
#define __STATIC_INLINE
Definition: bitstreamf.h:16
static unsigned long swap32(unsigned long value)
Definition: bitstreamf.h:33
#define UPDATE_CACHE(name, gb)
Definition: bitstreamf.h:166
struct VLC VLC
int size_in_bits
Definition: bitstreamf.h:74
uint8_t run
Definition: bitstreamf.h:88
static int unaligned32_be(const void *v)
Definition: bitstreamf.h:149
int index
Definition: bitstreamf.h:73
static int get_bits_count(GetBitContext *s)
Definition: bitstreamf.h:196
#define CLOSE_READER(name, gb)
Definition: bitstreamf.h:163
static void init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Definition: bitstreamf.h:262