comparison minijss/jssmod.c @ 1972:cedb5ca1533b

Clean up the sample conversion code a bit. Improve error handling in xm2jss.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 30 Jun 2018 18:44:59 +0300
parents 6aa0897265e8
children f60b4bacf6bf
comparison
equal deleted inserted replaced
1971:c27f7cd25684 1972:cedb5ca1533b
32 } 32 }
33 33
34 34
35 /* Encodes a given 8-bit sample 35 /* Encodes a given 8-bit sample
36 */ 36 */
37 BOOL jssEncodeSample8(Uint8 * data, const size_t len, const int ops) 37 int jssEncodeSample8(Uint8 * data, const size_t len, const int ops)
38 { 38 {
39 size_t count = len; 39 Sint8 value = 0;
40 Sint8 t, value = 0;
41 40
42 while (count--) 41 while (count--)
43 { 42 for (size_t offs = 0; offs < len; offs++)
44 t = *data; 43 {
44 Sint8 t = data[offs];
45 45
46 if (ops & jsampFlipSign) 46 if (ops & jsampFlipSign)
47 t ^= 0x80; 47 t ^= 0x80;
48 48
49 if (ops & jsampDelta) 49 if (ops & jsampDelta)
51 int n = t - value; 51 int n = t - value;
52 value = t; 52 value = t;
53 t = n; 53 t = n;
54 } 54 }
55 55
56 *(data++) = t; 56 data[offs] = t;
57 } 57 }
58 58
59 return TRUE; 59 return TRUE;
60 } 60 }
61 61
62 62
63 /* Decodes a given 16-bit sample 63 /* Decodes a given 16-bit sample
64 */ 64 */
65 BOOL jssEncodeSample16(Uint16 * data, const size_t len, const int ops) 65 int jssEncodeSample16(Uint16 * data, const size_t len, const int ops)
66 { 66 {
67 // "Split" the 16-bit samples into 8-bit halves 67 // "Split" the 16-bit samples into 8-bit halves
68 if (ops & jsampSplit) 68 if (ops & jsampSplit)
69 { 69 {
70 // Allocate temporary processing buffer 70 // Allocate temporary processing buffer
71 size_t count, bufSize = len * sizeof(Sint16);
72 Uint8 *bp1, *bp2; 71 Uint8 *bp1, *bp2;
73 Sint16 *sdata, *tmpBuf = dmMalloc(bufSize); 72 size_t bufSize = len * sizeof(Sint16);
74 if (tmpBuf == NULL) return FALSE; 73 Sint16 *tmpBuf;
75 74
76 sdata = tmpBuf; 75 if ((tmpBuf = dmMalloc(bufSize)) == NULL)
76 return DMERR_MALLOC;
77
77 bp1 = (Uint8 *) data; 78 bp1 = (Uint8 *) data;
78 bp2 = bp1 + len; 79 bp2 = bp1 + len;
79 count = len; 80
80 81 for (size_t offs = 0; offs < len; offs++)
81 while (count--) 82 {
82 { 83 const Sint16 t = tmpBuf[offs];
83 Sint16 t = (*sdata++); 84 bp1[offs] = t >> 8;
84 *bp1++ = t >> 8; 85 bp2[offs] = t & 0xff;
85 *bp2++ = t & 0xff;
86 } 86 }
87 87
88 memcpy(data, tmpBuf, bufSize); 88 memcpy(data, tmpBuf, bufSize);
89 dmFree(tmpBuf); 89 dmFree(tmpBuf);
90 90
91 return jssEncodeSample8((Uint8 *) data, bufSize, ops); 91 return jssEncodeSample8((Uint8 *) data, bufSize, ops);
92 } 92 }
93 else 93 else
94 { 94 {
95 Sint16 t, p, value = 0, *sdata = (Sint16 *) data; 95 Sint16 value = 0, *sdata = (Sint16 *) data;
96 size_t count = len; 96
97 97 for (size_t offs = 0; offs < len; offs++)
98 while (count--) 98 {
99 { 99 Sint16 t;
100
100 if (ops & jsampSwapEndianess) 101 if (ops & jsampSwapEndianess)
101 { 102 {
102 p = *sdata; 103 const Sint16 p = sdata[offs];
104 t = ((p >> 8) & 0xff) | ((p & 0xff) << 8);
105 }
106 else
107 t = sdata[offs];
108
109 if (ops & jsampDelta)
110 {
111 const int n = t - value;
112 value = t;
113 t = n;
114 }
115
116 if (ops & jsampFlipSign)
117 t ^= 0x8000;
118
119 sdata[offs] = t;
120 }
121 }
122
123 return DMERR_OK;
124 }
125
126 #endif
127
128
129 /* Decodes a given 8-bit sample
130 */
131 int jssDecodeSample8(Uint8 * data, const size_t len, const int ops)
132 {
133 Sint8 value = 0;
134
135 for (size_t offs = 0; offs < len; offs++)
136 {
137 Sint8 t = data[offs];
138
139 if (ops & jsampDelta)
140 t = value = t + value;
141
142 if (ops & jsampFlipSign)
143 t ^= 0x80;
144
145 data[offs] = t;
146 }
147
148 return DMERR_OK;
149 }
150
151
152 /* Decodes a given 16-bit sample
153 */
154 int jssDecodeSample16(Uint16 * data, const size_t len, const int ops)
155 {
156 if (ops & jsampSplit)
157 {
158 size_t bufSize = len * sizeof(Uint16);
159 Uint8 *bp1, *bp2;
160 Sint16 *tmpBuf;
161 int ret;
162
163 if ((ret = jssDecodeSample8((Uint8 *) data, bufSize, ops)) != DMERR_OK)
164 return ret;
165
166 if ((tmpBuf = dmMalloc(bufSize)) == NULL)
167 return DMERR_MALLOC;
168
169 // Copy original data to temporary buffer
170 memcpy(tmpBuf, data, bufSize);
171
172 bp1 = (Uint8 *) tmpBuf;
173 bp2 = bp1 + len;
174
175 for (size_t offs = 0; offs < len; offs++)
176 {
177 data[offs] = (bp1[offs] << 8) | (bp2[offs] & 0xff);
178 }
179
180 dmFree(tmpBuf);
181 }
182 else
183 {
184 Sint16 value = 0, *sdata = (Sint16 *) data;
185
186 for (size_t offs = 0; offs < len; offs++)
187 {
188 Sint16 t;
189
190 if (ops & jsampSwapEndianess)
191 {
192 const Sint16 p = sdata[offs];
103 t = ((p >> 8) & 0xff) | ((p & 0xff) << 8); 193 t = ((p >> 8) & 0xff) | ((p & 0xff) << 8);
104 } 194 }
105 else 195 else
106 t = *sdata; 196 t = *sdata;
107 197
108 if (ops & jsampDelta) 198 if (ops & jsampDelta)
109 { 199 t = value = t + value;
110 int n = t - value;
111 value = t;
112 t = n;
113 }
114 200
115 if (ops & jsampFlipSign) 201 if (ops & jsampFlipSign)
116 t ^= 0x8000; 202 t ^= 0x8000;
117 203
118 *(sdata++) = t; 204 sdata[offs] = t;
119 } 205 }
120 } 206 }
121 return TRUE; 207
122 }
123
124 #endif
125
126
127 /* Decodes a given 8-bit sample
128 */
129 int jssDecodeSample8(Uint8 * data, const size_t len, const int ops)
130 {
131 size_t count = len;
132 Sint8 t, value = 0;
133
134 while (count--)
135 {
136 t = *data;
137
138 if (ops & jsampDelta)
139 t = value = t + value;
140
141 if (ops & jsampFlipSign)
142 t ^= 0x80;
143
144 *(data++) = t;
145 }
146 return DMERR_OK;
147 }
148
149
150 /* Decodes a given 16-bit sample
151 */
152 int jssDecodeSample16(Uint16 * data, const size_t len, const int ops)
153 {
154 if (ops & jsampSplit)
155 {
156 size_t count, bufSize = len * sizeof(Uint16);
157 Uint8 *bp1, *bp2;
158 Sint16 *tmpBuf, *sdata;
159 int ret;
160
161 if ((ret = jssDecodeSample8((Uint8 *) data, bufSize, ops)) != DMERR_OK)
162 return ret;
163
164 if ((tmpBuf = dmMalloc(bufSize)) == NULL)
165 return DMERR_MALLOC;
166
167 memcpy(tmpBuf, data, bufSize);
168
169 sdata = (Sint16 *) data;
170 bp1 = (Uint8 *) tmpBuf;
171 bp2 = bp1 + len;
172 count = len;
173 while (count--)
174 {
175 *sdata++ = (*bp1++ << 8) | (*bp2++ & 0xff);
176 }
177
178 dmFree(tmpBuf);
179 }
180 else
181 {
182 Sint16 t, p, value = 0, *sdata = (Sint16 *) data;
183 size_t count = len;
184 while (count--)
185 {
186 if (ops & jsampSwapEndianess)
187 {
188 p = *sdata;
189 t = ((p >> 8) & 0xff) | ((p & 0xff) << 8);
190 }
191 else
192 t = *sdata;
193
194 if (ops & jsampDelta)
195 t = value = t + value;
196
197 if (ops & jsampFlipSign)
198 t ^= 0x8000;
199
200 *(sdata++) = t;
201 }
202 }
203 return DMERR_OK; 208 return DMERR_OK;
204 } 209 }
205 210
206 211
207 /* Convert sample data from U8 to S16 212 /* Convert sample data from U8 to S16
208 */ 213 */
209 int jssConvertSampleTo16(void **dst, void * src, const size_t len) 214 int jssConvertSampleTo16(void **dst, void * src, const size_t len)
210 { 215 {
211 size_t count = len;
212 Uint8 *in = (Uint8 *) src; 216 Uint8 *in = (Uint8 *) src;
213 Sint16 *out; 217 Sint16 *out;
214 218
215 if ((*dst = out = dmMalloc(sizeof(Sint16) * len)) == NULL) 219 if ((*dst = out = dmMalloc(sizeof(Sint16) * len)) == NULL)
216 return DMERR_MALLOC; 220 return DMERR_MALLOC;
217 221
218 while (count--) 222 for (size_t offs = 0; offs < len; offs++)
219 { 223 {
220 *(out++) = (*(in++) * 256) - 32768; 224 out[offs] = (in[offs] * 256) - 32768;
221 } 225 }
222 226
223 return DMERR_OK; 227 return DMERR_OK;
224 } 228 }
225 229