1 /* 2 * Copyright (c) 2012 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_UnpackBits.c 16 17 ******************************************************************/ 18 19 #include "modules/audio_coding/codecs/ilbc/unpack_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_UnpackBits(const uint16_t * bitstream,iLBC_bits * enc_bits,int16_t mode)27int16_t WebRtcIlbcfix_UnpackBits( /* (o) "Empty" frame indicator */ 28 const uint16_t *bitstream, /* (i) The packatized bitstream */ 29 iLBC_bits *enc_bits, /* (o) Paramerers from bitstream */ 30 int16_t mode /* (i) Codec mode (20 or 30) */ 31 ) { 32 const uint16_t *bitstreamPtr; 33 int i, k; 34 int16_t *tmpPtr; 35 36 bitstreamPtr=bitstream; 37 38 /* First int16_t */ 39 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++; 43 /* Second int16_t */ 44 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++; 52 /* Third int16_t */ 53 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++; 64 /* Third int16_t */ 65 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++; 71 /* 4:th int16_t */ 72 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 */ 81 /* 4:th to 6:th int16_t for 20 ms case 82 5:th to 7:th int16_t for 30 ms case */ 83 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) { 95 /* 7:th int16_t */ 96 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 */ 108 /* 8:th int16_t */ 109 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++; 120 /* 9:th int16_t */ 121 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++; 125 /* 10:th int16_t */ 126 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 */ 138 /* 8:th to 14:th int16_t for 20 ms case 139 11:th to 17:th int16_t for 30 ms case */ 140 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) { 150 /* 15:th int16_t */ 151 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++; 156 /* 16:th int16_t */ 157 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++; 164 /* 17:th int16_t */ 165 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++; 169 /* 18:th int16_t */ 170 enc_bits->cb_index[7] = (*bitstreamPtr)>>8; /* Bit 0..7 */ 171 enc_bits->cb_index[8] = (*bitstreamPtr)&0xFF; /* Bit 8..15 */ 172 bitstreamPtr++; 173 /* 19:th int16_t */ 174 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 */ 181 /* 18:th int16_t */ 182 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++; 188 /* 19:th int16_t */ 189 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++; 196 /* 20:th int16_t */ 197 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++; 202 /* 21:st int16_t */ 203 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++; 208 /* 22:nd int16_t */ 209 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++; 214 /* 23:rd int16_t */ 215 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++; 220 /* 24:rd int16_t */ 221 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++; 228 /* 25:rd int16_t */ 229 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 } 242