]> iEval git - fdkaac.git/blame - src/lpcm.c
win32: change _wfopen() -> wfsopen()
[fdkaac.git] / src / lpcm.c
CommitLineData
48e2f01c 1/*
2 * Copyright (C) 2013 nu774
3 * For conditions of distribution and use, see copyright notice in COPYING
4 */
5#if HAVE_CONFIG_H
6# include "config.h"
7#endif
8#if HAVE_STDINT_H
9# include <stdint.h>
10#endif
11#include <stdlib.h>
12#include <math.h>
13#include "lpcm.h"
14#include "m4af_endian.h"
15
16#ifdef _MSC_VER
17# define inline __inline
18# ifdef _M_IX86
19inline int lrint(double x)
20{
21 int n;
22 _asm {
23 fld x
24 fistp n
25 }
26 return n;
27}
28# else
29# include <emmintrin.h>
30inline int lrint(double x)
31{
32 return _mm_cvtsd_si32(_mm_load_sd(&x));
33}
34# endif
35#endif
36
01993d67 37static
6da14aae 38inline double pcm_clip(double n, double min_value, double max_value)
48e2f01c 39{
40 if (n < min_value)
41 return min_value;
42 else if (n > max_value)
43 return max_value;
44 return n;
45}
01993d67 46static
48e2f01c 47inline float pcm_i2f(int32_t n)
48{
49 union {
50 int32_t ivalue;
51 float fvalue;
52 } u;
53 u.ivalue = n;
54 return u.fvalue;
55}
01993d67 56static
48e2f01c 57inline double pcm_i2d(int64_t n)
58{
59 union {
60 int64_t ivalue;
61 double fvalue;
62 } u;
63 u.ivalue = n;
64 return u.fvalue;
65}
01993d67 66static
48e2f01c 67inline int16_t pcm_quantize_s32(int32_t n)
68{
69 n = ((n >> 15) + 1) >> 1;
70 return (n == 0x8000) ? 0x7fff : n;
71}
01993d67 72static
48e2f01c 73inline int16_t pcm_quantize_f64(double v)
74{
6da14aae 75 return (int16_t)lrint(pcm_clip(v * 32768.0, -32768.0, 32767.0));
48e2f01c 76}
01993d67 77static
48e2f01c 78inline int16_t pcm_s8_to_s16(int8_t n)
79{
80 return n << 8;
81}
01993d67 82static
48e2f01c 83inline int16_t pcm_u8_to_s16(uint8_t n)
84{
85 return (n << 8) ^ 0x8000;
86}
01993d67 87static
48e2f01c 88inline int16_t pcm_s16le_to_s16(int16_t n)
89{
90 return m4af_ltoh16(n);
91}
01993d67 92static
48e2f01c 93inline int16_t pcm_s16be_to_s16(int16_t n)
94{
95 return m4af_btoh16(n);
96}
01993d67 97static
48e2f01c 98inline int16_t pcm_u16le_to_s16(uint16_t n)
99{
100 return m4af_ltoh16(n) ^ 0x8000;
101}
01993d67 102static
48e2f01c 103inline int16_t pcm_u16be_to_s16(uint16_t n)
104{
105 return m4af_btoh16(n) ^ 0x8000;
106}
01993d67 107static
48e2f01c 108inline int32_t pcm_s24le_to_s32(uint8_t *p)
109{
110 return p[0]<<8 | p[1]<<16 | p[2]<<24;
111}
01993d67 112static
48e2f01c 113inline int32_t pcm_s24be_to_s32(uint8_t *p)
114{
115 return p[0]<<24 | p[1]<<16 | p[2]<<8;
116}
01993d67 117static
48e2f01c 118inline int32_t pcm_u24le_to_s32(uint8_t *p)
119{
120 return pcm_s24le_to_s32(p) ^ 0x80000000;
121}
01993d67 122static
48e2f01c 123inline int32_t pcm_u24be_to_s32(uint8_t *p)
124{
125 return pcm_s24be_to_s32(p) ^ 0x80000000;
126}
01993d67 127static
48e2f01c 128inline int16_t pcm_s24le_to_s16(uint8_t *p)
129{
130 return pcm_quantize_s32(pcm_s24le_to_s32(p));
131}
01993d67 132static
48e2f01c 133inline int16_t pcm_s24be_to_s16(uint8_t *p)
134{
135 return pcm_quantize_s32(pcm_s24be_to_s32(p));
136}
01993d67 137static
48e2f01c 138inline int16_t pcm_u24le_to_s16(uint8_t *p)
139{
140 return pcm_quantize_s32(pcm_u24le_to_s32(p));
141}
01993d67 142static
48e2f01c 143inline int16_t pcm_u24be_to_s16(uint8_t *p)
144{
145 return pcm_quantize_s32(pcm_u24be_to_s32(p));
146}
01993d67 147static
48e2f01c 148inline int16_t pcm_s32le_to_s16(int32_t n)
149{
150 return pcm_quantize_s32(m4af_ltoh32(n));
151}
01993d67 152static
48e2f01c 153inline int16_t pcm_s32be_to_s16(int32_t n)
154{
155 return pcm_quantize_s32(m4af_btoh32(n));
156}
01993d67 157static
48e2f01c 158inline int16_t pcm_u32le_to_s16(int32_t n)
159{
160 return pcm_quantize_s32(m4af_ltoh32(n) ^ 0x80000000);
161}
01993d67 162static
48e2f01c 163inline int16_t pcm_u32be_to_s16(int32_t n)
164{
165 return pcm_quantize_s32(m4af_btoh32(n) ^ 0x80000000);
166}
158dc13c 167static
48e2f01c 168inline int16_t pcm_f32le_to_s16(int32_t n)
169{
170 return pcm_quantize_f64(pcm_i2f(m4af_ltoh32(n)));
171}
01993d67 172static
48e2f01c 173inline int16_t pcm_f32be_to_s16(int32_t n)
174{
175 return pcm_quantize_f64(pcm_i2f(m4af_btoh32(n)));
176}
01993d67 177static
48e2f01c 178inline int16_t pcm_f64le_to_s16(int64_t n)
179{
180 return pcm_quantize_f64(pcm_i2d(m4af_ltoh64(n)));
181}
01993d67 182static
48e2f01c 183inline int16_t pcm_f64be_to_s16(int64_t n)
184{
185 return pcm_quantize_f64(pcm_i2d(m4af_btoh64(n)));
186}
187
188int pcm_convert_to_native_sint16(const pcm_sample_description_t *format,
189 const void *input, uint32_t nframes,
190 int16_t **result, uint32_t *osize)
191{
192#define CONVERT(type, conv) \
193 do { \
194 unsigned i; \
195 type *ip = (type *)input; \
196 for (i = 0; i < count; ++i) { \
197 (*result)[i] = conv(ip[i]); \
198 } \
199 } while(0)
200
201#define CONVERT_BYTES(conv) \
202 do { \
203 unsigned i, bytes_per_channel; \
204 uint8_t *ip = (uint8_t *)input; \
205 bytes_per_channel = PCM_BYTES_PER_CHANNEL(format); \
206 for (i = 0; i < count; ++i) { \
207 (*result)[i] = conv(ip); \
208 ip += bytes_per_channel; \
209 } \
210 } while(0)
211
212 uint32_t count = nframes * format->channels_per_frame;
213 if (!count)
214 return 0;
215 if (!*result || *osize < count) {
216 *osize = count;
217 *result = realloc(*result, count * sizeof(int16_t));
218 }
219
220 switch (PCM_BYTES_PER_CHANNEL(format) | format->sample_type<<4) {
221 case 1 | PCM_TYPE_SINT<<4:
222 CONVERT(int8_t, pcm_s8_to_s16); break;
223 case 1 | PCM_TYPE_UINT<<4:
224 CONVERT(uint8_t, pcm_u8_to_s16); break;
225 case 2 | PCM_TYPE_SINT<<4:
226 CONVERT(int16_t, pcm_s16le_to_s16); break;
227 case 2 | PCM_TYPE_UINT<<4:
228 CONVERT(uint16_t, pcm_u16le_to_s16); break;
229 case 2 | PCM_TYPE_SINT_BE<<4:
230 CONVERT(int16_t, pcm_s16be_to_s16); break;
231 case 2 | PCM_TYPE_UINT_BE<<4:
232 CONVERT(int16_t, pcm_u16be_to_s16); break;
233 case 3 | PCM_TYPE_SINT<<4:
234 CONVERT_BYTES(pcm_s24le_to_s16); break;
235 case 3 | PCM_TYPE_UINT<<4:
236 CONVERT_BYTES(pcm_u24le_to_s16); break;
237 case 3 | PCM_TYPE_SINT_BE<<4:
238 CONVERT_BYTES(pcm_s24be_to_s16); break;
239 case 3 | PCM_TYPE_UINT_BE<<4:
240 CONVERT_BYTES(pcm_u24be_to_s16); break;
241 case 4 | PCM_TYPE_SINT<<4:
242 CONVERT(int32_t, pcm_s32le_to_s16); break;
243 case 4 | PCM_TYPE_UINT<<4:
244 CONVERT(uint32_t, pcm_u32le_to_s16); break;
245 case 4 | PCM_TYPE_FLOAT<<4:
246 CONVERT(int32_t, pcm_f32le_to_s16); break;
247 case 4 | PCM_TYPE_SINT_BE<<4:
248 CONVERT(int32_t, pcm_s32be_to_s16); break;
249 case 4 | PCM_TYPE_UINT_BE<<4:
250 CONVERT(uint32_t, pcm_u32be_to_s16); break;
251 case 4 | PCM_TYPE_FLOAT_BE<<4:
252 CONVERT(int32_t, pcm_f32be_to_s16); break;
253 case 8 | PCM_TYPE_FLOAT<<4:
254 CONVERT(int64_t, pcm_f64le_to_s16); break;
255 case 8 | PCM_TYPE_FLOAT_BE<<4:
256 CONVERT(int64_t, pcm_f64be_to_s16); break;
257 default:
258 return -1;
259 }
260 return 0;
261}
This page took 0.057572 seconds and 4 git commands to generate.