]>
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
));
37 inline double pcm_clip(double n
, double min_value
, double max_value
)
41 else if (n
> max_value
)
45 inline float pcm_i2f(int32_t n
)
54 inline double pcm_i2d(int64_t n
)
63 inline int16_t pcm_quantize_s32(int32_t n
)
65 n
= ((n
>> 15) + 1) >> 1;
66 return (n
== 0x8000) ? 0x7fff : n
;
68 inline int16_t pcm_quantize_f64(double v
)
70 return (int16_t)lrint(pcm_clip(v
* 32768.0, -32768.0, 32767.0));
72 inline int16_t pcm_s8_to_s16(int8_t n
)
76 inline int16_t pcm_u8_to_s16(uint8_t n
)
78 return (n
<< 8) ^ 0x8000;
80 inline int16_t pcm_s16le_to_s16(int16_t n
)
82 return m4af_ltoh16(n
);
84 inline int16_t pcm_s16be_to_s16(int16_t n
)
86 return m4af_btoh16(n
);
88 inline int16_t pcm_u16le_to_s16(uint16_t n
)
90 return m4af_ltoh16(n
) ^ 0x8000;
92 inline int16_t pcm_u16be_to_s16(uint16_t n
)
94 return m4af_btoh16(n
) ^ 0x8000;
96 inline int32_t pcm_s24le_to_s32(uint8_t *p
)
98 return p
[0]<<8 | p
[1]<<16 | p
[2]<<24;
100 inline int32_t pcm_s24be_to_s32(uint8_t *p
)
102 return p
[0]<<24 | p
[1]<<16 | p
[2]<<8;
104 inline int32_t pcm_u24le_to_s32(uint8_t *p
)
106 return pcm_s24le_to_s32(p
) ^ 0x80000000;
108 inline int32_t pcm_u24be_to_s32(uint8_t *p
)
110 return pcm_s24be_to_s32(p
) ^ 0x80000000;
112 inline int16_t pcm_s24le_to_s16(uint8_t *p
)
114 return pcm_quantize_s32(pcm_s24le_to_s32(p
));
116 inline int16_t pcm_s24be_to_s16(uint8_t *p
)
118 return pcm_quantize_s32(pcm_s24be_to_s32(p
));
120 inline int16_t pcm_u24le_to_s16(uint8_t *p
)
122 return pcm_quantize_s32(pcm_u24le_to_s32(p
));
124 inline int16_t pcm_u24be_to_s16(uint8_t *p
)
126 return pcm_quantize_s32(pcm_u24be_to_s32(p
));
128 inline int16_t pcm_s32le_to_s16(int32_t n
)
130 return pcm_quantize_s32(m4af_ltoh32(n
));
132 inline int16_t pcm_s32be_to_s16(int32_t n
)
134 return pcm_quantize_s32(m4af_btoh32(n
));
136 inline int16_t pcm_u32le_to_s16(int32_t n
)
138 return pcm_quantize_s32(m4af_ltoh32(n
) ^ 0x80000000);
140 inline int16_t pcm_u32be_to_s16(int32_t n
)
142 return pcm_quantize_s32(m4af_btoh32(n
) ^ 0x80000000);
144 inline int16_t pcm_f32le_to_s16(int32_t n
)
146 return pcm_quantize_f64(pcm_i2f(m4af_ltoh32(n
)));
148 inline int16_t pcm_f32be_to_s16(int32_t n
)
150 return pcm_quantize_f64(pcm_i2f(m4af_btoh32(n
)));
152 inline int16_t pcm_f64le_to_s16(int64_t n
)
154 return pcm_quantize_f64(pcm_i2d(m4af_ltoh64(n
)));
156 inline int16_t pcm_f64be_to_s16(int64_t n
)
158 return pcm_quantize_f64(pcm_i2d(m4af_btoh64(n
)));
161 int pcm_convert_to_native_sint16(const pcm_sample_description_t
*format
,
162 const void *input
, uint32_t nframes
,
163 int16_t **result
, uint32_t *osize
)
165 #define CONVERT(type, conv) \
168 type *ip = (type *)input; \
169 for (i = 0; i < count; ++i) { \
170 (*result)[i] = conv(ip[i]); \
174 #define CONVERT_BYTES(conv) \
176 unsigned i, bytes_per_channel; \
177 uint8_t *ip = (uint8_t *)input; \
178 bytes_per_channel = PCM_BYTES_PER_CHANNEL(format); \
179 for (i = 0; i < count; ++i) { \
180 (*result)[i] = conv(ip); \
181 ip += bytes_per_channel; \
185 uint32_t count
= nframes
* format
->channels_per_frame
;
188 if (!*result
|| *osize
< count
) {
190 *result
= realloc(*result
, count
* sizeof(int16_t));
193 switch (PCM_BYTES_PER_CHANNEL(format
) | format
->sample_type
<<4) {
194 case 1 | PCM_TYPE_SINT
<<4:
195 CONVERT(int8_t, pcm_s8_to_s16
); break;
196 case 1 | PCM_TYPE_UINT
<<4:
197 CONVERT(uint8_t, pcm_u8_to_s16
); break;
198 case 2 | PCM_TYPE_SINT
<<4:
199 CONVERT(int16_t, pcm_s16le_to_s16
); break;
200 case 2 | PCM_TYPE_UINT
<<4:
201 CONVERT(uint16_t, pcm_u16le_to_s16
); break;
202 case 2 | PCM_TYPE_SINT_BE
<<4:
203 CONVERT(int16_t, pcm_s16be_to_s16
); break;
204 case 2 | PCM_TYPE_UINT_BE
<<4:
205 CONVERT(int16_t, pcm_u16be_to_s16
); break;
206 case 3 | PCM_TYPE_SINT
<<4:
207 CONVERT_BYTES(pcm_s24le_to_s16
); break;
208 case 3 | PCM_TYPE_UINT
<<4:
209 CONVERT_BYTES(pcm_u24le_to_s16
); break;
210 case 3 | PCM_TYPE_SINT_BE
<<4:
211 CONVERT_BYTES(pcm_s24be_to_s16
); break;
212 case 3 | PCM_TYPE_UINT_BE
<<4:
213 CONVERT_BYTES(pcm_u24be_to_s16
); break;
214 case 4 | PCM_TYPE_SINT
<<4:
215 CONVERT(int32_t, pcm_s32le_to_s16
); break;
216 case 4 | PCM_TYPE_UINT
<<4:
217 CONVERT(uint32_t, pcm_u32le_to_s16
); break;
218 case 4 | PCM_TYPE_FLOAT
<<4:
219 CONVERT(int32_t, pcm_f32le_to_s16
); break;
220 case 4 | PCM_TYPE_SINT_BE
<<4:
221 CONVERT(int32_t, pcm_s32be_to_s16
); break;
222 case 4 | PCM_TYPE_UINT_BE
<<4:
223 CONVERT(uint32_t, pcm_u32be_to_s16
); break;
224 case 4 | PCM_TYPE_FLOAT_BE
<<4:
225 CONVERT(int32_t, pcm_f32be_to_s16
); break;
226 case 8 | PCM_TYPE_FLOAT
<<4:
227 CONVERT(int64_t, pcm_f64le_to_s16
); break;
228 case 8 | PCM_TYPE_FLOAT_BE
<<4:
229 CONVERT(int64_t, pcm_f64be_to_s16
); break;
This page took 0.051722 seconds and 4 git commands to generate.