I guess -1 isn't allowed for the output
[openal-soft.git] / Alc / converter.c
blob913a3ed36ba97062fd24188d92e5b9c48ab7915d
2 #include "config.h"
4 #include "converter.h"
6 #include "mixer_defs.h"
9 SampleConverter *CreateSampleConverter(enum DevFmtType srcType, enum DevFmtType dstType, ALsizei numchans, ALsizei srcRate, ALsizei dstRate)
11 SampleConverter *converter;
12 ALsizei step;
14 if(numchans <= 0 || srcRate <= 0 || dstRate <= 0)
15 return NULL;
17 converter = al_calloc(16, FAM_SIZE(SampleConverter, Chan, numchans));
18 converter->mSrcType = srcType;
19 converter->mDstType = dstType;
20 converter->mNumChannels = numchans;
21 converter->mSrcTypeSize = BytesFromDevFmt(srcType);
22 converter->mDstTypeSize = BytesFromDevFmt(dstType);
24 converter->mSrcPrepCount = 0;
25 converter->mFracOffset = 0;
27 /* Have to set the mixer FPU mode since that's what the resampler code expects. */
28 START_MIXER_MODE();
29 step = fastf2i(minf((ALdouble)srcRate / dstRate, MAX_PITCH)*FRACTIONONE + 0.5f);
30 converter->mIncrement = maxi(step, 1);
31 if(converter->mIncrement == FRACTIONONE)
32 converter->mResample = Resample_copy_C;
33 else
35 /* TODO: Allow other resamplers. */
36 BsincPrepare(converter->mIncrement, &converter->mState.bsinc, &bsinc12);
37 converter->mResample = SelectResampler(BSinc12Resampler);
39 END_MIXER_MODE();
41 return converter;
44 void DestroySampleConverter(SampleConverter **converter)
46 if(converter)
48 al_free(*converter);
49 *converter = NULL;
54 static inline ALfloat Sample_ALbyte(ALbyte val)
55 { return val * (1.0f/128.0f); }
56 static inline ALfloat Sample_ALubyte(ALubyte val)
57 { return Sample_ALbyte((ALint)val - 128); }
59 static inline ALfloat Sample_ALshort(ALshort val)
60 { return val * (1.0f/32768.0f); }
61 static inline ALfloat Sample_ALushort(ALushort val)
62 { return Sample_ALshort((ALint)val - 32768); }
64 static inline ALfloat Sample_ALint(ALint val)
65 { return (val>>7) * (1.0f/16777216.0f); }
66 static inline ALfloat Sample_ALuint(ALuint val)
67 { return Sample_ALint(val - INT_MAX - 1); }
69 static inline ALfloat Sample_ALfloat(ALfloat val)
70 { return val; }
72 #define DECL_TEMPLATE(T) \
73 static inline void Load_##T(ALfloat *restrict dst, const T *restrict src, \
74 ALint srcstep, ALsizei samples) \
75 { \
76 ALsizei i; \
77 for(i = 0;i < samples;i++) \
78 dst[i] = Sample_##T(src[i*srcstep]); \
81 DECL_TEMPLATE(ALbyte)
82 DECL_TEMPLATE(ALubyte)
83 DECL_TEMPLATE(ALshort)
84 DECL_TEMPLATE(ALushort)
85 DECL_TEMPLATE(ALint)
86 DECL_TEMPLATE(ALuint)
87 DECL_TEMPLATE(ALfloat)
89 #undef DECL_TEMPLATE
91 static void LoadSamples(ALfloat *dst, const ALvoid *src, ALint srcstep, enum DevFmtType srctype, ALsizei samples)
93 switch(srctype)
95 case DevFmtByte:
96 Load_ALbyte(dst, src, srcstep, samples);
97 break;
98 case DevFmtUByte:
99 Load_ALubyte(dst, src, srcstep, samples);
100 break;
101 case DevFmtShort:
102 Load_ALshort(dst, src, srcstep, samples);
103 break;
104 case DevFmtUShort:
105 Load_ALushort(dst, src, srcstep, samples);
106 break;
107 case DevFmtInt:
108 Load_ALint(dst, src, srcstep, samples);
109 break;
110 case DevFmtUInt:
111 Load_ALuint(dst, src, srcstep, samples);
112 break;
113 case DevFmtFloat:
114 Load_ALfloat(dst, src, srcstep, samples);
115 break;
120 static inline ALbyte ALbyte_Sample(ALfloat val)
121 { return fastf2i(clampf(val*128.0f, -128.0f, 127.0f)); }
122 static inline ALubyte ALubyte_Sample(ALfloat val)
123 { return ALbyte_Sample(val)+128; }
125 static inline ALshort ALshort_Sample(ALfloat val)
126 { return fastf2i(clampf(val*32768.0f, -32768.0f, 32767.0f)); }
127 static inline ALushort ALushort_Sample(ALfloat val)
128 { return ALshort_Sample(val)+32768; }
130 static inline ALint ALint_Sample(ALfloat val)
131 { return fastf2i(clampf(val*16777216.0f, -16777216.0f, 16777215.0f)) << 7; }
132 static inline ALuint ALuint_Sample(ALfloat val)
133 { return ALint_Sample(val)+INT_MAX+1; }
135 static inline ALfloat ALfloat_Sample(ALfloat val)
136 { return val; }
138 #define DECL_TEMPLATE(T) \
139 static inline void Store_##T(T *restrict dst, const ALfloat *restrict src, \
140 ALint dststep, ALsizei samples) \
142 ALsizei i; \
143 for(i = 0;i < samples;i++) \
144 dst[i*dststep] = T##_Sample(src[i]); \
147 DECL_TEMPLATE(ALbyte)
148 DECL_TEMPLATE(ALubyte)
149 DECL_TEMPLATE(ALshort)
150 DECL_TEMPLATE(ALushort)
151 DECL_TEMPLATE(ALint)
152 DECL_TEMPLATE(ALuint)
153 DECL_TEMPLATE(ALfloat)
155 #undef DECL_TEMPLATE
157 static void StoreSamples(ALvoid *dst, const ALfloat *src, ALint dststep, enum DevFmtType dsttype, ALsizei samples)
159 switch(dsttype)
161 case DevFmtByte:
162 Store_ALbyte(dst, src, dststep, samples);
163 break;
164 case DevFmtUByte:
165 Store_ALubyte(dst, src, dststep, samples);
166 break;
167 case DevFmtShort:
168 Store_ALshort(dst, src, dststep, samples);
169 break;
170 case DevFmtUShort:
171 Store_ALushort(dst, src, dststep, samples);
172 break;
173 case DevFmtInt:
174 Store_ALint(dst, src, dststep, samples);
175 break;
176 case DevFmtUInt:
177 Store_ALuint(dst, src, dststep, samples);
178 break;
179 case DevFmtFloat:
180 Store_ALfloat(dst, src, dststep, samples);
181 break;
186 ALsizei SampleConverterAvailableOut(SampleConverter *converter, ALsizei srcframes)
188 ALint prepcount = converter->mSrcPrepCount;
189 ALsizei increment = converter->mIncrement;
190 ALsizei DataPosFrac = converter->mFracOffset;
191 ALuint64 DataSize64;
193 if(prepcount < 0)
195 /* Negative prepcount means we need to skip that many input samples. */
196 if(-prepcount >= srcframes)
197 return 0;
198 srcframes += prepcount;
199 prepcount = 0;
202 if(srcframes < 1)
204 /* No output samples if there's no input samples. */
205 return 0;
208 if(prepcount < MAX_POST_SAMPLES+MAX_PRE_SAMPLES &&
209 MAX_POST_SAMPLES+MAX_PRE_SAMPLES-prepcount >= srcframes)
211 /* Not enough input samples to generate an output sample. */
212 return 0;
215 DataSize64 = prepcount;
216 DataSize64 += srcframes;
217 DataSize64 -= MAX_POST_SAMPLES+MAX_PRE_SAMPLES;
218 DataSize64 <<= FRACTIONBITS;
219 DataSize64 -= DataPosFrac;
221 /* If we have a full prep, we can generate at least one sample. */
222 return (ALsizei)clampu64((DataSize64 + increment-1)/increment, 1, BUFFERSIZE);
226 ALsizei SampleConverterInput(SampleConverter *converter, const ALvoid **src, ALsizei *srcframes, ALvoid *dst, ALsizei dstframes)
228 const ALsizei SrcFrameSize = converter->mNumChannels * converter->mSrcTypeSize;
229 const ALsizei DstFrameSize = converter->mNumChannels * converter->mDstTypeSize;
230 const ALsizei increment = converter->mIncrement;
231 ALsizei pos = 0;
233 START_MIXER_MODE();
234 while(pos < dstframes && *srcframes > 0)
236 ALfloat *restrict SrcData = ASSUME_ALIGNED(converter->mSrcSamples, 16);
237 ALfloat *restrict DstData = ASSUME_ALIGNED(converter->mDstSamples, 16);
238 ALint prepcount = converter->mSrcPrepCount;
239 ALsizei DataPosFrac = converter->mFracOffset;
240 ALuint64 DataSize64;
241 ALsizei DstSize;
242 ALint toread;
243 ALsizei chan;
245 if(prepcount < 0)
247 /* Negative prepcount means we need to skip that many input samples. */
248 if(-prepcount >= *srcframes)
250 converter->mSrcPrepCount = prepcount + *srcframes;
251 *srcframes = 0;
252 break;
254 *src = (const ALbyte*)*src + SrcFrameSize*-prepcount;
255 *srcframes += prepcount;
256 converter->mSrcPrepCount = 0;
257 continue;
259 toread = mini(*srcframes, BUFFERSIZE-(MAX_POST_SAMPLES+MAX_PRE_SAMPLES));
261 if(prepcount < MAX_POST_SAMPLES+MAX_PRE_SAMPLES &&
262 MAX_POST_SAMPLES+MAX_PRE_SAMPLES-prepcount >= toread)
264 /* Not enough input samples to generate an output sample. Store
265 * what we're given for later.
267 for(chan = 0;chan < converter->mNumChannels;chan++)
268 LoadSamples(&converter->Chan[chan].mPrevSamples[prepcount],
269 (const ALbyte*)*src + converter->mSrcTypeSize*chan,
270 converter->mNumChannels, converter->mSrcType, toread
273 converter->mSrcPrepCount = prepcount + toread;
274 *srcframes = 0;
275 break;
278 DataSize64 = prepcount;
279 DataSize64 += toread;
280 DataSize64 -= MAX_POST_SAMPLES+MAX_PRE_SAMPLES;
281 DataSize64 <<= FRACTIONBITS;
282 DataSize64 -= DataPosFrac;
284 /* If we have a full prep, we can generate at least one sample. */
285 DstSize = (ALsizei)clampu64((DataSize64 + increment-1)/increment, 1, BUFFERSIZE);
286 DstSize = mini(DstSize, dstframes-pos);
288 for(chan = 0;chan < converter->mNumChannels;chan++)
290 const ALbyte *SrcSamples = (const ALbyte*)*src + converter->mSrcTypeSize*chan;
291 ALbyte *DstSamples = (ALbyte*)dst + converter->mDstTypeSize*chan;
292 const ALfloat *ResampledData;
293 ALsizei SrcDataEnd;
295 /* Load the previous samples into the source data first, then the
296 * new samples from the input buffer.
298 memcpy(SrcData, converter->Chan[chan].mPrevSamples,
299 prepcount*sizeof(ALfloat));
300 LoadSamples(SrcData + prepcount, SrcSamples,
301 converter->mNumChannels, converter->mSrcType, toread
304 /* Store as many prep samples for next time as possible, given the
305 * number of output samples being generated.
307 SrcDataEnd = (DataPosFrac + increment*DstSize)>>FRACTIONBITS;
308 if(SrcDataEnd >= prepcount+toread)
309 memset(converter->Chan[chan].mPrevSamples, 0,
310 sizeof(converter->Chan[chan].mPrevSamples));
311 else
313 size_t len = mini(MAX_PRE_SAMPLES+MAX_POST_SAMPLES, prepcount+toread-SrcDataEnd);
314 memcpy(converter->Chan[chan].mPrevSamples, &SrcData[SrcDataEnd],
315 len*sizeof(ALfloat));
316 memset(converter->Chan[chan].mPrevSamples+len, 0,
317 sizeof(converter->Chan[chan].mPrevSamples) - len*sizeof(ALfloat));
320 /* Now resample, and store the result in the output buffer. */
321 ResampledData = converter->mResample(&converter->mState,
322 SrcData+MAX_PRE_SAMPLES, DataPosFrac, increment,
323 DstData, DstSize
326 StoreSamples(DstSamples, ResampledData, converter->mNumChannels,
327 converter->mDstType, DstSize);
330 /* Update the number of prep samples still available, as well as the
331 * fractional offset.
333 DataPosFrac += increment*DstSize;
334 converter->mSrcPrepCount = mini(MAX_PRE_SAMPLES+MAX_POST_SAMPLES,
335 prepcount+toread-(DataPosFrac>>FRACTIONBITS));
336 converter->mFracOffset = DataPosFrac & FRACTIONMASK;
338 /* Update the src and dst pointers in case there's still more to do. */
339 *src = (const ALbyte*)*src + SrcFrameSize*(DataPosFrac>>FRACTIONBITS);
340 *srcframes -= mini(*srcframes, (DataPosFrac>>FRACTIONBITS));
342 dst = (ALbyte*)dst + DstFrameSize*DstSize;
343 pos += DstSize;
345 END_MIXER_MODE();
347 return pos;
351 ChannelConverter *CreateChannelConverter(enum DevFmtType srcType, enum DevFmtChannels srcChans, enum DevFmtChannels dstChans)
353 ChannelConverter *converter;
355 if(srcChans != dstChans && !((srcChans == DevFmtMono && dstChans == DevFmtStereo) ||
356 (srcChans == DevFmtStereo && dstChans == DevFmtMono)))
357 return NULL;
359 converter = al_calloc(DEF_ALIGN, sizeof(*converter));
360 converter->mSrcType = srcType;
361 converter->mSrcChans = srcChans;
362 converter->mDstChans = dstChans;
364 return converter;
367 void DestroyChannelConverter(ChannelConverter **converter)
369 if(converter)
371 al_free(*converter);
372 *converter = NULL;
377 #define DECL_TEMPLATE(T) \
378 static void Mono2Stereo##T(ALfloat *restrict dst, const T *src, ALsizei frames)\
380 ALsizei i; \
381 for(i = 0;i < frames;i++) \
382 dst[i*2 + 1] = dst[i*2 + 0] = Sample_##T(src[i]) * 0.707106781187f; \
385 static void Stereo2Mono##T(ALfloat *restrict dst, const T *src, ALsizei frames)\
387 ALsizei i; \
388 for(i = 0;i < frames;i++) \
389 dst[i] = (Sample_##T(src[i*2 + 0])+Sample_##T(src[i*2 + 1])) * \
390 0.707106781187f; \
393 DECL_TEMPLATE(ALbyte)
394 DECL_TEMPLATE(ALubyte)
395 DECL_TEMPLATE(ALshort)
396 DECL_TEMPLATE(ALushort)
397 DECL_TEMPLATE(ALint)
398 DECL_TEMPLATE(ALuint)
399 DECL_TEMPLATE(ALfloat)
401 #undef DECL_TEMPLATE
403 void ChannelConverterInput(ChannelConverter *converter, const ALvoid *src, ALfloat *dst, ALsizei frames)
405 if(converter->mSrcChans == converter->mDstChans)
407 LoadSamples(dst, src, 1, converter->mSrcType,
408 frames*ChannelsFromDevFmt(converter->mSrcChans, 0));
409 return;
412 if(converter->mSrcChans == DevFmtStereo && converter->mDstChans == DevFmtMono)
414 switch(converter->mSrcType)
416 case DevFmtByte:
417 Stereo2MonoALbyte(dst, src, frames);
418 break;
419 case DevFmtUByte:
420 Stereo2MonoALubyte(dst, src, frames);
421 break;
422 case DevFmtShort:
423 Stereo2MonoALshort(dst, src, frames);
424 break;
425 case DevFmtUShort:
426 Stereo2MonoALushort(dst, src, frames);
427 break;
428 case DevFmtInt:
429 Stereo2MonoALint(dst, src, frames);
430 break;
431 case DevFmtUInt:
432 Stereo2MonoALuint(dst, src, frames);
433 break;
434 case DevFmtFloat:
435 Stereo2MonoALfloat(dst, src, frames);
436 break;
439 else /*if(converter->mSrcChans == DevFmtMono && converter->mDstChans == DevFmtStereo)*/
441 switch(converter->mSrcType)
443 case DevFmtByte:
444 Mono2StereoALbyte(dst, src, frames);
445 break;
446 case DevFmtUByte:
447 Mono2StereoALubyte(dst, src, frames);
448 break;
449 case DevFmtShort:
450 Mono2StereoALshort(dst, src, frames);
451 break;
452 case DevFmtUShort:
453 Mono2StereoALushort(dst, src, frames);
454 break;
455 case DevFmtInt:
456 Mono2StereoALint(dst, src, frames);
457 break;
458 case DevFmtUInt:
459 Mono2StereoALuint(dst, src, frames);
460 break;
461 case DevFmtFloat:
462 Mono2StereoALfloat(dst, src, frames);
463 break;