]>
iEval git - fdkaac.git/blob - src/lpcm.c
2 * Copyright (C) 2013 nu774
3 * For conditions of distribution and use, see copyright notice in COPYING
14 #include "m4af_endian.h"
16 #if defined(_MSC_VER) && _MSC_VER < 1800
18 inline int lrint(double x
)
28 # include <emmintrin.h>
29 inline int lrint(double x
)
31 return _mm_cvtsd_si32(_mm_load_sd(&x
));
37 inline double pcm_clip(double n
, double min_value
, double max_value
)
41 else if (n
> max_value
)
46 inline float pcm_i2f(int32_t n
)
56 inline double pcm_i2d(int64_t n
)
66 inline int16_t pcm_quantize_s32(int32_t n
)
68 n
= ((n
>> 15) + 1) >> 1;
69 return (n
== 0x8000) ? 0x7fff : n
;
72 inline int16_t pcm_quantize_f64(double v
)
74 return (int16_t)lrint(pcm_clip(v
* 32768.0, -32768.0, 32767.0));
77 inline int16_t pcm_s8_to_s16(int8_t n
)
82 inline int16_t pcm_u8_to_s16(uint8_t n
)
84 return (n
<< 8) ^ 0x8000;
87 inline int16_t pcm_s16le_to_s16(int16_t n
)
89 return m4af_ltoh16(n
);
92 inline int16_t pcm_s16be_to_s16(int16_t n
)
94 return m4af_btoh16(n
);
97 inline int16_t pcm_u16le_to_s16(uint16_t n
)
99 return m4af_ltoh16(n
) ^ 0x8000;
102 inline int16_t pcm_u16be_to_s16(uint16_t n
)
104 return m4af_btoh16(n
) ^ 0x8000;
107 inline int32_t pcm_s24le_to_s32(uint8_t *p
)
109 return p
[0]<<8 | p
[1]<<16 | p
[2]<<24;
112 inline int32_t pcm_s24be_to_s32(uint8_t *p
)
114 return p
[0]<<24 | p
[1]<<16 | p
[2]<<8;
117 inline int32_t pcm_u24le_to_s32(uint8_t *p
)
119 return pcm_s24le_to_s32(p
) ^ 0x80000000;
122 inline int32_t pcm_u24be_to_s32(uint8_t *p
)
124 return pcm_s24be_to_s32(p
) ^ 0x80000000;
127 inline int16_t pcm_s24le_to_s16(uint8_t *p
)
129 return pcm_quantize_s32(pcm_s24le_to_s32(p
));
132 inline int16_t pcm_s24be_to_s16(uint8_t *p
)
134 return pcm_quantize_s32(pcm_s24be_to_s32(p
));
137 inline int16_t pcm_u24le_to_s16(uint8_t *p
)
139 return pcm_quantize_s32(pcm_u24le_to_s32(p
));
142 inline int16_t pcm_u24be_to_s16(uint8_t *p
)
144 return pcm_quantize_s32(pcm_u24be_to_s32(p
));
147 inline int16_t pcm_s32le_to_s16(int32_t n
)
149 return pcm_quantize_s32(m4af_ltoh32(n
));
152 inline int16_t pcm_s32be_to_s16(int32_t n
)
154 return pcm_quantize_s32(m4af_btoh32(n
));
157 inline int16_t pcm_u32le_to_s16(int32_t n
)
159 return pcm_quantize_s32(m4af_ltoh32(n
) ^ 0x80000000);
162 inline int16_t pcm_u32be_to_s16(int32_t n
)
164 return pcm_quantize_s32(m4af_btoh32(n
) ^ 0x80000000);
167 inline int16_t pcm_f32le_to_s16(int32_t n
)
169 return pcm_quantize_f64(pcm_i2f(m4af_ltoh32(n
)));
172 inline int16_t pcm_f32be_to_s16(int32_t n
)
174 return pcm_quantize_f64(pcm_i2f(m4af_btoh32(n
)));
177 inline int16_t pcm_f64le_to_s16(int64_t n
)
179 return pcm_quantize_f64(pcm_i2d(m4af_ltoh64(n
)));
182 inline int16_t pcm_f64be_to_s16(int64_t n
)
184 return pcm_quantize_f64(pcm_i2d(m4af_btoh64(n
)));
187 int pcm_convert_to_native_sint16(const pcm_sample_description_t
*format
,
188 const void *input
, uint32_t nframes
,
191 #define CONVERT(type, conv) \
194 type *ip = (type *)input; \
195 for (i = 0; i < count; ++i) { \
196 result[i] = conv(ip[i]); \
200 #define CONVERT_BYTES(conv) \
202 unsigned i, bytes_per_channel; \
203 uint8_t *ip = (uint8_t *)input; \
204 bytes_per_channel = PCM_BYTES_PER_CHANNEL(format); \
205 for (i = 0; i < count; ++i) { \
206 result[i] = conv(ip); \
207 ip += bytes_per_channel; \
211 uint32_t count
= nframes
* format
->channels_per_frame
;
214 switch (PCM_BYTES_PER_CHANNEL(format
) | format
->sample_type
<<4) {
215 case 1 | PCM_TYPE_SINT
<<4:
216 CONVERT(int8_t, pcm_s8_to_s16
); break;
217 case 1 | PCM_TYPE_UINT
<<4:
218 CONVERT(uint8_t, pcm_u8_to_s16
); break;
219 case 2 | PCM_TYPE_SINT
<<4:
220 CONVERT(int16_t, pcm_s16le_to_s16
); break;
221 case 2 | PCM_TYPE_UINT
<<4:
222 CONVERT(uint16_t, pcm_u16le_to_s16
); break;
223 case 2 | PCM_TYPE_SINT_BE
<<4:
224 CONVERT(int16_t, pcm_s16be_to_s16
); break;
225 case 2 | PCM_TYPE_UINT_BE
<<4:
226 CONVERT(int16_t, pcm_u16be_to_s16
); break;
227 case 3 | PCM_TYPE_SINT
<<4:
228 CONVERT_BYTES(pcm_s24le_to_s16
); break;
229 case 3 | PCM_TYPE_UINT
<<4:
230 CONVERT_BYTES(pcm_u24le_to_s16
); break;
231 case 3 | PCM_TYPE_SINT_BE
<<4:
232 CONVERT_BYTES(pcm_s24be_to_s16
); break;
233 case 3 | PCM_TYPE_UINT_BE
<<4:
234 CONVERT_BYTES(pcm_u24be_to_s16
); break;
235 case 4 | PCM_TYPE_SINT
<<4:
236 CONVERT(int32_t, pcm_s32le_to_s16
); break;
237 case 4 | PCM_TYPE_UINT
<<4:
238 CONVERT(uint32_t, pcm_u32le_to_s16
); break;
239 case 4 | PCM_TYPE_FLOAT
<<4:
240 CONVERT(int32_t, pcm_f32le_to_s16
); break;
241 case 4 | PCM_TYPE_SINT_BE
<<4:
242 CONVERT(int32_t, pcm_s32be_to_s16
); break;
243 case 4 | PCM_TYPE_UINT_BE
<<4:
244 CONVERT(uint32_t, pcm_u32be_to_s16
); break;
245 case 4 | PCM_TYPE_FLOAT_BE
<<4:
246 CONVERT(int32_t, pcm_f32be_to_s16
); break;
247 case 8 | PCM_TYPE_FLOAT
<<4:
248 CONVERT(int64_t, pcm_f64le_to_s16
); break;
249 case 8 | PCM_TYPE_FLOAT_BE
<<4:
250 CONVERT(int64_t, pcm_f64be_to_s16
); break;
This page took 0.064557 seconds and 4 git commands to generate.