comparison intercom/ilbc/packing.c @ 2:13be24d74cd2

import intercom-0.4.1
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 09:57:52 +0200
parents
children
comparison
equal deleted inserted replaced
1:9cadc470e3da 2:13be24d74cd2
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
26 void packsplit(int *index, /* (i) the value to split */
27 int *firstpart, /* (o) the value specified by most
28 significant bits */
29 int *rest, /* (o) the value specified by least
30 significant bits */
31 int bitno_firstpart, /* (i) number of bits in most
32 significant part */
33 int bitno_total /* (i) number of bits in full range
34 of value */
35 )
36 {
37 int bitno_rest = bitno_total - bitno_firstpart;
38
39 *firstpart = *index >> (bitno_rest);
40 *rest = *index - (*firstpart << (bitno_rest));
41 }
42
43 /*----------------------------------------------------------------*
44 * combining a value corresponding to msb's with a value
45 * corresponding to lsb's
46 *---------------------------------------------------------------*/
47
48 void packcombine(int *index, /* (i/o) the msb value in the
49 combined value out */
50 int rest, /* (i) the lsb value */
51 int bitno_rest /* (i) the number of bits in the
52 lsb part */
53 )
54 {
55 *index = *index << bitno_rest;
56 *index += rest;
57 }
58
59 /*----------------------------------------------------------------*
60 * packing of bits into bitstream, i.e., vector of bytes
61 *---------------------------------------------------------------*/
62
63 void dopack(unsigned char **bitstream, /* (i/o) on entrance pointer to
64 place in bitstream to pack
65 new data, on exit pointer
66 to place in bitstream to
67 pack future data */
68 int index, /* (i) the value to pack */
69 int bitno, /* (i) the number of bits that the
70 value will fit within */
71 int *pos /* (i/o) write position in the
72 current byte */
73 )
74 {
75 int posLeft;
76
77 /* Clear the bits before starting in a new byte */
78
79 if ((*pos) == 0) {
80
81
82
83
84
85 **bitstream = 0;
86 }
87
88 while (bitno > 0) {
89
90 /* Jump to the next byte if end of this byte is reached */
91
92 if (*pos == 8) {
93 *pos = 0;
94 (*bitstream)++;
95 **bitstream = 0;
96 }
97
98 posLeft = 8 - (*pos);
99
100 /* Insert index into the bitstream */
101
102 if (bitno <= posLeft) {
103 **bitstream |= (unsigned char) (index << (posLeft - bitno));
104 *pos += bitno;
105 bitno = 0;
106 } else {
107 **bitstream |= (unsigned char) (index >> (bitno - posLeft));
108
109 *pos = 8;
110 index -= ((index >> (bitno - posLeft)) << (bitno - posLeft));
111
112 bitno -= posLeft;
113 }
114 }
115 }
116
117 /*----------------------------------------------------------------*
118 * unpacking of bits from bitstream, i.e., vector of bytes
119 *---------------------------------------------------------------*/
120
121 void unpack(unsigned char **bitstream, /* (i/o) on entrance pointer to
122 place in bitstream to
123 unpack new data from, on
124 exit pointer to place in
125 bitstream to unpack future
126 data from */
127 int *index, /* (o) resulting value */
128 int bitno, /* (i) number of bits used to
129 represent the value */
130 int *pos /* (i/o) read position in the
131 current byte */
132 )
133 {
134 int BitsLeft;
135
136 *index = 0;
137
138 while (bitno > 0) {
139
140 /* move forward in bitstream when the end of the
141 byte is reached */
142
143 if (*pos == 8) {
144 *pos = 0;
145 (*bitstream)++;
146 }
147
148 BitsLeft = 8 - (*pos);
149
150 /* Extract bits to index */
151
152 if (BitsLeft >= bitno) {
153 *index += ((((**bitstream) << (*pos)) & 0xFF) >> (8 - bitno));
154
155 *pos += bitno;
156 bitno = 0;
157 } else {
158
159 if ((8 - bitno) > 0) {
160 *index += ((((**bitstream) << (*pos)) & 0xFF) >> (8 - bitno));
161 *pos = 8;
162 } else {
163 *index += (((int) (((**bitstream) << (*pos)) & 0xFF)) <<
164 (bitno - 8));
165 *pos = 8;
166 }
167 bitno -= BitsLeft;
168 }
169 }
170 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.