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 | |
01993d67 |
16 | static |
48e2f01c |
17 | inline float pcm_i2f(int32_t n) |
18 | { |
19 | union { |
20 | int32_t ivalue; |
21 | float fvalue; |
22 | } u; |
23 | u.ivalue = n; |
24 | return u.fvalue; |
25 | } |
01993d67 |
26 | static |
48e2f01c |
27 | inline double pcm_i2d(int64_t n) |
28 | { |
29 | union { |
30 | int64_t ivalue; |
31 | double fvalue; |
32 | } u; |
33 | u.ivalue = n; |
34 | return u.fvalue; |
35 | } |
01993d67 |
36 | static |
48e2f01c |
37 | inline int16_t pcm_quantize_s32(int32_t n) |
38 | { |
39 | n = ((n >> 15) + 1) >> 1; |
40 | return (n == 0x8000) ? 0x7fff : n; |
41 | } |
01993d67 |
42 | static |
48e2f01c |
43 | inline int16_t pcm_quantize_f64(double v) |
44 | { |
6da14aae |
45 | return (int16_t)lrint(pcm_clip(v * 32768.0, -32768.0, 32767.0)); |
48e2f01c |
46 | } |
01993d67 |
47 | static |
48e2f01c |
48 | inline int16_t pcm_s8_to_s16(int8_t n) |
49 | { |
50 | return n << 8; |
51 | } |
01993d67 |
52 | static |
48e2f01c |
53 | inline int16_t pcm_u8_to_s16(uint8_t n) |
54 | { |
55 | return (n << 8) ^ 0x8000; |
56 | } |
01993d67 |
57 | static |
48e2f01c |
58 | inline int16_t pcm_s16le_to_s16(int16_t n) |
59 | { |
60 | return m4af_ltoh16(n); |
61 | } |
01993d67 |
62 | static |
48e2f01c |
63 | inline int16_t pcm_s16be_to_s16(int16_t n) |
64 | { |
65 | return m4af_btoh16(n); |
66 | } |
01993d67 |
67 | static |
48e2f01c |
68 | inline int16_t pcm_u16le_to_s16(uint16_t n) |
69 | { |
70 | return m4af_ltoh16(n) ^ 0x8000; |
71 | } |
01993d67 |
72 | static |
48e2f01c |
73 | inline int16_t pcm_u16be_to_s16(uint16_t n) |
74 | { |
75 | return m4af_btoh16(n) ^ 0x8000; |
76 | } |
01993d67 |
77 | static |
48e2f01c |
78 | inline int32_t pcm_s24le_to_s32(uint8_t *p) |
79 | { |
80 | return p[0]<<8 | p[1]<<16 | p[2]<<24; |
81 | } |
01993d67 |
82 | static |
48e2f01c |
83 | inline int32_t pcm_s24be_to_s32(uint8_t *p) |
84 | { |
85 | return p[0]<<24 | p[1]<<16 | p[2]<<8; |
86 | } |
01993d67 |
87 | static |
48e2f01c |
88 | inline int32_t pcm_u24le_to_s32(uint8_t *p) |
89 | { |
90 | return pcm_s24le_to_s32(p) ^ 0x80000000; |
91 | } |
01993d67 |
92 | static |
48e2f01c |
93 | inline int32_t pcm_u24be_to_s32(uint8_t *p) |
94 | { |
95 | return pcm_s24be_to_s32(p) ^ 0x80000000; |
96 | } |
01993d67 |
97 | static |
48e2f01c |
98 | inline int16_t pcm_s24le_to_s16(uint8_t *p) |
99 | { |
100 | return pcm_quantize_s32(pcm_s24le_to_s32(p)); |
101 | } |
01993d67 |
102 | static |
48e2f01c |
103 | inline int16_t pcm_s24be_to_s16(uint8_t *p) |
104 | { |
105 | return pcm_quantize_s32(pcm_s24be_to_s32(p)); |
106 | } |
01993d67 |
107 | static |
48e2f01c |
108 | inline int16_t pcm_u24le_to_s16(uint8_t *p) |
109 | { |
110 | return pcm_quantize_s32(pcm_u24le_to_s32(p)); |
111 | } |
01993d67 |
112 | static |
48e2f01c |
113 | inline int16_t pcm_u24be_to_s16(uint8_t *p) |
114 | { |
115 | return pcm_quantize_s32(pcm_u24be_to_s32(p)); |
116 | } |
01993d67 |
117 | static |
48e2f01c |
118 | inline int16_t pcm_s32le_to_s16(int32_t n) |
119 | { |
120 | return pcm_quantize_s32(m4af_ltoh32(n)); |
121 | } |
01993d67 |
122 | static |
48e2f01c |
123 | inline int16_t pcm_s32be_to_s16(int32_t n) |
124 | { |
125 | return pcm_quantize_s32(m4af_btoh32(n)); |
126 | } |
01993d67 |
127 | static |
48e2f01c |
128 | inline int16_t pcm_u32le_to_s16(int32_t n) |
129 | { |
130 | return pcm_quantize_s32(m4af_ltoh32(n) ^ 0x80000000); |
131 | } |
01993d67 |
132 | static |
48e2f01c |
133 | inline int16_t pcm_u32be_to_s16(int32_t n) |
134 | { |
135 | return pcm_quantize_s32(m4af_btoh32(n) ^ 0x80000000); |
136 | } |
158dc13c |
137 | static |
48e2f01c |
138 | inline int16_t pcm_f32le_to_s16(int32_t n) |
139 | { |
140 | return pcm_quantize_f64(pcm_i2f(m4af_ltoh32(n))); |
141 | } |
01993d67 |
142 | static |
48e2f01c |
143 | inline int16_t pcm_f32be_to_s16(int32_t n) |
144 | { |
145 | return pcm_quantize_f64(pcm_i2f(m4af_btoh32(n))); |
146 | } |
01993d67 |
147 | static |
48e2f01c |
148 | inline int16_t pcm_f64le_to_s16(int64_t n) |
149 | { |
150 | return pcm_quantize_f64(pcm_i2d(m4af_ltoh64(n))); |
151 | } |
01993d67 |
152 | static |
48e2f01c |
153 | inline int16_t pcm_f64be_to_s16(int64_t n) |
154 | { |
155 | return pcm_quantize_f64(pcm_i2d(m4af_btoh64(n))); |
156 | } |
157 | |
158 | int pcm_convert_to_native_sint16(const pcm_sample_description_t *format, |
159 | const void *input, uint32_t nframes, |
e8e9f79e |
160 | int16_t *result) |
48e2f01c |
161 | { |
162 | #define CONVERT(type, conv) \ |
163 | do { \ |
164 | unsigned i; \ |
165 | type *ip = (type *)input; \ |
166 | for (i = 0; i < count; ++i) { \ |
e8e9f79e |
167 | result[i] = conv(ip[i]); \ |
48e2f01c |
168 | } \ |
169 | } while(0) |
170 | |
171 | #define CONVERT_BYTES(conv) \ |
172 | do { \ |
173 | unsigned i, bytes_per_channel; \ |
174 | uint8_t *ip = (uint8_t *)input; \ |
175 | bytes_per_channel = PCM_BYTES_PER_CHANNEL(format); \ |
176 | for (i = 0; i < count; ++i) { \ |
e8e9f79e |
177 | result[i] = conv(ip); \ |
48e2f01c |
178 | ip += bytes_per_channel; \ |
179 | } \ |
180 | } while(0) |
181 | |
182 | uint32_t count = nframes * format->channels_per_frame; |
183 | if (!count) |
184 | return 0; |
48e2f01c |
185 | switch (PCM_BYTES_PER_CHANNEL(format) | format->sample_type<<4) { |
186 | case 1 | PCM_TYPE_SINT<<4: |
187 | CONVERT(int8_t, pcm_s8_to_s16); break; |
188 | case 1 | PCM_TYPE_UINT<<4: |
189 | CONVERT(uint8_t, pcm_u8_to_s16); break; |
190 | case 2 | PCM_TYPE_SINT<<4: |
191 | CONVERT(int16_t, pcm_s16le_to_s16); break; |
192 | case 2 | PCM_TYPE_UINT<<4: |
193 | CONVERT(uint16_t, pcm_u16le_to_s16); break; |
194 | case 2 | PCM_TYPE_SINT_BE<<4: |
195 | CONVERT(int16_t, pcm_s16be_to_s16); break; |
196 | case 2 | PCM_TYPE_UINT_BE<<4: |
197 | CONVERT(int16_t, pcm_u16be_to_s16); break; |
198 | case 3 | PCM_TYPE_SINT<<4: |
199 | CONVERT_BYTES(pcm_s24le_to_s16); break; |
200 | case 3 | PCM_TYPE_UINT<<4: |
201 | CONVERT_BYTES(pcm_u24le_to_s16); break; |
202 | case 3 | PCM_TYPE_SINT_BE<<4: |
203 | CONVERT_BYTES(pcm_s24be_to_s16); break; |
204 | case 3 | PCM_TYPE_UINT_BE<<4: |
205 | CONVERT_BYTES(pcm_u24be_to_s16); break; |
206 | case 4 | PCM_TYPE_SINT<<4: |
207 | CONVERT(int32_t, pcm_s32le_to_s16); break; |
208 | case 4 | PCM_TYPE_UINT<<4: |
209 | CONVERT(uint32_t, pcm_u32le_to_s16); break; |
210 | case 4 | PCM_TYPE_FLOAT<<4: |
211 | CONVERT(int32_t, pcm_f32le_to_s16); break; |
212 | case 4 | PCM_TYPE_SINT_BE<<4: |
213 | CONVERT(int32_t, pcm_s32be_to_s16); break; |
214 | case 4 | PCM_TYPE_UINT_BE<<4: |
215 | CONVERT(uint32_t, pcm_u32be_to_s16); break; |
216 | case 4 | PCM_TYPE_FLOAT_BE<<4: |
217 | CONVERT(int32_t, pcm_f32be_to_s16); break; |
218 | case 8 | PCM_TYPE_FLOAT<<4: |
219 | CONVERT(int64_t, pcm_f64le_to_s16); break; |
220 | case 8 | PCM_TYPE_FLOAT_BE<<4: |
221 | CONVERT(int64_t, pcm_f64be_to_s16); break; |
222 | default: |
223 | return -1; |
224 | } |
225 | return 0; |
226 | } |