Make file access in cdr_custom similar to cdr_csv.
[asterisk-bristuff.git] / codecs / ilbc / packing.c
blob3071032e096feb4dacedd01232bd883d4eac8c54
2 /******************************************************************
4 iLBC Speech Coder ANSI-C Source Code
6 packing.c
8 Copyright (C) The Internet Society (2004).
9 All Rights Reserved.
11 ******************************************************************/
13 #include <math.h>
14 #include <stdlib.h>
16 #include "iLBC_define.h"
17 #include "constants.h"
18 #include "helpfun.h"
19 #include "packing.h"
20 #include "string.h"
22 /*----------------------------------------------------------------*
23 * splitting an integer into first most significant bits and
24 * remaining least significant bits
25 *---------------------------------------------------------------*/
27 void packsplit(
28 int *index, /* (i) the value to split */
29 int *firstpart, /* (o) the value specified by most
30 significant bits */
31 int *rest, /* (o) the value specified by least
32 significant bits */
33 int bitno_firstpart, /* (i) number of bits in most
34 significant part */
35 int bitno_total /* (i) number of bits in full range
36 of value */
38 int bitno_rest = bitno_total-bitno_firstpart;
42 *firstpart = *index>>(bitno_rest);
43 *rest = *index-(*firstpart<<(bitno_rest));
46 /*----------------------------------------------------------------*
47 * combining a value corresponding to msb's with a value
48 * corresponding to lsb's
49 *---------------------------------------------------------------*/
51 void packcombine(
52 int *index, /* (i/o) the msb value in the
53 combined value out */
54 int rest, /* (i) the lsb value */
55 int bitno_rest /* (i) the number of bits in the
56 lsb part */
58 *index = *index<<bitno_rest;
59 *index += rest;
62 /*----------------------------------------------------------------*
63 * packing of bits into bitstream, i.e., vector of bytes
64 *---------------------------------------------------------------*/
66 void dopack(
67 unsigned char **bitstream, /* (i/o) on entrance pointer to
68 place in bitstream to pack
69 new data, on exit pointer
70 to place in bitstream to
71 pack future data */
72 int index, /* (i) the value to pack */
73 int bitno, /* (i) the number of bits that the
74 value will fit within */
75 int *pos /* (i/o) write position in the
76 current byte */
78 int posLeft;
80 /* Clear the bits before starting in a new byte */
82 if ((*pos)==0) {
83 **bitstream=0;
86 while (bitno>0) {
88 /* Jump to the next byte if end of this byte is reached*/
90 if (*pos==8) {
91 *pos=0;
92 (*bitstream)++;
93 **bitstream=0;
98 posLeft=8-(*pos);
100 /* Insert index into the bitstream */
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));
109 *pos=8;
110 index-=((index>>(bitno-posLeft))<<(bitno-posLeft));
112 bitno-=posLeft;
117 /*----------------------------------------------------------------*
118 * unpacking of bits from bitstream, i.e., vector of bytes
119 *---------------------------------------------------------------*/
121 void unpack(
122 unsigned char **bitstream, /* (i/o) on entrance pointer to
123 place in bitstream to
124 unpack new data from, on
125 exit pointer to place in
126 bitstream to unpack future
127 data from */
128 int *index, /* (o) resulting value */
129 int bitno, /* (i) number of bits used to
130 represent the value */
131 int *pos /* (i/o) read position in the
132 current byte */
134 int BitsLeft;
136 *index=0;
138 while (bitno>0) {
140 /* move forward in bitstream when the end of the
141 byte is reached */
143 if (*pos==8) {
144 *pos=0;
145 (*bitstream)++;
148 BitsLeft=8-(*pos);
150 /* Extract bits to index */
154 if (BitsLeft>=bitno) {
155 *index+=((((**bitstream)<<(*pos)) & 0xFF)>>(8-bitno));
157 *pos+=bitno;
158 bitno=0;
159 } else {
161 if ((8-bitno)>0) {
162 *index+=((((**bitstream)<<(*pos)) & 0xFF)>>
163 (8-bitno));
164 *pos=8;
165 } else {
166 *index+=(((int)(((**bitstream)<<(*pos)) & 0xFF))<<
167 (bitno-8));
168 *pos=8;
170 bitno-=BitsLeft;