initial commit
[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
37inline int pcm_clip(int n, int min_value, int max_value)
38{
39 if (n < min_value)
40 return min_value;
41 else if (n > max_value)
42 return max_value;
43 return n;
44}
45inline float pcm_i2f(int32_t n)
46{
47 union {
48 int32_t ivalue;
49 float fvalue;
50 } u;
51 u.ivalue = n;
52 return u.fvalue;
53}
54inline double pcm_i2d(int64_t n)
55{
56 union {
57 int64_t ivalue;
58 double fvalue;
59 } u;
60 u.ivalue = n;
61 return u.fvalue;
62}
63inline int16_t pcm_quantize_s32(int32_t n)
64{
65 n = ((n >> 15) + 1) >> 1;
66 return (n == 0x8000) ? 0x7fff : n;
67}
68inline int16_t pcm_quantize_f64(double v)
69{
70 return pcm_clip(lrint(v * 32768.0), -32768, 32767);
71}
72inline int16_t pcm_s8_to_s16(int8_t n)
73{
74 return n << 8;
75}
76inline int16_t pcm_u8_to_s16(uint8_t n)
77{
78 return (n << 8) ^ 0x8000;
79}
80inline int16_t pcm_s16le_to_s16(int16_t n)
81{
82 return m4af_ltoh16(n);
83}
84inline int16_t pcm_s16be_to_s16(int16_t n)
85{
86 return m4af_btoh16(n);
87}
88inline int16_t pcm_u16le_to_s16(uint16_t n)
89{
90 return m4af_ltoh16(n) ^ 0x8000;
91}
92inline int16_t pcm_u16be_to_s16(uint16_t n)
93{
94 return m4af_btoh16(n) ^ 0x8000;
95}
96inline int32_t pcm_s24le_to_s32(uint8_t *p)
97{
98 return p[0]<<8 | p[1]<<16 | p[2]<<24;
99}
100inline int32_t pcm_s24be_to_s32(uint8_t *p)
101{
102 return p[0]<<24 | p[1]<<16 | p[2]<<8;
103}
104inline int32_t pcm_u24le_to_s32(uint8_t *p)
105{
106 return pcm_s24le_to_s32(p) ^ 0x80000000;
107}
108inline int32_t pcm_u24be_to_s32(uint8_t *p)
109{
110 return pcm_s24be_to_s32(p) ^ 0x80000000;
111}
112inline int16_t pcm_s24le_to_s16(uint8_t *p)
113{
114 return pcm_quantize_s32(pcm_s24le_to_s32(p));
115}
116inline int16_t pcm_s24be_to_s16(uint8_t *p)
117{
118 return pcm_quantize_s32(pcm_s24be_to_s32(p));
119}
120inline int16_t pcm_u24le_to_s16(uint8_t *p)
121{
122 return pcm_quantize_s32(pcm_u24le_to_s32(p));
123}
124inline int16_t pcm_u24be_to_s16(uint8_t *p)
125{
126 return pcm_quantize_s32(pcm_u24be_to_s32(p));
127}
128inline int16_t pcm_s32le_to_s16(int32_t n)
129{
130 return pcm_quantize_s32(m4af_ltoh32(n));
131}
132inline int16_t pcm_s32be_to_s16(int32_t n)
133{
134 return pcm_quantize_s32(m4af_btoh32(n));
135}
136inline int16_t pcm_u32le_to_s16(int32_t n)
137{
138 return pcm_quantize_s32(m4af_ltoh32(n) ^ 0x80000000);
139}
140inline int16_t pcm_u32be_to_s16(int32_t n)
141{
142 return pcm_quantize_s32(m4af_btoh32(n) ^ 0x80000000);
143}
144inline int16_t pcm_f32le_to_s16(int32_t n)
145{
146 return pcm_quantize_f64(pcm_i2f(m4af_ltoh32(n)));
147}
148inline int16_t pcm_f32be_to_s16(int32_t n)
149{
150 return pcm_quantize_f64(pcm_i2f(m4af_btoh32(n)));
151}
152inline int16_t pcm_f64le_to_s16(int64_t n)
153{
154 return pcm_quantize_f64(pcm_i2d(m4af_ltoh64(n)));
155}
156inline int16_t pcm_f64be_to_s16(int64_t n)
157{
158 return pcm_quantize_f64(pcm_i2d(m4af_btoh64(n)));
159}
160
161int 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)
164{
165#define CONVERT(type, conv) \
166 do { \
167 unsigned i; \
168 type *ip = (type *)input; \
169 for (i = 0; i < count; ++i) { \
170 (*result)[i] = conv(ip[i]); \
171 } \
172 } while(0)
173
174#define CONVERT_BYTES(conv) \
175 do { \
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; \
182 } \
183 } while(0)
184
185 uint32_t count = nframes * format->channels_per_frame;
186 if (!count)
187 return 0;
188 if (!*result || *osize < count) {
189 *osize = count;
190 *result = realloc(*result, count * sizeof(int16_t));
191 }
192
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;
230 default:
231 return -1;
232 }
233 return 0;
234}
This page took 0.02272 seconds and 4 git commands to generate.