blob: 507e25e3ea277e21eb272b92b38f162293c41e60 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11/******************************************************************
12
13 iLBC Speech Coder ANSI-C Source Code
14
15 WebRtcIlbcfix_PackBits.c
16
17******************************************************************/
18
Mirko Bonadei06c2aa92018-02-01 15:11:41 +010019#include "modules/audio_coding/codecs/ilbc/defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21/*----------------------------------------------------------------*
22 * unpacking of bits from bitstream, i.e., vector of bytes
23 *---------------------------------------------------------------*/
24
25void WebRtcIlbcfix_PackBits(
pbos@webrtc.org0946a562013-04-09 00:28:06 +000026 uint16_t *bitstream, /* (o) The packetized bitstream */
niklase@google.com470e71d2011-07-07 08:21:25 +000027 iLBC_bits *enc_bits, /* (i) Encoded bits */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000028 int16_t mode /* (i) Codec mode (20 or 30) */
niklase@google.com470e71d2011-07-07 08:21:25 +000029 ){
pbos@webrtc.org0946a562013-04-09 00:28:06 +000030 uint16_t *bitstreamPtr;
niklase@google.com470e71d2011-07-07 08:21:25 +000031 int i, k;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000032 int16_t *tmpPtr;
niklase@google.com470e71d2011-07-07 08:21:25 +000033
34 bitstreamPtr=bitstream;
35
36 /* Class 1 bits of ULP */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000037 /* First int16_t */
38 (*bitstreamPtr) = ((uint16_t)enc_bits->lsf[0])<<10; /* Bit 0..5 */
niklase@google.com470e71d2011-07-07 08:21:25 +000039 (*bitstreamPtr) |= (enc_bits->lsf[1])<<3; /* Bit 6..12 */
40 (*bitstreamPtr) |= (enc_bits->lsf[2]&0x70)>>4; /* Bit 13..15 */
41 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000042 /* Second int16_t */
43 (*bitstreamPtr) = ((uint16_t)enc_bits->lsf[2]&0xF)<<12; /* Bit 0..3 */
niklase@google.com470e71d2011-07-07 08:21:25 +000044
45 if (mode==20) {
46 (*bitstreamPtr) |= (enc_bits->startIdx)<<10; /* Bit 4..5 */
47 (*bitstreamPtr) |= (enc_bits->state_first)<<9; /* Bit 6 */
48 (*bitstreamPtr) |= (enc_bits->idxForMax)<<3; /* Bit 7..12 */
49 (*bitstreamPtr) |= ((enc_bits->cb_index[0])&0x70)>>4; /* Bit 13..15 */
50 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000051 /* Third int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +000052 (*bitstreamPtr) = ((enc_bits->cb_index[0])&0xE)<<12; /* Bit 0..2 */
53 (*bitstreamPtr) |= ((enc_bits->gain_index[0])&0x18)<<8; /* Bit 3..4 */
54 (*bitstreamPtr) |= ((enc_bits->gain_index[1])&0x8)<<7; /* Bit 5 */
55 (*bitstreamPtr) |= ((enc_bits->cb_index[3])&0xFE)<<2; /* Bit 6..12 */
56 (*bitstreamPtr) |= ((enc_bits->gain_index[3])&0x10)>>2; /* Bit 13 */
57 (*bitstreamPtr) |= ((enc_bits->gain_index[4])&0x8)>>2; /* Bit 14 */
58 (*bitstreamPtr) |= ((enc_bits->gain_index[6])&0x10)>>4; /* Bit 15 */
59 } else { /* mode==30 */
60 (*bitstreamPtr) |= (enc_bits->lsf[3])<<6; /* Bit 4..9 */
61 (*bitstreamPtr) |= (enc_bits->lsf[4]&0x7E)>>1; /* Bit 10..15 */
62 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000063 /* Third int16_t */
64 (*bitstreamPtr) = ((uint16_t)enc_bits->lsf[4]&0x1)<<15; /* Bit 0 */
niklase@google.com470e71d2011-07-07 08:21:25 +000065 (*bitstreamPtr) |= (enc_bits->lsf[5])<<8; /* Bit 1..7 */
66 (*bitstreamPtr) |= (enc_bits->startIdx)<<5; /* Bit 8..10 */
67 (*bitstreamPtr) |= (enc_bits->state_first)<<4; /* Bit 11 */
68 (*bitstreamPtr) |= ((enc_bits->idxForMax)&0x3C)>>2; /* Bit 12..15 */
69 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000070 /* 4:th int16_t */
71 (*bitstreamPtr) = ((uint16_t)enc_bits->idxForMax&0x3)<<14; /* Bit 0..1 */
niklase@google.com470e71d2011-07-07 08:21:25 +000072 (*bitstreamPtr) |= (enc_bits->cb_index[0]&0x78)<<7; /* Bit 2..5 */
73 (*bitstreamPtr) |= (enc_bits->gain_index[0]&0x10)<<5; /* Bit 6 */
74 (*bitstreamPtr) |= (enc_bits->gain_index[1]&0x8)<<5; /* Bit 7 */
75 (*bitstreamPtr) |= (enc_bits->cb_index[3]&0xFC); /* Bit 8..13 */
76 (*bitstreamPtr) |= (enc_bits->gain_index[3]&0x10)>>3; /* Bit 14 */
77 (*bitstreamPtr) |= (enc_bits->gain_index[4]&0x8)>>3; /* Bit 15 */
78 }
79 /* Class 2 bits of ULP */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000080 /* 4:th to 6:th int16_t for 20 ms case
81 5:th to 7:th int16_t for 30 ms case */
niklase@google.com470e71d2011-07-07 08:21:25 +000082 bitstreamPtr++;
83 tmpPtr=enc_bits->idxVec;
84 for (k=0; k<3; k++) {
85 (*bitstreamPtr) = 0;
86 for (i=15; i>=0; i--) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +000087 (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x4)>>2)<<i;
niklase@google.com470e71d2011-07-07 08:21:25 +000088 /* Bit 15-i */
89 tmpPtr++;
90 }
91 bitstreamPtr++;
92 }
93
94 if (mode==20) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +000095 /* 7:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +000096 (*bitstreamPtr) = 0;
97 for (i=15; i>6; i--) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +000098 (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x4)>>2)<<i;
niklase@google.com470e71d2011-07-07 08:21:25 +000099 /* Bit 15-i */
100 tmpPtr++;
101 }
102 (*bitstreamPtr) |= (enc_bits->gain_index[1]&0x4)<<4; /* Bit 9 */
103 (*bitstreamPtr) |= (enc_bits->gain_index[3]&0xC)<<2; /* Bit 10..11 */
104 (*bitstreamPtr) |= (enc_bits->gain_index[4]&0x4)<<1; /* Bit 12 */
105 (*bitstreamPtr) |= (enc_bits->gain_index[6]&0x8)>>1; /* Bit 13 */
106 (*bitstreamPtr) |= (enc_bits->gain_index[7]&0xC)>>2; /* Bit 14..15 */
107
108 } else { /* mode==30 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000109 /* 8:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000110 (*bitstreamPtr) = 0;
111 for (i=15; i>5; i--) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000112 (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x4)>>2)<<i;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113 /* Bit 15-i */
114 tmpPtr++;
115 }
116 (*bitstreamPtr) |= (enc_bits->cb_index[0]&0x6)<<3; /* Bit 10..11 */
117 (*bitstreamPtr) |= (enc_bits->gain_index[0]&0x8); /* Bit 12 */
118 (*bitstreamPtr) |= (enc_bits->gain_index[1]&0x4); /* Bit 13 */
119 (*bitstreamPtr) |= (enc_bits->cb_index[3]&0x2); /* Bit 14 */
120 (*bitstreamPtr) |= (enc_bits->cb_index[6]&0x80)>>7; /* Bit 15 */
121 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000122 /* 9:th int16_t */
123 (*bitstreamPtr) = ((uint16_t)enc_bits->cb_index[6]&0x7E)<<9;/* Bit 0..5 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000124 (*bitstreamPtr) |= (enc_bits->cb_index[9]&0xFE)<<2; /* Bit 6..12 */
125 (*bitstreamPtr) |= (enc_bits->cb_index[12]&0xE0)>>5; /* Bit 13..15 */
126 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000127 /* 10:th int16_t */
128 (*bitstreamPtr) = ((uint16_t)enc_bits->cb_index[12]&0x1E)<<11;/* Bit 0..3 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000129 (*bitstreamPtr) |= (enc_bits->gain_index[3]&0xC)<<8; /* Bit 4..5 */
130 (*bitstreamPtr) |= (enc_bits->gain_index[4]&0x6)<<7; /* Bit 6..7 */
131 (*bitstreamPtr) |= (enc_bits->gain_index[6]&0x18)<<3; /* Bit 8..9 */
132 (*bitstreamPtr) |= (enc_bits->gain_index[7]&0xC)<<2; /* Bit 10..11 */
133 (*bitstreamPtr) |= (enc_bits->gain_index[9]&0x10)>>1; /* Bit 12 */
134 (*bitstreamPtr) |= (enc_bits->gain_index[10]&0x8)>>1; /* Bit 13 */
135 (*bitstreamPtr) |= (enc_bits->gain_index[12]&0x10)>>3; /* Bit 14 */
136 (*bitstreamPtr) |= (enc_bits->gain_index[13]&0x8)>>3; /* Bit 15 */
137 }
138 bitstreamPtr++;
139 /* Class 3 bits of ULP */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000140 /* 8:th to 14:th int16_t for 20 ms case
141 11:th to 17:th int16_t for 30 ms case */
niklase@google.com470e71d2011-07-07 08:21:25 +0000142 tmpPtr=enc_bits->idxVec;
143 for (k=0; k<7; k++) {
144 (*bitstreamPtr) = 0;
145 for (i=14; i>=0; i-=2) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000146 (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x3))<<i; /* Bit 15-i..14-i*/
niklase@google.com470e71d2011-07-07 08:21:25 +0000147 tmpPtr++;
148 }
149 bitstreamPtr++;
150 }
151
152 if (mode==20) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000153 /* 15:th int16_t */
154 (*bitstreamPtr) = ((uint16_t)((enc_bits->idxVec[56])&0x3))<<14;/* Bit 0..1 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000155 (*bitstreamPtr) |= (((enc_bits->cb_index[0])&1))<<13; /* Bit 2 */
156 (*bitstreamPtr) |= ((enc_bits->cb_index[1]))<<6; /* Bit 3..9 */
157 (*bitstreamPtr) |= ((enc_bits->cb_index[2])&0x7E)>>1; /* Bit 10..15 */
158 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000159 /* 16:th int16_t */
160 (*bitstreamPtr) = ((uint16_t)((enc_bits->cb_index[2])&0x1))<<15;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161 /* Bit 0 */
162 (*bitstreamPtr) |= ((enc_bits->gain_index[0])&0x7)<<12; /* Bit 1..3 */
163 (*bitstreamPtr) |= ((enc_bits->gain_index[1])&0x3)<<10; /* Bit 4..5 */
164 (*bitstreamPtr) |= ((enc_bits->gain_index[2]))<<7; /* Bit 6..8 */
165 (*bitstreamPtr) |= ((enc_bits->cb_index[3])&0x1)<<6; /* Bit 9 */
166 (*bitstreamPtr) |= ((enc_bits->cb_index[4])&0x7E)>>1; /* Bit 10..15 */
167 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000168 /* 17:th int16_t */
169 (*bitstreamPtr) = ((uint16_t)((enc_bits->cb_index[4])&0x1))<<15;
niklase@google.com470e71d2011-07-07 08:21:25 +0000170 /* Bit 0 */
171 (*bitstreamPtr) |= (enc_bits->cb_index[5])<<8; /* Bit 1..7 */
172 (*bitstreamPtr) |= (enc_bits->cb_index[6]); /* Bit 8..15 */
173 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000174 /* 18:th int16_t */
175 (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[7]))<<8; /* Bit 0..7 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000176 (*bitstreamPtr) |= (enc_bits->cb_index[8]); /* Bit 8..15 */
177 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000178 /* 19:th int16_t */
179 (*bitstreamPtr) = ((uint16_t)((enc_bits->gain_index[3])&0x3))<<14;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180 /* Bit 0..1 */
181 (*bitstreamPtr) |= ((enc_bits->gain_index[4])&0x3)<<12; /* Bit 2..3 */
182 (*bitstreamPtr) |= ((enc_bits->gain_index[5]))<<9; /* Bit 4..6 */
183 (*bitstreamPtr) |= ((enc_bits->gain_index[6])&0x7)<<6; /* Bit 7..9 */
184 (*bitstreamPtr) |= ((enc_bits->gain_index[7])&0x3)<<4; /* Bit 10..11 */
185 (*bitstreamPtr) |= (enc_bits->gain_index[8])<<1; /* Bit 12..14 */
186 } else { /* mode==30 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000187 /* 18:th int16_t */
188 (*bitstreamPtr) = ((uint16_t)((enc_bits->idxVec[56])&0x3))<<14;/* Bit 0..1 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000189 (*bitstreamPtr) |= (((enc_bits->idxVec[57])&0x3))<<12; /* Bit 2..3 */
190 (*bitstreamPtr) |= (((enc_bits->cb_index[0])&1))<<11; /* Bit 4 */
191 (*bitstreamPtr) |= ((enc_bits->cb_index[1]))<<4; /* Bit 5..11 */
192 (*bitstreamPtr) |= ((enc_bits->cb_index[2])&0x78)>>3; /* Bit 12..15 */
193 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000194 /* 19:th int16_t */
195 (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[2])&0x7)<<13;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196 /* Bit 0..2 */
197 (*bitstreamPtr) |= ((enc_bits->gain_index[0])&0x7)<<10; /* Bit 3..5 */
198 (*bitstreamPtr) |= ((enc_bits->gain_index[1])&0x3)<<8; /* Bit 6..7 */
199 (*bitstreamPtr) |= ((enc_bits->gain_index[2])&0x7)<<5; /* Bit 8..10 */
200 (*bitstreamPtr) |= ((enc_bits->cb_index[3])&0x1)<<4; /* Bit 11 */
201 (*bitstreamPtr) |= ((enc_bits->cb_index[4])&0x78)>>3; /* Bit 12..15 */
202 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000203 /* 20:th int16_t */
204 (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[4])&0x7)<<13;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205 /* Bit 0..2 */
206 (*bitstreamPtr) |= ((enc_bits->cb_index[5]))<<6; /* Bit 3..9 */
207 (*bitstreamPtr) |= ((enc_bits->cb_index[6])&0x1)<<5; /* Bit 10 */
208 (*bitstreamPtr) |= ((enc_bits->cb_index[7])&0xF8)>>3; /* Bit 11..15 */
209 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000210 /* 21:st int16_t */
211 (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[7])&0x7)<<13;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212 /* Bit 0..2 */
213 (*bitstreamPtr) |= ((enc_bits->cb_index[8]))<<5; /* Bit 3..10 */
214 (*bitstreamPtr) |= ((enc_bits->cb_index[9])&0x1)<<4; /* Bit 11 */
215 (*bitstreamPtr) |= ((enc_bits->cb_index[10])&0xF0)>>4; /* Bit 12..15 */
216 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000217 /* 22:nd int16_t */
218 (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[10])&0xF)<<12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219 /* Bit 0..3 */
220 (*bitstreamPtr) |= ((enc_bits->cb_index[11]))<<4; /* Bit 4..11 */
221 (*bitstreamPtr) |= ((enc_bits->cb_index[12])&0x1)<<3; /* Bit 12 */
222 (*bitstreamPtr) |= ((enc_bits->cb_index[13])&0xE0)>>5; /* Bit 13..15 */
223 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000224 /* 23:rd int16_t */
225 (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[13])&0x1F)<<11;
niklase@google.com470e71d2011-07-07 08:21:25 +0000226 /* Bit 0..4 */
227 (*bitstreamPtr) |= ((enc_bits->cb_index[14]))<<3; /* Bit 5..12 */
228 (*bitstreamPtr) |= ((enc_bits->gain_index[3])&0x3)<<1; /* Bit 13..14 */
229 (*bitstreamPtr) |= ((enc_bits->gain_index[4])&0x1); /* Bit 15 */
230 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000231 /* 24:rd int16_t */
232 (*bitstreamPtr) = ((uint16_t)(enc_bits->gain_index[5]))<<13;
niklase@google.com470e71d2011-07-07 08:21:25 +0000233 /* Bit 0..2 */
234 (*bitstreamPtr) |= ((enc_bits->gain_index[6])&0x7)<<10; /* Bit 3..5 */
235 (*bitstreamPtr) |= ((enc_bits->gain_index[7])&0x3)<<8; /* Bit 6..7 */
236 (*bitstreamPtr) |= ((enc_bits->gain_index[8]))<<5; /* Bit 8..10 */
237 (*bitstreamPtr) |= ((enc_bits->gain_index[9])&0xF)<<1; /* Bit 11..14 */
238 (*bitstreamPtr) |= ((enc_bits->gain_index[10])&0x4)>>2; /* Bit 15 */
239 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000240 /* 25:rd int16_t */
241 (*bitstreamPtr) = ((uint16_t)(enc_bits->gain_index[10])&0x3)<<14;
niklase@google.com470e71d2011-07-07 08:21:25 +0000242 /* Bit 0..1 */
243 (*bitstreamPtr) |= ((enc_bits->gain_index[11]))<<11; /* Bit 2..4 */
244 (*bitstreamPtr) |= ((enc_bits->gain_index[12])&0xF)<<7; /* Bit 5..8 */
245 (*bitstreamPtr) |= ((enc_bits->gain_index[13])&0x7)<<4; /* Bit 9..11 */
246 (*bitstreamPtr) |= ((enc_bits->gain_index[14]))<<1; /* Bit 12..14 */
247 }
248 /* Last bit is automatically zero */
249
250 return;
251}