blob: dd44eb8fb6e0fcd97dc11c7c1b32aa7486eac3b2 [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
Timothy Gu31117832020-12-18 22:25:57 -080019#include "modules/audio_coding/codecs/ilbc/pack_bits.h"
20
Mirko Bonadei06c2aa92018-02-01 15:11:41 +010021#include "modules/audio_coding/codecs/ilbc/defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23/*----------------------------------------------------------------*
24 * unpacking of bits from bitstream, i.e., vector of bytes
25 *---------------------------------------------------------------*/
26
27void WebRtcIlbcfix_PackBits(
pbos@webrtc.org0946a562013-04-09 00:28:06 +000028 uint16_t *bitstream, /* (o) The packetized bitstream */
niklase@google.com470e71d2011-07-07 08:21:25 +000029 iLBC_bits *enc_bits, /* (i) Encoded bits */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000030 int16_t mode /* (i) Codec mode (20 or 30) */
niklase@google.com470e71d2011-07-07 08:21:25 +000031 ){
pbos@webrtc.org0946a562013-04-09 00:28:06 +000032 uint16_t *bitstreamPtr;
niklase@google.com470e71d2011-07-07 08:21:25 +000033 int i, k;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000034 int16_t *tmpPtr;
niklase@google.com470e71d2011-07-07 08:21:25 +000035
36 bitstreamPtr=bitstream;
37
38 /* Class 1 bits of ULP */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000039 /* First int16_t */
40 (*bitstreamPtr) = ((uint16_t)enc_bits->lsf[0])<<10; /* Bit 0..5 */
niklase@google.com470e71d2011-07-07 08:21:25 +000041 (*bitstreamPtr) |= (enc_bits->lsf[1])<<3; /* Bit 6..12 */
42 (*bitstreamPtr) |= (enc_bits->lsf[2]&0x70)>>4; /* Bit 13..15 */
43 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000044 /* Second int16_t */
45 (*bitstreamPtr) = ((uint16_t)enc_bits->lsf[2]&0xF)<<12; /* Bit 0..3 */
niklase@google.com470e71d2011-07-07 08:21:25 +000046
47 if (mode==20) {
48 (*bitstreamPtr) |= (enc_bits->startIdx)<<10; /* Bit 4..5 */
49 (*bitstreamPtr) |= (enc_bits->state_first)<<9; /* Bit 6 */
50 (*bitstreamPtr) |= (enc_bits->idxForMax)<<3; /* Bit 7..12 */
51 (*bitstreamPtr) |= ((enc_bits->cb_index[0])&0x70)>>4; /* Bit 13..15 */
52 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000053 /* Third int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +000054 (*bitstreamPtr) = ((enc_bits->cb_index[0])&0xE)<<12; /* Bit 0..2 */
55 (*bitstreamPtr) |= ((enc_bits->gain_index[0])&0x18)<<8; /* Bit 3..4 */
56 (*bitstreamPtr) |= ((enc_bits->gain_index[1])&0x8)<<7; /* Bit 5 */
57 (*bitstreamPtr) |= ((enc_bits->cb_index[3])&0xFE)<<2; /* Bit 6..12 */
58 (*bitstreamPtr) |= ((enc_bits->gain_index[3])&0x10)>>2; /* Bit 13 */
59 (*bitstreamPtr) |= ((enc_bits->gain_index[4])&0x8)>>2; /* Bit 14 */
60 (*bitstreamPtr) |= ((enc_bits->gain_index[6])&0x10)>>4; /* Bit 15 */
61 } else { /* mode==30 */
62 (*bitstreamPtr) |= (enc_bits->lsf[3])<<6; /* Bit 4..9 */
63 (*bitstreamPtr) |= (enc_bits->lsf[4]&0x7E)>>1; /* Bit 10..15 */
64 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000065 /* Third int16_t */
66 (*bitstreamPtr) = ((uint16_t)enc_bits->lsf[4]&0x1)<<15; /* Bit 0 */
niklase@google.com470e71d2011-07-07 08:21:25 +000067 (*bitstreamPtr) |= (enc_bits->lsf[5])<<8; /* Bit 1..7 */
68 (*bitstreamPtr) |= (enc_bits->startIdx)<<5; /* Bit 8..10 */
69 (*bitstreamPtr) |= (enc_bits->state_first)<<4; /* Bit 11 */
70 (*bitstreamPtr) |= ((enc_bits->idxForMax)&0x3C)>>2; /* Bit 12..15 */
71 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000072 /* 4:th int16_t */
73 (*bitstreamPtr) = ((uint16_t)enc_bits->idxForMax&0x3)<<14; /* Bit 0..1 */
niklase@google.com470e71d2011-07-07 08:21:25 +000074 (*bitstreamPtr) |= (enc_bits->cb_index[0]&0x78)<<7; /* Bit 2..5 */
75 (*bitstreamPtr) |= (enc_bits->gain_index[0]&0x10)<<5; /* Bit 6 */
76 (*bitstreamPtr) |= (enc_bits->gain_index[1]&0x8)<<5; /* Bit 7 */
77 (*bitstreamPtr) |= (enc_bits->cb_index[3]&0xFC); /* Bit 8..13 */
78 (*bitstreamPtr) |= (enc_bits->gain_index[3]&0x10)>>3; /* Bit 14 */
79 (*bitstreamPtr) |= (enc_bits->gain_index[4]&0x8)>>3; /* Bit 15 */
80 }
81 /* Class 2 bits of ULP */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000082 /* 4:th to 6:th int16_t for 20 ms case
83 5:th to 7:th int16_t for 30 ms case */
niklase@google.com470e71d2011-07-07 08:21:25 +000084 bitstreamPtr++;
85 tmpPtr=enc_bits->idxVec;
86 for (k=0; k<3; k++) {
87 (*bitstreamPtr) = 0;
88 for (i=15; i>=0; i--) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +000089 (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x4)>>2)<<i;
niklase@google.com470e71d2011-07-07 08:21:25 +000090 /* Bit 15-i */
91 tmpPtr++;
92 }
93 bitstreamPtr++;
94 }
95
96 if (mode==20) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +000097 /* 7:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +000098 (*bitstreamPtr) = 0;
99 for (i=15; i>6; i--) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000100 (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x4)>>2)<<i;
niklase@google.com470e71d2011-07-07 08:21:25 +0000101 /* Bit 15-i */
102 tmpPtr++;
103 }
104 (*bitstreamPtr) |= (enc_bits->gain_index[1]&0x4)<<4; /* Bit 9 */
105 (*bitstreamPtr) |= (enc_bits->gain_index[3]&0xC)<<2; /* Bit 10..11 */
106 (*bitstreamPtr) |= (enc_bits->gain_index[4]&0x4)<<1; /* Bit 12 */
107 (*bitstreamPtr) |= (enc_bits->gain_index[6]&0x8)>>1; /* Bit 13 */
108 (*bitstreamPtr) |= (enc_bits->gain_index[7]&0xC)>>2; /* Bit 14..15 */
109
110 } else { /* mode==30 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000111 /* 8:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 (*bitstreamPtr) = 0;
113 for (i=15; i>5; i--) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000114 (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x4)>>2)<<i;
niklase@google.com470e71d2011-07-07 08:21:25 +0000115 /* Bit 15-i */
116 tmpPtr++;
117 }
118 (*bitstreamPtr) |= (enc_bits->cb_index[0]&0x6)<<3; /* Bit 10..11 */
119 (*bitstreamPtr) |= (enc_bits->gain_index[0]&0x8); /* Bit 12 */
120 (*bitstreamPtr) |= (enc_bits->gain_index[1]&0x4); /* Bit 13 */
121 (*bitstreamPtr) |= (enc_bits->cb_index[3]&0x2); /* Bit 14 */
122 (*bitstreamPtr) |= (enc_bits->cb_index[6]&0x80)>>7; /* Bit 15 */
123 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000124 /* 9:th int16_t */
125 (*bitstreamPtr) = ((uint16_t)enc_bits->cb_index[6]&0x7E)<<9;/* Bit 0..5 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000126 (*bitstreamPtr) |= (enc_bits->cb_index[9]&0xFE)<<2; /* Bit 6..12 */
127 (*bitstreamPtr) |= (enc_bits->cb_index[12]&0xE0)>>5; /* Bit 13..15 */
128 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000129 /* 10:th int16_t */
130 (*bitstreamPtr) = ((uint16_t)enc_bits->cb_index[12]&0x1E)<<11;/* Bit 0..3 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000131 (*bitstreamPtr) |= (enc_bits->gain_index[3]&0xC)<<8; /* Bit 4..5 */
132 (*bitstreamPtr) |= (enc_bits->gain_index[4]&0x6)<<7; /* Bit 6..7 */
133 (*bitstreamPtr) |= (enc_bits->gain_index[6]&0x18)<<3; /* Bit 8..9 */
134 (*bitstreamPtr) |= (enc_bits->gain_index[7]&0xC)<<2; /* Bit 10..11 */
135 (*bitstreamPtr) |= (enc_bits->gain_index[9]&0x10)>>1; /* Bit 12 */
136 (*bitstreamPtr) |= (enc_bits->gain_index[10]&0x8)>>1; /* Bit 13 */
137 (*bitstreamPtr) |= (enc_bits->gain_index[12]&0x10)>>3; /* Bit 14 */
138 (*bitstreamPtr) |= (enc_bits->gain_index[13]&0x8)>>3; /* Bit 15 */
139 }
140 bitstreamPtr++;
141 /* Class 3 bits of ULP */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000142 /* 8:th to 14:th int16_t for 20 ms case
143 11:th to 17:th int16_t for 30 ms case */
niklase@google.com470e71d2011-07-07 08:21:25 +0000144 tmpPtr=enc_bits->idxVec;
145 for (k=0; k<7; k++) {
146 (*bitstreamPtr) = 0;
147 for (i=14; i>=0; i-=2) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000148 (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x3))<<i; /* Bit 15-i..14-i*/
niklase@google.com470e71d2011-07-07 08:21:25 +0000149 tmpPtr++;
150 }
151 bitstreamPtr++;
152 }
153
154 if (mode==20) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000155 /* 15:th int16_t */
156 (*bitstreamPtr) = ((uint16_t)((enc_bits->idxVec[56])&0x3))<<14;/* Bit 0..1 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000157 (*bitstreamPtr) |= (((enc_bits->cb_index[0])&1))<<13; /* Bit 2 */
158 (*bitstreamPtr) |= ((enc_bits->cb_index[1]))<<6; /* Bit 3..9 */
159 (*bitstreamPtr) |= ((enc_bits->cb_index[2])&0x7E)>>1; /* Bit 10..15 */
160 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000161 /* 16:th int16_t */
162 (*bitstreamPtr) = ((uint16_t)((enc_bits->cb_index[2])&0x1))<<15;
niklase@google.com470e71d2011-07-07 08:21:25 +0000163 /* Bit 0 */
164 (*bitstreamPtr) |= ((enc_bits->gain_index[0])&0x7)<<12; /* Bit 1..3 */
165 (*bitstreamPtr) |= ((enc_bits->gain_index[1])&0x3)<<10; /* Bit 4..5 */
166 (*bitstreamPtr) |= ((enc_bits->gain_index[2]))<<7; /* Bit 6..8 */
167 (*bitstreamPtr) |= ((enc_bits->cb_index[3])&0x1)<<6; /* Bit 9 */
168 (*bitstreamPtr) |= ((enc_bits->cb_index[4])&0x7E)>>1; /* Bit 10..15 */
169 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000170 /* 17:th int16_t */
171 (*bitstreamPtr) = ((uint16_t)((enc_bits->cb_index[4])&0x1))<<15;
niklase@google.com470e71d2011-07-07 08:21:25 +0000172 /* Bit 0 */
173 (*bitstreamPtr) |= (enc_bits->cb_index[5])<<8; /* Bit 1..7 */
174 (*bitstreamPtr) |= (enc_bits->cb_index[6]); /* Bit 8..15 */
175 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000176 /* 18:th int16_t */
177 (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[7]))<<8; /* Bit 0..7 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000178 (*bitstreamPtr) |= (enc_bits->cb_index[8]); /* Bit 8..15 */
179 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000180 /* 19:th int16_t */
181 (*bitstreamPtr) = ((uint16_t)((enc_bits->gain_index[3])&0x3))<<14;
niklase@google.com470e71d2011-07-07 08:21:25 +0000182 /* Bit 0..1 */
183 (*bitstreamPtr) |= ((enc_bits->gain_index[4])&0x3)<<12; /* Bit 2..3 */
184 (*bitstreamPtr) |= ((enc_bits->gain_index[5]))<<9; /* Bit 4..6 */
185 (*bitstreamPtr) |= ((enc_bits->gain_index[6])&0x7)<<6; /* Bit 7..9 */
186 (*bitstreamPtr) |= ((enc_bits->gain_index[7])&0x3)<<4; /* Bit 10..11 */
187 (*bitstreamPtr) |= (enc_bits->gain_index[8])<<1; /* Bit 12..14 */
188 } else { /* mode==30 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000189 /* 18:th int16_t */
190 (*bitstreamPtr) = ((uint16_t)((enc_bits->idxVec[56])&0x3))<<14;/* Bit 0..1 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000191 (*bitstreamPtr) |= (((enc_bits->idxVec[57])&0x3))<<12; /* Bit 2..3 */
192 (*bitstreamPtr) |= (((enc_bits->cb_index[0])&1))<<11; /* Bit 4 */
193 (*bitstreamPtr) |= ((enc_bits->cb_index[1]))<<4; /* Bit 5..11 */
194 (*bitstreamPtr) |= ((enc_bits->cb_index[2])&0x78)>>3; /* Bit 12..15 */
195 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000196 /* 19:th int16_t */
197 (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[2])&0x7)<<13;
niklase@google.com470e71d2011-07-07 08:21:25 +0000198 /* Bit 0..2 */
199 (*bitstreamPtr) |= ((enc_bits->gain_index[0])&0x7)<<10; /* Bit 3..5 */
200 (*bitstreamPtr) |= ((enc_bits->gain_index[1])&0x3)<<8; /* Bit 6..7 */
201 (*bitstreamPtr) |= ((enc_bits->gain_index[2])&0x7)<<5; /* Bit 8..10 */
202 (*bitstreamPtr) |= ((enc_bits->cb_index[3])&0x1)<<4; /* Bit 11 */
203 (*bitstreamPtr) |= ((enc_bits->cb_index[4])&0x78)>>3; /* Bit 12..15 */
204 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000205 /* 20:th int16_t */
206 (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[4])&0x7)<<13;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 /* Bit 0..2 */
208 (*bitstreamPtr) |= ((enc_bits->cb_index[5]))<<6; /* Bit 3..9 */
209 (*bitstreamPtr) |= ((enc_bits->cb_index[6])&0x1)<<5; /* Bit 10 */
210 (*bitstreamPtr) |= ((enc_bits->cb_index[7])&0xF8)>>3; /* Bit 11..15 */
211 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000212 /* 21:st int16_t */
213 (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[7])&0x7)<<13;
niklase@google.com470e71d2011-07-07 08:21:25 +0000214 /* Bit 0..2 */
215 (*bitstreamPtr) |= ((enc_bits->cb_index[8]))<<5; /* Bit 3..10 */
216 (*bitstreamPtr) |= ((enc_bits->cb_index[9])&0x1)<<4; /* Bit 11 */
217 (*bitstreamPtr) |= ((enc_bits->cb_index[10])&0xF0)>>4; /* Bit 12..15 */
218 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000219 /* 22:nd int16_t */
220 (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[10])&0xF)<<12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000221 /* Bit 0..3 */
222 (*bitstreamPtr) |= ((enc_bits->cb_index[11]))<<4; /* Bit 4..11 */
223 (*bitstreamPtr) |= ((enc_bits->cb_index[12])&0x1)<<3; /* Bit 12 */
224 (*bitstreamPtr) |= ((enc_bits->cb_index[13])&0xE0)>>5; /* Bit 13..15 */
225 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000226 /* 23:rd int16_t */
227 (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[13])&0x1F)<<11;
niklase@google.com470e71d2011-07-07 08:21:25 +0000228 /* Bit 0..4 */
229 (*bitstreamPtr) |= ((enc_bits->cb_index[14]))<<3; /* Bit 5..12 */
230 (*bitstreamPtr) |= ((enc_bits->gain_index[3])&0x3)<<1; /* Bit 13..14 */
231 (*bitstreamPtr) |= ((enc_bits->gain_index[4])&0x1); /* Bit 15 */
232 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000233 /* 24:rd int16_t */
234 (*bitstreamPtr) = ((uint16_t)(enc_bits->gain_index[5]))<<13;
niklase@google.com470e71d2011-07-07 08:21:25 +0000235 /* Bit 0..2 */
236 (*bitstreamPtr) |= ((enc_bits->gain_index[6])&0x7)<<10; /* Bit 3..5 */
237 (*bitstreamPtr) |= ((enc_bits->gain_index[7])&0x3)<<8; /* Bit 6..7 */
238 (*bitstreamPtr) |= ((enc_bits->gain_index[8]))<<5; /* Bit 8..10 */
239 (*bitstreamPtr) |= ((enc_bits->gain_index[9])&0xF)<<1; /* Bit 11..14 */
240 (*bitstreamPtr) |= ((enc_bits->gain_index[10])&0x4)>>2; /* Bit 15 */
241 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000242 /* 25:rd int16_t */
243 (*bitstreamPtr) = ((uint16_t)(enc_bits->gain_index[10])&0x3)<<14;
niklase@google.com470e71d2011-07-07 08:21:25 +0000244 /* Bit 0..1 */
245 (*bitstreamPtr) |= ((enc_bits->gain_index[11]))<<11; /* Bit 2..4 */
246 (*bitstreamPtr) |= ((enc_bits->gain_index[12])&0xF)<<7; /* Bit 5..8 */
247 (*bitstreamPtr) |= ((enc_bits->gain_index[13])&0x7)<<4; /* Bit 9..11 */
248 (*bitstreamPtr) |= ((enc_bits->gain_index[14]))<<1; /* Bit 12..14 */
249 }
250 /* Last bit is automatically zero */
251
252 return;
253}