• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Siren Encoder/Decoder library
3  *
4  *   @author: Youness Alaoui <kakaroto@kakaroto.homelinux.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 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  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 
23 #include "siren7.h"
24 
25 SirenDecoder
Siren7_NewDecoder(int sample_rate)26 Siren7_NewDecoder (int sample_rate)
27 {
28   SirenDecoder decoder = (SirenDecoder) malloc (sizeof (struct stSirenDecoder));
29   decoder->sample_rate = sample_rate;
30 
31   decoder->WavHeader.riff.RiffId = ME_TO_LE32 (RIFF_ID);
32   decoder->WavHeader.riff.RiffSize = sizeof (PCMWavHeader) - 2 * sizeof (int);
33   decoder->WavHeader.riff.RiffSize =
34       ME_TO_LE32 (decoder->WavHeader.riff.RiffSize);
35   decoder->WavHeader.WaveId = ME_TO_LE32 (WAVE_ID);
36 
37   decoder->WavHeader.FmtId = ME_TO_LE32 (FMT__ID);
38   decoder->WavHeader.FmtSize = ME_TO_LE32 (sizeof (FmtChunk));
39 
40   decoder->WavHeader.fmt.Format = ME_TO_LE16 (0x01);
41   decoder->WavHeader.fmt.Channels = ME_TO_LE16 (1);
42   decoder->WavHeader.fmt.SampleRate = ME_TO_LE32 (16000);
43   decoder->WavHeader.fmt.ByteRate = ME_TO_LE32 (32000);
44   decoder->WavHeader.fmt.BlockAlign = ME_TO_LE16 (2);
45   decoder->WavHeader.fmt.BitsPerSample = ME_TO_LE16 (16);
46 
47   decoder->WavHeader.FactId = ME_TO_LE32 (FACT_ID);
48   decoder->WavHeader.FactSize = ME_TO_LE32 (sizeof (int));
49   decoder->WavHeader.Samples = ME_TO_LE32 (0);
50 
51   decoder->WavHeader.DataId = ME_TO_LE32 (DATA_ID);
52   decoder->WavHeader.DataSize = ME_TO_LE32 (0);
53 
54   memset (decoder->context, 0, sizeof (decoder->context));
55   memset (decoder->backup_frame, 0, sizeof (decoder->backup_frame));
56 
57   decoder->dw1 = 1;
58   decoder->dw2 = 1;
59   decoder->dw3 = 1;
60   decoder->dw4 = 1;
61 
62   siren_init ();
63   return decoder;
64 }
65 
66 void
Siren7_CloseDecoder(SirenDecoder decoder)67 Siren7_CloseDecoder (SirenDecoder decoder)
68 {
69   free (decoder);
70 }
71 
72 int
Siren7_DecodeFrame(SirenDecoder decoder,unsigned char * DataIn,unsigned char * DataOut)73 Siren7_DecodeFrame (SirenDecoder decoder, unsigned char *DataIn,
74     unsigned char *DataOut)
75 {
76   int number_of_coefs,
77       sample_rate_bits,
78       rate_control_bits,
79       rate_control_possibilities,
80       checksum_bits,
81       esf_adjustment,
82       scale_factor, number_of_regions, sample_rate_code, bits_per_frame;
83   int decoded_sample_rate_code;
84 
85   int absolute_region_power_index[28] = { 0 };
86   float decoder_standard_deviation[28] = { 0 };
87   int power_categories[28] = { 0 };
88   int category_balance[28] = { 0 };
89   int ChecksumTable[4] = { 0x7F80, 0x7878, 0x6666, 0x5555 };
90   int i, j;
91 
92   int dwRes = 0;
93   int envelope_bits = 0;
94   int rate_control = 0;
95   int number_of_available_bits;
96   int number_of_valid_coefs;
97   int frame_error = 0;
98 
99   int In[20];
100   float coefs[320];
101   float BufferOut[320];
102   int sum;
103   int checksum;
104   int calculated_checksum;
105   int idx;
106   int temp1;
107   int temp2;
108 
109   for (i = 0; i < 20; i++)
110 #ifdef __BIG_ENDIAN__
111     In[i] = ((short *) DataIn)[i];
112 #else
113     In[i] =
114         ((((short *) DataIn)[i] << 8) & 0xFF00) | ((((short *) DataIn)[i] >> 8)
115         & 0x00FF);
116 #endif
117 
118   dwRes =
119       GetSirenCodecInfo (1, decoder->sample_rate, &number_of_coefs,
120       &sample_rate_bits, &rate_control_bits, &rate_control_possibilities,
121       &checksum_bits, &esf_adjustment, &scale_factor, &number_of_regions,
122       &sample_rate_code, &bits_per_frame);
123 
124   if (dwRes != 0)
125     return dwRes;
126 
127 
128   set_bitstream (In);
129 
130   decoded_sample_rate_code = 0;
131   for (i = 0; i < sample_rate_bits; i++) {
132     decoded_sample_rate_code <<= 1;
133     decoded_sample_rate_code |= next_bit ();
134   }
135 
136 
137   if (decoded_sample_rate_code != sample_rate_code)
138     return 7;
139 
140   number_of_valid_coefs = region_size * number_of_regions;
141   number_of_available_bits = bits_per_frame - sample_rate_bits - checksum_bits;
142 
143 
144   envelope_bits =
145       decode_envelope (number_of_regions, decoder_standard_deviation,
146       absolute_region_power_index, esf_adjustment);
147 
148   number_of_available_bits -= envelope_bits;
149 
150   for (i = 0; i < rate_control_bits; i++) {
151     rate_control <<= 1;
152     rate_control |= next_bit ();
153   }
154 
155   number_of_available_bits -= rate_control_bits;
156 
157   categorize_regions (number_of_regions, number_of_available_bits,
158       absolute_region_power_index, power_categories, category_balance);
159 
160   for (i = 0; i < rate_control; i++) {
161     power_categories[category_balance[i]]++;
162   }
163 
164   number_of_available_bits =
165       decode_vector (decoder, number_of_regions, number_of_available_bits,
166       decoder_standard_deviation, power_categories, coefs, scale_factor);
167 
168 
169   frame_error = 0;
170   if (number_of_available_bits > 0) {
171     for (i = 0; i < number_of_available_bits; i++) {
172       if (next_bit () == 0)
173         frame_error = 1;
174     }
175   } else if (number_of_available_bits < 0
176       && rate_control + 1 < rate_control_possibilities) {
177     frame_error |= 2;
178   }
179 
180   for (i = 0; i < number_of_regions; i++) {
181     if (absolute_region_power_index[i] > 33
182         || absolute_region_power_index[i] < -31)
183       frame_error |= 4;
184   }
185 
186   if (checksum_bits > 0) {
187     bits_per_frame >>= 4;
188     checksum = In[bits_per_frame - 1] & ((1 << checksum_bits) - 1);
189     In[bits_per_frame - 1] &= ~checksum;
190     sum = 0;
191     idx = 0;
192     do {
193       sum ^= (In[idx] & 0xFFFF) << (idx % 15);
194     } while (++idx < bits_per_frame);
195 
196     sum = (sum >> 15) ^ (sum & 0x7FFF);
197     calculated_checksum = 0;
198     for (i = 0; i < 4; i++) {
199       temp1 = ChecksumTable[i] & sum;
200       for (j = 8; j > 0; j >>= 1) {
201         temp2 = temp1 >> j;
202         temp1 ^= temp2;
203       }
204       calculated_checksum <<= 1;
205       calculated_checksum |= temp1 & 1;
206     }
207 
208     if (checksum != calculated_checksum)
209       frame_error |= 8;
210   }
211 
212   if (frame_error != 0) {
213     for (i = 0; i < number_of_valid_coefs; i++) {
214       coefs[i] = decoder->backup_frame[i];
215       decoder->backup_frame[i] = 0;
216     }
217   } else {
218     for (i = 0; i < number_of_valid_coefs; i++)
219       decoder->backup_frame[i] = coefs[i];
220   }
221 
222 
223   for (i = number_of_valid_coefs; i < number_of_coefs; i++)
224     coefs[i] = 0;
225 
226 
227   dwRes = siren_rmlt_decode_samples (coefs, decoder->context, 320, BufferOut);
228 
229 
230   for (i = 0; i < 320; i++) {
231     if (BufferOut[i] > 32767.0)
232       ((short *) DataOut)[i] = (short) ME_TO_LE16 ((short) 32767);
233     else if (BufferOut[i] <= -32768.0)
234       ((short *) DataOut)[i] = (short) ME_TO_LE16 ((short) 32768);
235     else
236       ((short *) DataOut)[i] = (short) ME_TO_LE16 ((short) BufferOut[i]);
237   }
238 
239   decoder->WavHeader.Samples = ME_FROM_LE32 (decoder->WavHeader.Samples);
240   decoder->WavHeader.Samples += 320;
241   decoder->WavHeader.Samples = ME_TO_LE32 (decoder->WavHeader.Samples);
242   decoder->WavHeader.DataSize = ME_FROM_LE32 (decoder->WavHeader.DataSize);
243   decoder->WavHeader.DataSize += 640;
244   decoder->WavHeader.DataSize = ME_TO_LE32 (decoder->WavHeader.DataSize);
245   decoder->WavHeader.riff.RiffSize =
246       ME_FROM_LE32 (decoder->WavHeader.riff.RiffSize);
247   decoder->WavHeader.riff.RiffSize += 640;
248   decoder->WavHeader.riff.RiffSize =
249       ME_TO_LE32 (decoder->WavHeader.riff.RiffSize);
250 
251 
252   return 0;
253 }
254