Merge pull request #204 from jhasse/android-byte-order
[openal-soft.git] / Alc / converter.c
blobef2eb9af23369792b19090a4de3d1d83a4d49565
2 #include "config.h"
4 #include "converter.h"
6 #include "fpu_modes.h"
7 #include "mixer/defs.h"
10 SampleConverter *CreateSampleConverter(enum DevFmtType srcType, enum DevFmtType dstType, ALsizei numchans, ALsizei srcRate, ALsizei dstRate)
12 SampleConverter *converter;
13 ALsizei step;
15 if(numchans <= 0 || srcRate <= 0 || dstRate <= 0)
16 return NULL;
18 converter = al_calloc(16, FAM_SIZE(SampleConverter, Chan, numchans));
19 converter->mSrcType = srcType;
20 converter->mDstType = dstType;
21 converter->mNumChannels = numchans;
22 converter->mSrcTypeSize = BytesFromDevFmt(srcType);
23 converter->mDstTypeSize = BytesFromDevFmt(dstType);
25 converter->mSrcPrepCount = 0;
26 converter->mFracOffset = 0;
28 /* Have to set the mixer FPU mode since that's what the resampler code expects. */
29 START_MIXER_MODE();
30 step = (ALsizei)mind(((ALdouble)srcRate/dstRate*FRACTIONONE) + 0.5,
31 MAX_PITCH * FRACTIONONE);
32 converter->mIncrement = maxi(step, 1);
33 if(converter->mIncrement == FRACTIONONE)
34 converter->mResample = Resample_copy_C;
35 else
37 /* TODO: Allow other resamplers. */
38 BsincPrepare(converter->mIncrement, &converter->mState.bsinc, &bsinc12);
39 converter->mResample = SelectResampler(BSinc12Resampler);
41 END_MIXER_MODE();
43 return converter;
46 void DestroySampleConverter(SampleConverter **converter)
48 if(converter)
50 al_free(*converter);
51 *converter = NULL;
56 static inline ALfloat Sample_ALbyte(ALbyte val)
57 { return val * (1.0f/128.0f); }
58 static inline ALfloat Sample_ALubyte(ALubyte val)
59 { return Sample_ALbyte((ALint)val - 128); }
61 static inline ALfloat Sample_ALshort(ALshort val)
62 { return val * (1.0f/32768.0f); }
63 static inline ALfloat Sample_ALushort(ALushort val)
64 { return Sample_ALshort((ALint)val - 32768); }
66 static inline ALfloat Sample_ALint(ALint val)
67 { return (val>>7) * (1.0f/16777216.0f); }
68 static inline ALfloat Sample_ALuint(ALuint val)
69 { return Sample_ALint(val - INT_MAX - 1); }
71 static inline ALfloat Sample_ALfloat(ALfloat val)
72 { return val; }
74 #define DECL_TEMPLATE(T) \
75 static inline void Load_##T(ALfloat *restrict dst, const T *restrict src, \
76 ALint srcstep, ALsizei samples) \
77 { \
78 ALsizei i; \
79 for(i = 0;i < samples;i++) \
80 dst[i] = Sample_##T(src[i*srcstep]); \
83 DECL_TEMPLATE(ALbyte)
84 DECL_TEMPLATE(ALubyte)
85 DECL_TEMPLATE(ALshort)
86 DECL_TEMPLATE(ALushort)
87 DECL_TEMPLATE(ALint)
88 DECL_TEMPLATE(ALuint)
89 DECL_TEMPLATE(ALfloat)
91 #undef DECL_TEMPLATE
93 static void LoadSamples(ALfloat *dst, const ALvoid *src, ALint srcstep, enum DevFmtType srctype, ALsizei samples)
95 switch(srctype)
97 case DevFmtByte:
98 Load_ALbyte(dst, src, srcstep, samples);
99 break;
100 case DevFmtUByte:
101 Load_ALubyte(dst, src, srcstep, samples);
102 break;
103 case DevFmtShort:
104 Load_ALshort(dst, src, srcstep, samples);
105 break;
106 case DevFmtUShort:
107 Load_ALushort(dst, src, srcstep, samples);
108 break;
109 case DevFmtInt:
110 Load_ALint(dst, src, srcstep, samples);
111 break;
112 case DevFmtUInt:
113 Load_ALuint(dst, src, srcstep, samples);
114 break;
115 case DevFmtFloat:
116 Load_ALfloat(dst, src, srcstep, samples);
117 break;
122 static inline ALbyte ALbyte_Sample(ALfloat val)
123 { return fastf2i(clampf(val*128.0f, -128.0f, 127.0f)); }
124 static inline ALubyte ALubyte_Sample(ALfloat val)
125 { return ALbyte_Sample(val)+128; }
127 static inline ALshort ALshort_Sample(ALfloat val)
128 { return fastf2i(clampf(val*32768.0f, -32768.0f, 32767.0f)); }
129 static inline ALushort ALushort_Sample(ALfloat val)
130 { return ALshort_Sample(val)+32768; }
132 static inline ALint ALint_Sample(ALfloat val)
133 { return fastf2i(clampf(val*16777216.0f, -16777216.0f, 16777215.0f)) << 7; }
134 static inline ALuint ALuint_Sample(ALfloat val)
135 { return ALint_Sample(val)+INT_MAX+1; }
137 static inline ALfloat ALfloat_Sample(ALfloat val)
138 { return val; }
140 #define DECL_TEMPLATE(T) \
141 static inline void Store_##T(T *restrict dst, const ALfloat *restrict src, \
142 ALint dststep, ALsizei samples) \
144 ALsizei i; \
145 for(i = 0;i < samples;i++) \
146 dst[i*dststep] = T##_Sample(src[i]); \
149 DECL_TEMPLATE(ALbyte)
150 DECL_TEMPLATE(ALubyte)
151 DECL_TEMPLATE(ALshort)
152 DECL_TEMPLATE(ALushort)
153 DECL_TEMPLATE(ALint)
154 DECL_TEMPLATE(ALuint)
155 DECL_TEMPLATE(ALfloat)
157 #undef DECL_TEMPLATE
159 static void StoreSamples(ALvoid *dst, const ALfloat *src, ALint dststep, enum DevFmtType dsttype, ALsizei samples)
161 switch(dsttype)
163 case DevFmtByte:
164 Store_ALbyte(dst, src, dststep, samples);
165 break;
166 case DevFmtUByte:
167 Store_ALubyte(dst, src, dststep, samples);
168 break;
169 case DevFmtShort:
170 Store_ALshort(dst, src, dststep, samples);
171 break;
172 case DevFmtUShort:
173 Store_ALushort(dst, src, dststep, samples);
174 break;
175 case DevFmtInt:
176 Store_ALint(dst, src, dststep, samples);
177 break;
178 case DevFmtUInt:
179 Store_ALuint(dst, src, dststep, samples);
180 break;
181 case DevFmtFloat:
182 Store_ALfloat(dst, src, dststep, samples);
183 break;
188 ALsizei SampleConverterAvailableOut(SampleConverter *converter, ALsizei srcframes)
190 ALint prepcount = converter->mSrcPrepCount;
191 ALsizei increment = converter->mIncrement;
192 ALsizei DataPosFrac = converter->mFracOffset;
193 ALuint64 DataSize64;
195 if(prepcount < 0)
197 /* Negative prepcount means we need to skip that many input samples. */
198 if(-prepcount >= srcframes)
199 return 0;
200 srcframes += prepcount;
201 prepcount = 0;
204 if(srcframes < 1)
206 /* No output samples if there's no input samples. */
207 return 0;
210 if(prepcount < MAX_RESAMPLE_PADDING*2 &&
211 MAX_RESAMPLE_PADDING*2 - prepcount >= srcframes)
213 /* Not enough input samples to generate an output sample. */
214 return 0;
217 DataSize64 = prepcount;
218 DataSize64 += srcframes;
219 DataSize64 -= MAX_RESAMPLE_PADDING*2;
220 DataSize64 <<= FRACTIONBITS;
221 DataSize64 -= DataPosFrac;
223 /* If we have a full prep, we can generate at least one sample. */
224 return (ALsizei)clampu64((DataSize64 + increment-1)/increment, 1, BUFFERSIZE);
228 ALsizei SampleConverterInput(SampleConverter *converter, const ALvoid **src, ALsizei *srcframes, ALvoid *dst, ALsizei dstframes)
230 const ALsizei SrcFrameSize = converter->mNumChannels * converter->mSrcTypeSize;
231 const ALsizei DstFrameSize = converter->mNumChannels * converter->mDstTypeSize;
232 const ALsizei increment = converter->mIncrement;
233 ALsizei pos = 0;
235 START_MIXER_MODE();
236 while(pos < dstframes && *srcframes > 0)
238 ALfloat *restrict SrcData = ASSUME_ALIGNED(converter->mSrcSamples, 16);
239 ALfloat *restrict DstData = ASSUME_ALIGNED(converter->mDstSamples, 16);
240 ALint prepcount = converter->mSrcPrepCount;
241 ALsizei DataPosFrac = converter->mFracOffset;
242 ALuint64 DataSize64;
243 ALsizei DstSize;
244 ALint toread;
245 ALsizei chan;
247 if(prepcount < 0)
249 /* Negative prepcount means we need to skip that many input samples. */
250 if(-prepcount >= *srcframes)
252 converter->mSrcPrepCount = prepcount + *srcframes;
253 *srcframes = 0;
254 break;
256 *src = (const ALbyte*)*src + SrcFrameSize*-prepcount;
257 *srcframes += prepcount;
258 converter->mSrcPrepCount = 0;
259 continue;
261 toread = mini(*srcframes, BUFFERSIZE - MAX_RESAMPLE_PADDING*2);
263 if(prepcount < MAX_RESAMPLE_PADDING*2 &&
264 MAX_RESAMPLE_PADDING*2 - prepcount >= toread)
266 /* Not enough input samples to generate an output sample. Store
267 * what we're given for later.
269 for(chan = 0;chan < converter->mNumChannels;chan++)
270 LoadSamples(&converter->Chan[chan].mPrevSamples[prepcount],
271 (const ALbyte*)*src + converter->mSrcTypeSize*chan,
272 converter->mNumChannels, converter->mSrcType, toread
275 converter->mSrcPrepCount = prepcount + toread;
276 *srcframes = 0;
277 break;
280 DataSize64 = prepcount;
281 DataSize64 += toread;
282 DataSize64 -= MAX_RESAMPLE_PADDING*2;
283 DataSize64 <<= FRACTIONBITS;
284 DataSize64 -= DataPosFrac;
286 /* If we have a full prep, we can generate at least one sample. */
287 DstSize = (ALsizei)clampu64((DataSize64 + increment-1)/increment, 1, BUFFERSIZE);
288 DstSize = mini(DstSize, dstframes-pos);
290 for(chan = 0;chan < converter->mNumChannels;chan++)
292 const ALbyte *SrcSamples = (const ALbyte*)*src + converter->mSrcTypeSize*chan;
293 ALbyte *DstSamples = (ALbyte*)dst + converter->mDstTypeSize*chan;
294 const ALfloat *ResampledData;
295 ALsizei SrcDataEnd;
297 /* Load the previous samples into the source data first, then the
298 * new samples from the input buffer.
300 memcpy(SrcData, converter->Chan[chan].mPrevSamples,
301 prepcount*sizeof(ALfloat));
302 LoadSamples(SrcData + prepcount, SrcSamples,
303 converter->mNumChannels, converter->mSrcType, toread
306 /* Store as many prep samples for next time as possible, given the
307 * number of output samples being generated.
309 SrcDataEnd = (DataPosFrac + increment*DstSize)>>FRACTIONBITS;
310 if(SrcDataEnd >= prepcount+toread)
311 memset(converter->Chan[chan].mPrevSamples, 0,
312 sizeof(converter->Chan[chan].mPrevSamples));
313 else
315 size_t len = mini(MAX_RESAMPLE_PADDING*2, prepcount+toread-SrcDataEnd);
316 memcpy(converter->Chan[chan].mPrevSamples, &SrcData[SrcDataEnd],
317 len*sizeof(ALfloat));
318 memset(converter->Chan[chan].mPrevSamples+len, 0,
319 sizeof(converter->Chan[chan].mPrevSamples) - len*sizeof(ALfloat));
322 /* Now resample, and store the result in the output buffer. */
323 ResampledData = converter->mResample(&converter->mState,
324 SrcData+MAX_RESAMPLE_PADDING, DataPosFrac, increment,
325 DstData, DstSize
328 StoreSamples(DstSamples, ResampledData, converter->mNumChannels,
329 converter->mDstType, DstSize);
332 /* Update the number of prep samples still available, as well as the
333 * fractional offset.
335 DataPosFrac += increment*DstSize;
336 converter->mSrcPrepCount = mini(prepcount + toread - (DataPosFrac>>FRACTIONBITS),
337 MAX_RESAMPLE_PADDING*2);
338 converter->mFracOffset = DataPosFrac & FRACTIONMASK;
340 /* Update the src and dst pointers in case there's still more to do. */
341 *src = (const ALbyte*)*src + SrcFrameSize*(DataPosFrac>>FRACTIONBITS);
342 *srcframes -= mini(*srcframes, (DataPosFrac>>FRACTIONBITS));
344 dst = (ALbyte*)dst + DstFrameSize*DstSize;
345 pos += DstSize;
347 END_MIXER_MODE();
349 return pos;
353 ChannelConverter *CreateChannelConverter(enum DevFmtType srcType, enum DevFmtChannels srcChans, enum DevFmtChannels dstChans)
355 ChannelConverter *converter;
357 if(srcChans != dstChans && !((srcChans == DevFmtMono && dstChans == DevFmtStereo) ||
358 (srcChans == DevFmtStereo && dstChans == DevFmtMono)))
359 return NULL;
361 converter = al_calloc(DEF_ALIGN, sizeof(*converter));
362 converter->mSrcType = srcType;
363 converter->mSrcChans = srcChans;
364 converter->mDstChans = dstChans;
366 return converter;
369 void DestroyChannelConverter(ChannelConverter **converter)
371 if(converter)
373 al_free(*converter);
374 *converter = NULL;
379 #define DECL_TEMPLATE(T) \
380 static void Mono2Stereo##T(ALfloat *restrict dst, const T *src, ALsizei frames)\
382 ALsizei i; \
383 for(i = 0;i < frames;i++) \
384 dst[i*2 + 1] = dst[i*2 + 0] = Sample_##T(src[i]) * 0.707106781187f; \
387 static void Stereo2Mono##T(ALfloat *restrict dst, const T *src, ALsizei frames)\
389 ALsizei i; \
390 for(i = 0;i < frames;i++) \
391 dst[i] = (Sample_##T(src[i*2 + 0])+Sample_##T(src[i*2 + 1])) * \
392 0.707106781187f; \
395 DECL_TEMPLATE(ALbyte)
396 DECL_TEMPLATE(ALubyte)
397 DECL_TEMPLATE(ALshort)
398 DECL_TEMPLATE(ALushort)
399 DECL_TEMPLATE(ALint)
400 DECL_TEMPLATE(ALuint)
401 DECL_TEMPLATE(ALfloat)
403 #undef DECL_TEMPLATE
405 void ChannelConverterInput(ChannelConverter *converter, const ALvoid *src, ALfloat *dst, ALsizei frames)
407 if(converter->mSrcChans == converter->mDstChans)
409 LoadSamples(dst, src, 1, converter->mSrcType,
410 frames*ChannelsFromDevFmt(converter->mSrcChans, 0));
411 return;
414 if(converter->mSrcChans == DevFmtStereo && converter->mDstChans == DevFmtMono)
416 switch(converter->mSrcType)
418 case DevFmtByte:
419 Stereo2MonoALbyte(dst, src, frames);
420 break;
421 case DevFmtUByte:
422 Stereo2MonoALubyte(dst, src, frames);
423 break;
424 case DevFmtShort:
425 Stereo2MonoALshort(dst, src, frames);
426 break;
427 case DevFmtUShort:
428 Stereo2MonoALushort(dst, src, frames);
429 break;
430 case DevFmtInt:
431 Stereo2MonoALint(dst, src, frames);
432 break;
433 case DevFmtUInt:
434 Stereo2MonoALuint(dst, src, frames);
435 break;
436 case DevFmtFloat:
437 Stereo2MonoALfloat(dst, src, frames);
438 break;
441 else /*if(converter->mSrcChans == DevFmtMono && converter->mDstChans == DevFmtStereo)*/
443 switch(converter->mSrcType)
445 case DevFmtByte:
446 Mono2StereoALbyte(dst, src, frames);
447 break;
448 case DevFmtUByte:
449 Mono2StereoALubyte(dst, src, frames);
450 break;
451 case DevFmtShort:
452 Mono2StereoALshort(dst, src, frames);
453 break;
454 case DevFmtUShort:
455 Mono2StereoALushort(dst, src, frames);
456 break;
457 case DevFmtInt:
458 Mono2StereoALint(dst, src, frames);
459 break;
460 case DevFmtUInt:
461 Mono2StereoALuint(dst, src, frames);
462 break;
463 case DevFmtFloat:
464 Mono2StereoALfloat(dst, src, frames);
465 break;