xref: /aosp_15_r20/external/webrtc/modules/audio_coding/codecs/ilbc/pack_bits.c (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
19 #include "modules/audio_coding/codecs/ilbc/pack_bits.h"
20 
21 #include "modules/audio_coding/codecs/ilbc/defines.h"
22 
23 /*----------------------------------------------------------------*
24  *  unpacking of bits from bitstream, i.e., vector of bytes
25  *---------------------------------------------------------------*/
26 
WebRtcIlbcfix_PackBits(uint16_t * bitstream,iLBC_bits * enc_bits,int16_t mode)27 void WebRtcIlbcfix_PackBits(
28     uint16_t *bitstream,   /* (o) The packetized bitstream */
29     iLBC_bits *enc_bits,  /* (i) Encoded bits */
30     int16_t mode     /* (i) Codec mode (20 or 30) */
31                              ){
32   uint16_t *bitstreamPtr;
33   int i, k;
34   int16_t *tmpPtr;
35 
36   bitstreamPtr=bitstream;
37 
38   /* Class 1 bits of ULP */
39   /* First int16_t */
40   (*bitstreamPtr)  = ((uint16_t)enc_bits->lsf[0])<<10;   /* Bit 0..5  */
41   (*bitstreamPtr) |= (enc_bits->lsf[1])<<3;     /* Bit 6..12 */
42   (*bitstreamPtr) |= (enc_bits->lsf[2]&0x70)>>4;    /* Bit 13..15 */
43   bitstreamPtr++;
44   /* Second int16_t */
45   (*bitstreamPtr)  = ((uint16_t)enc_bits->lsf[2]&0xF)<<12;  /* Bit 0..3  */
46 
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++;
53     /* Third int16_t */
54     (*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++;
65     /* Third int16_t */
66     (*bitstreamPtr)  = ((uint16_t)enc_bits->lsf[4]&0x1)<<15;  /* Bit 0  */
67     (*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++;
72     /* 4:th int16_t */
73     (*bitstreamPtr)  = ((uint16_t)enc_bits->idxForMax&0x3)<<14; /* Bit 0..1  */
74     (*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 */
82   /* 4:th to 6:th int16_t for 20 ms case
83      5:th to 7:th int16_t for 30 ms case */
84   bitstreamPtr++;
85   tmpPtr=enc_bits->idxVec;
86   for (k=0; k<3; k++) {
87     (*bitstreamPtr) = 0;
88     for (i=15; i>=0; i--) {
89       (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x4)>>2)<<i;
90       /* Bit 15-i  */
91       tmpPtr++;
92     }
93     bitstreamPtr++;
94   }
95 
96   if (mode==20) {
97     /* 7:th int16_t */
98     (*bitstreamPtr) = 0;
99     for (i=15; i>6; i--) {
100       (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x4)>>2)<<i;
101       /* 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 */
111     /* 8:th int16_t */
112     (*bitstreamPtr) = 0;
113     for (i=15; i>5; i--) {
114       (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x4)>>2)<<i;
115       /* 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++;
124     /* 9:th int16_t */
125     (*bitstreamPtr)  = ((uint16_t)enc_bits->cb_index[6]&0x7E)<<9;/* Bit 0..5  */
126     (*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++;
129     /* 10:th int16_t */
130     (*bitstreamPtr)  = ((uint16_t)enc_bits->cb_index[12]&0x1E)<<11;/* Bit 0..3 */
131     (*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 */
142   /*  8:th to 14:th int16_t for 20 ms case
143       11:th to 17:th int16_t for 30 ms case */
144   tmpPtr=enc_bits->idxVec;
145   for (k=0; k<7; k++) {
146     (*bitstreamPtr) = 0;
147     for (i=14; i>=0; i-=2) {
148       (*bitstreamPtr) |= ((uint16_t)((*tmpPtr)&0x3))<<i; /* Bit 15-i..14-i*/
149       tmpPtr++;
150     }
151     bitstreamPtr++;
152   }
153 
154   if (mode==20) {
155     /* 15:th int16_t */
156     (*bitstreamPtr)  = ((uint16_t)((enc_bits->idxVec[56])&0x3))<<14;/* Bit 0..1 */
157     (*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++;
161     /* 16:th int16_t */
162     (*bitstreamPtr) = ((uint16_t)((enc_bits->cb_index[2])&0x1))<<15;
163     /* 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++;
170     /* 17:th int16_t */
171     (*bitstreamPtr) = ((uint16_t)((enc_bits->cb_index[4])&0x1))<<15;
172     /* 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++;
176     /* 18:th int16_t */
177     (*bitstreamPtr) = ((uint16_t)(enc_bits->cb_index[7]))<<8; /* Bit 0..7  */
178     (*bitstreamPtr) |= (enc_bits->cb_index[8]);     /* Bit 8..15 */
179     bitstreamPtr++;
180     /* 19:th int16_t */
181     (*bitstreamPtr) = ((uint16_t)((enc_bits->gain_index[3])&0x3))<<14;
182     /* 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 */
189     /* 18:th int16_t */
190     (*bitstreamPtr)  = ((uint16_t)((enc_bits->idxVec[56])&0x3))<<14;/* Bit 0..1 */
191     (*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++;
196     /* 19:th int16_t */
197     (*bitstreamPtr)  = ((uint16_t)(enc_bits->cb_index[2])&0x7)<<13;
198     /* 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++;
205     /* 20:th int16_t */
206     (*bitstreamPtr)  = ((uint16_t)(enc_bits->cb_index[4])&0x7)<<13;
207     /* 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++;
212     /* 21:st int16_t */
213     (*bitstreamPtr)  = ((uint16_t)(enc_bits->cb_index[7])&0x7)<<13;
214     /* 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++;
219     /* 22:nd int16_t */
220     (*bitstreamPtr)  = ((uint16_t)(enc_bits->cb_index[10])&0xF)<<12;
221     /* 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++;
226     /* 23:rd int16_t */
227     (*bitstreamPtr)  = ((uint16_t)(enc_bits->cb_index[13])&0x1F)<<11;
228     /* 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++;
233     /* 24:rd int16_t */
234     (*bitstreamPtr)  = ((uint16_t)(enc_bits->gain_index[5]))<<13;
235     /* 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++;
242     /* 25:rd int16_t */
243     (*bitstreamPtr)  = ((uint16_t)(enc_bits->gain_index[10])&0x3)<<14;
244     /* 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 }
254