1 /*******************************************************************************
2  * Copyright 2009-2016 Jörg Müller
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 #include "respec/ConverterFunctions.h"
18 
19 #include <stdint.h>
20 
21 #define U8_0		0x80
22 #define S16_MAX		((int16_t)0x7FFF)
23 #define S16_MIN		((int16_t)0x8000)
24 #define S16_FLT		32767.0f
25 #define S32_MAX		((int32_t)0x7FFFFFFF)
26 #define S32_MIN		((int32_t)0x80000000)
27 #define S32_FLT		2147483647.0f
28 #define FLT_MAX		1.0f
29 #define FLT_MIN		-1.0f
30 
31 AUD_NAMESPACE_BEGIN
32 
convert_u8_s16(data_t * target,data_t * source,int length)33 void convert_u8_s16(data_t* target, data_t* source, int length)
34 {
35 	int16_t* t = (int16_t*) target;
36 	for(int i = length - 1; i >= 0; i--)
37 		t[i] = (((int16_t)source[i]) - U8_0) << 8;
38 }
39 
convert_u8_s24_be(data_t * target,data_t * source,int length)40 void convert_u8_s24_be(data_t* target, data_t* source, int length)
41 {
42 	for(int i = length - 1; i >= 0; i--)
43 	{
44 		target[i*3] = source[i] - U8_0;
45 		target[i*3+1] = 0;
46 		target[i*3+2] = 0;
47 	}
48 }
49 
convert_u8_s24_le(data_t * target,data_t * source,int length)50 void convert_u8_s24_le(data_t* target, data_t* source, int length)
51 {
52 	for(int i = length - 1; i >= 0; i--)
53 	{
54 		target[i*3+2] = source[i] - U8_0;
55 		target[i*3+1] = 0;
56 		target[i*3] = 0;
57 	}
58 }
59 
convert_u8_s32(data_t * target,data_t * source,int length)60 void convert_u8_s32(data_t* target, data_t* source, int length)
61 {
62 	int32_t* t = (int32_t*) target;
63 	for(int i = length - 1; i >= 0; i--)
64 		t[i] = (((int32_t)source[i]) - U8_0) << 24;
65 }
66 
convert_u8_float(data_t * target,data_t * source,int length)67 void convert_u8_float(data_t* target, data_t* source, int length)
68 {
69 	float* t = (float*) target;
70 	for(int i = length - 1; i >= 0; i--)
71 		t[i] = (((int32_t)source[i]) - U8_0) / ((float)U8_0);
72 }
73 
convert_u8_double(data_t * target,data_t * source,int length)74 void convert_u8_double(data_t* target, data_t* source, int length)
75 {
76 	double* t = (double*) target;
77 	for(int i = length - 1; i >= 0; i--)
78 		t[i] = (((int32_t)source[i]) - U8_0) / ((double)U8_0);
79 }
80 
convert_s16_u8(data_t * target,data_t * source,int length)81 void convert_s16_u8(data_t* target, data_t* source, int length)
82 {
83 	int16_t* s = (int16_t*) source;
84 	for(int i = 0; i < length; i++)
85 		target[i] = (unsigned char)((s[i] >> 8) + U8_0);
86 }
87 
convert_s16_s24_be(data_t * target,data_t * source,int length)88 void convert_s16_s24_be(data_t* target, data_t* source, int length)
89 {
90 	int16_t* s = (int16_t*) source;
91 	int16_t t;
92 	for(int i = length - 1; i >= 0; i--)
93 	{
94 		t = s[i];
95 		target[i*3] = t >> 8 & 0xFF;
96 		target[i*3+1] = t & 0xFF;
97 		target[i*3+2] = 0;
98 	}
99 }
100 
convert_s16_s24_le(data_t * target,data_t * source,int length)101 void convert_s16_s24_le(data_t* target, data_t* source, int length)
102 {
103 	int16_t* s = (int16_t*) source;
104 	int16_t t;
105 	for(int i = length - 1; i >= 0; i--)
106 	{
107 		t = s[i];
108 		target[i*3+2] = t >> 8 & 0xFF;
109 		target[i*3+1] = t & 0xFF;
110 		target[i*3] = 0;
111 	}
112 }
113 
convert_s16_s32(data_t * target,data_t * source,int length)114 void convert_s16_s32(data_t* target, data_t* source, int length)
115 {
116 	int16_t* s = (int16_t*) source;
117 	int32_t* t = (int32_t*) target;
118 	for(int i = length - 1; i >= 0; i--)
119 		t[i] = ((int32_t)s[i]) << 16;
120 }
121 
convert_s16_float(data_t * target,data_t * source,int length)122 void convert_s16_float(data_t* target, data_t* source, int length)
123 {
124 	int16_t* s = (int16_t*) source;
125 	float* t = (float*) target;
126 	for(int i = length - 1; i >= 0; i--)
127 		t[i] = s[i] / S16_FLT;
128 }
129 
convert_s16_double(data_t * target,data_t * source,int length)130 void convert_s16_double(data_t* target, data_t* source, int length)
131 {
132 	int16_t* s = (int16_t*) source;
133 	double* t = (double*) target;
134 	for(int i = length - 1; i >= 0; i--)
135 		t[i] = s[i] / S16_FLT;
136 }
137 
convert_s24_u8_be(data_t * target,data_t * source,int length)138 void convert_s24_u8_be(data_t* target, data_t* source, int length)
139 {
140 	for(int i = 0; i < length; i++)
141 		target[i] = source[i*3] ^ U8_0;
142 }
143 
convert_s24_u8_le(data_t * target,data_t * source,int length)144 void convert_s24_u8_le(data_t* target, data_t* source, int length)
145 {
146 	for(int i = 0; i < length; i++)
147 		target[i] = source[i*3+2] ^ U8_0;
148 }
149 
convert_s24_s16_be(data_t * target,data_t * source,int length)150 void convert_s24_s16_be(data_t* target, data_t* source, int length)
151 {
152 	int16_t* t = (int16_t*) target;
153 	for(int i = 0; i < length; i++)
154 		t[i] = source[i*3] << 8 | source[i*3+1];
155 }
156 
convert_s24_s16_le(data_t * target,data_t * source,int length)157 void convert_s24_s16_le(data_t* target, data_t* source, int length)
158 {
159 	int16_t* t = (int16_t*) target;
160 	for(int i = 0; i < length; i++)
161 		t[i] = source[i*3+2] << 8 | source[i*3+1];
162 }
163 
convert_s24_s24(data_t * target,data_t * source,int length)164 void convert_s24_s24(data_t* target, data_t* source, int length)
165 {
166 	std::memcpy(target, source, length * 3);
167 }
168 
convert_s24_s32_be(data_t * target,data_t * source,int length)169 void convert_s24_s32_be(data_t* target, data_t* source, int length)
170 {
171 	int32_t* t = (int32_t*) target;
172 	for(int i = length - 1; i >= 0; i--)
173 		t[i] = source[i*3] << 24 | source[i*3+1] << 16 | source[i*3+2] << 8;
174 }
175 
convert_s24_s32_le(data_t * target,data_t * source,int length)176 void convert_s24_s32_le(data_t* target, data_t* source, int length)
177 {
178 	int32_t* t = (int32_t*) target;
179 	for(int i = length - 1; i >= 0; i--)
180 		t[i] = source[i*3+2] << 24 | source[i*3+1] << 16 | source[i*3] << 8;
181 }
182 
convert_s24_float_be(data_t * target,data_t * source,int length)183 void convert_s24_float_be(data_t* target, data_t* source, int length)
184 {
185 	float* t = (float*) target;
186 	int32_t s;
187 	for(int i = length - 1; i >= 0; i--)
188 	{
189 		s = source[i*3] << 24 | source[i*3+1] << 16 | source[i*3+2] << 8;
190 		t[i] = s / S32_FLT;
191 	}
192 }
193 
convert_s24_float_le(data_t * target,data_t * source,int length)194 void convert_s24_float_le(data_t* target, data_t* source, int length)
195 {
196 	float* t = (float*) target;
197 	int32_t s;
198 	for(int i = length - 1; i >= 0; i--)
199 	{
200 		s = source[i*3+2] << 24 | source[i*3+1] << 16 | source[i*3] << 8;
201 		t[i] = s / S32_FLT;
202 	}
203 }
204 
convert_s24_double_be(data_t * target,data_t * source,int length)205 void convert_s24_double_be(data_t* target, data_t* source, int length)
206 {
207 	double* t = (double*) target;
208 	int32_t s;
209 	for(int i = length - 1; i >= 0; i--)
210 	{
211 		s = source[i*3] << 24 | source[i*3+1] << 16 | source[i*3+2] << 8;
212 		t[i] = s / S32_FLT;
213 	}
214 }
215 
convert_s24_double_le(data_t * target,data_t * source,int length)216 void convert_s24_double_le(data_t* target, data_t* source, int length)
217 {
218 	double* t = (double*) target;
219 	int32_t s;
220 	for(int i = length - 1; i >= 0; i--)
221 	{
222 		s = source[i*3+2] << 24 | source[i*3+1] << 16 | source[i*3] << 8;
223 		t[i] = s / S32_FLT;
224 	}
225 }
226 
convert_s32_u8(data_t * target,data_t * source,int length)227 void convert_s32_u8(data_t* target, data_t* source, int length)
228 {
229 	int16_t* s = (int16_t*) source;
230 	for(int i = 0; i < length; i++)
231 		target[i] = (unsigned char)((s[i] >> 24) + U8_0);
232 }
233 
convert_s32_s16(data_t * target,data_t * source,int length)234 void convert_s32_s16(data_t* target, data_t* source, int length)
235 {
236 	int16_t* t = (int16_t*) target;
237 	int32_t* s = (int32_t*) source;
238 	for(int i = 0; i < length; i++)
239 		t[i] = s[i] >> 16;
240 }
241 
convert_s32_s24_be(data_t * target,data_t * source,int length)242 void convert_s32_s24_be(data_t* target, data_t* source, int length)
243 {
244 	int32_t* s = (int32_t*) source;
245 	int32_t t;
246 	for(int i = 0; i < length; i++)
247 	{
248 		t = s[i];
249 		target[i*3] = t >> 24 & 0xFF;
250 		target[i*3+1] = t >> 16 & 0xFF;
251 		target[i*3+2] = t >> 8 & 0xFF;
252 	}
253 }
254 
convert_s32_s24_le(data_t * target,data_t * source,int length)255 void convert_s32_s24_le(data_t* target, data_t* source, int length)
256 {
257 	int32_t* s = (int32_t*) source;
258 	int32_t t;
259 	for(int i = 0; i < length; i++)
260 	{
261 		t = s[i];
262 		target[i*3+2] = t >> 24 & 0xFF;
263 		target[i*3+1] = t >> 16 & 0xFF;
264 		target[i*3] = t >> 8 & 0xFF;
265 	}
266 }
267 
convert_s32_float(data_t * target,data_t * source,int length)268 void convert_s32_float(data_t* target, data_t* source, int length)
269 {
270 	int32_t* s = (int32_t*) source;
271 	float* t = (float*) target;
272 	for(int i = 0; i < length; i++)
273 		t[i] = s[i] / S32_FLT;
274 }
275 
convert_s32_double(data_t * target,data_t * source,int length)276 void convert_s32_double(data_t* target, data_t* source, int length)
277 {
278 	int32_t* s = (int32_t*) source;
279 	double* t = (double*) target;
280 	for(int i = length - 1; i >= 0; i--)
281 		t[i] = s[i] / S32_FLT;
282 }
283 
convert_float_u8(data_t * target,data_t * source,int length)284 void convert_float_u8(data_t* target, data_t* source, int length)
285 {
286 	float* s = (float*) source;
287 	float t;
288 	for(int i = 0; i < length; i++)
289 	{
290 		t = s[i] + FLT_MAX;
291 		if(t <= 0.0f)
292 			target[i] = 0;
293 		else if(t >= 2.0f)
294 			target[i] = 255;
295 		else
296 			target[i] = (unsigned char)(t*127);
297 	}
298 }
299 
convert_float_s16(data_t * target,data_t * source,int length)300 void convert_float_s16(data_t* target, data_t* source, int length)
301 {
302 	int16_t* t = (int16_t*) target;
303 	float* s = (float*) source;
304 	for(int i = 0; i < length; i++)
305 	{
306 		if(s[i] <= FLT_MIN)
307 			t[i] = S16_MIN;
308 		else if(s[i] >= FLT_MAX)
309 			t[i] = S16_MAX;
310 		else
311 			t[i] = (int16_t)(s[i] * S16_MAX);
312 	}
313 }
314 
convert_float_s24_be(data_t * target,data_t * source,int length)315 void convert_float_s24_be(data_t* target, data_t* source, int length)
316 {
317 	int32_t t;
318 	float* s = (float*) source;
319 	for(int i = 0; i < length; i++)
320 	{
321 		if(s[i] <= FLT_MIN)
322 			t = S32_MIN;
323 		else if(s[i] >= FLT_MAX)
324 			t = S32_MAX;
325 		else
326 			t = (int32_t)(s[i]*S32_MAX);
327 		target[i*3] = t >> 24 & 0xFF;
328 		target[i*3+1] = t >> 16 & 0xFF;
329 		target[i*3+2] = t >> 8 & 0xFF;
330 	}
331 }
332 
convert_float_s24_le(data_t * target,data_t * source,int length)333 void convert_float_s24_le(data_t* target, data_t* source, int length)
334 {
335 	int32_t t;
336 	float* s = (float*) source;
337 	for(int i = 0; i < length; i++)
338 	{
339 		if(s[i] <= FLT_MIN)
340 			t = S32_MIN;
341 		else if(s[i] >= FLT_MAX)
342 			t = S32_MAX;
343 		else
344 			t = (int32_t)(s[i]*S32_MAX);
345 		target[i*3+2] = t >> 24 & 0xFF;
346 		target[i*3+1] = t >> 16 & 0xFF;
347 		target[i*3] = t >> 8 & 0xFF;
348 	}
349 }
350 
convert_float_s32(data_t * target,data_t * source,int length)351 void convert_float_s32(data_t* target, data_t* source, int length)
352 {
353 	int32_t* t = (int32_t*) target;
354 	float* s = (float*) source;
355 	for(int i = 0; i < length; i++)
356 	{
357 		if(s[i] <= FLT_MIN)
358 			t[i] = S32_MIN;
359 		else if(s[i] >= FLT_MAX)
360 			t[i] = S32_MAX;
361 		else
362 			t[i] = (int32_t)(s[i]*S32_MAX);
363 	}
364 }
365 
convert_float_double(data_t * target,data_t * source,int length)366 void convert_float_double(data_t* target, data_t* source, int length)
367 {
368 	float* s = (float*) source;
369 	double* t = (double*) target;
370 	for(int i = length - 1; i >= 0; i--)
371 		t[i] = s[i];
372 }
373 
convert_double_u8(data_t * target,data_t * source,int length)374 void convert_double_u8(data_t* target, data_t* source, int length)
375 {
376 	double* s = (double*) source;
377 	double t;
378 	for(int i = 0; i < length; i++)
379 	{
380 		t = s[i] + FLT_MAX;
381 		if(t <= 0.0)
382 			target[i] = 0;
383 		else if(t >= 2.0)
384 			target[i] = 255;
385 		else
386 			target[i] = (unsigned char)(t*127);
387 	}
388 }
389 
convert_double_s16(data_t * target,data_t * source,int length)390 void convert_double_s16(data_t* target, data_t* source, int length)
391 {
392 	int16_t* t = (int16_t*) target;
393 	double* s = (double*) source;
394 	for(int i = 0; i < length; i++)
395 	{
396 		if(s[i] <= FLT_MIN)
397 			t[i] = S16_MIN;
398 		else if(s[i] >= FLT_MAX)
399 			t[i] = S16_MAX;
400 		else
401 			t[i] = (int16_t)(s[i]*S16_MAX);
402 	}
403 }
404 
convert_double_s24_be(data_t * target,data_t * source,int length)405 void convert_double_s24_be(data_t* target, data_t* source, int length)
406 {
407 	int32_t t;
408 	double* s = (double*) source;
409 	for(int i = 0; i < length; i++)
410 	{
411 		if(s[i] <= FLT_MIN)
412 			t = S32_MIN;
413 		else if(s[i] >= FLT_MAX)
414 			t = S32_MAX;
415 		else
416 			t = (int32_t)(s[i]*S32_MAX);
417 		target[i*3] = t >> 24 & 0xFF;
418 		target[i*3+1] = t >> 16 & 0xFF;
419 		target[i*3+2] = t >> 8 & 0xFF;
420 	}
421 }
422 
convert_double_s24_le(data_t * target,data_t * source,int length)423 void convert_double_s24_le(data_t* target, data_t* source, int length)
424 {
425 	int32_t t;
426 	double* s = (double*) source;
427 	for(int i = 0; i < length; i++)
428 	{
429 		if(s[i] <= FLT_MIN)
430 			t = S32_MIN;
431 		else if(s[i] >= FLT_MAX)
432 			t = S32_MAX;
433 		else
434 			t = (int32_t)(s[i]*S32_MAX);
435 		target[i*3+2] = t >> 24 & 0xFF;
436 		target[i*3+1] = t >> 16 & 0xFF;
437 		target[i*3] = t >> 8 & 0xFF;
438 	}
439 }
440 
convert_double_s32(data_t * target,data_t * source,int length)441 void convert_double_s32(data_t* target, data_t* source, int length)
442 {
443 	int32_t* t = (int32_t*) target;
444 	double* s = (double*) source;
445 	for(int i = 0; i < length; i++)
446 	{
447 		if(s[i] <= FLT_MIN)
448 			t[i] = S32_MIN;
449 		else if(s[i] >= FLT_MAX)
450 			t[i] = S32_MAX;
451 		else
452 			t[i] = (int32_t)(s[i]*S32_MAX);
453 	}
454 }
455 
convert_double_float(data_t * target,data_t * source,int length)456 void convert_double_float(data_t* target, data_t* source, int length)
457 {
458 	double* s = (double*) source;
459 	float* t = (float*) target;
460 	for(int i = 0; i < length; i++)
461 		t[i] = s[i];
462 }
463 
464 AUD_NAMESPACE_END
465