12024
+ 鐃緒申 1 /*
+ 鐃緒申 2 The mediastreamer library aims at providing modular media processing and I/O
+ 鐃緒申 3 for linphone, but also for any telephony application.
+ 鐃緒申 4 Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org
+ 鐃緒申 5
+ 鐃緒申 6 This library is free software; you can redistribute it and/or
+ 鐃緒申 7 modify it under the terms of the GNU Lesser General Public
+ 鐃緒申 8 License as published by the Free Software Foundation; either
+ 鐃緒申 9 version 2.1 of the License, or (at your option) any later version.
+ 鐃緒申 10
+ 鐃緒申 11 This library is distributed in the hope that it will be useful,
+ 鐃緒申 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
+ 鐃緒申 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ 鐃緒申 14 Lesser General Public License for more details.
+ 鐃緒申 15
+ 鐃緒申 16 You should have received a copy of the GNU Lesser General Public
+ 鐃緒申 17 License along with this library; if not, write to the Free Software
+ 鐃緒申 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ 鐃緒申 19 */
+ 鐃緒申 20
+ 鐃緒申 21 #include <stdlib.h>
+ 鐃緒申 22 #include "msLPC10encoder.h"
+ 鐃緒申 23 #include <lpc10.h>
+ 鐃緒申 24
+ 鐃緒申 25
+ 鐃緒申 26 extern MSCodecInfo LPC10info;
+ 鐃緒申 27
+ 鐃緒申 28 /* The return value of each of these calls is the same as that
+ 鐃緒申 29 returned by fread/fwrite, which should be the number of samples
+ 鐃緒申 30 successfully read/written, not the number of bytes. */
+ 鐃緒申 31
+ 鐃緒申 32 int
+ 鐃緒申 33 read_16bit_samples(INT16 int16samples[], float speech[], int n)
+ 鐃緒申 34 {
+ 鐃緒申 35 int i;
+ 鐃緒申 36
+ 鐃緒申 37 /* Convert 16 bit integer samples to floating point values in the
+ 鐃緒申 38 range [-1,+1]. */
+ 鐃緒申 39
+ 鐃緒申 40 for (i = 0; i < n; i++) {
+ 鐃緒申 41 speech[i] = ((float) int16samples[i]) / 32768.0;
+ 鐃緒申 42 }
+ 鐃緒申 43
+ 鐃緒申 44 return (n);
+ 鐃緒申 45 }
+ 鐃緒申 46
+ 鐃緒申 47
+ 鐃緒申 48
+ 鐃緒申 49 int
+ 鐃緒申 50 write_16bit_samples(INT16 int16samples[], float speech[], int n)
+ 鐃緒申 51 {
+ 鐃緒申 52 int i;
+ 鐃緒申 53 float real_sample;
+ 鐃緒申 54
+ 鐃緒申 55 /* Convert floating point samples in range [-1,+1] to 16 bit
+ 鐃緒申 56 integers. */
+ 鐃緒申 57 for (i = 0; i < n; i++) {
+ 鐃緒申 58 real_sample = 32768.0 * speech[i];
+ 鐃緒申 59 if (real_sample < -32768.0) {
+ 鐃緒申 60 int16samples[i] = -32768;
+ 鐃緒申 61 } else if (real_sample > 32767.0) {
+ 鐃緒申 62 int16samples[i] = 32767;
+ 鐃緒申 63 } else {
+ 鐃緒申 64 int16samples[i] = real_sample;
+ 鐃緒申 65 }
+ 鐃緒申 66 }
+ 鐃緒申 67 return (n);
+ 鐃緒申 68 }
+ 鐃緒申 69
+ 鐃緒申 70 /*
+ 鐃緒申 71
+ 鐃緒申 72 Write the bits in bits[0] through bits[len-1] to file f, in "packed"
+ 鐃緒申 73 format.
+ 鐃緒申 74
+ 鐃緒申 75 bits is expected to be an array of len integer values, where each
+ 鐃緒申 76 integer is 0 to represent a 0 bit, and any other value represents a 1
+ 鐃緒申 77 bit. This bit string is written to the file f in the form of several
+ 鐃緒申 78 8 bit characters. If len is not a multiple of 8, then the last
+ 鐃緒申 79 character is padded with 0 bits -- the padding is in the least
+ 鐃緒申 80 significant bits of the last byte. The 8 bit characters are "filled"
+ 鐃緒申 81 in order from most significant bit to least significant.
+ 鐃緒申 82
+ 鐃緒申 83 */
+ 鐃緒申 84
+ 鐃緒申 85 void
+ 鐃緒申 86 write_bits(unsigned char *data, INT32 *bits, int len)
+ 鐃緒申 87 {
+ 鐃緒申 88 int i; /* generic loop variable */
+ 鐃緒申 89 unsigned char mask; /* The next bit position within the
+ 鐃緒申 90 variable "data" to place the next
+ 鐃緒申 91 bit. */
+ 鐃緒申 92
+ 鐃緒申 93
+ 鐃緒申 94 /* Fill in the array bits.
+ 鐃緒申 95 * The first compressed output bit will be the most significant
+ 鐃緒申 96 * bit of the byte, so initialize mask to 0x80. The next byte of
+ 鐃緒申 97 * compressed data is initially 0, and the desired bits will be
+ 鐃緒申 98 * turned on below.
+ 鐃緒申 99 */
+ 鐃緒申 100 mask = 0x80;
+ 鐃緒申 101 *data = 0;
+ 鐃緒申 102
+ 鐃緒申 103 for (i = 0; i < len; i++) {
+ 鐃緒申 104 /* Turn on the next bit of output data, if necessary. */
+ 鐃緒申 105 if (bits[i]) {
+ 鐃緒申 106 (*data) |= mask;
+ 鐃緒申 107 }
+ 鐃緒申 108 /*
+ 鐃緒申 109 * If the byte data is full, determined by mask becoming 0,
+ 鐃緒申 110 * then write the byte to the output file, and reinitialize
+ 鐃緒申 111 * data and mask for the next output byte. Also add the byte
+ 鐃緒申 112 * if (i == len-1), because if len is not a multiple of 8,
+ 鐃緒申 113 * then mask won't yet be 0. */
+ 鐃緒申 114 mask >>= 1;
+ 鐃緒申 115 if ((mask == 0) || (i == len-1)) {
+ 鐃緒申 116 data++;
+ 鐃緒申 117 *data = 0;
+ 鐃緒申 118 mask = 0x80;
+ 鐃緒申 119 }
+ 鐃緒申 120 }
+ 鐃緒申 121 }
+ 鐃緒申 122
+ 鐃緒申 123
+ 鐃緒申 124
+ 鐃緒申 125 /*
+ 鐃緒申 126
+ 鐃緒申 127 Read bits from file f into bits[0] through bits[len-1], in "packed"
+ 鐃緒申 128 format.
+ 鐃緒申 129
+ 鐃緒申 130 Read ceiling(len/8) characters from file f, if that many are available
+ 鐃緒申 131 to read, otherwise read to the end of the file. The first character's
+ 鐃緒申 132 8 bits, in order from MSB to LSB, are used to fill bits[0] through
+ 鐃緒申 133 bits[7]. The second character's bits are used to fill bits[8] through
+ 鐃緒申 134 bits[15], and so on. If ceiling(len/8) characters are available to
+ 鐃緒申 135 read, and len is not a multiple of 8, then some of the least
+ 鐃緒申 136 significant bits of the last character read are completely ignored.
+ 鐃緒申 137 Every entry of bits[] that is modified is changed to either a 0 or a
+ 鐃緒申 138 1.
+ 鐃緒申 139
+ 鐃緒申 140 The number of bits successfully read is returned, and is always in the
+ 鐃緒申 141 range 0 to len, inclusive. If it is less than len, it will always be
+ 鐃緒申 142 a multiple of 8.
+ 鐃緒申 143
+ 鐃緒申 144 */
+ 鐃緒申 145
+ 鐃緒申 146 int
+ 鐃緒申 147 read_bits(unsigned char *data, INT32 *bits, int len)
+ 鐃緒申 148 {
+ 鐃緒申 149 int i,ind=0; /* generic loop variable */
+ 鐃緒申 150 int c=0;
+ 鐃緒申 151
+ 鐃緒申 152 /* Unpack the array bits into coded_frame. */
+ 鐃緒申 153 for (i = 0; i < len; i++) {
+ 鐃緒申 154 if ((i % 8) == 0) {
+ 鐃緒申 155 c = (int)(data[ind]);
+ 鐃緒申 156 ind++;
+ 鐃緒申 157 }
+ 鐃緒申 158 if (c & (0x80 >> (i & 7))) {
+ 鐃緒申 159 bits[i] = 1;
+ 鐃緒申 160 } else {
+ 鐃緒申 161 bits[i] = 0;
+ 鐃緒申 162 }
+ 鐃緒申 163 }
+ 鐃緒申 164 return (len);
+ 鐃緒申 165 }
+ 鐃緒申 166
+ 鐃緒申 167
+ 鐃緒申 168
+ 鐃緒申 169
+ 鐃緒申 170 static MSLPC10EncoderClass *ms_LPC10encoder_class=NULL;
+ 鐃緒申 171
+ 鐃緒申 172 MSFilter * ms_LPC10encoder_new(void)
+ 鐃緒申 173 {
+ 鐃緒申 174 MSLPC10Encoder *r;
+ 鐃緒申 175
+ 鐃緒申 176 r=g_new(MSLPC10Encoder,1);
+ 鐃緒申 177 ms_LPC10encoder_init(r);
+ 鐃緒申 178 if (ms_LPC10encoder_class==NULL)
+ 鐃緒申 179 {
+ 鐃緒申 180 ms_LPC10encoder_class=g_new(MSLPC10EncoderClass,1);
+ 鐃緒申 181 ms_LPC10encoder_class_init(ms_LPC10encoder_class);
+ 鐃緒申 182 }
+ 鐃緒申 183 MS_FILTER(r)->klass=MS_FILTER_CLASS(ms_LPC10encoder_class);
+ 鐃緒申 184 return(MS_FILTER(r));
+ 鐃緒申 185 }
+ 鐃緒申 186
+ 鐃緒申 187
+ 鐃緒申 188 /* FOR INTERNAL USE*/
+ 鐃緒申 189 void ms_LPC10encoder_init(MSLPC10Encoder *r)
+ 鐃緒申 190 {
+ 鐃緒申 191 ms_filter_init(MS_FILTER(r));
+ 鐃緒申 192 MS_FILTER(r)->infifos=r->f_inputs;
+ 鐃緒申 193 MS_FILTER(r)->outfifos=r->f_outputs;
+ 鐃緒申 194 MS_FILTER(r)->r_mingran=LPC10_SAMPLES_PER_FRAME*2;
+ 鐃緒申 195 memset(r->f_inputs,0,sizeof(MSFifo*)*MSLPC10ENCODER_MAX_INPUTS);
+ 鐃緒申 196 memset(r->f_outputs,0,sizeof(MSFifo*)*MSLPC10ENCODER_MAX_INPUTS);
+ 鐃緒申 197 r->lpc10_enc=create_lpc10_encoder_state();
+ 鐃緒申 198 }
+ 鐃緒申 199
+ 鐃緒申 200 void ms_LPC10encoder_class_init(MSLPC10EncoderClass *klass)
+ 鐃緒申 201 {
+ 鐃緒申 202 ms_filter_class_init(MS_FILTER_CLASS(klass));
+ 鐃緒申 203 ms_filter_class_set_name(MS_FILTER_CLASS(klass),"LPC10Enc");
+ 鐃緒申 204 MS_FILTER_CLASS(klass)->max_finputs=MSLPC10ENCODER_MAX_INPUTS;
+ 鐃緒申 205 MS_FILTER_CLASS(klass)->max_foutputs=MSLPC10ENCODER_MAX_INPUTS;
+ 鐃緒申 206 MS_FILTER_CLASS(klass)->r_maxgran=LPC10_SAMPLES_PER_FRAME*2;
+ 鐃緒申 207 MS_FILTER_CLASS(klass)->w_maxgran=7;
+ 鐃緒申 208 MS_FILTER_CLASS(klass)->destroy=(MSFilterDestroyFunc)ms_LPC10encoder_destroy;
+ 鐃緒申 209 MS_FILTER_CLASS(klass)->process=(MSFilterProcessFunc)ms_LPC10encoder_process;
+ 鐃緒申 210 MS_FILTER_CLASS(klass)->info=(MSFilterInfo*)&LPC10info;
+ 鐃緒申 211 }
+ 鐃緒申 212
+ 鐃緒申 213 void ms_LPC10encoder_process(MSLPC10Encoder *r)
+ 鐃緒申 214 {
+ 鐃緒申 215 MSFifo *fi,*fo;
+ 鐃緒申 216 int err1;
+ 鐃緒申 217 void *s,*d;
+ 鐃緒申 218 float speech[LPC10_SAMPLES_PER_FRAME];
+ 鐃緒申 219 INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME];
+ 鐃緒申 220
+ 鐃緒申 221 /* process output fifos, but there is only one for this class of filter*/
+ 鐃緒申 222
+ 鐃緒申 223 fi=r->f_inputs[0];
+ 鐃緒申 224 fo=r->f_outputs[0];
+ 鐃緒申 225 if (fi!=NULL)
+ 鐃緒申 226 {
+ 鐃緒申 227 err1=ms_fifo_get_read_ptr(fi,LPC10_SAMPLES_PER_FRAME*2,&s);
+ 鐃緒申 228 if (err1>0)
+ 鐃緒申 229 {
+ 鐃緒申 230 err1=ms_fifo_get_write_ptr(fo,7,&d);
+ 鐃緒申 231 if (d!=NULL)
+ 鐃緒申 232 {
+ 鐃緒申 233 read_16bit_samples((INT16*)s, speech, LPC10_SAMPLES_PER_FRAME);
+ 鐃緒申 234 lpc10_encode(speech, bits, r->lpc10_enc);
+ 鐃緒申 235 write_bits(d, bits, LPC10_BITS_IN_COMPRESSED_FRAME);
+ 鐃緒申 236 }
+ 鐃緒申 237 }
+ 鐃緒申 238
+ 鐃緒申 239 }
+ 鐃緒申 240 }
+ 鐃緒申 241
+ 鐃緒申 242 void ms_LPC10encoder_uninit(MSLPC10Encoder *obj)
+ 鐃緒申 243 {
+ 鐃緒申 244 free(obj->lpc10_enc);
+ 鐃緒申 245 }
+ 鐃緒申 246
+ 鐃緒申 247 void ms_LPC10encoder_destroy( MSLPC10Encoder *obj)
+ 鐃緒申 248 {
+ 鐃緒申 249 ms_LPC10encoder_uninit(obj);
+ 鐃緒申 250 g_free(obj);
+ 鐃緒申 251 }