]>
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"
17 # define inline __inline
19 inline int lrint(double x
)
29 # include <emmintrin.h>
30 inline int lrint(double x
)
32 return _mm_cvtsd_si32(_mm_load_sd(&x
));
38 inline double pcm_clip(double n
, double min_value
, double max_value
)
42 else if (n
> max_value
)
47 inline float pcm_i2f(int32_t n
)
57 inline double pcm_i2d(int64_t n
)
67 inline int16_t pcm_quantize_s32(int32_t n
)
69 n
= ((n
>> 15) + 1) >> 1;
70 return (n
== 0x8000) ? 0x7fff : n
;
73 inline int16_t pcm_quantize_f64(double v
)
75 return (int16_t)lrint(pcm_clip(v
* 32768.0, -32768.0, 32767.0));
78 inline int16_t pcm_s8_to_s16(int8_t n
)
83 inline int16_t pcm_u8_to_s16(uint8_t n
)
85 return (n
<< 8) ^ 0x8000;
88 inline int16_t pcm_s16le_to_s16(int16_t n
)
90 return m4af_ltoh16(n
);
93 inline int16_t pcm_s16be_to_s16(int16_t n
)
95 return m4af_btoh16(n
);
98 inline int16_t pcm_u16le_to_s16(uint16_t n
)
100 return m4af_ltoh16(n
) ^ 0x8000;
103 inline int16_t pcm_u16be_to_s16(uint16_t n
)
105 return m4af_btoh16(n
) ^ 0x8000;
108 inline int32_t pcm_s24le_to_s32(uint8_t *p
)
110 return p
[0]<<8 | p
[1]<<16 | p
[2]<<24;
113 inline int32_t pcm_s24be_to_s32(uint8_t *p
)
115 return p
[0]<<24 | p
[1]<<16 | p
[2]<<8;
118 inline int32_t pcm_u24le_to_s32(uint8_t *p
)
120 return pcm_s24le_to_s32(p
) ^ 0x80000000;
123 inline int32_t pcm_u24be_to_s32(uint8_t *p
)
125 return pcm_s24be_to_s32(p
) ^ 0x80000000;
128 inline int16_t pcm_s24le_to_s16(uint8_t *p
)
130 return pcm_quantize_s32(pcm_s24le_to_s32(p
));
133 inline int16_t pcm_s24be_to_s16(uint8_t *p
)
135 return pcm_quantize_s32(pcm_s24be_to_s32(p
));
138 inline int16_t pcm_u24le_to_s16(uint8_t *p
)
140 return pcm_quantize_s32(pcm_u24le_to_s32(p
));
143 inline int16_t pcm_u24be_to_s16(uint8_t *p
)
145 return pcm_quantize_s32(pcm_u24be_to_s32(p
));
148 inline int16_t pcm_s32le_to_s16(int32_t n
)
150 return pcm_quantize_s32(m4af_ltoh32(n
));
153 inline int16_t pcm_s32be_to_s16(int32_t n
)
155 return pcm_quantize_s32(m4af_btoh32(n
));
158 inline int16_t pcm_u32le_to_s16(int32_t n
)
160 return pcm_quantize_s32(m4af_ltoh32(n
) ^ 0x80000000);
163 inline int16_t pcm_u32be_to_s16(int32_t n
)
165 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
,
189 int16_t **result
, uint32_t *osize
)
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 if (!*result
|| *osize
< count
) {
216 *result
= realloc(*result
, count
* sizeof(int16_t));
219 switch (PCM_BYTES_PER_CHANNEL(format
) | format
->sample_type
<<4) {
220 case 1 | PCM_TYPE_SINT
<<4:
221 CONVERT(int8_t, pcm_s8_to_s16
); break;
222 case 1 | PCM_TYPE_UINT
<<4:
223 CONVERT(uint8_t, pcm_u8_to_s16
); break;
224 case 2 | PCM_TYPE_SINT
<<4:
225 CONVERT(int16_t, pcm_s16le_to_s16
); break;
226 case 2 | PCM_TYPE_UINT
<<4:
227 CONVERT(uint16_t, pcm_u16le_to_s16
); break;
228 case 2 | PCM_TYPE_SINT_BE
<<4:
229 CONVERT(int16_t, pcm_s16be_to_s16
); break;
230 case 2 | PCM_TYPE_UINT_BE
<<4:
231 CONVERT(int16_t, pcm_u16be_to_s16
); break;
232 case 3 | PCM_TYPE_SINT
<<4:
233 CONVERT_BYTES(pcm_s24le_to_s16
); break;
234 case 3 | PCM_TYPE_UINT
<<4:
235 CONVERT_BYTES(pcm_u24le_to_s16
); break;
236 case 3 | PCM_TYPE_SINT_BE
<<4:
237 CONVERT_BYTES(pcm_s24be_to_s16
); break;
238 case 3 | PCM_TYPE_UINT_BE
<<4:
239 CONVERT_BYTES(pcm_u24be_to_s16
); break;
240 case 4 | PCM_TYPE_SINT
<<4:
241 CONVERT(int32_t, pcm_s32le_to_s16
); break;
242 case 4 | PCM_TYPE_UINT
<<4:
243 CONVERT(uint32_t, pcm_u32le_to_s16
); break;
244 case 4 | PCM_TYPE_FLOAT
<<4:
245 CONVERT(int32_t, pcm_f32le_to_s16
); break;
246 case 4 | PCM_TYPE_SINT_BE
<<4:
247 CONVERT(int32_t, pcm_s32be_to_s16
); break;
248 case 4 | PCM_TYPE_UINT_BE
<<4:
249 CONVERT(uint32_t, pcm_u32be_to_s16
); break;
250 case 4 | PCM_TYPE_FLOAT_BE
<<4:
251 CONVERT(int32_t, pcm_f32be_to_s16
); break;
252 case 8 | PCM_TYPE_FLOAT
<<4:
253 CONVERT(int64_t, pcm_f64le_to_s16
); break;
254 case 8 | PCM_TYPE_FLOAT_BE
<<4:
255 CONVERT(int64_t, pcm_f64be_to_s16
); break;
This page took 0.095621 seconds and 4 git commands to generate.