Asterisk - The Open Source Telephony Project GIT-master-f36a736
packing.c
Go to the documentation of this file.
1
2 /******************************************************************
3
4 iLBC Speech Coder ANSI-C Source Code
5
6 packing.c
7
8 Copyright (C) The Internet Society (2004).
9 All Rights Reserved.
10
11 ******************************************************************/
12
13 #include <math.h>
14 #include <stdlib.h>
15
16 #include "iLBC_define.h"
17 #include "constants.h"
18 #include "helpfun.h"
19 #include "string.h"
20
21 /*----------------------------------------------------------------*
22 * splitting an integer into first most significant bits and
23 * remaining least significant bits
24 *---------------------------------------------------------------*/
25
27 int *index, /* (i) the value to split */
28 int *firstpart, /* (o) the value specified by most
29 significant bits */
30 int *rest, /* (o) the value specified by least
31 significant bits */
32
33
34
35
36
37 int bitno_firstpart, /* (i) number of bits in most
38 significant part */
39 int bitno_total /* (i) number of bits in full range
40 of value */
41 ){
42 int bitno_rest = bitno_total-bitno_firstpart;
43
44 *firstpart = *index>>(bitno_rest);
45 *rest = *index-(*firstpart<<(bitno_rest));
46 }
47
48 /*----------------------------------------------------------------*
49 * combining a value corresponding to msb's with a value
50 * corresponding to lsb's
51 *---------------------------------------------------------------*/
52
54 int *index, /* (i/o) the msb value in the
55 combined value out */
56 int rest, /* (i) the lsb value */
57 int bitno_rest /* (i) the number of bits in the
58 lsb part */
59 ){
60 *index = *index<<bitno_rest;
61 *index += rest;
62 }
63
64 /*----------------------------------------------------------------*
65 * packing of bits into bitstream, i.e., vector of bytes
66 *---------------------------------------------------------------*/
67
68 void dopack(
69 unsigned char **bitstream, /* (i/o) on entrance pointer to
70 place in bitstream to pack
71 new data, on exit pointer
72 to place in bitstream to
73 pack future data */
74 int index, /* (i) the value to pack */
75 int bitno, /* (i) the number of bits that the
76 value will fit within */
77 int *pos /* (i/o) write position in the
78 current byte */
79 ){
80 int posLeft;
81
82 /* Clear the bits before starting in a new byte */
83
84 if ((*pos)==0) {
85
86
87
88
89
90 **bitstream=0;
91 }
92
93 while (bitno>0) {
94
95 /* Jump to the next byte if end of this byte is reached*/
96
97 if (*pos==8) {
98 *pos=0;
99 (*bitstream)++;
100 **bitstream=0;
101 }
102
103 posLeft=8-(*pos);
104
105 /* Insert index into the bitstream */
106
107 if (bitno <= posLeft) {
108 **bitstream |= (unsigned char)(index<<(posLeft-bitno));
109 *pos+=bitno;
110 bitno=0;
111 } else {
112 **bitstream |= (unsigned char)(index>>(bitno-posLeft));
113
114 *pos=8;
115 index-=((index>>(bitno-posLeft))<<(bitno-posLeft));
116
117 bitno-=posLeft;
118 }
119 }
120 }
121
122 /*----------------------------------------------------------------*
123 * unpacking of bits from bitstream, i.e., vector of bytes
124 *---------------------------------------------------------------*/
125
126 void unpack(
127 unsigned char **bitstream, /* (i/o) on entrance pointer to
128 place in bitstream to
129 unpack new data from, on
130 exit pointer to place in
131 bitstream to unpack future
132 data from */
133 int *index, /* (o) resulting value */
134 int bitno, /* (i) number of bits used to
135 represent the value */
136 int *pos /* (i/o) read position in the
137 current byte */
138
139
140
141
142
143 ){
144 int BitsLeft;
145
146 *index=0;
147
148 while (bitno>0) {
149
150 /* move forward in bitstream when the end of the
151 byte is reached */
152
153 if (*pos==8) {
154 *pos=0;
155 (*bitstream)++;
156 }
157
158 BitsLeft=8-(*pos);
159
160 /* Extract bits to index */
161
162 if (BitsLeft>=bitno) {
163 *index+=((((**bitstream)<<(*pos)) & 0xFF)>>(8-bitno));
164
165 *pos+=bitno;
166 bitno=0;
167 } else {
168
169 if ((8-bitno)>0) {
170 *index+=((((**bitstream)<<(*pos)) & 0xFF)>>
171 (8-bitno));
172 *pos=8;
173 } else {
174 *index+=(((int)(((**bitstream)<<(*pos)) & 0xFF))<<
175 (bitno-8));
176 *pos=8;
177 }
178 bitno-=BitsLeft;
179 }
180 }
181 }
void packcombine(int *index, int rest, int bitno_rest)
Definition: packing.c:53
void dopack(unsigned char **bitstream, int index, int bitno, int *pos)
Definition: packing.c:68
void packsplit(int *index, int *firstpart, int *rest, int bitno_firstpart, int bitno_total)
Definition: packing.c:26
void unpack(unsigned char **bitstream, int *index, int bitno, int *pos)
Definition: packing.c:126