1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/*
* Copyright 2003-2019 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "PcmFormat.hxx"
#include "Buffer.hxx"
#include "Traits.hxx"
#include "FloatConvert.hxx"
#include "ShiftConvert.hxx"
#include "util/ConstBuffer.hxx"
#include "util/TransformN.hxx"
#include "Dither.cxx" // including the .cxx file to get inlined templates
/**
* Wrapper for a class that converts one sample at a time into one
* that converts a buffer at a time.
*/
template<typename C>
struct PerSampleConvert : C {
typedef typename C::SrcTraits SrcTraits;
typedef typename C::DstTraits DstTraits;
void Convert(typename DstTraits::pointer gcc_restrict out,
typename SrcTraits::const_pointer gcc_restrict in,
size_t n) const {
transform_n(in, n, out, C::Convert);
}
};
struct Convert8To16
: PerSampleConvert<LeftShiftSampleConvert<SampleFormat::S8,
SampleFormat::S16>> {};
struct Convert24To16 {
typedef SampleTraits<SampleFormat::S24_P32> SrcTraits;
typedef SampleTraits<SampleFormat::S16> DstTraits;
PcmDither &dither;
Convert24To16(PcmDither &_dither):dither(_dither) {}
void Convert(int16_t *out, const int32_t *in, size_t n) {
dither.Dither24To16(out, in, in + n);
}
};
struct Convert32To16 {
typedef SampleTraits<SampleFormat::S32> SrcTraits;
typedef SampleTraits<SampleFormat::S16> DstTraits;
PcmDither &dither;
Convert32To16(PcmDither &_dither):dither(_dither) {}
void Convert(int16_t *out, const int32_t *in, size_t n) {
dither.Dither32To16(out, in, in + n);
}
};
template<SampleFormat F, class Traits=SampleTraits<F>>
struct PortableFloatToInteger
: PerSampleConvert<FloatToIntegerSampleConvert<F, Traits>> {};
template<SampleFormat F, class Traits=SampleTraits<F>>
struct FloatToInteger : PortableFloatToInteger<F, Traits> {};
/**
* A template class that attempts to use the "optimized" algorithm for
* large portions of the buffer, and calls the "portable" algorithm"
* for the rest when the last block is not full.
*/
template<typename Optimized, typename Portable>
class GlueOptimizedConvert : Optimized, Portable {
public:
typedef typename Portable::SrcTraits SrcTraits;
typedef typename Portable::DstTraits DstTraits;
void Convert(typename DstTraits::pointer out,
typename SrcTraits::const_pointer in,
size_t n) const {
Optimized::Convert(out, in, n);
/* use the "portable" algorithm for the trailing
samples */
size_t remaining = n % Optimized::BLOCK_SIZE;
size_t done = n - remaining;
Portable::Convert(out + done, in + done, remaining);
}
};
#ifdef __ARM_NEON__
#include "Neon.hxx"
template<>
struct FloatToInteger<SampleFormat::S16, SampleTraits<SampleFormat::S16>>
: GlueOptimizedConvert<NeonFloatTo16,
PortableFloatToInteger<SampleFormat::S16>> {};
#endif
template<class C>
static ConstBuffer<typename C::DstTraits::value_type>
AllocateConvert(PcmBuffer &buffer, C convert,
ConstBuffer<typename C::SrcTraits::value_type> src)
{
auto dest = buffer.GetT<typename C::DstTraits::value_type>(src.size);
convert.Convert(dest, src.data, src.size);
return { dest, src.size };
}
template<SampleFormat F, class Traits=SampleTraits<F>>
static ConstBuffer<typename Traits::value_type>
AllocateFromFloat(PcmBuffer &buffer, ConstBuffer<float> src)
{
return AllocateConvert(buffer, FloatToInteger<F, Traits>(), src);
}
static ConstBuffer<int16_t>
pcm_allocate_8_to_16(PcmBuffer &buffer, ConstBuffer<int8_t> src)
{
return AllocateConvert(buffer, Convert8To16(), src);
}
static ConstBuffer<int16_t>
pcm_allocate_24p32_to_16(PcmBuffer &buffer, PcmDither &dither,
ConstBuffer<int32_t> src)
{
return AllocateConvert(buffer, Convert24To16(dither), src);
}
static ConstBuffer<int16_t>
pcm_allocate_32_to_16(PcmBuffer &buffer, PcmDither &dither,
ConstBuffer<int32_t> src)
{
return AllocateConvert(buffer, Convert32To16(dither), src);
}
static ConstBuffer<int16_t>
pcm_allocate_float_to_16(PcmBuffer &buffer, ConstBuffer<float> src)
{
return AllocateFromFloat<SampleFormat::S16>(buffer, src);
}
ConstBuffer<int16_t>
pcm_convert_to_16(PcmBuffer &buffer, PcmDither &dither,
SampleFormat src_format, ConstBuffer<void> src) noexcept
{
switch (src_format) {
case SampleFormat::UNDEFINED:
case SampleFormat::DSD:
break;
case SampleFormat::S8:
return pcm_allocate_8_to_16(buffer,
ConstBuffer<int8_t>::FromVoid(src));
case SampleFormat::S16:
return ConstBuffer<int16_t>::FromVoid(src);
case SampleFormat::S24_P32:
return pcm_allocate_24p32_to_16(buffer, dither,
ConstBuffer<int32_t>::FromVoid(src));
case SampleFormat::S32:
return pcm_allocate_32_to_16(buffer, dither,
ConstBuffer<int32_t>::FromVoid(src));
case SampleFormat::FLOAT:
return pcm_allocate_float_to_16(buffer,
ConstBuffer<float>::FromVoid(src));
}
return nullptr;
}
struct Convert8To24
: PerSampleConvert<LeftShiftSampleConvert<SampleFormat::S8,
SampleFormat::S24_P32>> {};
struct Convert16To24
: PerSampleConvert<LeftShiftSampleConvert<SampleFormat::S16,
SampleFormat::S24_P32>> {};
static ConstBuffer<int32_t>
pcm_allocate_8_to_24(PcmBuffer &buffer, ConstBuffer<int8_t> src)
{
return AllocateConvert(buffer, Convert8To24(), src);
}
static ConstBuffer<int32_t>
pcm_allocate_16_to_24(PcmBuffer &buffer, ConstBuffer<int16_t> src)
{
return AllocateConvert(buffer, Convert16To24(), src);
}
struct Convert32To24
: PerSampleConvert<RightShiftSampleConvert<SampleFormat::S32,
SampleFormat::S24_P32>> {};
static ConstBuffer<int32_t>
pcm_allocate_32_to_24(PcmBuffer &buffer, ConstBuffer<int32_t> src)
{
return AllocateConvert(buffer, Convert32To24(), src);
}
static ConstBuffer<int32_t>
pcm_allocate_float_to_24(PcmBuffer &buffer, ConstBuffer<float> src)
{
return AllocateFromFloat<SampleFormat::S24_P32>(buffer, src);
}
ConstBuffer<int32_t>
pcm_convert_to_24(PcmBuffer &buffer,
SampleFormat src_format, ConstBuffer<void> src) noexcept
{
switch (src_format) {
case SampleFormat::UNDEFINED:
case SampleFormat::DSD:
break;
case SampleFormat::S8:
return pcm_allocate_8_to_24(buffer,
ConstBuffer<int8_t>::FromVoid(src));
case SampleFormat::S16:
return pcm_allocate_16_to_24(buffer,
ConstBuffer<int16_t>::FromVoid(src));
case SampleFormat::S24_P32:
return ConstBuffer<int32_t>::FromVoid(src);
case SampleFormat::S32:
return pcm_allocate_32_to_24(buffer,
ConstBuffer<int32_t>::FromVoid(src));
case SampleFormat::FLOAT:
return pcm_allocate_float_to_24(buffer,
ConstBuffer<float>::FromVoid(src));
}
return nullptr;
}
struct Convert8To32
: PerSampleConvert<LeftShiftSampleConvert<SampleFormat::S8,
SampleFormat::S32>> {};
struct Convert16To32
: PerSampleConvert<LeftShiftSampleConvert<SampleFormat::S16,
SampleFormat::S32>> {};
struct Convert24To32
: PerSampleConvert<LeftShiftSampleConvert<SampleFormat::S24_P32,
SampleFormat::S32>> {};
static ConstBuffer<int32_t>
pcm_allocate_8_to_32(PcmBuffer &buffer, ConstBuffer<int8_t> src)
{
return AllocateConvert(buffer, Convert8To32(), src);
}
static ConstBuffer<int32_t>
pcm_allocate_16_to_32(PcmBuffer &buffer, ConstBuffer<int16_t> src)
{
return AllocateConvert(buffer, Convert16To32(), src);
}
static ConstBuffer<int32_t>
pcm_allocate_24p32_to_32(PcmBuffer &buffer, ConstBuffer<int32_t> src)
{
return AllocateConvert(buffer, Convert24To32(), src);
}
static ConstBuffer<int32_t>
pcm_allocate_float_to_32(PcmBuffer &buffer, ConstBuffer<float> src)
{
return AllocateFromFloat<SampleFormat::S32>(buffer, src);
}
ConstBuffer<int32_t>
pcm_convert_to_32(PcmBuffer &buffer,
SampleFormat src_format, ConstBuffer<void> src) noexcept
{
switch (src_format) {
case SampleFormat::UNDEFINED:
case SampleFormat::DSD:
break;
case SampleFormat::S8:
return pcm_allocate_8_to_32(buffer,
ConstBuffer<int8_t>::FromVoid(src));
case SampleFormat::S16:
return pcm_allocate_16_to_32(buffer,
ConstBuffer<int16_t>::FromVoid(src));
case SampleFormat::S24_P32:
return pcm_allocate_24p32_to_32(buffer,
ConstBuffer<int32_t>::FromVoid(src));
case SampleFormat::S32:
return ConstBuffer<int32_t>::FromVoid(src);
case SampleFormat::FLOAT:
return pcm_allocate_float_to_32(buffer,
ConstBuffer<float>::FromVoid(src));
}
return nullptr;
}
struct Convert8ToFloat
: PerSampleConvert<IntegerToFloatSampleConvert<SampleFormat::S8>> {};
struct Convert16ToFloat
: PerSampleConvert<IntegerToFloatSampleConvert<SampleFormat::S16>> {};
struct Convert24ToFloat
: PerSampleConvert<IntegerToFloatSampleConvert<SampleFormat::S24_P32>> {};
struct Convert32ToFloat
: PerSampleConvert<IntegerToFloatSampleConvert<SampleFormat::S32>> {};
static ConstBuffer<float>
pcm_allocate_8_to_float(PcmBuffer &buffer, ConstBuffer<int8_t> src)
{
return AllocateConvert(buffer, Convert8ToFloat(), src);
}
static ConstBuffer<float>
pcm_allocate_16_to_float(PcmBuffer &buffer, ConstBuffer<int16_t> src)
{
return AllocateConvert(buffer, Convert16ToFloat(), src);
}
static ConstBuffer<float>
pcm_allocate_24p32_to_float(PcmBuffer &buffer, ConstBuffer<int32_t> src)
{
return AllocateConvert(buffer, Convert24ToFloat(), src);
}
static ConstBuffer<float>
pcm_allocate_32_to_float(PcmBuffer &buffer, ConstBuffer<int32_t> src)
{
return AllocateConvert(buffer, Convert32ToFloat(), src);
}
ConstBuffer<float>
pcm_convert_to_float(PcmBuffer &buffer,
SampleFormat src_format, ConstBuffer<void> src) noexcept
{
switch (src_format) {
case SampleFormat::UNDEFINED:
case SampleFormat::DSD:
break;
case SampleFormat::S8:
return pcm_allocate_8_to_float(buffer,
ConstBuffer<int8_t>::FromVoid(src));
case SampleFormat::S16:
return pcm_allocate_16_to_float(buffer,
ConstBuffer<int16_t>::FromVoid(src));
case SampleFormat::S32:
return pcm_allocate_32_to_float(buffer,
ConstBuffer<int32_t>::FromVoid(src));
case SampleFormat::S24_P32:
return pcm_allocate_24p32_to_float(buffer,
ConstBuffer<int32_t>::FromVoid(src));
case SampleFormat::FLOAT:
return ConstBuffer<float>::FromVoid(src);
}
return nullptr;
}
|