1 /*
2  * Copyright 2008-2013 Various Authors
3  * Copyright 2004 Timo Hirvonen
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "pcm.h"
20 #include "utils.h"
21 
22 #include <stdint.h>
23 #include <stdlib.h>
24 
25 /*
26  * Functions to convert PCM to 16-bit signed little-endian stereo
27  *
28  * Conversion for 8-bit PCM):
29  *   1. phase
30  *      unsigned -> signed
31  *      mono -> stereo
32  *      8 -> 16
33  *
34  * Conversion for 16-bit PCM:
35  *   1. phase
36  *      be -> le
37  *      unsigned -> signed
38  *
39  *   2. phase
40  *      mono -> stereo
41  *
42  * There's no reason to split 8-bit conversion to 2 phases because we need to
43  * use separate buffer for 8->16 conversion anyway.
44  *
45  * Conversions for 16-bit stereo can be done in place. 16-bit mono needs to be
46  * converted to stereo so it's worthwhile to split the conversion to 2 phases.
47  */
48 
convert_u8_1ch_to_s16_2ch(void * dst,const void * src,int count)49 static void convert_u8_1ch_to_s16_2ch(void *dst, const void *src, int count)
50 {
51 	int16_t *d = dst;
52 	const uint8_t *s = src;
53 	int i, j = 0;
54 
55 	for (i = 0; i < count; i++) {
56 		int16_t sample = s[i] << 8;
57 		sample -= 32768;
58 		d[j++] = sample;
59 		d[j++] = sample;
60 	}
61 }
62 
convert_s8_1ch_to_s16_2ch(void * dst,const void * src,int count)63 static void convert_s8_1ch_to_s16_2ch(void *dst, const void *src, int count)
64 {
65 	int16_t *d = dst;
66 	const int8_t *s = src;
67 	int i, j = 0;
68 
69 	for (i = 0; i < count; i++) {
70 		int16_t sample = s[i] << 8;
71 		d[j++] = sample;
72 		d[j++] = sample;
73 	}
74 }
75 
convert_u8_2ch_to_s16_2ch(void * dst,const void * src,int count)76 static void convert_u8_2ch_to_s16_2ch(void *dst, const void *src, int count)
77 {
78 	int16_t *d = dst;
79 	const int8_t *s = src;
80 	int i;
81 
82 	for (i = 0; i < count; i++) {
83 		int16_t sample = s[i] << 8;
84 		sample -= 32768;
85 		d[i] = sample;
86 	}
87 }
88 
convert_s8_2ch_to_s16_2ch(void * dst,const void * src,int count)89 static void convert_s8_2ch_to_s16_2ch(void *dst, const void *src, int count)
90 {
91 	int16_t *d = dst;
92 	const int8_t *s = src;
93 	int i;
94 
95 	for (i = 0; i < count; i++) {
96 		int16_t sample = s[i] << 8;
97 		d[i] = sample;
98 	}
99 }
100 
convert_u16_le_to_s16_le(void * buf,int count)101 static void convert_u16_le_to_s16_le(void *buf, int count)
102 {
103 	int16_t *b = buf;
104 	int i;
105 
106 	for (i = 0; i < count; i++) {
107 		int sample = (uint16_t)b[i];
108 		sample -= 32768;
109 		b[i] = sample;
110 	}
111 }
112 
convert_u16_be_to_s16_le(void * buf,int count)113 static void convert_u16_be_to_s16_le(void *buf, int count)
114 {
115 	int16_t *b = buf;
116 	int i;
117 
118 	for (i = 0; i < count; i++) {
119 		uint16_t u = b[i];
120 		int sample;
121 
122 		u = swap_uint16(u);
123 		sample = (int)u - 32768;
124 		b[i] = sample;
125 	}
126 }
127 
swap_s16_byte_order(void * buf,int count)128 static void swap_s16_byte_order(void *buf, int count)
129 {
130 	int16_t *b = buf;
131 	int i;
132 
133 	for (i = 0; i < count; i++)
134 		b[i] = swap_uint16(b[i]);
135 }
136 
convert_16_1ch_to_16_2ch(void * dst,const void * src,int count)137 static void convert_16_1ch_to_16_2ch(void *dst, const void *src, int count)
138 {
139 	int16_t *d = dst;
140 	const int16_t *s = src;
141 	int i, j = 0;
142 
143 	for (i = 0; i < count; i++) {
144 		d[j++] = s[i];
145 		d[j++] = s[i];
146 	}
147 }
148 
149 /* index is ((bits >> 2) & 4) | (is_signed << 1) | (channels - 1) */
150 pcm_conv_func pcm_conv[8] = {
151 	convert_u8_1ch_to_s16_2ch,
152 	convert_u8_2ch_to_s16_2ch,
153 	convert_s8_1ch_to_s16_2ch,
154 	convert_s8_2ch_to_s16_2ch,
155 
156 	convert_16_1ch_to_16_2ch,
157 	NULL,
158 	convert_16_1ch_to_16_2ch,
159 	NULL
160 };
161 
162 /* index is ((bits >> 2) & 4) | (is_signed << 1) | bigendian */
163 pcm_conv_in_place_func pcm_conv_in_place[8] = {
164 	NULL,
165 	NULL,
166 	NULL,
167 	NULL,
168 
169 	convert_u16_le_to_s16_le,
170 	convert_u16_be_to_s16_le,
171 
172 #ifdef WORDS_BIGENDIAN
173 	swap_s16_byte_order,
174 	NULL,
175 #else
176 	NULL,
177 	swap_s16_byte_order,
178 #endif
179 };
180