blob: a9a0147b9da0f1514da9162c44014b5c76bae14d [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
turaj@webrtc.org837bc7b2012-07-14 00:34:54 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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_UnpackBits.c
16
17******************************************************************/
18
Timothy Gu31117832020-12-18 22:25:57 -080019#include "modules/audio_coding/codecs/ilbc/unpack_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
pbos@webrtc.org0946a562013-04-09 00:28:06 +000027int16_t WebRtcIlbcfix_UnpackBits( /* (o) "Empty" frame indicator */
28 const uint16_t *bitstream, /* (i) The packatized bitstream */
niklase@google.com470e71d2011-07-07 08:21:25 +000029 iLBC_bits *enc_bits, /* (o) Paramerers from bitstream */
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 const 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
pbos@webrtc.org0946a562013-04-09 00:28:06 +000038 /* First int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +000039 enc_bits->lsf[0] = (*bitstreamPtr)>>10; /* Bit 0..5 */
40 enc_bits->lsf[1] = ((*bitstreamPtr)>>3)&0x7F; /* Bit 6..12 */
41 enc_bits->lsf[2] = ((*bitstreamPtr)&0x7)<<4; /* Bit 13..15 */
42 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000043 /* Second int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +000044 enc_bits->lsf[2] |= ((*bitstreamPtr)>>12)&0xF; /* Bit 0..3 */
45
46 if (mode==20) {
47 enc_bits->startIdx = ((*bitstreamPtr)>>10)&0x3; /* Bit 4..5 */
48 enc_bits->state_first = ((*bitstreamPtr)>>9)&0x1; /* Bit 6 */
49 enc_bits->idxForMax = ((*bitstreamPtr)>>3)&0x3F; /* Bit 7..12 */
50 enc_bits->cb_index[0] = ((*bitstreamPtr)&0x7)<<4; /* Bit 13..15 */
51 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000052 /* Third int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +000053 enc_bits->cb_index[0] |= ((*bitstreamPtr)>>12)&0xE; /* Bit 0..2 */
54 enc_bits->gain_index[0] = ((*bitstreamPtr)>>8)&0x18; /* Bit 3..4 */
55 enc_bits->gain_index[1] = ((*bitstreamPtr)>>7)&0x8; /* Bit 5 */
56 enc_bits->cb_index[3] = ((*bitstreamPtr)>>2)&0xFE; /* Bit 6..12 */
57 enc_bits->gain_index[3] = ((*bitstreamPtr)<<2)&0x10; /* Bit 13 */
58 enc_bits->gain_index[4] = ((*bitstreamPtr)<<2)&0x8; /* Bit 14 */
59 enc_bits->gain_index[6] = ((*bitstreamPtr)<<4)&0x10; /* Bit 15 */
60 } else { /* mode==30 */
61 enc_bits->lsf[3] = ((*bitstreamPtr)>>6)&0x3F; /* Bit 4..9 */
62 enc_bits->lsf[4] = ((*bitstreamPtr)<<1)&0x7E; /* Bit 10..15 */
63 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000064 /* Third int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +000065 enc_bits->lsf[4] |= ((*bitstreamPtr)>>15)&0x1; /* Bit 0 */
66 enc_bits->lsf[5] = ((*bitstreamPtr)>>8)&0x7F; /* Bit 1..7 */
67 enc_bits->startIdx = ((*bitstreamPtr)>>5)&0x7; /* Bit 8..10 */
68 enc_bits->state_first = ((*bitstreamPtr)>>4)&0x1; /* Bit 11 */
69 enc_bits->idxForMax = ((*bitstreamPtr)<<2)&0x3C; /* Bit 12..15 */
70 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000071 /* 4:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +000072 enc_bits->idxForMax |= ((*bitstreamPtr)>>14)&0x3; /* Bit 0..1 */
73 enc_bits->cb_index[0] = ((*bitstreamPtr)>>7)&0x78; /* Bit 2..5 */
74 enc_bits->gain_index[0] = ((*bitstreamPtr)>>5)&0x10; /* Bit 6 */
75 enc_bits->gain_index[1] = ((*bitstreamPtr)>>5)&0x8; /* Bit 7 */
76 enc_bits->cb_index[3] = ((*bitstreamPtr))&0xFC; /* Bit 8..13 */
77 enc_bits->gain_index[3] = ((*bitstreamPtr)<<3)&0x10; /* Bit 14 */
78 enc_bits->gain_index[4] = ((*bitstreamPtr)<<3)&0x8; /* Bit 15 */
79 }
80 /* Class 2 bits of ULP */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000081 /* 4:th to 6:th int16_t for 20 ms case
82 5:th to 7:th int16_t for 30 ms case */
niklase@google.com470e71d2011-07-07 08:21:25 +000083 bitstreamPtr++;
84 tmpPtr=enc_bits->idxVec;
85 for (k=0; k<3; k++) {
86 for (i=15; i>=0; i--) {
87 (*tmpPtr) = (((*bitstreamPtr)>>i)<<2)&0x4;
88 /* 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 for (i=15; i>6; i--) {
97 (*tmpPtr) = (((*bitstreamPtr)>>i)<<2)&0x4;
98 /* Bit 15-i */
99 tmpPtr++;
100 }
101 enc_bits->gain_index[1] |= ((*bitstreamPtr)>>4)&0x4; /* Bit 9 */
102 enc_bits->gain_index[3] |= ((*bitstreamPtr)>>2)&0xC; /* Bit 10..11 */
103 enc_bits->gain_index[4] |= ((*bitstreamPtr)>>1)&0x4; /* Bit 12 */
104 enc_bits->gain_index[6] |= ((*bitstreamPtr)<<1)&0x8; /* Bit 13 */
105 enc_bits->gain_index[7] = ((*bitstreamPtr)<<2)&0xC; /* Bit 14..15 */
106
107 } else { /* mode==30 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000108 /* 8:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000109 for (i=15; i>5; i--) {
110 (*tmpPtr) = (((*bitstreamPtr)>>i)<<2)&0x4;
111 /* Bit 15-i */
112 tmpPtr++;
113 }
114 enc_bits->cb_index[0] |= ((*bitstreamPtr)>>3)&0x6; /* Bit 10..11 */
115 enc_bits->gain_index[0] |= ((*bitstreamPtr))&0x8; /* Bit 12 */
116 enc_bits->gain_index[1] |= ((*bitstreamPtr))&0x4; /* Bit 13 */
117 enc_bits->cb_index[3] |= ((*bitstreamPtr))&0x2; /* Bit 14 */
118 enc_bits->cb_index[6] = ((*bitstreamPtr)<<7)&0x80; /* Bit 15 */
119 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000120 /* 9:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000121 enc_bits->cb_index[6] |= ((*bitstreamPtr)>>9)&0x7E; /* Bit 0..5 */
122 enc_bits->cb_index[9] = ((*bitstreamPtr)>>2)&0xFE; /* Bit 6..12 */
123 enc_bits->cb_index[12] = ((*bitstreamPtr)<<5)&0xE0; /* Bit 13..15 */
124 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000125 /* 10:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000126 enc_bits->cb_index[12] |= ((*bitstreamPtr)>>11)&0x1E;/* Bit 0..3 */
127 enc_bits->gain_index[3] |= ((*bitstreamPtr)>>8)&0xC; /* Bit 4..5 */
128 enc_bits->gain_index[4] |= ((*bitstreamPtr)>>7)&0x6; /* Bit 6..7 */
129 enc_bits->gain_index[6] = ((*bitstreamPtr)>>3)&0x18; /* Bit 8..9 */
130 enc_bits->gain_index[7] = ((*bitstreamPtr)>>2)&0xC; /* Bit 10..11 */
131 enc_bits->gain_index[9] = ((*bitstreamPtr)<<1)&0x10; /* Bit 12 */
132 enc_bits->gain_index[10] = ((*bitstreamPtr)<<1)&0x8; /* Bit 13 */
133 enc_bits->gain_index[12] = ((*bitstreamPtr)<<3)&0x10; /* Bit 14 */
134 enc_bits->gain_index[13] = ((*bitstreamPtr)<<3)&0x8; /* Bit 15 */
135 }
136 bitstreamPtr++;
137 /* Class 3 bits of ULP */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000138 /* 8:th to 14:th int16_t for 20 ms case
139 11:th to 17:th int16_t for 30 ms case */
niklase@google.com470e71d2011-07-07 08:21:25 +0000140 tmpPtr=enc_bits->idxVec;
141 for (k=0; k<7; k++) {
142 for (i=14; i>=0; i-=2) {
143 (*tmpPtr) |= ((*bitstreamPtr)>>i)&0x3; /* Bit 15-i..14-i*/
144 tmpPtr++;
145 }
146 bitstreamPtr++;
147 }
148
149 if (mode==20) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000150 /* 15:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000151 enc_bits->idxVec[56] |= ((*bitstreamPtr)>>14)&0x3; /* Bit 0..1 */
152 enc_bits->cb_index[0] |= ((*bitstreamPtr)>>13)&0x1; /* Bit 2 */
153 enc_bits->cb_index[1] = ((*bitstreamPtr)>>6)&0x7F; /* Bit 3..9 */
154 enc_bits->cb_index[2] = ((*bitstreamPtr)<<1)&0x7E; /* Bit 10..15 */
155 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000156 /* 16:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000157 enc_bits->cb_index[2] |= ((*bitstreamPtr)>>15)&0x1; /* Bit 0 */
158 enc_bits->gain_index[0] |= ((*bitstreamPtr)>>12)&0x7; /* Bit 1..3 */
159 enc_bits->gain_index[1] |= ((*bitstreamPtr)>>10)&0x3; /* Bit 4..5 */
160 enc_bits->gain_index[2] = ((*bitstreamPtr)>>7)&0x7; /* Bit 6..8 */
161 enc_bits->cb_index[3] |= ((*bitstreamPtr)>>6)&0x1; /* Bit 9 */
162 enc_bits->cb_index[4] = ((*bitstreamPtr)<<1)&0x7E; /* Bit 10..15 */
163 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000164 /* 17:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000165 enc_bits->cb_index[4] |= ((*bitstreamPtr)>>15)&0x1; /* Bit 0 */
166 enc_bits->cb_index[5] = ((*bitstreamPtr)>>8)&0x7F; /* Bit 1..7 */
167 enc_bits->cb_index[6] = ((*bitstreamPtr))&0xFF; /* Bit 8..15 */
168 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000169 /* 18:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000170 enc_bits->cb_index[7] = (*bitstreamPtr)>>8; /* Bit 0..7 */
171 enc_bits->cb_index[8] = (*bitstreamPtr)&0xFF; /* Bit 8..15 */
172 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000173 /* 19:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000174 enc_bits->gain_index[3] |= ((*bitstreamPtr)>>14)&0x3; /* Bit 0..1 */
175 enc_bits->gain_index[4] |= ((*bitstreamPtr)>>12)&0x3; /* Bit 2..3 */
176 enc_bits->gain_index[5] = ((*bitstreamPtr)>>9)&0x7; /* Bit 4..6 */
177 enc_bits->gain_index[6] |= ((*bitstreamPtr)>>6)&0x7; /* Bit 7..9 */
178 enc_bits->gain_index[7] |= ((*bitstreamPtr)>>4)&0x3; /* Bit 10..11 */
179 enc_bits->gain_index[8] = ((*bitstreamPtr)>>1)&0x7; /* Bit 12..14 */
180 } else { /* mode==30 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000181 /* 18:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000182 enc_bits->idxVec[56] |= ((*bitstreamPtr)>>14)&0x3; /* Bit 0..1 */
183 enc_bits->idxVec[57] |= ((*bitstreamPtr)>>12)&0x3; /* Bit 2..3 */
184 enc_bits->cb_index[0] |= ((*bitstreamPtr)>>11)&1; /* Bit 4 */
185 enc_bits->cb_index[1] = ((*bitstreamPtr)>>4)&0x7F; /* Bit 5..11 */
186 enc_bits->cb_index[2] = ((*bitstreamPtr)<<3)&0x78; /* Bit 12..15 */
187 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000188 /* 19:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000189 enc_bits->cb_index[2] |= ((*bitstreamPtr)>>13)&0x7; /* Bit 0..2 */
190 enc_bits->gain_index[0] |= ((*bitstreamPtr)>>10)&0x7; /* Bit 3..5 */
191 enc_bits->gain_index[1] |= ((*bitstreamPtr)>>8)&0x3; /* Bit 6..7 */
192 enc_bits->gain_index[2] = ((*bitstreamPtr)>>5)&0x7; /* Bit 8..10 */
193 enc_bits->cb_index[3] |= ((*bitstreamPtr)>>4)&0x1; /* Bit 11 */
194 enc_bits->cb_index[4] = ((*bitstreamPtr)<<3)&0x78; /* Bit 12..15 */
195 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000196 /* 20:th int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000197 enc_bits->cb_index[4] |= ((*bitstreamPtr)>>13)&0x7; /* Bit 0..2 */
198 enc_bits->cb_index[5] = ((*bitstreamPtr)>>6)&0x7F; /* Bit 3..9 */
199 enc_bits->cb_index[6] |= ((*bitstreamPtr)>>5)&0x1; /* Bit 10 */
200 enc_bits->cb_index[7] = ((*bitstreamPtr)<<3)&0xF8; /* Bit 11..15 */
201 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000202 /* 21:st int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000203 enc_bits->cb_index[7] |= ((*bitstreamPtr)>>13)&0x7; /* Bit 0..2 */
204 enc_bits->cb_index[8] = ((*bitstreamPtr)>>5)&0xFF; /* Bit 3..10 */
205 enc_bits->cb_index[9] |= ((*bitstreamPtr)>>4)&0x1; /* Bit 11 */
206 enc_bits->cb_index[10] = ((*bitstreamPtr)<<4)&0xF0; /* Bit 12..15 */
207 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000208 /* 22:nd int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000209 enc_bits->cb_index[10] |= ((*bitstreamPtr)>>12)&0xF; /* Bit 0..3 */
210 enc_bits->cb_index[11] = ((*bitstreamPtr)>>4)&0xFF; /* Bit 4..11 */
211 enc_bits->cb_index[12] |= ((*bitstreamPtr)>>3)&0x1; /* Bit 12 */
212 enc_bits->cb_index[13] = ((*bitstreamPtr)<<5)&0xE0; /* Bit 13..15 */
213 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000214 /* 23:rd int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000215 enc_bits->cb_index[13] |= ((*bitstreamPtr)>>11)&0x1F;/* Bit 0..4 */
216 enc_bits->cb_index[14] = ((*bitstreamPtr)>>3)&0xFF; /* Bit 5..12 */
217 enc_bits->gain_index[3] |= ((*bitstreamPtr)>>1)&0x3; /* Bit 13..14 */
218 enc_bits->gain_index[4] |= ((*bitstreamPtr)&0x1); /* Bit 15 */
219 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000220 /* 24:rd int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000221 enc_bits->gain_index[5] = ((*bitstreamPtr)>>13)&0x7; /* Bit 0..2 */
222 enc_bits->gain_index[6] |= ((*bitstreamPtr)>>10)&0x7; /* Bit 3..5 */
223 enc_bits->gain_index[7] |= ((*bitstreamPtr)>>8)&0x3; /* Bit 6..7 */
224 enc_bits->gain_index[8] = ((*bitstreamPtr)>>5)&0x7; /* Bit 8..10 */
225 enc_bits->gain_index[9] |= ((*bitstreamPtr)>>1)&0xF; /* Bit 11..14 */
226 enc_bits->gain_index[10] |= ((*bitstreamPtr)<<2)&0x4; /* Bit 15 */
227 bitstreamPtr++;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000228 /* 25:rd int16_t */
niklase@google.com470e71d2011-07-07 08:21:25 +0000229 enc_bits->gain_index[10] |= ((*bitstreamPtr)>>14)&0x3; /* Bit 0..1 */
230 enc_bits->gain_index[11] = ((*bitstreamPtr)>>11)&0x7; /* Bit 2..4 */
231 enc_bits->gain_index[12] |= ((*bitstreamPtr)>>7)&0xF; /* Bit 5..8 */
232 enc_bits->gain_index[13] |= ((*bitstreamPtr)>>4)&0x7; /* Bit 9..11 */
233 enc_bits->gain_index[14] = ((*bitstreamPtr)>>1)&0x7; /* Bit 12..14 */
234 }
235 /* Last bit should be zero, otherwise it's an "empty" frame */
236 if (((*bitstreamPtr)&0x1) == 1) {
237 return(1);
238 } else {
239 return(0);
240 }
241}